Skip to content

Commit

Permalink
fix: change promise/persisted state key to support _state also, will …
Browse files Browse the repository at this point in the history
…slowly migrate to that
  • Loading branch information
jmeistrich committed Oct 21, 2023
1 parent ed93e60 commit bb21dd8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/ObservableObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ const proxyHandler: ProxyHandler<any> = {
return fnOrComputed;
}

if (p === 'state' && node.state) {
// TODOV3: Remove "state"
if (vProp === undefined && (p === 'state' || p === '_state') && node.state) {
return node.state;
}

Expand Down
6 changes: 4 additions & 2 deletions src/observableInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ export interface ObservableState {
error?: Error;
}
export interface WithState {
state?: ObservableState;
state?: ObservableState; // TODOV3: remove this
_state?: ObservableState;
}
export interface ObservablePersistState extends ObservableState {
isLoadedLocal: boolean;
Expand All @@ -320,7 +321,8 @@ export interface ObservablePersistState extends ObservableState {
| undefined;
}
export interface WithPersistState {
state?: ObservablePersistState;
state?: ObservablePersistState; // TODOV3: remove this
_state?: ObservablePersistState;
}
export type RecordValue<T> = T extends Record<string, infer t> ? t : never;
export type ArrayValue<T> = T extends Array<infer t> ? t : never;
Expand Down
14 changes: 11 additions & 3 deletions src/persist/persistObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
PersistOptionsLocal,
PersistOptionsRemote,
PersistTransform,
Primitive,
TypeAtPath,
WithPersistState,
} from '@legendapp/state';
Expand Down Expand Up @@ -610,16 +611,17 @@ async function loadLocal<T>(
}
syncState.isLoadedLocal.set(true);
}
type WithoutState = any[] | Primitive | (Record<string, any> & { _state?: never });

export function persistObservable<T>(
export function persistObservable<T extends WithoutState>(
observable: ObservableWriteable<T>,
persistOptions: PersistOptions<T>,
): Observable<WithPersistState & T>;
export function persistObservable<T>(
export function persistObservable<T extends WithoutState>(
initial: T | (() => T) | (() => Promise<T>),
persistOptions: PersistOptions<T>,
): Observable<WithPersistState & T>;
export function persistObservable<T>(
export function persistObservable<T extends WithoutState>(
initialOrObservable: ObservableWriteable<T> | T | (() => T) | (() => Promise<T>),
persistOptions: PersistOptions<T>,
): Observable<WithPersistState & T> {
Expand All @@ -630,6 +632,12 @@ export function persistObservable<T>(
) as Observable<T>;
const node = getNode(obs);

if (process.env.NODE_ENV === 'development' && obs?.peek()?._state) {
console.warn(
'[legend-state] WARNING: persistObservable creates a property named "_state" but your observable already has "state" in it',
);
}

// Merge remote persist options with clobal options
if (persistOptions.remote) {
persistOptions.remote = Object.assign({}, observablePersistConfiguration.remoteOptions, persistOptions.remote);
Expand Down
9 changes: 9 additions & 0 deletions tests/tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2142,6 +2142,15 @@ describe('Promise values', () => {
expect(obs.promise.get()).toEqual(10);
expect(obs.promise.state.isLoaded.get()).toEqual(true);
});
test('Promise child with _state', async () => {
const promise = Promise.resolve(10);
const obs = observable({ promise });
expect(obs.promise).resolves.toEqual(10);
expect(obs.promise._state.isLoaded.get()).toEqual(false);
await promise;
expect(obs.promise.get()).toEqual(10);
expect(obs.promise._state.isLoaded.get()).toEqual(true);
});
test('Promise child works when set later', async () => {
const obs = observable({ promise: undefined as unknown as Promise<number> });
let resolver: (value: number) => void;
Expand Down

0 comments on commit bb21dd8

Please sign in to comment.