ducks
Version:
🦆🦆🦆 Ducks is a Reducer Bundles Manager that Implementing the Redux Ducks Modular Proposal with Great Convenience.
108 lines • 4.13 kB
JavaScript
import { VERSION, } from './config.js';
class Bundle {
constructor(duck) {
this.duck = duck;
this.namespaces = [];
/**
* We provide a convenience way to call `selectors` and `operations`
* by encapsulating the `store` and `state`(includes the `namespace`)
* and take care of them for users
*
* Huan(202005): I'd like to call this: ducksify
*/
this.ducksifiedOperations = this.ducksifyOperations(this.duck);
this.ducksifiedSelectors = this.ducksifySelectors(this.duck);
}
get store() {
if (!this._store) {
throw new Error('Bundle store has not been set yet: Bundle can only be used after its store has been initialized.');
}
return this._store;
}
get reducer() { return this.duck.default; }
get actions() { return this.duck.actions; }
get types() { return this.duck.types; }
// get epics () : A['epics'] { return this.duck.epics }
// get sagas () : A['sagas'] { return this.duck.sagas }
get operations() {
return this.ducksifiedOperations;
}
get selectors() {
return this.ducksifiedSelectors;
}
get state() {
// console.info('[duck] state:', this.store.getState())
// console.info('[duck] namespaces:', this.namespaces)
// console.info('[duck] state[namespaces[0]]:', this.store.getState()[this.namespaces[0]])
// console.info('[duck] state[namespaces[1]]:', this.store.getState()[this.namespaces[1]])
const duckStateReducer = (duckState, namespace, idx) => {
if (namespace in duckState) {
return duckState[namespace];
}
throw new Error('duckStateReducer() can not get state from namespace: ' + this.namespaces[idx] + ' with index: ' + idx);
};
const duckState = this.namespaces.reduce(duckStateReducer, this.store.getState());
return duckState;
}
setStore(store) {
if (this._store) {
throw new Error('A store has already been set, and it can not be set twice.');
}
this._store = store;
}
setNamespaces(...namespaces) {
if (this.namespaces.length > 0) {
throw new Error('Namespaces has already been set, and it can not be set twice.');
}
this.namespaces = namespaces;
}
ducksifyOperations(duck) {
let ducksifiedOperations = {};
const operations = duck.operations;
if (!operations) {
return ducksifiedOperations;
}
const that = this;
Object.keys(operations).forEach(operation => {
/**
* Inferred function names
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
*/
ducksifiedOperations = {
...ducksifiedOperations,
[operation]: function (...args) {
return operations[operation](
/**
* We have to make sure `store` has been initialized before the following code has been ran
*/
that.store.dispatch)(...args);
},
};
});
return ducksifiedOperations;
}
ducksifySelectors(duck) {
let ducksifiedSelectors = {};
const selectors = duck.selectors;
if (!selectors) {
return ducksifiedSelectors;
}
const that = this;
Object.keys(selectors).forEach(selector => {
/**
* Inferred function names
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
*/
ducksifiedSelectors = {
...ducksifiedSelectors,
[selector]: function (...args) {
return selectors[selector](that.state)(...args);
},
};
});
return ducksifiedSelectors;
}
}
Bundle.VERSION = VERSION;
export { Bundle, };
//# sourceMappingURL=bundle.js.map