mwg API
    Preparing search index...

    Interface Handles<T>

    Numeric leases for values a plain-data command cannot carry.

    A simulation command must survive structuredClone, so anything live (a callback, an externally-owned RNG stream, a world-query closure) travels as a numeric id resolved inside the rule. This is the one-line container for that recipe: put a value, carry the id in the command, get it in the rule, and drop it when done. with scopes the lease so the drop runs even when the rule throws.

    import { createHandles } from '@datamoc/mwg/core';

    const handles = createHandles<() => number>();
    const result = handles.with(() => 7, (id) => {
    const fn = handles.get(id);
    return fn();
    });
    console.log(result); // 7
    interface Handles<T> {
        size: number;
        clear(): void;
        drop(id: number): void;
        get(id: number): T;
        put(value: T): number;
        with<R>(value: T, body: (id: number) => R): R;
    }

    Type Parameters

    • T
    Index
    size: number

    the number of values currently leased

    • ends a lease; unknown ids are ignored

      Parameters

      • id: number

      Returns void

    • resolves a leased id; throws when the id is unknown or was dropped

      Parameters

      • id: number

      Returns T

    • stores value and returns its numeric lease id

      Parameters

      • value: T

      Returns number

    • Leases value for body, dropping the id afterwards even when body throws. The id is only valid inside body.

      Type Parameters

      • R

      Parameters

      • value: T
      • body: (id: number) => R

      Returns R