Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,31 +187,31 @@ export function registerRemoteMethods(
* @return result the callback's return value, with an extra array of transferable objects
* assigned to rimless' private symbol `SYM_TRANSFERABLES`
*
* The `transfer(...)` function can be called with one or more objects to transfer. When
* called with one object, it returns that object. When called with zero or multiple objects,
* it returns an array of the objects. Calling `transfer` will only modify the callback result,
* not the original object itself (or objects themselves).
* The `transfer(...)` function is called with an object to transfer; if there
* are many objects to transfer, you may call it multiple times. It will always
* return the input object. Calling `transfer` only modifies the callback
* result, not the transferred object itself (or objects themselves).
*
* @example
* host.connect({
* foo: (...args) => {
* const foo = new ArrayBuffer(8);
* const bar = new ArrayBuffer(8);
*
* return withTransferable((transfer) => transfer(foo, bar));
* }), // equal to [transfer(foo), transfer(bar)]
* return withTransferable((transfer) => transfer(foo));
* }),
* });
*
* @example
* host.remote.foo(withTransferable((transfer) => ({
* stream: transfer(new ReadableStream()),
* })));
*/
export const withTransferable = <T, V extends object>(cb: (transfer: (transferable: T) => void) => V) => {
const transferables: T[] = [];
const transfer = (...toTransfer: [T, ...T[]]) => {
transferables.push(...toTransfer);
return toTransfer.length === 1 ? toTransfer[0] : toTransfer;
export const withTransferable = <Transferable, Result extends object>(
cb: (transfer: <T extends Transferable>(transferable: T) => T) => Result
) => {
const transferables: Transferable[] = [];
const transfer = <T extends Transferable>(transferable: T) => {
transferables.push(transferable);
return transferable;
};

const result = cb(transfer);
Expand Down
Loading