49 lines
899 B
JavaScript
49 lines
899 B
JavaScript
import { createAtom, toObserver } from "./atom.js";
|
|
class Store {
|
|
constructor(valueOrFn) {
|
|
this.atom = createAtom(
|
|
valueOrFn
|
|
);
|
|
}
|
|
setState(updater) {
|
|
this.atom.set(updater);
|
|
}
|
|
get state() {
|
|
return this.atom.get();
|
|
}
|
|
get() {
|
|
return this.state;
|
|
}
|
|
subscribe(observerOrFn) {
|
|
return this.atom.subscribe(toObserver(observerOrFn));
|
|
}
|
|
}
|
|
class ReadonlyStore {
|
|
constructor(valueOrFn) {
|
|
this.atom = createAtom(
|
|
valueOrFn
|
|
);
|
|
}
|
|
get state() {
|
|
return this.atom.get();
|
|
}
|
|
get() {
|
|
return this.state;
|
|
}
|
|
subscribe(observerOrFn) {
|
|
return this.atom.subscribe(toObserver(observerOrFn));
|
|
}
|
|
}
|
|
function createStore(valueOrFn) {
|
|
if (typeof valueOrFn === "function") {
|
|
return new ReadonlyStore(valueOrFn);
|
|
}
|
|
return new Store(valueOrFn);
|
|
}
|
|
export {
|
|
ReadonlyStore,
|
|
Store,
|
|
createStore
|
|
};
|
|
//# sourceMappingURL=store.js.map
|