@quilted/threads
Advanced tools
Comparing version 1.0.0 to 1.0.1
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 |
export { createThreadSignal } from './signals/create.ts'; | ||
export { acceptThreadSignal } from './signals/accept.ts'; | ||
export { acceptThreadSignal, isThreadSignal } from './signals/accept.ts'; | ||
export type { ThreadSignal } from './signals/types.ts'; | ||
//# sourceMappingURL=signals.d.ts.map |
@@ -18,2 +18,6 @@ import { type Signal } from '@preact/signals-core'; | ||
}): Signal<T>; | ||
/** | ||
* Returns `true` if the passed object is a `ThreadSignal`. | ||
*/ | ||
export declare function isThreadSignal<T = unknown>(value?: unknown): value is ThreadSignal<T>; | ||
//# sourceMappingURL=accept.d.ts.map |
# @quilted/threads | ||
## 1.0.1 | ||
### Patch Changes | ||
- [`c3f3298b`](https://github.com/lemonmade/quilt/commit/c3f3298b4458d939c0bfc7b83fe6ce120f4fbde8) Thanks [@lemonmade](https://github.com/lemonmade)! - Fix missing `isThreadSignal()` utility | ||
- [`0366b6c7`](https://github.com/lemonmade/quilt/commit/0366b6c7bc321054325d036199e75fa7222913de) Thanks [@lemonmade](https://github.com/lemonmade)! - Add back encoding overrides feature | ||
## 1.0.0 | ||
@@ -4,0 +12,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"type": "module", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"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 @@ } |
export {createThreadSignal} from './signals/create.ts'; | ||
export {acceptThreadSignal} from './signals/accept.ts'; | ||
export {acceptThreadSignal, isThreadSignal} from './signals/accept.ts'; | ||
export type {ThreadSignal} from './signals/types.ts'; |
@@ -59,1 +59,15 @@ import {signal as createSignal, type Signal} from '@preact/signals-core'; | ||
} | ||
/** | ||
* Returns `true` if the passed object is a `ThreadSignal`. | ||
*/ | ||
export function isThreadSignal<T = unknown>( | ||
value?: unknown, | ||
): value is ThreadSignal<T> { | ||
return ( | ||
value != null && | ||
typeof value === 'object' && | ||
'initial' in value && | ||
typeof (value as any).start === 'function' | ||
); | ||
} |
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
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
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
253442
4500
0