osra 0.6.6 → 0.6.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -106,7 +106,7 @@ Transports are either **structured-clone** (Worker, Window, MessagePort, SharedW
106
106
  | `Date`, `BigInt`, `Map`, `Set` | ✅ | ✅ | |
107
107
  | `ArrayBuffer`, `Int8Array`, `Uint8Array`, `Uint8ClampedArray`, `Int16Array`, `Uint16Array`, `Int32Array`, `Uint32Array`, `Float16Array`, `Float32Array`, `Float64Array`, `BigInt64Array`, `BigUint64Array` | ✅ | ✅ | |
108
108
  | `Error` + subclasses | ✅ | ✅ | built-ins errors properly preserve their subclass; custom error classes becomes generic `Error` |
109
- | `Symbol` | ✅ | ✅ | `Symbol.for` properly preserves the Symbol's key; `Symbol()` is automatically wrapped with [`identity()`](#identity) |
109
+ | `Symbol` | ✅ | ✅ | `Symbol.for` properly preserves the Symbol's key; `Symbol()` automatically rides the [`identity()`](#identity) machinery |
110
110
  | `RegExp` | ✅ | ❌ | |
111
111
  | `SharedArrayBuffer` | ✅ | ❌ | |
112
112
  | Function | ✅ | ✅ | becomes `(...args) => Promise<result>`; arguments and results are properly handled too |
@@ -128,14 +128,15 @@ Transports are either **structured-clone** (Worker, Window, MessagePort, SharedW
128
128
 
129
129
  ## Identity
130
130
 
131
- `identity(value)` preserves reference equality across contexts, sending the same identity wrapped value twice results in the same object reference on the peer.
131
+ `identity(value)` marks a value so it keeps its reference across contexts: the peer sees one object for it however many times it is sent, and the mark stays on the value rather than on that one send.
132
132
 
133
133
  `worker.ts`
134
134
  ```ts
135
135
  import { expose, identity } from 'osra'
136
136
 
137
- const value = { foo: 'bar' }
138
- const payload = { value, ref1: identity(value), ref2: identity(value) }
137
+ const plain = { foo: 'bar' }
138
+ const shared = { foo: 'bar' }
139
+ const payload = { plain1: plain, plain2: plain, ref1: identity(shared), ref2: shared }
139
140
 
140
141
  expose(payload, { transport: globalThis })
141
142
  export type Payload = typeof payload
@@ -147,10 +148,23 @@ import type { Payload } from './worker'
147
148
  import { expose } from 'osra'
148
149
 
149
150
  const worker = new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' })
150
- const { value, ref1, ref2 } = await expose<Payload>({}, { transport: worker })
151
+ const { plain1, plain2, ref1, ref2 } = await expose<Payload>({}, { transport: worker })
151
152
 
152
- value === ref1 // false
153
- ref1 === ref2 // true
153
+ plain1 === plain2 // false, the same object in two places arrives as two copies
154
+ ref1 === ref2 // true, marking it once was enough
155
+ ```
156
+
157
+ Because the mark travels with the value, a peer can send it back, or pass it on to a third context and have it come back from there, without marking anything itself. Each context resolves it to exactly what it handed out, down to the original reference.
158
+
159
+ ```ts
160
+ const settings = { theme: 'dark' }
161
+
162
+ expose({
163
+ getSettings: async () => identity(settings),
164
+ saveSettings: async (saved: typeof settings) => {
165
+ saved === settings // true, however many contexts it went through
166
+ }
167
+ }, { transport: globalThis })
154
168
  ```
155
169
 
156
170