import { Mutex } from 'async-mutex' /* Simple thread-safe mutable vars. It feels like these should exist in a library somewhere already but ¯\_(ツ)_/¯ */ /** async mutex-guarded variable */ export class AsyncVar { private mutex: Mutex constructor(private value: T) { this.mutex = new Mutex() } public async get(): Promise { const release = await this.mutex.acquire() const value = this.value release() return value } public async set(value: T): Promise { const release = await this.mutex.acquire() this.value = value release() } } /** async mutex-guarded array */ export class AsyncArray extends AsyncVar> { constructor(value?: Array) { super(value || []) } /** Pushes a new item to the array. */ public async push(value: T): Promise { const arr = await this.get() arr.push(value) await this.set(arr) } /** Filter array in-place. */ public async filter(filterFn: (value: T) => boolean): Promise { const arr = await this.get() const filtered = arr.filter(filterFn) await this.set(filtered) } /** Returns true if value is included in the array. */ public async includes(value: T): Promise { const arr = await this.get() return arr.includes(value) } /** Returns length of array. */ public async length(): Promise { const arr = await this.get() return arr.length } }