Skip to content

Commit

Permalink
fix: isSyncEnabled was not disabling get
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeistrich committed Jul 1, 2024
1 parent 670f410 commit 730ff1c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/sync/syncObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,8 @@ export function syncObservable<T>(
let isSubscribed = false;
let unsubscribe: void | (() => void) = undefined;
sync = async () => {
if (isSynced && shouldIgnoreUnobserved(node, sync)) {
// If this node is not being observed or sync is not enabled then don't sync
if (isSynced && (!getNodeValue(getNode(syncState$)).isSyncEnabled || shouldIgnoreUnobserved(node, sync))) {
if (unsubscribe) {
isSubscribed = false;
unsubscribe();
Expand Down
79 changes: 79 additions & 0 deletions tests/persist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ObservablePersistPlugin, SyncedOptions, configureObservableSync, synced
import { BasicValue, ObservablePersistLocalStorage, getPersistName, localStorage, promiseTimeout } from './testglobals';
import { observe } from '../src/observe';
import { syncedCrud } from '@legendapp/state/sync-plugins/crud';
import { event } from '../src/event';

describe('Creating', () => {
test('Loading state works correctly', async () => {
Expand Down Expand Up @@ -1208,6 +1209,65 @@ describe('onBeforeGet', () => {
expect(obs$.get()).toEqual({ id3: { id: 'id3', test: 'yo' } });
});
});
describe('isSyncEnabled', () => {
test('isSyncEnabled disables get and set', async () => {
const obs$ = observable<Record<string, { id: string; test: string }>>();
const ev$ = event();
let gets = 0;
let sets = 0;
const state$ = syncObservable(obs$, {
get: () => {
gets++;
ev$.get();
return {
id1: {
id: 'id1',
test: 'hi',
},
};
},
set: () => {
sets++;
},
} as SyncedOptions);

expect(obs$.get()).toEqual({
id1: {
id: 'id1',
test: 'hi',
},
});
expect(gets).toEqual(1);
expect(sets).toEqual(0);

obs$.id1.test.set('hello');

await promiseTimeout(0);

expect(gets).toEqual(1);
expect(sets).toEqual(1);

ev$.fire();
obs$.get();

expect(gets).toEqual(2);
expect(sets).toEqual(1);

state$.isSyncEnabled.set(false);

obs$.id1.test.set('yo');

await promiseTimeout(0);

expect(gets).toEqual(2);
expect(sets).toEqual(1);

ev$.fire();

expect(gets).toEqual(2);
expect(sets).toEqual(1);
});
});
describe('synced is observer', () => {
test('synced runs once when not calling get', () => {
const num$ = observable(0);
Expand Down Expand Up @@ -1266,6 +1326,25 @@ describe('synced is observer', () => {
expect(latestValue).toEqual(1);
expect(runs).toEqual(2);
});
test('syncObservable is observer', () => {
const num$ = observable(0);
let runs = 0;
const obs$ = observable();
syncObservable(
obs$,
synced({
get: () => {
runs++;
return num$.get();
},
}),
);
expect(obs$.get()).toEqual(0);
expect(runs).toEqual(1);
num$.set(1);
expect(obs$.get()).toEqual(1);
expect(runs).toEqual(2);
});
test('syncedCrud is observer', async () => {
const num$ = observable(0);
let runs = 0;
Expand Down

0 comments on commit 730ff1c

Please sign in to comment.