This commit is contained in:
Yanick Champoux 2022-08-30 14:16:29 -04:00
parent fbe624a14c
commit 16397ce3ee
4 changed files with 24 additions and 22 deletions

View File

@ -76,10 +76,11 @@ export class Updux {
} }
get initial() { get initial() {
if (Object.keys(this.#subduxes).length === 0) return this.#localInitial ?? {}; if (Object.keys(this.#subduxes).length === 0)
return this.#localInitial ?? {};
if( this.#subduxes['*'] ) { if (this.#subduxes['*']) {
if( this.#localInitial ) return this.#localInitial; if (this.#localInitial) return this.#localInitial;
return []; return [];
} }
@ -183,19 +184,18 @@ export class Updux {
this.#effects = [...this.#effects, [action, effect]]; this.#effects = [...this.#effects, [action, effect]];
} }
addReaction( reaction ) { addReaction(reaction) {
this.#localReactions = [ ...this.#localReactions, reaction ] this.#localReactions = [...this.#localReactions, reaction];
} }
subscribeTo(store, subscription) { subscribeTo(store, subscription) {
const localStore = augmentMiddlewareApi( const localStore = augmentMiddlewareApi(
{ {
...store, ...store,
subscribe: (subscriber) => subscribe: (subscriber) => this.subscribeTo(store, subscriber), // TODO not sure
this.subscribeTo(store, subscriber), // TODO not sure
}, },
this.actions, this.actions,
this.selectors this.selectors,
); );
const subscriber = subscription(localStore); const subscriber = subscription(localStore);
@ -221,7 +221,7 @@ export class Updux {
subscribeAll(store) { subscribeAll(store) {
let results = this.#localReactions.map((sub) => let results = this.#localReactions.map((sub) =>
this.subscribeTo(store, sub) this.subscribeTo(store, sub),
); );
for (const subdux in this.#subduxes) { for (const subdux in this.#subduxes) {
@ -237,10 +237,10 @@ export class Updux {
return { return {
unsub: () => results.forEach(({ unsub }) => unsub()), unsub: () => results.forEach(({ unsub }) => unsub()),
subscriberMemoized: () => subscriberMemoized: () =>
results.forEach(({ subscriberMemoized}) => subscriberMemoized()), results.forEach(({ subscriberMemoized }) =>
subscriber: () => subscriberMemoized(),
results.forEach(({ subscriber }) => subscriber()
), ),
subscriber: () => results.forEach(({ subscriber }) => subscriber()),
}; };
} }
} }

View File

@ -26,7 +26,7 @@ test('initial value', () => {
}); });
}); });
test( "splat initial", async () => { test('splat initial', async () => {
const bar = new Updux({ const bar = new Updux({
initial: { id: 0 }, initial: { id: 0 },
}); });
@ -37,8 +37,10 @@ test( "splat initial", async () => {
expect(foo.initial).toEqual([]); expect(foo.initial).toEqual([]);
expect( new Updux({ expect(
new Updux({
initial: 'overriden', initial: 'overriden',
subduxes: { '*': bar }, subduxes: { '*': bar },
}).initial ).toEqual('overriden'); }).initial,
).toEqual('overriden');
}); });

View File

@ -30,7 +30,7 @@ test('subduxes reactions', async () => {
subduxes: { subduxes: {
a: new Updux({ a: new Updux({
initial: 1, initial: 1,
reactions: [() => state => spyA(state)], reactions: [() => (state) => spyA(state)],
actions: { inc: null }, actions: { inc: null },
mutations: { mutations: {
inc: () => (state) => state + 1, inc: () => (state) => state + 1,
@ -45,6 +45,6 @@ test('subduxes reactions', async () => {
store.dispatch.inc(); store.dispatch.inc();
expect(spyA).toHaveBeenCalledTimes(2); expect(spyA).toHaveBeenCalledTimes(2);
expect(spyA).toHaveBeenCalledWith(3) expect(spyA).toHaveBeenCalledWith(3);
expect(spyB).toHaveBeenCalledOnce(); // the original inc initialized the state expect(spyB).toHaveBeenCalledOnce(); // the original inc initialized the state
}); });