import tap from 'tap'; import Updux from '.'; import u from '@yanick/updeep'; tap.test( 'subscriptions', async() => { const inc = action('inc'); const set_copy = action('set_copy'); const dux = new Updux({ initial: { x: 0, copy: 0, }, actions: { inc, }, mutations: { inc: payload => u({ x: x => x + 1 }), set_copy: copy => u({ copy }), }, }); dux.addSubscription(store => (state, unsubscribe) => { if (state.x > 2) return unsubscribe(); store.dispatch(set_copy(state.x)); }); const store = dux.createStore(); store.dispatch(inc()); tap.same(store.getState(), { x: 1, copy: 1 }); store.dispatch(inc()); store.dispatch(inc()); tap.same(store.getState(), { x: 3, copy: 2 }, 'we unsubscribed'); } ); tap.test('subduxes subscriptions', async t => { const inc_top = action('inc_top'); const inc_bar = action('inc_bar'); const transform_bar = action('transform_bar'); const bar = new Updux({ initial: 'a', mutations: [ [inc_bar, () => state => state + 'a'], [transform_bar, outcome => () => outcome], ], subscriptions: [ store => (state, unsubscribe) => { console.log({ state }); if (state.length <= 2) return; unsubscribe(); store.dispatch(transform_bar('look at ' + state)); }, ], }); const dux = new Updux({ initial: { count: 0, }, subduxes: { bar: bar.asDux, }, mutations: [[inc_top, () => u({ count: count => count + 1 })]], effects: [ [ '*', () => next => action => { console.log('before ', action.type); next(action); console.log({ action }); }, ], ], subscriptions: [ store => { let previous; return ({ count }) => { if (count !== previous) { previous = count; store.dispatch(inc_bar()); } }; }, ], }); const store = dux.createStore(); store.dispatch(inc_top()); store.dispatch(inc_top()); t.same(store.getState(), { count: 2, bar: 'look at look at aaa', }); store.dispatch(inc_top()); t.same(store.getState(), { count: 3, bar: 'look at look at aaaa', }); });