happy-rusty
    Preparing search index...

    Interface FnOnce<A, R>

    A function wrapper that can only be called once.

    After the first invocation, the function is consumed and cannot be called again. This mirrors Rust's FnOnce trait, which represents closures that take ownership of captured variables and can only be called once.

    Use cases:

    • One-time callbacks (cleanup functions, event handlers)
    • Ensuring certain operations execute exactly once
    • Resource disposal patterns

    1.8.0

    // Basic usage
    const greet = FnOnce((name: string) => `Hello, ${name}!`);
    console.log(greet.call('World')); // 'Hello, World!'
    // greet.call('Again'); // Throws Error: FnOnce has already been consumed

    // Safe call with tryCall
    const cleanup = FnOnce(() => console.log('Cleaned up'));
    cleanup.tryCall(); // Some(undefined), logs 'Cleaned up'
    cleanup.tryCall(); // None, no effect
    // One-time event handler
    const onFirstClick = FnOnce((event: MouseEvent) => {
    console.log('First click at:', event.clientX, event.clientY);
    });

    button.addEventListener('click', (e) => {
    onFirstClick.tryCall(e); // Only handles first click
    });
    // Resource cleanup pattern
    function createConnection(): { close: FnOnce<[], void> } {
    const socket = new WebSocket('ws://example.com');
    return {
    close: FnOnce(() => {
    socket.close();
    console.log('Connection closed');
    })
    };
    }

    const conn = createConnection();
    conn.close.call(); // Closes connection
    conn.close.call(); // Throws - already closed
    interface FnOnce<A extends unknown[], R> {
        "[toStringTag]": "FnOnce";
        call(...args: A): R;
        isConsumed(): boolean;
        toString(): string;
        tryCall(...args: A): Option<R>;
    }

    Type Parameters

    • A extends unknown[]

      Tuple type of the function arguments.

    • R

      Return type of the function.

    Index

    Properties

    "[toStringTag]": "FnOnce"

    The well-known symbol Symbol.toStringTag used by Object.prototype.toString(). Returns 'FnOnce' so that Object.prototype.toString.call(fn) produces '[object FnOnce]'.

    const fn = FnOnce(() => 42);
    console.log(Object.prototype.toString.call(fn)); // '[object FnOnce]'

    Methods

    • Calls the function with the provided arguments, consuming it.

      Parameters

      • ...args: A

        The arguments to pass to the function.

      Returns R

      The return value of the function.

      If the function has already been called.

      const add = FnOnce((a: number, b: number) => a + b);
      console.log(add.call(2, 3)); // 5
      // add.call(1, 1); // Throws Error
    • Returns true if the function has been consumed (called).

      Returns boolean

      const fn = FnOnce(() => 'done');
      console.log(fn.isConsumed()); // false
      fn.call();
      console.log(fn.isConsumed()); // true
    • Custom toString implementation.

      Returns string

      const fn = FnOnce(() => 42);
      console.log(fn.toString()); // 'FnOnce(pending)' or 'FnOnce(consumed)'
    • Attempts to call the function, returning Some(result) if successful or None if the function has already been consumed.

      This is the safe alternative to call() that never throws.

      Parameters

      • ...args: A

        The arguments to pass to the function.

      Returns Option<R>

      Some(result) if the function was called, None if already consumed.

      const greet = FnOnce((name: string) => `Hi, ${name}`);
      console.log(greet.tryCall('Alice')); // Some('Hi, Alice')
      console.log(greet.tryCall('Bob')); // None