What about defining property accessors at the top level of the module?
On clients sometimes its desired to expose a simple API for updating some application state while implementation may want to update some internal values to deliver that update:
//main.mjs
const link = document.getElementById('theme-controller');
const availableThemes = [
'/default.css',
'/dark.css'
];
let currentTheme = '/default.css';
export set theme(newTheme) {
if (newTheme === currentTheme) return true;
if (availableThemes.includes(newTheme)) {
link.href = newTheme;
currentTheme = newTheme;
return true;
}
return false;
}
//consumer.mjs
import { theme } from '/main.mjs';
theme = '/dark.css';
In aforementioned example there is no get
accessor, so I'm still thinking how/why this should work:
import { theme } from '/main.mjs'; //Shouldn't this throw, because there is no getter (or exported getter)?
Considering that in es-modules bindings are live, I'd expect theme
to contain a reference to the accessor property and I'd expect it to throw only when the value is read.
I have a bad gut feeling about this idea. Let's discuss?