Class RwLock<TRead, TWrite>

Source
Expand description

Ensure you have exclusive access in concurrent code

Only guaranteed if no one else has reference to the inner object

It can take a second type parameter to specify interface with write methods

deprecated

unstable API

Constructors§

Source§

new RwLock<TRead, TWrite = TRead>(t: TWrite): RwLock<TRead, TWrite>

Properties§

§public inner: TWrite

This is public so inner object can be accessed directly ONLY SAFE in sync context

Methods§

Source§

scopedRead<R>(fn: (t: TRead) => Promise<R>): Promise<R>

Acquire a read (shared) lock and call fn with the value. Release the lock when fn returns or throws.

Source§

scopedWrite<R>(
    fn: (t: TWrite, setter: (t: TWrite) => TWrite) => Promise<R>,
): Promise<R>

Acquire a write (exclusive) lock and call fn with the value. Release the lock when fn returns or throws.

fn takes a setter function as second parameter, which you can use to update the value like x = set(newX)