|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +--- |
| 4 | + |
| 5 | +# createSerializable |
| 6 | + |
| 7 | +`createSerializable` recursively converts JavaScript values into serializable references that can be used on different Runtimes than RN Runtime. |
| 8 | +The reference cannot be manipulated, as it doesn't represent any standard JavaScript object. Changes to the original value don't affect the `Serializable`. |
| 9 | +It handles various data types including primitives, objects, arrays, functions, and special cases like host objects and worklets. |
| 10 | +To prevent misconceptions Worklets library freezes the original value for object-like values and for arrays. |
| 11 | + |
| 12 | + |
| 13 | +<figcaption style={{ textAlign: 'center' }}>Serializable flow diagram</figcaption> |
| 14 | + |
| 15 | +**Functions like `runOnUI`, `runOnRuntime`, `runOnJS`, `executeOnUIRuntimeSync` and `runOnUIAsync` automatically convert values to `Serializable` references.** |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +```tsx |
| 20 | +import { createSerializable, runOnUI } from 'react-native-worklets'; |
| 21 | + |
| 22 | +const object = { |
| 23 | + a: 1, |
| 24 | + b: 2, |
| 25 | + c: 4, |
| 26 | +}; |
| 27 | + |
| 28 | +object.c = 3; // <-- Correct: you can modify the object before serializing it |
| 29 | + |
| 30 | +createSerializable(object); |
| 31 | + |
| 32 | +object.a = 10; // <-- Warning: You can't mutate the object after serializing it |
| 33 | + |
| 34 | +runOnUI(() => { |
| 35 | + object.a = 10; // <-- Warning: You can't mutate the object after serializing it |
| 36 | + console.log(object); // { a: 1, b: 2, c: 3 } |
| 37 | +})(); |
| 38 | +``` |
| 39 | + |
| 40 | +<details> |
| 41 | +<summary>Type definitions</summary> |
| 42 | + |
| 43 | +```typescript |
| 44 | +type SerializableRef<T = unknown> = { |
| 45 | + __serializableRef: true; |
| 46 | + __nativeStateSerializableJSRef: T; |
| 47 | +}; |
| 48 | + |
| 49 | +function createSerializable<T>(value: T): SerializableRef<T> { |
| 50 | + return value as SerializableRef<T>; |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +</details> |
| 55 | + |
| 56 | +## Remarks |
| 57 | + |
| 58 | +- The function automatically detects the type of the input value and applies appropriate serialization strategies. |
| 59 | + |
| 60 | +Supported types: |
| 61 | + | Type | Supported | |
| 62 | + |------|-----------| |
| 63 | + | string | ✅ | |
| 64 | + | number | ✅ | |
| 65 | + | boolean | ✅ | |
| 66 | + | object | ✅ | |
| 67 | + | array | ✅ | |
| 68 | + | function (non-worklet) | ✅ | |
| 69 | + | HostObject | ✅ | |
| 70 | + | worklet | ✅ | |
| 71 | + | Map | ✅ | |
| 72 | + | Set | ✅ | |
| 73 | + | ArrayBuffer | ✅ | |
| 74 | + | RegExp | ✅ | |
| 75 | + | Cyclic objects | ❌ | |
| 76 | + | Objects with custom prototype | ❌ | |
| 77 | + |
| 78 | +- For objects and arrays, it recursively processes nested properties |
| 79 | +- The function includes cycle detection to prevent infinite recursion |
| 80 | +- Objects are frozen after serialization to prevent accidental modifications that won't propagate to the Serializable |
| 81 | +- Functions that aren't worklets are serialized as references to function instances on the respective runtime |
0 commit comments