happy-rusty
    Preparing search index...

    Function Once

    • Creates a new empty Once<T>.

      Type Parameters

      • T

        The type of value to store.

      Returns Once<T>

      A new uninitialized Once.

      // Basic usage
      const once = Once<string>();
      once.set('hello');
      console.log(once.get().unwrap()); // 'hello'
      // Sync lazy singleton pattern
      const logger = Once<Logger>();

      function getLogger(): Logger {
      return logger.getOrInit(() => new Logger('app'));
      }
      // Fallible sync initialization
      const config = Once<Config>();

      function loadConfig(): Result<Config, Error> {
      return config.getOrTryInit(() => {
      const data = readFileSync('config.json');
      return data ? Ok(JSON.parse(data)) : Err(new Error('Config not found'));
      });
      }