S
SolidJS10mo ago
skrebbel

captureStoreUpdates vs reconcile

Hi! I'm trying to get a stream of immutable object values from a store. The store is frequently updated from a data source that sends big chunky records with minimal actual changes, so I use reconcile to keep the amount of signalled changes to a minimum. This works very well, but the captureStoreUpdates primitive from https://primitives.solidjs.community/package/deep somehow doesn't play ball and I can't figure out why not.
const [state, setState] = createStore({ foo: { dummy: "a", count: 1 } });
const increment = () =>
setState(
reconcile(
{ foo: { dummy: "a", count: state.foo.count + 1 } },
{ merge: true },
),
);

const getDelta = captureStoreUpdates(state as any);
createEffect(() => {
const delta = getDelta();

// after an increment(), i'd expect to get:
// [{path: ["foo", "count"], value: 2}]
// but i get the entire reconciled value:
// [{path: ["foo"], value: {dummy: "a", count: 2}}]
console.log("delta:", delta);
});
const [state, setState] = createStore({ foo: { dummy: "a", count: 1 } });
const increment = () =>
setState(
reconcile(
{ foo: { dummy: "a", count: state.foo.count + 1 } },
{ merge: true },
),
);

const getDelta = captureStoreUpdates(state as any);
createEffect(() => {
const delta = getDelta();

// after an increment(), i'd expect to get:
// [{path: ["foo", "count"], value: 2}]
// but i get the entire reconciled value:
// [{path: ["foo"], value: {dummy: "a", count: 2}}]
console.log("delta:", delta);
});
Full playground example: https://playground.solidjs.com/anonymous/f5d92603-c66c-4d00-82ba-9cce9ca452fe Is there any way to get captureStoreUpdates to only see the data that actually changed, post reconcile, and to not have it assume that the entire object changed?
4 Replies
thetarnav
thetarnav10mo ago
captureStoreUpdates gives you path to the object that has changed not the property that is by design because only that information can be tracked relatively easily tracking properties requires diffing the objects if you want to do that, you have to do it yourself by maintaining a copy of the state, and diffing the object at path returned from getDelta Also {merge: true} does nothing in this example its for merging arrays without item ids I'm not exactly sure why you are using captureStoreUpdates with reconcile, but if you know your own data structure you may want to reconcile yourself by recursively diffing the current state and the new state to know where the changes need to happen unless your data is a black box that can be anything
skrebbel
skrebbelOP10mo ago
if you know your own data structure you may want to reconcile yourself by recursively diffing the current state and the new state to know where the changes need to happen
Right! OK so I think this means that I completely misunderstand what reconcile does - cause I thought it did exactly this! If it doesn't do "recursively diffing the current state", then what does it do? The way I read the docs (also misreading the merge parameter description) is that it walks both trees and applies any differences at the leaves to the value in the store. Sidenote: I really appreciate the work you did on deep! And the effort you're taking to respond to me! Thanks ❤️
thetarnav
thetarnav10mo ago
You’re not misunderstanding reconcile, it does what you say. It’s just that it’s not meant to give you information about what has been updated, which is why you are trying to resort to using another helper to get to that information. I’m just saying that if you do the reconciliation yourself it will be not only more performant but also give you exact information about what has changed.
skrebbel
skrebbelOP10mo ago
thanks!
Want results from more Discord servers?
Add your server