gristlabs_grist-core/app/client/lib/pausableObs.ts
George Gevoian 8a0bb4d4fe (core) Improve dark mode
Summary:
Enhances dark mode support for the formula editor, and adds support to
the color select popup. Also fixes some bugs with dark mode.

Test Plan: Tested manually.

Reviewers: jarek

Reviewed By: jarek

Differential Revision: https://phab.getgrist.com/D3847
2023-04-12 01:58:48 -04:00

42 lines
1.2 KiB
TypeScript

import {IDisposableOwner, Observable} from 'grainjs';
export interface PausableObservable<T> extends Observable<T> {
pause(shouldPause?: boolean): void;
}
/**
* Creates and returns an `Observable` that can be paused, effectively causing all
* calls to `set` to become noops until unpaused, at which point the last value
* passed to set, if any, will be applied.
*
* NOTE: It's only advisable to use this when there are no other alternatives; pausing
* updates and notifications to subscribers increases the chances of introducing bugs.
*/
export function createPausableObs<T>(
owner: IDisposableOwner|null,
value: T,
): PausableObservable<T> {
let _isPaused = false;
let _lastValue: T | undefined = undefined;
const obs = Observable.create<T>(owner, value);
const set = Symbol('set');
return Object.assign(obs, {
pause(shouldPause: boolean = true) {
_isPaused = shouldPause;
if (shouldPause) {
_lastValue = undefined;
} else if (_lastValue) {
obs.set(_lastValue);
_lastValue = undefined;
}
},
[set]: obs.set,
set(val: T) {
_lastValue = val;
if (_isPaused) { return; }
this[set](val);
}
});
}