Creates a new empty Once<T>.
Once<T>
The type of value to store.
A new uninitialized Once.
Once
// Basic usageconst once = Once<string>();once.set('hello');console.log(once.get().unwrap()); // 'hello' Copy
// Basic usageconst once = Once<string>();once.set('hello');console.log(once.get().unwrap()); // 'hello'
// Sync lazy singleton patternconst logger = Once<Logger>();function getLogger(): Logger { return logger.getOrInit(() => new Logger('app'));} Copy
// Sync lazy singleton patternconst logger = Once<Logger>();function getLogger(): Logger { return logger.getOrInit(() => new Logger('app'));}
// Fallible sync initializationconst 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')); });} Copy
// Fallible sync initializationconst 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')); });}
Creates a new empty
Once<T>.