From b7b18bc29029aab80b5a56242d9345e85d80f09b Mon Sep 17 00:00:00 2001 From: Jay Meistrich Date: Sun, 22 Oct 2023 11:13:05 +0100 Subject: [PATCH] refactor: move isObservable and isComputed to globals to fix circular references --- index.ts | 2 +- src/ObservableObject.ts | 2 +- src/computed.ts | 4 ++-- src/globals.ts | 17 +++++++++++++++-- src/helpers.ts | 11 +---------- src/observable.ts | 2 +- src/trackSelector.ts | 3 ++- tests/helpers.test.ts | 3 ++- tests/mapset.test.ts | 2 +- tests/tests.test.ts | 13 +++---------- 10 files changed, 29 insertions(+), 30 deletions(-) diff --git a/index.ts b/index.ts index abc1d3ff7..8e0b4abdb 100644 --- a/index.ts +++ b/index.ts @@ -7,7 +7,6 @@ export { constructObjectWithPath, deconstructObjectWithPath, getObservableIndex, - isObservable, isObservableValueReady, lockObservable, mergeIntoObservable, @@ -16,6 +15,7 @@ export { setInObservableAtPath, setSilently, } from './src/helpers'; +export { isObservable } from './src/globals'; export { hasOwnProperty, isArray, diff --git a/src/ObservableObject.ts b/src/ObservableObject.ts index 151d7a0a8..492bc3472 100644 --- a/src/ObservableObject.ts +++ b/src/ObservableObject.ts @@ -10,6 +10,7 @@ import { getNode, getNodeValue, globalState, + isObservable, optimized, setNodeValue, symbolDelete, @@ -17,7 +18,6 @@ import { symbolOpaque, symbolToPrimitive, } from './globals'; -import { isObservable } from './helpers'; import { hasOwnProperty, isArray, diff --git a/src/computed.ts b/src/computed.ts index 5b839ef7c..345778187 100644 --- a/src/computed.ts +++ b/src/computed.ts @@ -1,7 +1,7 @@ import { set as setBase } from './ObservableObject'; import { batch, notify } from './batching'; -import { getNode, getNodeValue, setNodeValue } from './globals'; -import { isObservable, lockObservable } from './helpers'; +import { getNode, getNodeValue, isObservable, setNodeValue } from './globals'; +import { lockObservable } from './helpers'; import { isPromise } from './is'; import { observable } from './observable'; import { ObservableComputed, ObservableComputedTwoWay, ObservableReadable } from './observableInterfaces'; diff --git a/src/globals.ts b/src/globals.ts index 5ce4de31e..e63d60bdb 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -1,6 +1,11 @@ -import { isComputed, isObservable } from './helpers'; import { isChildNodeValue, isFunction, isObject } from './is'; -import { NodeValue, ObservableComputed, ObservablePrimitive, ObservableReadable } from './observableInterfaces'; +import { + NodeValue, + ObservableComputed, + ObservableObject, + ObservablePrimitive, + ObservableReadable, +} from './observableInterfaces'; export const symbolToPrimitive = Symbol.toPrimitive; export const symbolGetNode = Symbol('getNode'); @@ -19,6 +24,14 @@ export const globalState = { onChangeRemote: undefined as unknown as (cb: () => void) => void, }; +export function isObservable(obs: any): obs is ObservableObject { + return !!obs && !!obs[symbolGetNode as any]; +} + +export function isComputed(obs: any): obs is ObservableComputed { + return obs && (obs[symbolGetNode as any] as NodeValue)?.isComputed; +} + export function checkActivate(node: NodeValue) { const root = node.root; root.activate?.(); diff --git a/src/helpers.ts b/src/helpers.ts index 5dec7fb9e..bea4c8f22 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -1,10 +1,9 @@ import { beginBatch, endBatch } from './batching'; -import { getNode, globalState, setNodeValue, symbolDelete, symbolGetNode, symbolOpaque } from './globals'; +import { getNode, globalState, isObservable, setNodeValue, symbolDelete, symbolGetNode, symbolOpaque } from './globals'; import { isArray, isEmpty, isFunction, isObject } from './is'; import type { NodeValue, ObservableChild, - ObservableComputed, ObservableEvent, ObservableObject, ObservableReadable, @@ -15,18 +14,10 @@ import type { TypeAtPath, } from './observableInterfaces'; -export function isObservable(obs: any): obs is ObservableObject { - return !!obs && !!obs[symbolGetNode as any]; -} - export function isEvent(obs: any): obs is ObservableEvent { return obs && (obs[symbolGetNode as any] as NodeValue)?.isEvent; } -export function isComputed(obs: any): obs is ObservableComputed { - return obs && (obs[symbolGetNode as any] as NodeValue)?.isComputed; -} - export function computeSelector(selector: Selector, e?: ObserveEvent, retainObservable?: boolean) { let c = selector as any; if (isFunction(c)) { diff --git a/src/observable.ts b/src/observable.ts index 3b40f1b48..605aae69e 100644 --- a/src/observable.ts +++ b/src/observable.ts @@ -31,7 +31,7 @@ export function observable(value: (params: ComputedParams) => Observable): export function observable(value: (params: ComputedParams) => T): Observable; export function observable(value: (params: ComputedProxyParams) => void): Observable>; export function observable(value?: TWithFunctions): Observable; -export function observable(value?: any): any { +export function observable(value?: T): any { return createObservable(value, false, extractPromise, getProxy, ObservablePrimitiveClass) as Observable; } diff --git a/src/trackSelector.ts b/src/trackSelector.ts index e7db3e478..d7734c6ba 100644 --- a/src/trackSelector.ts +++ b/src/trackSelector.ts @@ -1,4 +1,4 @@ -import { computeSelector, isObservable } from './helpers'; +import { computeSelector } from './helpers'; import type { ListenerParams, NodeValue, @@ -7,6 +7,7 @@ import type { Selector, TrackingNode, } from './observableInterfaces'; +import { isObservable } from './globals'; import type { ObserveOptions } from './observe'; import { setupTracking } from './setupTracking'; import { beginTracking, endTracking, tracking } from './tracking'; diff --git a/tests/helpers.test.ts b/tests/helpers.test.ts index 281a8bbe8..539757bac 100644 --- a/tests/helpers.test.ts +++ b/tests/helpers.test.ts @@ -1,5 +1,6 @@ import { symbolDelete } from '@legendapp/state'; -import { isObservable, isObservableValueReady, mergeIntoObservable } from '../src/helpers'; +import { isObservable } from '../src/globals'; +import { isObservableValueReady, mergeIntoObservable } from '../src/helpers'; import { observable } from '../src/observable'; describe('mergeIntoObservable', () => { diff --git a/tests/mapset.test.ts b/tests/mapset.test.ts index 8c746a917..73dc9169e 100644 --- a/tests/mapset.test.ts +++ b/tests/mapset.test.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/no-empty-function */ -import { isObservable } from '../src/helpers'; +import { isObservable } from '../src/globals'; import { observable } from '../src/observable'; import { Change, ObservableReadable, TrackingType } from '../src/observableInterfaces'; diff --git a/tests/tests.test.ts b/tests/tests.test.ts index 0e384e7b3..931aac18c 100644 --- a/tests/tests.test.ts +++ b/tests/tests.test.ts @@ -6,17 +6,10 @@ import '../src/config/enableDirectAccess'; import { enableDirectAccess } from '../src/config/enableDirectAccess'; import { enableDirectPeek } from '../src/config/enableDirectPeek'; import { event } from '../src/event'; -import { getNodeValue, optimized, symbolGetNode } from '../src/globals'; -import { isEvent, isObservable, lockObservable, opaqueObject, setAtPath } from '../src/helpers'; +import { getNodeValue, isObservable, optimized, symbolGetNode } from '../src/globals'; +import { isEvent, lockObservable, opaqueObject, setAtPath } from '../src/helpers'; import { observable, observablePrimitive } from '../src/observable'; -import { - Change, - ComputedParams, - ComputedProxyParams, - NodeValue, - ObservableReadable, - TrackingType, -} from '../src/observableInterfaces'; +import { Change, NodeValue, ObservableReadable, TrackingType } from '../src/observableInterfaces'; import { observe } from '../src/observe'; import { when } from '../src/when';