Skip to content

Commit

Permalink
refactor: simplify sync system logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tm-ruxandra committed Oct 24, 2024
1 parent ca1c094 commit 12221d1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 36 deletions.
24 changes: 8 additions & 16 deletions dev-client/src/model/sync/sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import {
ChangeRecords,
getChanges,
getRevisionId,
getUnsyncedRecords,
getUpToDateResults,
isUnsynced,
Expand All @@ -32,21 +31,11 @@ describe('sync', () => {
jest.useFakeTimers().setSystemTime(new Date('2020-01-01'));
});

describe('getRevisionId', () => {
test('zero for undefined revision', () => {
expect(getRevisionId(undefined)).toEqual(0);
});

test('zero for empty revision', () => {
expect(getRevisionId({})).toEqual(0);
describe('nextRevisionId', () => {
test('assumes zero initial value', () => {
expect(nextRevisionId(undefined)).toEqual(1);
});

test('value for revision', () => {
expect(getRevisionId({revisionId: 123})).toEqual(123);
});
});

describe('nextRevisionId', () => {
test('increments by one', () => {
expect(nextRevisionId(1)).toEqual(2);
});
Expand Down Expand Up @@ -202,8 +191,11 @@ describe('sync', () => {

test('handles results with no change records', () => {
expect(
getUpToDateResults({}, {a: {revisionId: 0}, b: {revisionId: 1}}),
).toEqual({a: {revisionId: 0}});
getUpToDateResults(
{},
{a: {revisionId: undefined}, b: {revisionId: 1}},
),
).toEqual({a: {revisionId: undefined}});
});
});
});
32 changes: 12 additions & 20 deletions dev-client/src/model/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,10 @@ export type SyncResult<T> = {

export const INITIAL_REVISION_ID = 0;

export const getRevisionId = <T>(
record?: ChangeRecord<T>,
): ChangeRevisionId => {
return record?.revisionId ?? INITIAL_REVISION_ID;
};

export const nextRevisionId = (
revisionId: ChangeRevisionId,
revisionId?: ChangeRevisionId,
): ChangeRevisionId => {
return revisionId + 1;
return (revisionId ?? INITIAL_REVISION_ID) + 1;
};

export const getChanges = <T>(
Expand Down Expand Up @@ -87,15 +81,14 @@ export const markChanged = <T>(
at: ChangeTimestamp,
) => {
const prevRecord = getChange(records, id);
const prevRevisionId = getRevisionId(prevRecord);
const revisionId = nextRevisionId(prevRevisionId);
const revisionId = nextRevisionId(prevRecord.revisionId);
records[id] = {
revisionId: revisionId,
lastModifiedAt: at,

lastSyncedRevisionId: prevRecord?.lastSyncedRevisionId,
lastSyncedData: prevRecord?.lastSyncedData,
lastSyncedAt: prevRecord?.lastSyncedAt,
lastSyncedRevisionId: prevRecord.lastSyncedRevisionId,
lastSyncedData: prevRecord.lastSyncedData,
lastSyncedAt: prevRecord.lastSyncedAt,
};
};

Expand All @@ -116,10 +109,9 @@ export const markSynced = <T>(
at: ChangeTimestamp,
) => {
const prevRecord = getChange(records, id);
const prevRevisionId = getRevisionId(prevRecord);
records[id] = {
revisionId: prevRevisionId,
lastModifiedAt: prevRecord?.lastModifiedAt,
revisionId: prevRecord.revisionId,
lastModifiedAt: prevRecord.lastModifiedAt,
lastSyncedAt: at,
lastSyncedRevisionId: result.revisionId,
lastSyncedData: result.data,
Expand All @@ -146,7 +138,7 @@ export const isUnsynced = <T>(record: ChangeRecord<T>): boolean => {
return true;
} else {
/* Unsynced changes if the record's current revision is not the last-synced one */
return getRevisionId(record) !== record.lastSyncedRevisionId;
return record.revisionId !== record.lastSyncedRevisionId;
}
};

Expand All @@ -156,14 +148,14 @@ export const getUpToDateResults = <T>(
): SyncResults<T> => {
return Object.fromEntries(
Object.entries(results).filter(([id, result]) =>
isUpToDate(records[id], result),
isUpToDate(getChange(records, id), result),
),
);
};

export const isUpToDate = <T>(
record: ChangeRecord<T> | undefined,
record: ChangeRecord<T>,
result: SyncResult<T>,
): boolean => {
return getRevisionId(record) === result.revisionId;
return record.revisionId === result.revisionId;
};

0 comments on commit 12221d1

Please sign in to comment.