@quilted/threads
Advanced tools
Comparing version 0.0.0-preview-20230808005228 to 0.0.0-preview-20230808012828
import type { ThreadEncoder } from '../types.ts'; | ||
import { type MemoryRetainer } from '../memory.ts'; | ||
export interface ThreadEncoderOptions { | ||
/** | ||
* Customizes the encoding of each value in a passed message. | ||
*/ | ||
encode?(value: unknown, defaultEncode: (value: unknown) => [any, Transferable[]?]): [any, Transferable[]?]; | ||
/** | ||
* Customizes the decoding of each value in a passed message. | ||
*/ | ||
decode?(value: unknown, defaultDecode: (value: unknown, retainedBy?: Iterable<MemoryRetainer>) => unknown, retainedBy?: Iterable<MemoryRetainer>): unknown; | ||
} | ||
/** | ||
@@ -6,3 +17,3 @@ * Creates an encoder that converts most common JavaScript types into a format | ||
*/ | ||
export declare function createBasicEncoder(): ThreadEncoder; | ||
export declare function createBasicEncoder({ encode: encodeOverride, decode: decodeOverride, }?: ThreadEncoderOptions): ThreadEncoder; | ||
//# sourceMappingURL=basic.d.ts.map |
# @quilted/threads | ||
## 0.0.0-preview-20230808005228 | ||
## 0.0.0-preview-20230808012828 | ||
@@ -9,2 +9,4 @@ ### Patch Changes | ||
- Add back encoding overrides feature | ||
## 1.0.1 | ||
@@ -16,2 +18,4 @@ | ||
- [`0366b6c7`](https://github.com/lemonmade/quilt/commit/0366b6c7bc321054325d036199e75fa7222913de) Thanks [@lemonmade](https://github.com/lemonmade)! - Add back encoding overrides feature | ||
## 1.0.0 | ||
@@ -18,0 +22,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"type": "module", | ||
"version": "0.0.0-preview-20230808005228", | ||
"version": "0.0.0-preview-20230808012828", | ||
"license": "MIT", | ||
@@ -8,0 +8,0 @@ "engines": { |
@@ -21,2 +21,24 @@ import {ENCODE_METHOD} from '../constants.ts'; | ||
export interface ThreadEncoderOptions { | ||
/** | ||
* Customizes the encoding of each value in a passed message. | ||
*/ | ||
encode?( | ||
value: unknown, | ||
defaultEncode: (value: unknown) => [any, Transferable[]?], | ||
): [any, Transferable[]?]; | ||
/** | ||
* Customizes the decoding of each value in a passed message. | ||
*/ | ||
decode?( | ||
value: unknown, | ||
defaultDecode: ( | ||
value: unknown, | ||
retainedBy?: Iterable<MemoryRetainer>, | ||
) => unknown, | ||
retainedBy?: Iterable<MemoryRetainer>, | ||
): unknown; | ||
} | ||
/** | ||
@@ -26,3 +48,6 @@ * Creates an encoder that converts most common JavaScript types into a format | ||
*/ | ||
export function createBasicEncoder(): ThreadEncoder { | ||
export function createBasicEncoder({ | ||
encode: encodeOverride, | ||
decode: decodeOverride, | ||
}: ThreadEncoderOptions = {}): ThreadEncoder { | ||
return { | ||
@@ -35,7 +60,29 @@ encode, | ||
function encode( | ||
interface EncodeContext { | ||
api: ThreadEncoderApi; | ||
seen: Map<any, EncodeResult>; | ||
encode: Parameters<NonNullable<ThreadEncoderOptions['encode']>>[1]; | ||
} | ||
function encode(value: unknown, api: ThreadEncoderApi): EncodeResult { | ||
const context: EncodeContext = { | ||
api, | ||
seen: new Map(), | ||
encode: (value: any) => encodeInternal(value, context, true), | ||
}; | ||
return encodeInternal(value, context); | ||
} | ||
function encodeInternal( | ||
value: unknown, | ||
api: ThreadEncoderApi, | ||
seen: Map<any, EncodeResult> = new Map(), | ||
context: EncodeContext, | ||
isFromOverride = false, | ||
): EncodeResult { | ||
const {seen, api, encode} = context; | ||
if (!isFromOverride && encodeOverride) { | ||
return encodeOverride(value, encode); | ||
} | ||
if (value == null) return [value]; | ||
@@ -51,3 +98,6 @@ | ||
const encodeValue = (value: any) => { | ||
const [fieldValue, nestedTransferables = []] = encode(value, api, seen); | ||
const [fieldValue, nestedTransferables = []] = encodeInternal( | ||
value, | ||
context, | ||
); | ||
transferables.push(...nestedTransferables); | ||
@@ -154,2 +204,7 @@ return fieldValue; | ||
interface DecodeContext { | ||
api: ThreadEncoderApi; | ||
decode: Parameters<NonNullable<ThreadEncoderOptions['decode']>>[1]; | ||
} | ||
function decode( | ||
@@ -159,3 +214,23 @@ value: unknown, | ||
retainedBy?: Iterable<MemoryRetainer>, | ||
) { | ||
const context: DecodeContext = { | ||
api, | ||
decode: (value: any) => decodeInternal(value, context, retainedBy, true), | ||
}; | ||
return decodeInternal(value, context); | ||
} | ||
function decodeInternal( | ||
value: unknown, | ||
context: DecodeContext, | ||
retainedBy?: Iterable<MemoryRetainer>, | ||
isFromOverride = false, | ||
): any { | ||
const {api, decode} = context; | ||
if (!isFromOverride && decodeOverride) { | ||
return decodeOverride(value, decode, retainedBy); | ||
} | ||
if (typeof value === 'object') { | ||
@@ -167,3 +242,3 @@ if (value == null) { | ||
if (Array.isArray(value)) { | ||
return value.map((value) => decode(value, api, retainedBy)); | ||
return value.map((value) => decodeInternal(value, context, retainedBy)); | ||
} | ||
@@ -186,4 +261,4 @@ | ||
(value as {[MAP]: [any, any]})[MAP].map(([key, value]) => [ | ||
decode(key, api, retainedBy), | ||
decode(value, api, retainedBy), | ||
decodeInternal(key, context, retainedBy), | ||
decodeInternal(value, context, retainedBy), | ||
]), | ||
@@ -196,3 +271,3 @@ ); | ||
(value as {[SET]: any[]})[SET].map((entry) => | ||
decode(entry, api, retainedBy), | ||
decodeInternal(entry, context, retainedBy), | ||
), | ||
@@ -222,3 +297,7 @@ ); | ||
} else { | ||
result[key] = decode((value as any)[key], api, retainedBy); | ||
result[key] = decodeInternal( | ||
(value as any)[key], | ||
context, | ||
retainedBy, | ||
); | ||
} | ||
@@ -225,0 +304,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
253598
4500