memo is rerun but any side effects subscribed are'nt reran

const [jsonData, { mutate: setJsonData }] = createResource(initJsonData);
const [saved, setSaved] = createWritableMemo(
on(jsonData, () => {
console.log("update saved");
return false;
}),
);

createEffect(() => console.log("json data:", jsonData()));
createEffect(() => console.log("saved:", saved()));
const [jsonData, { mutate: setJsonData }] = createResource(initJsonData);
const [saved, setSaved] = createWritableMemo(
on(jsonData, () => {
console.log("update saved");
return false;
}),
);

createEffect(() => console.log("json data:", jsonData()));
createEffect(() => console.log("saved:", saved()));
No description
40 Replies
Danielius
DanieliusOP5mo ago
also note the last saved state was true and the calculation always return false whenever jsonData changes, therfore the effect should rerun bc false !== true bump! btw forgot to mention createWritableMemo is from @solid-primitives/memo
REEEEE
REEEEE5mo ago
what is setting saved to true?
Danielius
DanieliusOP5mo ago
inside of initJsonData
REEEEE
REEEEE5mo ago
so saved is always being set to true?
Danielius
DanieliusOP5mo ago
only the one time the initJsonData is ran
REEEEE
REEEEE5mo ago
and there is no way for whatever is setting saved to true to happen again?
Danielius
DanieliusOP5mo ago
the only way saved is set to true after this is if the prompts to "save" or "save as"
REEEEE
REEEEE5mo ago
right but doesn't initJsonData rerun based on the console logs in your screenshot? Could it be setting it to true again?
Danielius
DanieliusOP5mo ago
but the thing im so confused about is 1. jsonData is loading (undefined ) 2. saved memo is run and set to false 3. side effect for save is ran 4. initJsonData sets saved to true 5. side effect for save is ran 6. jsonData is updated to the initialised value returned by initJsonData 7. saved memo is ran (proven by print log) and is supposed to be set to false 8. side effect for saved is not ran....🤔
REEEEE
REEEEE5mo ago
🤷‍♂️ It seems like jsonData is being updated more than twice which presumably could mean that initJsonData could be running more than twice and it could be setting saved to true again
Danielius
DanieliusOP5mo ago
oh yes it is bc i updated it purposly to see if save would update, which i dint but the update was done using mutate not refetch the last update to be clear
REEEEE
REEEEE5mo ago
update saved is being logged twice after saved is set to true which seems odd. I think the issue could be setting the signal/memo in an async context
Danielius
DanieliusOP5mo ago
also think that setting saved to true before the return caused the problem, bc dont forget the memo reruns when jsonData is updated
async function initJsonData() {
// ...

// jsonData is undefined
setSaved(true);
// jsonData is updated after this and memo should rerun
return jsonData;
}
async function initJsonData() {
// ...

// jsonData is undefined
setSaved(true);
// jsonData is updated after this and memo should rerun
return jsonData;
}
but the confusing part is why isnt the following side effect running after the return jsonData????
createEffect(() => console.log("saved:", saved()));
createEffect(() => console.log("saved:", saved()));
log
update saved
update saved
meaning saved updated, but this side effect is unresponsive?
REEEEE
REEEEE5mo ago
reactivity gets weird with async stuff. Afaik reactivity after an await call can break stuff, but I'm not 100% sure on that if you log inside of `initJsonData, how many times is it run
Danielius
DanieliusOP5mo ago
once, as expected
REEEEE
REEEEE5mo ago
for testing, try to setSaved before any of your await's in initJsonData
Danielius
DanieliusOP5mo ago
ReferenceError, assuming its for setSaved
REEEEE
REEEEE5mo ago
probably
Danielius
DanieliusOP5mo ago
i assume something like this might work instead 🤔
createEffect(() => {
if (!jsonData.loading) setSaved(true);
});
createEffect(() => {
if (!jsonData.loading) setSaved(true);
});
REEEEE
REEEEE5mo ago
or just make saved always derive true or false
Danielius
DanieliusOP5mo ago
wdym? also with this i still dont get a side effect after saved is ran after updating jsonData (after its been initialised already) true !== false why tf is this not responding... @REEEEE do think this is a problem with createWritableMemo and how im always returning false?
REEEEE
REEEEE5mo ago
What values are passed to the function in the memo?
on(jsonData, (...args) => {...}
on(jsonData, (...args) => {...}
Could you log args
Danielius
DanieliusOP5mo ago
No description
Danielius
DanieliusOP5mo ago
to keep it simple i used the callback without on
No description
REEEEE
REEEEE5mo ago
Hmm not too sure, could you make a reproduction in the playground and I can take a look
Danielius
DanieliusOP5mo ago
ok btw, unrelated bu how do i make the libs i import in solid playground have types....
REEEEE
REEEEE5mo ago
afaik it's not possible yet
Danielius
DanieliusOP5mo ago
the lib is added to the import map dang
Danielius
DanieliusOP5mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
REEEEE
REEEEE5mo ago
that's really weird
Danielius
DanieliusOP5mo ago
ye... im honestly lost
REEEEE
REEEEE5mo ago
Might be an issue with createWritableMemo
REEEEE
REEEEE5mo ago
Not sure what's happening but I copy pasted the createWritableMemo code into the playground and it seems to work now... https://playground.solidjs.com/anonymous/6cc41b34-caa6-4f9f-9e9d-512de0ee8c6b
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
Maciek50322
Maciek503225mo ago
There was an issue with createWritableMemo https://github.com/solidjs-community/solid-primitives/issues/633 but it's resolved, so maybe you don't have latest version?
GitHub
createWritableMemo() cannot be set to the same value twice in a r...
Describe the bug Given distinct values A and B, when the value of the writable memo is set to A, later computes as B, and then is set to A again, the writable memo holds value B, even though it sho...
Maciek50322
Maciek503225mo ago
Yeah so that's weird, playground even though having newest version works differently than just copied code. And checked that it isn't about the issue above
Maciek50322
Maciek503225mo ago
So createWritableMemo just doesn't work at all in playground https://playground.solidjs.com/anonymous/ccb67468-0581-4d36-a7c9-0ee5cbac4e4a
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
Danielius
DanieliusOP5mo ago
i do infact have the latest version.... something strage is going on with this function wtaf is going on https://codesandbox.io/p/devbox/silly-rgb-ygt2th take a look ^
Maciek50322
Maciek503225mo ago
the inverse that's really weird. I'll check it locally later as suggested https://discord.com/channels/722131463138705510/958428791124951050/1269900932259381290 maybe these two sandboxes take different priorities in packages if duplication of dependencies is the case
Danielius
DanieliusOP5mo ago
i think i will opt for the something like this for now ig:
const [saved, setSaved] = createSignal(false);

createEffect(() => {
if(!jsonData.loading) setSaved(true);
});
createEffect(() => {
jsonData();
setSaved(false);
});
const [saved, setSaved] = createSignal(false);

createEffect(() => {
if(!jsonData.loading) setSaved(true);
});
createEffect(() => {
jsonData();
setSaved(false);
});
Madaxen86
Madaxen865mo ago
Maybe you could also :
const [_saved, setSaved] = createSignal(false);

const saved = createMemo(() => _saved() || !jsonData.loading)
const [_saved, setSaved] = createSignal(false);

const saved = createMemo(() => _saved() || !jsonData.loading)
Want results from more Discord servers?
Add your server