From c1c1edf588f5b916ae140c7229c0fb44c7e03dd2 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sat, 9 Oct 2021 12:13:50 -0400 Subject: [PATCH] subscribe test --- src/Updux.js | 12 ++++++++- src/subscriptions.test.js | 55 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/Updux.js b/src/Updux.js index 0a74852..6d8b2c2 100644 --- a/src/Updux.js +++ b/src/Updux.js @@ -67,7 +67,17 @@ export class Updux { constructor(config) { this.#initial = config.initial ?? {}; 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.#mutations = config.mutations ?? {}; diff --git a/src/subscriptions.test.js b/src/subscriptions.test.js index 848b887..d7cec99 100644 --- a/src/subscriptions.test.js +++ b/src/subscriptions.test.js @@ -1,4 +1,5 @@ import tap from 'tap'; +import sinon from 'sinon'; import u from '@yanick/updeep'; import { Updux } from './Updux.js'; @@ -105,3 +106,57 @@ tap.test('subduxes subscriptions', async (t) => { 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) + + +} );