Skip to content

Commit

Permalink
change: get to take TrackingType or GetOptions parameter so it can ha…
Browse files Browse the repository at this point in the history
…ve { shallow: true } and also be extended by enableReactAutoTracking to support suspense options
  • Loading branch information
jmeistrich committed Oct 12, 2023
1 parent 0091b8a commit 0937267
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/ObservableObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from './is';
import type {
ChildNodeValue,
GetOptions,
NodeValue,
ObservableObject,
ObservableState,
Expand Down Expand Up @@ -783,7 +784,8 @@ export function extractFunctionOrComputed(node: NodeValue, obj: Record<string, a
}
}

export function get(node: NodeValue, track?: TrackingType) {
export function get(node: NodeValue, options?: TrackingType | GetOptions) {
const track = options ? (isObject(options) ? (options.shallow as TrackingType) : options) : undefined;
// Track by default
updateTracking(node, track);

Expand Down
28 changes: 23 additions & 5 deletions src/config/enableReactAutoTracking.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { configureLegendState, internal, type NodeValue, tracking, type TrackingType } from '@legendapp/state';
import { useSelector } from '@legendapp/state/react';
import {
configureLegendState,
internal,
type NodeValue,
tracking,
type TrackingType,
isObject,
} from '@legendapp/state';
import { UseSelectorOptions, useSelector } from '@legendapp/state/react';
import { createContext, useContext } from 'react';
// @ts-expect-error Internals
import { __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED as ReactInternals } from 'react';
Expand Down Expand Up @@ -30,13 +37,24 @@ export function enableReactAutoTracking() {

configureLegendState({
observableFunctions: {
get: (node: NodeValue, track: TrackingType) => {
get: (node: NodeValue, options?: TrackingType | (GetOptions & UseSelectorOptions)) => {
if (needsSelector()) {
return useSelector(() => get(node, track));
return useSelector(() => get(node, options), isObject(options) ? options : undefined);
} else {
return get(node, track);
return get(node, options);
}
},
},
});
}

// Types:

// eslint-disable-next-line @typescript-eslint/no-unused-vars
import type { GetOptions, ObservableBaseFns } from '@legendapp/state';

declare module '@legendapp/state' {
interface ObservableBaseFns<T> {
get(options?: TrackingType | (GetOptions & { suspense?: boolean })): T;
}
}
5 changes: 4 additions & 1 deletion src/observableInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ export interface MapGet<T extends Map<any, any> | WeakMap<any, any>> {
get(): T;
size: ObservableChild<number>;
}
export interface GetOptions {
shallow: boolean;
}
export interface ObservableBaseFns<T> {
peek(): T;
get(trackingType?: TrackingType): T;
get(options?: TrackingType | GetOptions): T;
onChange(
cb: ListenerFn<T>,
options?: { trackingType?: TrackingType; initial?: boolean; immediate?: boolean; noArgs?: boolean },
Expand Down

0 comments on commit 0937267

Please sign in to comment.