Skip to content

Commit

Permalink
chore: update types
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed May 30, 2023
1 parent 42cbc46 commit b64ad25
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
12 changes: 6 additions & 6 deletions packages/loot-core/src/server/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ export function deserializeValue(value: string): string | number | null {
}

// TODO make this type stricter.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type DataMap = Map<[string, any], any>;
let _syncListeners: ((oldData: DataMap, newData: DataMap) => unknown)[] = [];
type DataMap = Map<string, unknown>;
type SyncListener = (oldData: DataMap, newData: DataMap) => unknown;
let _syncListeners: SyncListener[] = [];

export function addSyncListener(func: () => unknown) {
export function addSyncListener(func: SyncListener) {
_syncListeners.push(func);

return () => {
Expand Down Expand Up @@ -242,12 +242,12 @@ function applyMessagesForImport(messages: Message[]) {
});
}

type Message = {
export type Message = {
column: string;
dataset: string;
old?: unknown;
row: string;
timestamp: number;
timestamp: string;
value: string | number | null;
};

Expand Down
17 changes: 12 additions & 5 deletions packages/loot-core/src/server/sync/migrate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as db from '../db';

import { listen, unlisten } from './migrate';

import { addSyncListener, sendMessages } from './index';
import { Message, addSyncListener, sendMessages } from './index';

beforeEach(() => {
listen();
Expand All @@ -25,7 +25,7 @@ function toInternalField(publicField) {
return schemaConfig.views.transactions.fields[publicField];
}

let messageArb = fc
let messageArb: fc.Arbitrary<Message> = fc
.oneof(...fields.filter(f => f !== 'id').map(field => fc.constant(field)))
.chain(field => {
let value = arbs
Expand All @@ -41,7 +41,7 @@ let messageArb = fc
.noShrink()
.map(date => date.toISOString() + '-0000-0123456789ABCDEF');

return fc.record({
return fc.record<Message>({
timestamp: timestamp,
dataset: fc.constant('transactions'),
column: fc.constant(toInternalField(field) || field),
Expand All @@ -61,7 +61,11 @@ describe('sync migrations', () => {
tracer.start();

let cleanup = addSyncListener((oldValues, newValues) => {
tracer.event('applied', [...newValues.get('transactions').keys()]);
let transationsMap = newValues.get('transactions') as Map<
string,
unknown
>;
tracer.event('applied', [...transationsMap.keys()]);
});

await db.insert('transactions', {
Expand All @@ -87,7 +91,10 @@ describe('sync migrations', () => {
let tracer = execTracer();
tracer.start();
let cleanup = addSyncListener((oldValues, newValues) => {
let ts = newValues.get('transactions');
let ts = newValues.get('transactions') as Map<
string,
{ isChild: number; parent_id: string | null; id: string }
>;
if (
ts &&
[...ts.values()].find(
Expand Down
12 changes: 8 additions & 4 deletions packages/loot-core/src/server/sync/sync.property.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const uuidGenerator = jsc.integer(97, 122).smap(
x => x.charCodeAt(Number(x)),
);

function isError(value: unknown): value is { error: unknown } {
return (value as { error: unknown }).error !== undefined;
}

beforeEach(() => {
sync.setSyncingMode('enabled');
mockSyncServer.reset();
Expand Down Expand Up @@ -278,10 +282,10 @@ async function run(msgs) {
),
);

let { error } = await syncPromise;
if (error) {
console.log(error);
throw error;
let result = await syncPromise;
if (isError(result)) {
console.log(result.error);
throw result.error;
}

let serverMerkle = mockSyncServer.getClock().merkle;
Expand Down
15 changes: 10 additions & 5 deletions packages/loot-core/src/server/sync/sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ afterEach(() => {
setSyncingMode('disabled');
});

function isError(value: unknown): value is { error: unknown } {
return (value as { error: unknown }).error !== undefined;
}

describe('Sync', () => {
it('should send messages to the server', async () => {
prefs.loadPrefs();
Expand Down Expand Up @@ -85,9 +89,9 @@ describe('Sync', () => {

expect(mockSyncServer.getMessages().length).toBe(0);

const { messages, error } = await fullSync();
expect(error).toBeFalsy();
expect(messages.length).toBe(0);
const result = await fullSync();
if (isError(result)) throw Error('fullSync returned error');
expect(result.messages.length).toBe(0);
expect(mockSyncServer.getMessages().length).toBe(2);
});

Expand Down Expand Up @@ -132,8 +136,9 @@ describe('Sync', () => {
},
]);

const { messages } = await fullSync();
expect(messages.length).toBe(2);
const result = await fullSync();
if (isError(result)) throw Error('fullSync returned error');
expect(result.messages.length).toBe(2);
expect(mockSyncServer.getMessages().length).toBe(3);
});
});
Expand Down
12 changes: 8 additions & 4 deletions packages/loot-core/src/shared/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,22 @@ export function debugMemoFailure(prevProps, nextProps) {
return changed === null;
}

export function setIn(map, keys, item) {
export function setIn(
map: Map<string, unknown>,
keys: string[],
item: unknown,
): void {
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
const key = keys[i];

if (i === keys.length - 1) {
map.set(key, item);
} else {
if (!map.has(key)) {
map.set(key, new Map());
map.set(key, new Map<string, unknown>());
}

map = map.get(key);
map = map.get(key) as Map<string, unknown>;
}
}
}
Expand Down

0 comments on commit b64ad25

Please sign in to comment.