happy-rusty
    Preparing search index...

    Interface Once<T>

    A container which can be written to only once.

    Useful for lazy initialization of global data or expensive computations that should only happen once.

    1.6.0

    const once = Once<number>();

    // Set value (only works once)
    once.set(42); // Ok(undefined)
    once.set(100); // Err(100) - already set

    // Get value
    console.log(once.get()); // Some(42)
    // Sync lazy initialization
    const config = Once<Config>();
    const cfg = config.getOrInit(() => loadConfigFromFile());
    interface Once<T> {
        "[toStringTag]": "Once";
        get(): Option<T>;
        getOrInit(fn: () => T): T;
        getOrTryInit<E>(fn: () => Result<T, E>): Result<T, E>;
        isInitialized(): boolean;
        set(value: T): VoidResult<T>;
        take(): Option<T>;
        toString(): string;
        tryInsert(value: T): Result<T, [T, T]>;
    }

    Type Parameters

    • T

      The type of the value stored.

    Index

    Properties

    "[toStringTag]": "Once"

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

    Methods

    • Gets the reference to the underlying value.

      Returns Option<T>

      Some(value) if initialized, None otherwise.

      const once = Once<number>();
      console.log(once.get()); // None

      once.set(42);
      console.log(once.get()); // Some(42)
    • Gets the contents, initializing it with fn if empty.

      Parameters

      • fn: () => T

        The synchronous initialization function, called only if empty.

      Returns T

      The stored value.

      const once = Once<number>();

      const value = once.getOrInit(() => {
      console.log('Initializing...');
      return 42;
      });
      console.log(value); // 42

      // Second call - fn is not called
      const value2 = once.getOrInit(() => 100);
      console.log(value2); // 42
    • Gets the contents, initializing it with fn if empty. If fn returns Err, remains uninitialized.

      Type Parameters

      • E

        The error type.

      Parameters

      • fn: () => Result<T, E>

        The initialization function that may fail.

      Returns Result<T, E>

      Ok(value) if initialized, Err(error) if initialization failed.

      const once = Once<Config>();

      const result = once.getOrTryInit(() => {
      const config = parseConfig(rawData);
      return config ? Ok(config) : Err(new Error('Invalid config'));
      });

      if (result.isOk()) {
      console.log('Config loaded:', result.unwrap());
      }
    • Returns true if initialized.

      Returns boolean

      const once = Once<number>();
      console.log(once.isInitialized()); // false

      once.set(42);
      console.log(once.isInitialized()); // true
    • Sets the contents to value.

      Parameters

      • value: T

        The value to store.

      Returns VoidResult<T>

      Ok(undefined) if empty, Err(value) if already initialized.

      const once = Once<number>();

      console.log(once.set(42)); // Ok(undefined)
      console.log(once.set(100)); // Err(100) - value returned back
      console.log(once.get()); // Some(42)
    • Takes the value out, leaving it uninitialized.

      Returns Option<T>

      Some(value) if initialized, None otherwise.

      const once = Once<number>();
      once.set(42);

      console.log(once.take()); // Some(42)
      console.log(once.get()); // None - now empty
      console.log(once.take()); // None
    • Custom toString implementation.

      Returns string

      const once = Once<number>();
      console.log(once.toString()); // 'Once(<uninitialized>)'

      once.set(42);
      console.log(once.toString()); // 'Once(42)'
    • Sets the contents to value if empty, returning a reference to the value.

      Unlike set(), this method returns the stored value on success, and returns both the current and passed values on failure.

      Parameters

      • value: T

        The value to store.

      Returns Result<T, [T, T]>

      Ok(value) if empty, Err([currentValue, passedValue]) if already initialized.

      const once = Once<number>();

      // First insert succeeds, returns the stored value
      const result1 = once.tryInsert(42);
      console.log(result1.unwrap()); // 42

      // Second insert fails, returns [current, passed]
      const result2 = once.tryInsert(100);
      console.log(result2.unwrapErr()); // [42, 100]