-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: Serializable docs
#8043
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
Merged
Merged
feat: Serializable docs
#8043
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5217ae0
Mark the old API as obsolete in the documentation.
skusnierz a8c7557
Add createSerializable page
skusnierz 4ccacea
Add isSerializableRef page
skusnierz c2636d7
Update glossary
skusnierz c31e77b
adjust to review
skusnierz 52ea328
adjust to review
skusnierz cd2e826
Remove PlatformCompatibility component
skusnierz 9bc7801
Remove PlatformCompatibility from MDXComponents
skusnierz 3470221
adjust to review
skusnierz fcf3e29
Merge branch 'main' into @skusnierz/serializable-docs
skusnierz f0ec61f
Merge branch 'main' into @skusnierz/serializable-docs
skusnierz 4082881
adjust to review
skusnierz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| --- | ||
| sidebar_position: 1 | ||
| --- | ||
|
|
||
| # createSerializable | ||
|
|
||
| `createSerializable` recursively converts JavaScript values into serializable references that can be used on different Runtimes than RN Runtime. | ||
| The reference cannot be manipulated, as it doesn't represent any standard JavaScript object. Changes to the original value don't affect the `Serializable`. | ||
| It handles various data types including primitives, objects, arrays, functions, and special cases like host objects and worklets. | ||
| To prevent misconceptions Worklets library freezes the original value for object-like values and for arrays. | ||
|
|
||
|  | ||
| <figcaption style={{ textAlign: 'center' }}>Serializable flow diagram</figcaption> | ||
|
|
||
| **Functions like `runOnUI`, `runOnRuntime`, `runOnJS`, `executeOnUIRuntimeSync` and `runOnUIAsync` automatically convert values to `Serializable` references.** | ||
|
|
||
| ## Usage | ||
|
|
||
| ```tsx | ||
| import { createSerializable, runOnUI } from 'react-native-worklets'; | ||
|
|
||
| const object = { | ||
| a: 1, | ||
| b: 2, | ||
| c: 4, | ||
| }; | ||
|
|
||
| object.c = 3; // <-- Correct: you can modify the object before serializing it | ||
|
|
||
| createSerializable(object); | ||
|
|
||
| object.a = 10; // <-- Warning: You can't mutate the object after serializing it | ||
|
|
||
| runOnUI(() => { | ||
| object.a = 10; // <-- Warning: You can't mutate the object after serializing it | ||
| console.log(object); // { a: 1, b: 2, c: 3 } | ||
| })(); | ||
| ``` | ||
|
|
||
| <details> | ||
| <summary>Type definitions</summary> | ||
|
|
||
| ```typescript | ||
| type SerializableRef<T = unknown> = { | ||
| __serializableRef: true; | ||
| __nativeStateSerializableJSRef: T; | ||
| }; | ||
|
|
||
| function createSerializable<T>(value: T): SerializableRef<T> { | ||
| return value as SerializableRef<T>; | ||
| } | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| ## Remarks | ||
|
|
||
| - The function automatically detects the type of the input value and applies appropriate serialization strategies. | ||
|
|
||
| Supported types: | ||
| | Type | Supported | | ||
| |------|-----------| | ||
| | string | ✅ | | ||
| | number | ✅ | | ||
| | boolean | ✅ | | ||
| | object | ✅ | | ||
| | array | ✅ | | ||
| | function (non-worklet) | ✅ | | ||
| | HostObject | ✅ | | ||
| | worklet | ✅ | | ||
| | Map | ✅ | | ||
| | Set | ✅ | | ||
| | ArrayBuffer | ✅ | | ||
| | RegExp | ✅ | | ||
| | Cyclic objects | ❌ | | ||
| | Objects with custom prototype | ❌ | | ||
|
|
||
| - For objects and arrays, it recursively processes nested properties | ||
| - The function includes cycle detection to prevent infinite recursion | ||
| - Objects are frozen after serialization to prevent accidental modifications that won't propagate to the Serializable | ||
| - Functions that aren't worklets are serialized as references to function instances on the respective runtime |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| --- | ||
| sidebar_position: 2 | ||
| --- | ||
|
|
||
| # isSerializableRef | ||
|
|
||
| `isSerializableRef` checks if a value is a serializable reference. | ||
|
|
||
| ## Reference | ||
|
|
||
| ```javascript | ||
| import { isSerializableRef, createSerializable } from 'react-native-worklets'; | ||
|
|
||
| const object = { | ||
| a: 1, | ||
| }; | ||
|
|
||
| const serializableRef = createSerializable(object); | ||
|
|
||
| console.log(isSerializableRef(serializableRef)); // true | ||
| ``` | ||
|
|
||
| <details> | ||
| <summary>Type definitions</summary> | ||
|
|
||
| ```typescript | ||
| function isSerializableRef(value: unknown): value is SerializableRef | ||
| ``` | ||
| </details> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| sidebar_position: 1 | ||
| sidebar_position: 3 | ||
| --- | ||
|
|
||
| # makeShareable | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 0 additions & 46 deletions
46
packages/docs-worklets/src/components/PlatformCompatibility/index.tsx
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.