Fexelitche
Fexelitche
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
Nvm, its just stores, not JS. Object equivalence is lost because all values are proxied. Is there a way to retrieve the underlying value so that one can use object equivalence?
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
I'm at a loss. Damn.
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
Object equavalence fails, string version fails, weak object equivalence fails ("==")...
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
Yeah well I'm still trying to wrap my head around JS it seems... What is going on here even?
const index = proxy.findIndex(e => {
console.log("e", JSON.stringify(e), "element", JSON.stringify(element), " === ", e === element);
return e == element
});

Logs:
e {"id":1} element {"id":1} === false
index -1
e {"id":2} element {"id":2} === false
index -1
e {"id":1,"value":"old"} element {"id":1,"value":"old"} === false
index -1
const index = proxy.findIndex(e => {
console.log("e", JSON.stringify(e), "element", JSON.stringify(element), " === ", e === element);
return e == element
});

Logs:
e {"id":1} element {"id":1} === false
index -1
e {"id":2} element {"id":2} === false
index -1
e {"id":1,"value":"old"} element {"id":1,"value":"old"} === false
index -1
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
Also, with a purpose build primitive, maybe some of those less ideal cases can be remedied? Sure you can't for the generic store, but if you know you're dealing with a top-level array, you could maybe mitigate the mayhem?
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
Not sure I follow. Isn't that exactly what you'd want. If there is a change to some object at index i, you would want an update for that index?
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
As for "non-change" - it could be an in-place change, like changing a sub-field on the object. Reallocating should make sure that in-place changes still trigger updates.
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
A snake biting its own tail. - The proxy doesn't work, because I mutate in setStore (and wrongly so), and accessing current state of the proxy outside of setStore doesn't work, because I mutate in setStore...
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
Ahh. So its an ouroboros
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
I'd like to do that too, but the proxy doesn't seem to update synchronously, so I've been unable to retrieve the most recent state of the array. I'll try a rewrite using the proxy, but I've had nothing but issues
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
That's optional. You can mutate the existing or replace it. Admittedly, that fails too for object replace
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
That is not what I'm experiencing. Take the nested if statement here, currently I'm reallocating - which I shouldn't have to if reactivity extended arbitrarily, but it fails to cause an update regardless of what I do aside from reallocating everything:
const [proxy, setStore] = createStore<T[]>(initValue ?? []);

mutateByPredicate: (predicate: (element: T) => boolean, mutator: (element: T) => T | void) => {
setStore((prev) => {
for (let i = 0; i < prev.length; i++) {
if (predicate(prev[i])) {
const newElement = mutator(prev[i]);
if (newElement !== undefined && newElement !== prev[i]) {
prev[i] = newElement;
} else {
prev[i] = {...prev[i]};
}
}
}
return prev;
});
},
const [proxy, setStore] = createStore<T[]>(initValue ?? []);

mutateByPredicate: (predicate: (element: T) => boolean, mutator: (element: T) => T | void) => {
setStore((prev) => {
for (let i = 0; i < prev.length; i++) {
if (predicate(prev[i])) {
const newElement = mutator(prev[i]);
if (newElement !== undefined && newElement !== prev[i]) {
prev[i] = newElement;
} else {
prev[i] = {...prev[i]};
}
}
}
return prev;
});
},
Admittedly, writing tests for this have been... difficult, so the issue might not be the current implementation:
it('mutateByPredicate should be reactive (object replace)', () => {
return testEffect((dispose) => {
const store = createArrayStore<{ id: number }>([{ id: 0 }, { id: 1 }]);
let triggerCount = 0;

createEffect(() => {
const items = store.get();
const currentID = items[0].id;
if(currentID === 10) {
expect(triggerCount).toBe(10);
dispose();
}
const mutationCount = store.mutateByPredicate(
(el) => el.id === currentID,
(el) => ({ id: el.id + 1 })
);
expect(mutationCount).toBe(1);
triggerCount++;
});
});
});
it('mutateByPredicate should be reactive (object replace)', () => {
return testEffect((dispose) => {
const store = createArrayStore<{ id: number }>([{ id: 0 }, { id: 1 }]);
let triggerCount = 0;

createEffect(() => {
const items = store.get();
const currentID = items[0].id;
if(currentID === 10) {
expect(triggerCount).toBe(10);
dispose();
}
const mutationCount = store.mutateByPredicate(
(el) => el.id === currentID,
(el) => ({ id: el.id + 1 })
);
expect(mutationCount).toBe(1);
triggerCount++;
});
});
});
^ fail by time out
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
One last question as for how store trees work: How deep does it go? It seems like values are only tracked one layer down from root. Meaning if you had a store of an object with an array (so the entire array can be reactive), and objects in that array, the array itself is tracked, but no fields in each object is?
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
Would optionally enabling root-node checks be a good candidate for a PR?
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
Or, reversely, encapsulate the store in a signal, so that you can replace the entire thing and react to that instead.
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
So that it can be concidered a leaf?
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
Hang on, wouldn't that also mean, that since the top-level node, is never checked (concidered immutable), that if you intend to sort an array and react to that, it has to be encapsulated in some other top-level node?
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
Darn. Oh well, - as for the arborial behaviour, wouldn't that break on sort operations on arrays? Since the leaves arents reallocated, but reordered. Something you would want an update for
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
Like in a case where I know I want a reactive update, even though no "leaves" have been mutated?
62 replies
SSolidJS
Created by Fexelitche on 10/9/2024 in #support
How do one test a custom primitives?
Well that's... actually pretty neat, but I'd never had guessed that from the docs. Any way to turn on a top-level check?
62 replies