lazy-init-value
English | 中文
A library for lazy initialization of values, supporting both synchronous and asynchronous scenarios.
Installation
npm i lazy-init-value
Usage
import { LazyInitValue } from 'lazy-init-value'
const value = new LazyInitValue(() => {
console.log('Initializing...')
return 42
})
console.log(value.value)
console.log(value.value)
import { LazyAsyncInitValue } from 'lazy-init-value'
const asyncValue = new LazyAsyncInitValue(async () => {
console.log('Initializing...')
await new Promise(resolve => setTimeout(resolve, 1000))
return 'Async result'
})
console.log(await asyncValue.value())
console.log(await asyncValue.value())
import { LazyInitNull } from 'lazy-init-value'
const nullValue = new LazyInitNull(false)
console.log(nullValue.inited)
nullValue.init()
console.log(nullValue.inited)
nullValue.reset()
console.log(nullValue.inited)
About Freezing
Freeze a value by calling the freeze()
method, after which it cannot be reset(...)
.
LazyInitValue
and LazyAsyncInitValue
are set to autoFreeze
by default when constructed, which automatically freezes after initialization.
LazyInitValue
, LazyAsyncInitValue
, and LazyInitNull
all have a static autoFreeze
property, defaulting to true
, which controls whether subsequent constructor calls automatically freeze by default. These three properties are bound to the same value, modifying any of them will affect all.
Can also control autoFreeze
when creating each instance.
import { LazyInitValue } from 'lazy-init-value'
const value1 = new LazyInitValue(() => 42, true)
const value2 = new LazyInitValue(() => 42, false)
value2.reset(() => 43)
console.log(value2.value)
value2.freeze()
try {
value2.reset(() => 44)
} catch (e) {
console.log(e)
}
console.log(LazyInitValue.autoFreeze)
const value3 = new LazyInitValue(() => 42)
LazyInitValue.autoFreeze = false
const value4 = new LazyInitValue(() => 42)
import { LazyAsyncInitValue } from 'lazy-init-value'
console.log(LazyAsyncInitValue.autoFreeze)
API
LazyInitValue<T>(initFn: () => T, autoFreeze?: boolean)
inited: boolean
- Check if the value has been initializedfreeze()
- Freeze the value, cannot reset
afterwardsvalue: T
- If initialized, return the value; if not initialized, trigger initialization and return the valueinit(): boolean
- Try to initialize. Returns true
if initialization is triggered; returns false
if already initialized and won't reinitializereset(initFn: () => T)
- Reset to uninitialized state, and set a new initialization function
LazyAsyncInitValue<T>(initFn: () => Promise<T>, autoFreeze?: boolean)
inited: boolean
- Check if the value has been initializedfreeze()
- Freeze the value, cannot reset
afterwardsvalue(): Promise<T> | T
- If initialized, return the value; if not initialized, return a Promise<T>
value_sync: T | undefined
- If initialized, return the value; if not initialized, trigger initialization and return undefined
init(): false | Promise<true>
- Try to initialize. Returns Promise<true>
if initialization is triggered; returns false
if already initialized and won't reinitializeiniting: boolean
- Check if async initialization is in progressreset(initFn: () => Promise<T>)
- Reset to uninitialized state, and set a new initialization function
Usage tips for LazyAsyncInitValue
:
- When unsure if the value has been initialized, use
await obj.value()
to get the value and don't need to worry about the timing of initialization - When certain that the value has been initialized, use
obj.value_sync
to get the value. This is more performant
LazyInitNull(autoFreeze?: boolean)
A special lazy initialization class that always initializes to null
. Generally used with manual init()
and the inited
property to determine if initialization has occurred.
inited: boolean
- Check if the value has been initializedfreeze()
- Freeze the value, cannot reset
afterwardsvalue: null
- Get null value (triggers initialization if not yet initialized)init(): boolean
- Try to initialize. Returns true
if initialization is triggered; returns false
if already initialized and won't reinitializereset()
- Reset to uninitialized state