-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: custom serialization #7223
base: build/v2
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 9070c30 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Looks great, but what about deserialization those objects? |
That's what the createSerialized$ does |
Does this mean we might be able to serialize functions from other libraries, like vanilla js? |
aca1de8
to
47942f4
Compare
@Varixo @thejackshelton gaah I had forgotten to push the actual commit 😅 |
Yes exactly. You need to add a symbol prop that serializes the value and then provide a deserializer that will get called lazily. |
47942f4
to
9009520
Compare
commit: |
built with Refined Cloudflare Pages Action⚡ Cloudflare Pages Deployment
|
9009520
to
103e3c1
Compare
165ac6d
to
804f01d
Compare
FEAT: `NoSerializeSymbol`: objects that have this defined will not be serialized FEAT: `SerializerSymbol`: objects that have this defined as a function will get it called with the object as a parameter during serialization. The function should return the data that should be serialized. Use this to remove cached data, consolidate things etc.
804f01d
to
9070c30
Compare
: obj instanceof ComputedSignal && | ||
!(obj instanceof SerializerSignal) && | ||
(obj.$invalid$ || fastSkipSerialize(obj)) | ||
? NEEDS_COMPUTATION | ||
: obj.$untrackedValue$; | ||
if (v !== NEEDS_COMPUTATION) { | ||
discoveredValues.push(v); | ||
if (obj instanceof SerializerSignal) { | ||
promises.push( | ||
(obj.$computeQrl$ as any as QRLInternal<SerializerArg<any, any>>) | ||
.resolve() | ||
.then((arg) => { | ||
let data; | ||
if ((arg as any).serialize) { | ||
data = (arg as any).serialize(v); | ||
} | ||
if (data === undefined) { | ||
data = NEEDS_COMPUTATION; | ||
} | ||
serializationResults.set(obj, data); | ||
discoveredValues.push(data); | ||
}) | ||
); | ||
} else { | ||
discoveredValues.push(v); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we could organise it somehow? It is very complicated right now.
What about doing just
if (obj instanceof WrappedSignal) {
} else if (obj instanceof ComputedSignal) {
} else if ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very thorough PR. Awesome work ⚡
@@ -4386,7 +4447,7 @@ Creates a computed signal which is calculated from the given function. A compute | |||
The function must be synchronous and must not have any side effects. | |||
|
|||
```typescript | |||
useComputed$: <T>(qrl: import("./use-computed").ComputedFn<T>) => T extends Promise<any> ? never : import("..").ReadonlySignal<T> | |||
useComputed$: <T>(qrl: ComputedFn<T>) => T extends Promise<any> ? never : ReadonlySignal<T> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason on why the useComputed$ type changes here with useSerializer$?
: obj instanceof ComputedSignal && (obj.$invalid$ || fastSkipSerialize(obj)) | ||
: obj instanceof ComputedSignal && | ||
!(obj instanceof SerializerSignal) && | ||
(obj.$invalid$ || fastSkipSerialize(obj)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The nested ternaries here were a bit difficult to understand.
It looks to me like computed signal now checks if it's not a serializer signal, if so then if the object is invalid, or can skip serialization, then it needs computation. otherwise it grabs the untracked values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also what is v? The current value?
* | ||
* This function must not return a promise. | ||
*/ | ||
deserialize: (data: S | undefined, previous: T | undefined) => T; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So on first render, the previous param is -> initial data / inital (3rd argument param of useSerializer$)?
* | ||
* If you do not provide it, the object will be serialized as `undefined`. | ||
*/ | ||
serialize?: (customObject: T) => S | Promise<S>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's an example of when you may need the deserialize function but not the serialize function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong account 😓
This PR adds symbols to mark objects as (no/yes)serializable via symbols instead of via a Set, adds a symbol prop that will get called on serialization for custom serialization, and provides a new Signal type that lazily manages a non-serializable value.
Note that this is basically the same as
useComputed$
, except that it is invalidated during SSR so that when the value gets read on the client it will always run the compute function.Also note that
useComputed$
now passes the previous value to the compute function, to keep the implementation compact. This seems like it might be useful, should we document this?Everything works, looking for comments on implementation, naming etc.