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,7 +76,8 @@ export class Updux {
}
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.#localInitial) return this.#localInitial;
@ -184,18 +185,17 @@ export class Updux {
}
addReaction(reaction) {
this.#localReactions = [ ...this.#localReactions, reaction ]
this.#localReactions = [...this.#localReactions, reaction];
}
subscribeTo(store, subscription) {
const localStore = augmentMiddlewareApi(
{
...store,
subscribe: (subscriber) =>
this.subscribeTo(store, subscriber), // TODO not sure
subscribe: (subscriber) => this.subscribeTo(store, subscriber), // TODO not sure
},
this.actions,
this.selectors
this.selectors,
);
const subscriber = subscription(localStore);
@ -221,7 +221,7 @@ export class Updux {
subscribeAll(store) {
let results = this.#localReactions.map((sub) =>
this.subscribeTo(store, sub)
this.subscribeTo(store, sub),
);
for (const subdux in this.#subduxes) {
@ -237,10 +237,10 @@ export class Updux {
return {
unsub: () => results.forEach(({ unsub }) => unsub()),
subscriberMemoized: () =>
results.forEach(({ subscriberMemoized}) => subscriberMemoized()),
subscriber: () =>
results.forEach(({ subscriber }) => subscriber()
results.forEach(({ subscriberMemoized }) =>
subscriberMemoized(),
),
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({
initial: { id: 0 },
});
@ -37,8 +37,10 @@ test( "splat initial", async () => {
expect(foo.initial).toEqual([]);
expect( new Updux({
expect(
new Updux({
initial: 'overriden',
subduxes: { '*': bar },
}).initial ).toEqual('overriden');
}).initial,
).toEqual('overriden');
});

View File

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