Skip to content

Commit 02fa5ce

Browse files
committed
Move getters into nested prop in store snapshot
1 parent 817aaf2 commit 02fa5ce

File tree

5 files changed

+38
-28
lines changed

5 files changed

+38
-28
lines changed

.changeset/fresh-keys-enjoy.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ const store = createStore({
2020
});
2121

2222
// Getters are available on store snapshots
23-
var snapshot = store.getSnapshot();
24-
assert.equal(snapshot.doubled, 4);
25-
assert.equal(snapshot.squared, 4);
26-
assert.equal(snapshot.sum, 8);
23+
var getters = store.getSnapshot().getters;
24+
assert.equal(getters.doubled, 4);
25+
assert.equal(getters.squared, 4);
26+
assert.equal(getters.sum, 8);
2727

2828
// Automatically update when context changes
2929
store.send({ type: 'inc' });
30-
var snapshot = store.getSnapshot();
31-
assert.equal(snapshot.doubled, 6);
32-
assert.equal(snapshot.squared, 36);
33-
assert.equal(snapshot.sum, 42);
30+
var getters = store.getSnapshot().getters;
31+
assert.equal(getters.doubled, 6);
32+
assert.equal(getters.squared, 36);
33+
assert.equal(getters.sum, 42);
3434
```
3535

3636
Key features:

packages/xstate-store/src/fromStore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function fromStore<
7777
const newSnapshot = {
7878
...snapshot,
7979
context: newContext,
80-
...computeGetters(newContext, getters)
80+
getters: computeGetters(newContext, getters)
8181
} as StoreSnapshot<TContext, TGetters>;
8282

8383
for (const effect of effects) {
@@ -101,7 +101,7 @@ export function fromStore<
101101
context,
102102
output: undefined,
103103
error: undefined,
104-
...computeGetters(context, getters)
104+
getters: computeGetters(context, getters)
105105
} satisfies StoreSnapshot<TContext, TGetters>;
106106
},
107107
getPersistedSnapshot: (s) => s,

packages/xstate-store/src/store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function createStoreCore<
8282
status: 'active',
8383
output: undefined,
8484
error: undefined,
85-
...computeGetters(initialContext, getters)
85+
getters: computeGetters(initialContext, getters)
8686
};
8787
let currentSnapshot: StoreSnapshot<TContext, TGetters> = initialSnapshot;
8888

@@ -105,7 +105,7 @@ function createStoreCore<
105105
currentSnapshot = {
106106
...currentSnapshot,
107107
context: newContext,
108-
...computeGetters(newContext, getters)
108+
getters: computeGetters(newContext, getters)
109109
} as StoreSnapshot<TContext, TGetters>;
110110

111111
inspectionObservers.get(store)?.forEach((observer) => {

packages/xstate-store/src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export type StoreSnapshot<
5858
TGetters extends Record<string, (context: TContext, getters: any) => any>
5959
> = Snapshot<unknown> & {
6060
context: TContext;
61-
} & ResolvedGetters<TGetters>;
61+
getters: StoreGetters<TContext, TGetters>;
62+
};
6263

6364
/**
6465
* An actor-like object that:

packages/xstate-store/test/store.test.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,12 @@ describe('getters', () => {
544544
}
545545
});
546546

547-
expect(store.getSnapshot().doubled).toBe(4);
548-
expect(store.getSnapshot().squared).toBe(4);
547+
expect(store.getSnapshot().getters.doubled).toBe(4);
548+
expect(store.getSnapshot().getters.squared).toBe(4);
549549

550550
store.send({ type: 'inc' });
551-
expect(store.getSnapshot().doubled).toBe(6);
552-
expect(store.getSnapshot().squared).toBe(9);
551+
expect(store.getSnapshot().getters.doubled).toBe(6);
552+
expect(store.getSnapshot().getters.squared).toBe(9);
553553
});
554554

555555
it('handles getter dependencies', () => {
@@ -567,10 +567,10 @@ describe('getters', () => {
567567
}
568568
});
569569

570-
expect(store.getSnapshot().total).toBeCloseTo(22); // 20 + 2 = 22
570+
expect(store.getSnapshot().getters.total).toBeCloseTo(22); // 20 + 2 = 22
571571

572572
store.send({ type: 'updatePrice', value: 20 });
573-
expect(store.getSnapshot().total).toBeCloseTo(44); // 40 + 4 = 44
573+
expect(store.getSnapshot().getters.total).toBeCloseTo(44); // 40 + 4 = 44
574574
});
575575

576576
it('updates getters when context changes', () => {
@@ -587,10 +587,10 @@ describe('getters', () => {
587587
}
588588
});
589589

590-
expect(store.getSnapshot().hasItems).toBe(false);
590+
expect(store.getSnapshot().getters.hasItems).toBe(false);
591591

592592
store.send({ type: 'addItem', item: 'test' });
593-
expect(store.getSnapshot().hasItems).toBe(true);
593+
expect(store.getSnapshot().getters.hasItems).toBe(true);
594594
});
595595

596596
it('works with immer producer', () => {
@@ -608,12 +608,12 @@ describe('getters', () => {
608608
}
609609
});
610610

611-
expect(store.getSnapshot().sum).toBe(3);
612-
expect(store.getSnapshot().product).toBe(2);
611+
expect(store.getSnapshot().getters.sum).toBe(3);
612+
expect(store.getSnapshot().getters.product).toBe(2);
613613

614614
store.send({ type: 'update', a: 3 });
615-
expect(store.getSnapshot().sum).toBe(5);
616-
expect(store.getSnapshot().product).toBe(6);
615+
expect(store.getSnapshot().getters.sum).toBe(5);
616+
expect(store.getSnapshot().getters.product).toBe(6);
617617
});
618618

619619
it('includes getters in inspection snapshots', () => {
@@ -638,9 +638,18 @@ describe('getters', () => {
638638
store.send({ type: 'increment' });
639639

640640
expect(snapshots).toEqual([
641-
expect.objectContaining({ context: { value: 5 }, squared: 25 }),
642-
expect.objectContaining({ context: { value: 6 }, squared: 36 }),
643-
expect.objectContaining({ context: { value: 7 }, squared: 49 })
641+
expect.objectContaining({
642+
context: { value: 5 },
643+
getters: { squared: 25 }
644+
}),
645+
expect.objectContaining({
646+
context: { value: 6 },
647+
getters: { squared: 36 }
648+
}),
649+
expect.objectContaining({
650+
context: { value: 7 },
651+
getters: { squared: 49 }
652+
})
644653
]);
645654
});
646655
});

0 commit comments

Comments
 (0)