subscribe test
This commit is contained in:
parent
5eeb4d4ab7
commit
c1c1edf588
12
src/Updux.js
12
src/Updux.js
@ -67,7 +67,17 @@ export class Updux {
|
|||||||
constructor(config) {
|
constructor(config) {
|
||||||
this.#initial = config.initial ?? {};
|
this.#initial = config.initial ?? {};
|
||||||
this.#subduxes = config.subduxes ?? {};
|
this.#subduxes = config.subduxes ?? {};
|
||||||
this.#actions = config.actions ?? {};
|
if( config.actions ) {
|
||||||
|
for (const [ type, actionArg ] of Object.entries(config.actions)) {
|
||||||
|
if( typeof actionArg === 'function' && actionArg.type ) {
|
||||||
|
this.#actions[type] = actionArg;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.#actions[type] = action(type,actionArg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.#selectors = config.selectors ?? {};
|
this.#selectors = config.selectors ?? {};
|
||||||
|
|
||||||
this.#mutations = config.mutations ?? {};
|
this.#mutations = config.mutations ?? {};
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import tap from 'tap';
|
import tap from 'tap';
|
||||||
|
import sinon from 'sinon';
|
||||||
import u from '@yanick/updeep';
|
import u from '@yanick/updeep';
|
||||||
|
|
||||||
import { Updux } from './Updux.js';
|
import { Updux } from './Updux.js';
|
||||||
@ -105,3 +106,57 @@ tap.test('subduxes subscriptions', async (t) => {
|
|||||||
bar: 'look at look at aaaa',
|
bar: 'look at look at aaaa',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
tap.test( "subscription within subduxes", {only: true},async(t) => {
|
||||||
|
|
||||||
|
let innerState = sinon.fake.returns(null);
|
||||||
|
let outerState = sinon.fake.returns(null);
|
||||||
|
|
||||||
|
const inner = new Updux({
|
||||||
|
initial: 1,
|
||||||
|
actions: { inc: null },
|
||||||
|
mutations: {
|
||||||
|
inc: () => state => state + 1,
|
||||||
|
},
|
||||||
|
subscriptions: [
|
||||||
|
store => (state, previous, unsub) => {
|
||||||
|
if(!previous) return;
|
||||||
|
store.subscribe( innerState );
|
||||||
|
unsub();
|
||||||
|
}
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
const dux = new Updux({
|
||||||
|
subduxes: { inner },
|
||||||
|
subscriptions: [
|
||||||
|
store => (state, previous, unsub) => {
|
||||||
|
console.log(state,previous);
|
||||||
|
if(!previous) return;
|
||||||
|
store.subscribe( outerState );
|
||||||
|
unsub();
|
||||||
|
}
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const store = dux.createStore();
|
||||||
|
|
||||||
|
store.dispatch({ type: 'noop' });
|
||||||
|
store.dispatch({ type: 'noop' });
|
||||||
|
|
||||||
|
t.notOk( innerState.called );
|
||||||
|
t.notOk( outerState.called );
|
||||||
|
|
||||||
|
store.dispatch.inc();
|
||||||
|
// still not called, but waiting, now
|
||||||
|
t.notOk( innerState.called );
|
||||||
|
t.notOk( outerState.called );
|
||||||
|
|
||||||
|
store.dispatch.inc();
|
||||||
|
console.log(outerState.firstCall.args);
|
||||||
|
// console.log(outerState.firstCall)
|
||||||
|
|
||||||
|
|
||||||
|
} );
|
||||||
|
Loading…
Reference in New Issue
Block a user