Skip to content

Commit

Permalink
rename to enableReactTracking, add params for auto mode or warning if…
Browse files Browse the repository at this point in the history
… unobserved
  • Loading branch information
jmeistrich committed Oct 12, 2023
1 parent e6fa1b8 commit 3b4c66b
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,27 @@ function needsSelector() {
return false;
}

export function enableReactAutoTracking() {
interface ReactTrackingOptions {
auto?: boolean; // Make all get() calls act as useSelector() hooks
warnUnobserved?: boolean; // Warn if get() is used outside of an observer
}

export function enableReactTracking({ auto, warnUnobserved }: ReactTrackingOptions) {
const { get } = internal;

configureLegendState({
observableFunctions: {
get: (node: NodeValue, options?: TrackingType | (GetOptions & UseSelectorOptions)) => {
if (needsSelector()) {
return useSelector(() => get(node, options), isObject(options) ? options : undefined);
} else {
return get(node, options);
if (auto) {
return useSelector(() => get(node, options), isObject(options) ? options : undefined);
} else if (process.env.NODE_ENV === 'development' && warnUnobserved) {
console.warn(
'[legend-state] Detected a `get()` call in an unobserved component. You may want to wrap it in observer: https://legendapp.com/open-source/state/react-api/#observer-hoc',
);
}
}
return get(node, options);
},
},
});
Expand Down

0 comments on commit 3b4c66b

Please sign in to comment.