Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
lazy-init-value
Advanced tools
Lazy initialization of values, supporting both synchronous and asynchronous scenarios
English | 中文
A library for lazy initialization of values, supporting both synchronous and asynchronous scenarios.
npm i lazy-init-value
import { LazyInitValue } from 'lazy-init-value'
const value = new LazyInitValue(() => {
console.log('Initializing...')
return 42
})
console.log(value.value) // Outputs: "Initializing..." then "42"
console.log(value.value) // "42" (won't reinitialize)
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()) // Outputs: "Initializing..." then "Async result"
console.log(await asyncValue.value()) // "Async result" (won't reinitialize)
import { LazyInitNull } from 'lazy-init-value'
const nullValue = new LazyInitNull(false) // avoid auto-freezing
console.log(nullValue.inited) // false
nullValue.init()
console.log(nullValue.inited) // true
nullValue.reset()
console.log(nullValue.inited) // false
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) // Will auto-freeze, default behavior
const value2 = new LazyInitValue(() => 42, false) // Won't be frozen, can be `reset(...)` later
value2.reset(() => 43)
console.log(value2.value) // 43
value2.freeze()
try {
value2.reset(() => 44)
} catch (e) {
console.log(e) // Can't reset value after `freeze()`
}
console.log(LazyInitValue.autoFreeze) // true, default value
const value3 = new LazyInitValue(() => 42) // Will auto-freeze
LazyInitValue.autoFreeze = false
const value4 = new LazyInitValue(() => 42) // Won't be frozen
import { LazyAsyncInitValue } from 'lazy-init-value'
console.log(LazyAsyncInitValue.autoFreeze) // false, same as `LazyInitValue.autoFreeze`
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 functionLazyAsyncInitValue<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 functionUsage tips for LazyAsyncInitValue
:
await obj.value()
to get the value and don't need to worry about the timing of initializationobj.value_sync
to get the value. This is more performantLazyInitNull(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 stateFAQs
Lazy initialization of values, supporting both synchronous and asynchronous scenarios
We found that lazy-init-value demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.