|
| 1 | +--- |
| 2 | +title: registerCustomSerializable |
| 3 | +sidebar_position: 42 # Use alphabetical order |
| 4 | +--- |
| 5 | + |
| 6 | +# registerCustomSerializable <AvailableFrom version="0.7.0" /> |
| 7 | + |
| 8 | +`registerCustomSerializable` lets you register your own pre-serialization and post-deserialization logic. This is necessary for objects with prototypes different than just `Object.prototype` or some other built-in prototypes like `Map` etc. Worklets can't handle such objects by default to convert into [Serializables](/docs/memory/serializable) hence you need to register them as **Custom Serializables**. This way you can tell Worklets how to transfer your custom data structures between different Runtimes without manually serializing and deserializing them every time. |
| 9 | + |
| 10 | +List of supported types for Serialization can be found [here](/docs/memory/serializable#supported-types). |
| 11 | + |
| 12 | +## Reference |
| 13 | + |
| 14 | +```typescript |
| 15 | +const obj = { a: 42 }; |
| 16 | +Object.setPrototypeOf(obj, console); // because why not |
| 17 | + |
| 18 | +type ConsoleLike = typeof console; |
| 19 | + |
| 20 | +registerCustomSerializable({ |
| 21 | + name: 'ConsoleLike', |
| 22 | + determine(value: object): value is ConsoleLike { |
| 23 | + 'worklet'; |
| 24 | + return Object.getPrototypeOf(value) === console; |
| 25 | + }, |
| 26 | + pack(value: ConsoleLike) { |
| 27 | + 'worklet'; |
| 28 | + return { a: value.a }; // transfer data |
| 29 | + }, |
| 30 | + unpack(value: object) { |
| 31 | + 'worklet'; |
| 32 | + // recreate object with prototype |
| 33 | + return Object.create(console, value); |
| 34 | + }, |
| 35 | +}); |
| 36 | +``` |
| 37 | + |
| 38 | +<details> |
| 39 | +<summary>Type definitions</summary> |
| 40 | + |
| 41 | +```typescript |
| 42 | +function registerCustomSerializable< |
| 43 | + TValue extends object, |
| 44 | + TPacked extends object, |
| 45 | +>(registrationData: RegistrationData<TValue, TPacked>): void; |
| 46 | + |
| 47 | +type RegistrationData<TValue extends object, TPacked = unknown> = { |
| 48 | + name: string; |
| 49 | + determine: (value: object) => value is TValue; |
| 50 | + pack: (value: TValue) => TPacked; |
| 51 | + unpack: (value: TPacked) => TValue; |
| 52 | +}; |
| 53 | +``` |
| 54 | + |
| 55 | +</details> |
| 56 | + |
| 57 | +## Arguments |
| 58 | + |
| 59 | +### registrationData |
| 60 | + |
| 61 | +An object containing the registration data for the Custom Serializable. |
| 62 | + |
| 63 | +Available properties: |
| 64 | + |
| 65 | +| Name | Type | Description | |
| 66 | +| --------- | ------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 67 | +| name | `string` | A unique name for the Custom Serializable. It's used to prevent duplicate registrations of the same Custom Serializable. You will get warned if you attempt to register a Custom Serializable with a name that has already been used. | |
| 68 | +| determine | `(value: object) => value is TValue` | A [worklet](/docs/fundamentals/glossary#worklet) that checks whether a given JavaScript value is of the type handled by this Custom Serializable. | |
| 69 | +| pack | `(value: TValue) => TPacked` | A [worklet](/docs/fundamentals/glossary#worklet) that packs the JavaScript value of type `TValue` into a value that can be serialized by default as [Serializable](/docs/memory/serializable). The function must return a [supported type for Serialization](/docs/memory/serializable#supported-types). | |
| 70 | +| unpack | `(value: TPacked) => TValue` | A [worklet](/docs/fundamentals/glossary#worklet) that unpacks the packed value, after it's been deserialized from it's packed form, back into the JavaScript value of type `TValue`. | |
| 71 | + |
| 72 | +## Motivation |
| 73 | + |
| 74 | +Custom prototypes are bound to a single Runtime and don't exist in other Runtimes. Due to that it's impossible to transfer them between runtimes directly. This is why a pre-serialization (packing) and pre-deserialization (unpacking) logic is required. |
| 75 | + |
| 76 | +Consider the following examples: |
| 77 | + |
| 78 | +<table> |
| 79 | +<tr style={{padding: '20px'}}> |
| 80 | +<td> |
| 81 | + |
| 82 | +```typescript |
| 83 | +const obj = {}; |
| 84 | + |
| 85 | +// because why not |
| 86 | +Object.setPrototypeOf(obj, console); |
| 87 | + |
| 88 | +scheduleOnUI(() => { |
| 89 | + // This will throw because `obj` |
| 90 | + // had a custom prototype and |
| 91 | + // it couldn't be serialized. |
| 92 | + obj.log('Hello!'); |
| 93 | +}); |
| 94 | +``` |
| 95 | + |
| 96 | +</td> |
| 97 | +<td style={{textAlign: 'center', flex: 1}}> |
| 98 | + |
| 99 | +```mermaid |
| 100 | +flowchart TD |
| 101 | + A[obj] -->|automatic serialization| B[no known serialization method, serialized as a dummy value] |
| 102 | + B -->|scheduleOnUI| C[transferred to UI Runtime] |
| 103 | + C -->|automatic deserialization| D[deserialized from a dummy value] |
| 104 | + D -->|"obj.log('Hello!')"| E["<b>Error: serialization failed</b>"] |
| 105 | + style E fill:#ff000022,stroke:#990000 |
| 106 | +``` |
| 107 | + |
| 108 | +</td> |
| 109 | +</tr> |
| 110 | +<tr> |
| 111 | +<td> |
| 112 | + |
| 113 | +```typescript |
| 114 | +const obj = {}; |
| 115 | + |
| 116 | +// because why not |
| 117 | +Object.setPrototypeOf(obj, console); |
| 118 | + |
| 119 | +type ConsoleLike = typeof console; |
| 120 | + |
| 121 | +registerCustomSerializable({ |
| 122 | + name: 'ConsoleLike', |
| 123 | + determine(value: object): value is ConsoleLike { |
| 124 | + 'worklet'; |
| 125 | + return Object.getPrototypeOf(value) === console; |
| 126 | + }, |
| 127 | + pack(value: ConsoleLike) { |
| 128 | + 'worklet'; |
| 129 | + // We don't need to transfer any data, |
| 130 | + // so we can pack it to an empty object. |
| 131 | + return {}; |
| 132 | + }, |
| 133 | + unpack(value: object) { |
| 134 | + 'worklet'; |
| 135 | + // We can recreate the original object. |
| 136 | + return Object.create(console); |
| 137 | + }, |
| 138 | +}); |
| 139 | + |
| 140 | +scheduleOnUI(() => { |
| 141 | + obj.log('Hello!'); // prints 'Hello!' |
| 142 | +}); |
| 143 | +``` |
| 144 | + |
| 145 | +</td> |
| 146 | +<td style={{textAlign: 'center', flex: 1}}> |
| 147 | + |
| 148 | +```mermaid |
| 149 | +flowchart TD |
| 150 | + A[obj] -->|automatic serialization| B[identified as a Custom Serializable '<i>ConsoleLike</i>'] |
| 151 | + subgraph S1 [ ] |
| 152 | + B -->|pack| C[packed with Custom Serializable's pack method] |
| 153 | + end |
| 154 | + C -->|actual serialization| D[serialized packed object as a default supported type] |
| 155 | + D -->|scheduleOnUI| E[transferred to Runtime B] |
| 156 | + E -->|automatic deserialization| F[deserialized to the packed object] |
| 157 | + F -->|unpack| G |
| 158 | + subgraph S2 [ ] |
| 159 | + G[unpacked with Custom Serializable's unpack method] |
| 160 | + end |
| 161 | + G -->|"obj.log('Hello!')"| H["<b>'Hello!'</b> printed to console"] |
| 162 | + style H fill:#00ff0022,stroke:#009900 |
| 163 | +``` |
| 164 | + |
| 165 | +</td> |
| 166 | +</tr> |
| 167 | +</table> |
| 168 | + |
| 169 | +## Remarks |
| 170 | + |
| 171 | +- To use Custom Serializables which require `new` keyword for instantiation, you need to [disable Worklet Classes](/docs/worklets-babel-plugin/plugin-options#disableworkletclasses-) option in Worklets Babel plugin configuration. |
| 172 | +- Custom Serializables are global and shared between all [Worklet Runtimes](/docs/fundamentals/runtimeKinds#worklet-runtime). Once you register a Custom Serializable, it will be available in all Runtimes. |
| 173 | +- You can use `registerCustomSerializable` only on the [RN Runtime](/docs/fundamentals/runtimeKinds#rn-runtime). |
0 commit comments