mobx#_allowStateChanges TypeScript Examples

The following examples show how to use mobx#_allowStateChanges. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: fromResource.ts    From vscode-drawio with GNU General Public License v3.0 5 votes vote down vote up
export function fromResource<T>(
	subscriber: (sink: (newValue?: T) => void) => DisposableLike,
	getValue: (() => T) | undefined = undefined
): IResource<T | undefined> {
	let isActive = false;
	let isDisposed = false;
	let value = getValue ? getValue() : undefined;
	let disposable: DisposableLike;

	const initializer = () => {
		invariant(!isActive && !isDisposed);
		isActive = true;
		disposable = subscriber((...args) => {
			_allowStateChanges(true, () => {
				if (args.length > 0) {
					value = args[0];
				} else if (getValue) {
					value = getValue();
				} else {
					throw new Error(
						"Either an argument or getValue must be provided"
					);
				}
				atom.reportChanged();
			});
		});
	};

	const suspender = () => {
		if (isActive) {
			isActive = false;
			dispose(disposable);
		}
	};

	const atom = createAtom("ResourceBasedObservable", initializer, suspender);

	return {
		current: () => {
			invariant(
				!isDisposed,
				"subscribingObservable has already been disposed"
			);
			const isBeingTracked = atom.reportObserved();
			if (!isBeingTracked && !isActive) {
				if (getValue) {
					return getValue();
				} else {
					console.warn(
						"Called `get` of a subscribingObservable outside a reaction. Current value will be returned but no new subscription has started"
					);
				}
			}

			return value;
		},
		dispose: () => {
			isDisposed = true;
			suspender();
		},
		isAlive: () => isActive,
	};
}
Example #2
Source File: fromResource.ts    From vscode-lean4 with Apache License 2.0 5 votes vote down vote up
export function fromResource<T>(
	subscriber: (sink: (newValue?: T) => void) => Disposable,
	getValue: (() => T) | undefined = undefined
): IResource<T | undefined> {
	let isActive = false;
	let isDisposed = false;
	let value = getValue ? getValue() : undefined;
	let disposable: Disposable;

	const initializer = () => {
		invariant(!isActive && !isDisposed);
		isActive = true;
		disposable = subscriber((...args) => {
			_allowStateChanges(true, () => {
				if (args.length > 0) {
					value = args[0];
				} else if (getValue) {
					value = getValue();
				} else {
					throw new Error(
						'Either an argument or getValue must be provided'
					);
				}
				atom.reportChanged();
			});
		});
	};

	const suspender = () => {
		if (isActive) {
			isActive = false;
			disposable.dispose();
		}
	};

	const atom = createAtom('ResourceBasedObservable', initializer, suspender);

	return {
		current: () => {
			invariant(
				!isDisposed,
				'subscribingObservable has already been disposed'
			);
			const isBeingTracked = atom.reportObserved() as unknown;
			if (!isBeingTracked && !isActive) {
				if (getValue) {
					return getValue();
				} else {
					console.warn(
						'Called `get` of a subscribingObservable outside a reaction. Current value will be returned but no new subscription has started'
					);
				}
			}

			return value;
		},
		dispose: () => {
			isDisposed = true;
			suspender();
		},
		isAlive: () => isActive,
	};
}