Skip to content

Commit

Permalink
refactor: move isObservable and isComputed to globals to fix circular…
Browse files Browse the repository at this point in the history
… references
  • Loading branch information
jmeistrich committed Oct 23, 2023
1 parent 2ae4c23 commit b7b18bc
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 30 deletions.
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export {
constructObjectWithPath,
deconstructObjectWithPath,
getObservableIndex,
isObservable,
isObservableValueReady,
lockObservable,
mergeIntoObservable,
Expand All @@ -16,6 +15,7 @@ export {
setInObservableAtPath,
setSilently,
} from './src/helpers';
export { isObservable } from './src/globals';
export {
hasOwnProperty,
isArray,
Expand Down
2 changes: 1 addition & 1 deletion src/ObservableObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import {
getNode,
getNodeValue,
globalState,
isObservable,
optimized,
setNodeValue,
symbolDelete,
symbolGetNode,
symbolOpaque,
symbolToPrimitive,
} from './globals';
import { isObservable } from './helpers';
import {
hasOwnProperty,
isArray,
Expand Down
4 changes: 2 additions & 2 deletions src/computed.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
17 changes: 15 additions & 2 deletions src/globals.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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?.();
Expand Down
11 changes: 1 addition & 10 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<T>(selector: Selector<T>, e?: ObserveEvent<T>, retainObservable?: boolean) {
let c = selector as any;
if (isFunction(c)) {
Expand Down
2 changes: 1 addition & 1 deletion src/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function observable<T>(value: (params: ComputedParams) => Observable<T>):
export function observable<T>(value: (params: ComputedParams) => T): Observable<T>;
export function observable<T>(value: (params: ComputedProxyParams<T>) => void): Observable<Record<string, T>>;
export function observable<T>(value?: TWithFunctions<T>): Observable<T>;
export function observable<T>(value?: any): any {
export function observable<T>(value?: T): any {
return createObservable(value, false, extractPromise, getProxy, ObservablePrimitiveClass) as Observable<any>;
}

Expand Down
3 changes: 2 additions & 1 deletion src/trackSelector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computeSelector, isObservable } from './helpers';
import { computeSelector } from './helpers';
import type {
ListenerParams,
NodeValue,
Expand All @@ -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';
Expand Down
3 changes: 2 additions & 1 deletion tests/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/mapset.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
13 changes: 3 additions & 10 deletions tests/tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit b7b18bc

Please sign in to comment.