Skip to content

Commit

Permalink
chore(zero-client): review nits from #3704
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Feb 6, 2025
1 parent 5d7d856 commit 8493a72
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 39 deletions.
50 changes: 22 additions & 28 deletions packages/replicache/src/sync/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,33 @@ export async function apply(
patch: readonly PatchOperationInternal[],
): Promise<readonly Diff[]> {
const ret: Diff[] = [];
function pushChangeOrAdd(
key: string,
oldValue: FrozenJSONValue | undefined,
newValue: FrozenJSONValue,
) {
if (oldValue === undefined) {
ret.push({
op: 'add',
key,
newValue,
});
} else {
ret.push({
op: 'change',
key,
oldValue,
newValue,
});
}
}
for (const p of patch) {
switch (p.op) {
case 'put': {
const existing = await dbWrite.get(p.key);
const frozen = deepFreeze(p.value);
pushChangeOrAdd(p.key, existing, frozen);
await dbWrite.put(lc, p.key, frozen);
if (existing === undefined) {
ret.push({
op: 'add',
key: p.key,
newValue: frozen,
});
} else {
ret.push({
op: 'change',
key: p.key,
oldValue: existing,
newValue: frozen,
});
}
break;
}
case 'update': {
Expand Down Expand Up @@ -72,21 +79,8 @@ export async function apply(
addToEntries(p.merge);
}
const frozen = deepFreeze(Object.fromEntries(entries));
pushChangeOrAdd(p.key, existing, frozen);
await dbWrite.put(lc, p.key, frozen);
if (existing === undefined) {
ret.push({
op: 'add',
key: p.key,
newValue: frozen,
});
} else {
ret.push({
op: 'change',
key: p.key,
oldValue: existing,
newValue: frozen,
});
}

break;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/zero-client/src/client/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import type {
GotCallback,
QueryDelegate,
} from '../../../zql/src/query/query-impl.ts';
import {nameFromKey, type IVMSourceBranch} from './ivm-source-repo.ts';
import {ENTITIES_KEY_PREFIX} from './keys.ts';
import {type IVMSourceBranch} from './ivm-source-repo.ts';
import {ENTITIES_KEY_PREFIX, sourceNameFromKey} from './keys.ts';

export type AddQuery = (
ast: AST,
Expand Down Expand Up @@ -84,7 +84,7 @@ export class ZeroContext implements QueryDelegate {
for (const diff of changes) {
const {key} = diff;
assert(key.startsWith(ENTITIES_KEY_PREFIX));
const name = nameFromKey(key);
const name = sourceNameFromKey(key);
const source = this.getSource(name);
if (!source) {
continue;
Expand Down
11 changes: 3 additions & 8 deletions packages/zero-client/src/client/ivm-source-repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {Store} from '../../../replicache/src/dag/store.ts';
import {withRead} from '../../../replicache/src/with-transactions.ts';
import type {Hash} from '../../../replicache/src/hash.ts';
import * as FormatVersion from '../../../replicache/src/format-version-enum.ts';
import {ENTITIES_KEY_PREFIX} from './keys.ts';
import {ENTITIES_KEY_PREFIX, sourceNameFromKey} from './keys.ts';
import {must} from '../../../shared/src/must.ts';
import type {Row} from '../../../zero-protocol/src/data.ts';
import type {Diff} from '../../../replicache/src/sync/patch.ts';
Expand Down Expand Up @@ -80,7 +80,7 @@ export class IVMSourceRepo {
if (!entry[0].startsWith(ENTITIES_KEY_PREFIX)) {
break;
}
const name = nameFromKey(entry[0]);
const name = sourceNameFromKey(entry[0]);
const source = must(syncSources.getSource(name));
source.push({
type: 'add',
Expand All @@ -101,7 +101,7 @@ export class IVMSourceRepo {
if (!key.startsWith(ENTITIES_KEY_PREFIX)) {
continue;
}
const name = nameFromKey(key);
const name = sourceNameFromKey(key);
const source = must(this.#sync.getSource(name));
switch (patch.op) {
case 'del':
Expand Down Expand Up @@ -132,11 +132,6 @@ export class IVMSourceRepo {
};
}

export function nameFromKey(key: string): string {
const slash = key.indexOf('/', ENTITIES_KEY_PREFIX.length);
return key.slice(ENTITIES_KEY_PREFIX.length, slash);
}

export class IVMSourceBranch {
readonly #sources: Map<string, MemorySource | undefined>;
readonly #tables: Record<string, TableSchema>;
Expand Down
5 changes: 5 additions & 0 deletions packages/zero-client/src/client/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ export function toPrimaryKeyString(
const idSegment = h128(str);
return ENTITIES_KEY_PREFIX + tableName + '/' + idSegment;
}

export function sourceNameFromKey(key: string): string {
const slash = key.indexOf('/', ENTITIES_KEY_PREFIX.length);
return key.slice(ENTITIES_KEY_PREFIX.length, slash);
}

0 comments on commit 8493a72

Please sign in to comment.