Skip to content

Commit

Permalink
refactor: rename ObservablePersistState properties isLoadedRemote to …
Browse files Browse the repository at this point in the history
…isLoaded and remoteError to error
  • Loading branch information
jmeistrich committed Oct 8, 2023
1 parent 8b40318 commit 1cccb4a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/observableInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,10 @@ export interface ObservablePersistRemoteFunctions<T = any, TState = {}> {

export interface ObservablePersistState {
isLoadedLocal: boolean;
isLoadedRemote: boolean;
isLoaded: boolean;
isEnabledLocal: boolean;
isEnabledRemote: boolean;
remoteError?: Error;
error?: Error;
dateModified?: number;
clearLocal: () => Promise<void>;
sync: () => Promise<void>;
Expand Down
4 changes: 2 additions & 2 deletions src/persist-plugins/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ class ObservablePersistFirebaseBase implements ObservablePersistRemoteClass {
onLoadParams,
status$,
});
params.state.remoteError.set(err);
params.state.error.set(err);
onLoadError?.(err);
if (allowSetIfError) {
status$.numWaitingCanSave.set((v) => v - 1);
Expand Down Expand Up @@ -740,7 +740,7 @@ class ObservablePersistFirebaseBase implements ObservablePersistRemoteClass {

// If this path previously errored, clear the error state
const obs = params.obs;
params.state.remoteError.delete();
params.state.error.delete();
this.listenErrors.delete(obs);

status$.startedLoading.set(true);
Expand Down
12 changes: 4 additions & 8 deletions src/persist/persistObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,7 @@ async function doChange(

if (changesRemote.length > 0) {
// Wait for remote to be ready before saving
await when(
() => syncState.isLoadedRemote.get() || (configRemote?.allowSetIfError && syncState.remoteError.get()),
);
await when(() => syncState.isLoaded.get() || (configRemote?.allowSetIfError && syncState.error.get()));

const value = obs.peek();

Expand Down Expand Up @@ -639,7 +637,7 @@ export function persistObservable<T, TState = {}>(

const syncState = observable<ObservablePersistState>({
isLoadedLocal: false,
isLoadedRemote: false,
isLoaded: false,
isEnabledLocal: true,
isEnabledRemote: true,
clearLocal: undefined as unknown as () => Promise<void>,
Expand Down Expand Up @@ -684,7 +682,7 @@ export function persistObservable<T, TState = {}>(
options: persistOptions as PersistOptions<T, any>,
dateModified,
onGet: () => {
syncState.isLoadedRemote.set(true);
syncState.isLoaded.set(true);
},
onChange: async ({ value, path = [], pathTypes = [], mode = 'set', dateModified }) => {
// Note: value is the constructed value, path is used for setInObservableAtPath
Expand Down Expand Up @@ -748,9 +746,7 @@ export function persistObservable<T, TState = {}>(
});

// Wait for remote to be ready before saving pending
await when(
() => syncState.isLoadedRemote.get() || (remote.allowSetIfError && syncState.remoteError.get()),
);
await when(() => syncState.isLoaded.get() || (remote.allowSetIfError && syncState.error.get()));

const pending = localState.pendingChanges;
if (pending && !isEmpty(pending)) {
Expand Down
6 changes: 3 additions & 3 deletions tests/persist-remote-fn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Persist remote with functions', () => {

expect(obs.peek()).toEqual({ test: { x: 'hi' } });

await when(state.isLoadedRemote);
await when(state.isLoaded);

expect(obs.peek()).toEqual({ test: { x: 'hello' } });
});
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('Persist remote with functions', () => {

expect(obs$.peek()).toEqual({ test: { x: 'hi' } });

await when(state.isLoadedRemote);
await when(state.isLoaded);

expect(obs$.peek()).toEqual({ test: { x: 'hello' } });

Expand Down Expand Up @@ -102,7 +102,7 @@ describe('Persist remote with functions', () => {

expect(obs$.peek()).toEqual({ test: { x: 'hi' } });

await when(state.isLoadedRemote);
await when(state.isLoaded);

expect(obs$.peek()).toEqual({ test: { x: 'hello' } });

Expand Down

0 comments on commit 1cccb4a

Please sign in to comment.