mirror of
https://github.com/Novaloop-AG/healthchat-web.git
synced 2026-12-03 21:51:06 +00:00
4aa1a3549e
* Remove stale max-len disablements * Remove stale camelCase & naming-convention disablements * Remove stale ban-ts-comment disablements * Remove stale no-var disablements * Remove stale no-empty-property disablements * Remove stale react rule disablements * Remove stale no-constant-condition disablements * Remove stale no-unused-vars disablements * Remove stale disablements for disabled rules * fixup camelcase * Remove dead code * Tidy code * Tweak oxlint config * Use oxlint to apply jsx/tsx extension consistently * Fix import * Fix imports * Rename affected snapshots * Update more imports * Enable restriction ruleset * Make code comply with new rules * Make code comply with react/button-has-type * Make code comply with typescript/non-nullable-type-assertion-style * Comply with node/no-process-env * Comply with unicorn/prefer-node-protocol * Comply with unicorn/import-style * Comply with unicorn/no-process-exit * Comply with no-proto * Comply with node/handle-callback-err * Comply with import/no-commonjs * Comply with node/no-path-concat * Comply with unicorn/no-length-as-slice-end * Comply with unicorn/no-document-cookie * Comply with unicorn/prefer-module * Comply with typescript/prefer-literal-enum-member * Comply with jsx-a11y/anchor-ambiguous-text * Tweak oxlint config * Fix resolves * Iterate * Iterate * Iterate * Iterate * Iterate * Iterate * Iterate
107 lines
3.5 KiB
TypeScript
107 lines
3.5 KiB
TypeScript
/*
|
|
Copyright 2024 New Vector Ltd.
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
|
Please see LICENSE files in the repository root for full details.
|
|
*/
|
|
|
|
// oxlint-disable-next-line no-restricted-imports
|
|
import { EventEmitter } from "events";
|
|
import AwaitLock from "await-lock";
|
|
|
|
import { type ActionPayload } from "../dispatcher/payloads";
|
|
import { type MatrixDispatcher } from "../dispatcher/dispatcher";
|
|
|
|
/**
|
|
* The event/channel to listen for in an AsyncStore.
|
|
*/
|
|
export const UPDATE_EVENT = "update";
|
|
|
|
/**
|
|
* Represents a minimal store which works similar to Flux stores. Instead
|
|
* of everything needing to happen in a dispatch cycle, everything can
|
|
* happen async to that cycle.
|
|
*
|
|
* The store operates by using Object.assign() to mutate state - it sends the
|
|
* state objects (current and new) through the function onto a new empty
|
|
* object. Because of this, it is recommended to break out your state to be as
|
|
* safe as possible. The state mutations are also locked, preventing concurrent
|
|
* writes.
|
|
*
|
|
* All updates to the store happen on the UPDATE_EVENT event channel with the
|
|
* one argument being the instance of the store.
|
|
*
|
|
* To update the state, use updateState() and preferably await the result to
|
|
* help prevent lock conflicts.
|
|
*/
|
|
export abstract class AsyncStore<T extends object> extends EventEmitter {
|
|
private storeState: Readonly<T>;
|
|
private lock = new AwaitLock();
|
|
private readonly dispatcherRef: string;
|
|
|
|
/**
|
|
* Creates a new AsyncStore using the given dispatcher.
|
|
* @param {Dispatcher<ActionPayload>} dispatcher The dispatcher to rely upon.
|
|
* @param {T} initialState The initial state for the store.
|
|
*/
|
|
protected constructor(
|
|
protected dispatcher: MatrixDispatcher,
|
|
initialState: T = <T>{},
|
|
) {
|
|
super();
|
|
|
|
this.dispatcherRef = dispatcher.register(this.onDispatch.bind(this));
|
|
this.storeState = initialState;
|
|
}
|
|
|
|
/**
|
|
* The current state of the store. Cannot be mutated.
|
|
*/
|
|
protected get state(): T {
|
|
return this.storeState;
|
|
}
|
|
|
|
/**
|
|
* Stops the store's listening functions, such as the listener to the dispatcher.
|
|
*/
|
|
protected stop(): void {
|
|
this.dispatcher.unregister(this.dispatcherRef);
|
|
}
|
|
|
|
/**
|
|
* Updates the state of the store.
|
|
* @param {T|*} newState The state to update in the store using Object.assign()
|
|
*/
|
|
protected async updateState(newState: T | object): Promise<void> {
|
|
await this.lock.acquireAsync();
|
|
try {
|
|
this.storeState = Object.freeze(Object.assign(<T>{}, this.storeState, newState));
|
|
this.emit(UPDATE_EVENT, this);
|
|
} finally {
|
|
this.lock.release();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Resets the store's to the provided state or an empty object.
|
|
* @param {T|*} newState The new state of the store.
|
|
* @param {boolean} quiet If true, the function will not raise an UPDATE_EVENT.
|
|
*/
|
|
protected async reset(newState: T | object | null = null, quiet = false): Promise<void> {
|
|
await this.lock.acquireAsync();
|
|
try {
|
|
this.storeState = Object.freeze(<T>(newState || {}));
|
|
if (!quiet) this.emit(UPDATE_EVENT, this);
|
|
} finally {
|
|
this.lock.release();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Called when the dispatcher broadcasts a dispatch event.
|
|
* @param {ActionPayload} payload The event being dispatched.
|
|
*/
|
|
protected abstract onDispatch(payload: ActionPayload): void;
|
|
}
|