Skip to content

Commit

Permalink
refactor(json-crdt): 💡 move PatchLog to /log folder and rename to Log
Browse files Browse the repository at this point in the history
BREAKING CHANGE: 🧨 PatchLog is no Log
  • Loading branch information
streamich committed Apr 2, 2024
1 parent bd31263 commit 50cc8d3
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/json-crdt/file/File.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Model} from '../model';
import {PatchLog} from '../history/PatchLog';
import {Log} from '../log/Log';
import {printTree} from '../../util/print/printTree';
import {decodeModel, decodeNdjsonComponents, decodePatch, decodeSeqCborComponents} from './util';
import {Patch} from '../../json-crdt-patch';
Expand Down Expand Up @@ -42,11 +42,11 @@ export class File implements Printable {
decodedModel = decodeModel(model);
}
}
let log: PatchLog | null = null;
let log: Log | null = null;
if (history) {
const [start, patches] = history;
if (start) {
log = new PatchLog(() => decodeModel(start));
log = new Log(() => decodeModel(start));
for (const patch of patches) log.end.applyPatch(decodePatch(patch));
}
}
Expand Down Expand Up @@ -74,12 +74,12 @@ export class File implements Printable {
}

public static fromModel(model: Model<any>, options: FileOptions = {}): File {
return new File(model, PatchLog.fromNewModel(model), options);
return new File(model, Log.fromNewModel(model), options);
}

constructor(
public readonly model: Model,
public readonly log: PatchLog,
public readonly log: Log,
protected readonly options: FileOptions = {},
) {}

Expand Down
8 changes: 4 additions & 4 deletions src/json-crdt/history/LocalHistoryCrud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {CborEncoder} from '../../json-pack/cbor/CborEncoder';
import type {CrudApi} from 'memfs/lib/crud/types';
import type {Locks} from 'thingies/es2020/Locks';
import type {Patch} from '../../json-crdt-patch';
import type {PatchLog} from './PatchLog';
import type {Log} from '../log/Log';
import type {LocalHistory} from './types';

export const genId = (octets: number = 8): string => {
Expand All @@ -25,7 +25,7 @@ export class LocalHistoryCrud implements LocalHistory {
protected readonly locks: Locks,
) {}

public async create(collection: string[], log: PatchLog): Promise<{id: string}> {
public async create(collection: string[], log: Log): Promise<{id: string}> {
// TODO: Remove `log.end`, just `log` should be enough.
const file = new File(log.end, log, this.fileOpts);
const blob = file.toBinary({
Expand All @@ -39,7 +39,7 @@ export class LocalHistoryCrud implements LocalHistory {
return {id};
}

public async read(collection: string[], id: string): Promise<{log: PatchLog; cursor: string}> {
public async read(collection: string[], id: string): Promise<{log: Log; cursor: string}> {
const blob = await this.crud.get([...collection, id], STATE_FILE_NAME);
const {log} = File.fromSeqCbor(blob);
return {
Expand All @@ -48,7 +48,7 @@ export class LocalHistoryCrud implements LocalHistory {
};
}

public readHistory(collection: string[], id: string, cursor: string): Promise<{log: PatchLog; cursor: string}> {
public readHistory(collection: string[], id: string, cursor: string): Promise<{log: Log; cursor: string}> {
throw new Error('Method not implemented.');
}

Expand Down
4 changes: 2 additions & 2 deletions src/json-crdt/history/SessionHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {FanOutUnsubscribe} from 'thingies/es2020/fanout';
import {InsValOp, Patch} from '../../json-crdt-patch';
import {ValNode} from '../nodes';
import {toSchema} from '../schema/toSchema';
import {PatchLog} from './PatchLog';
import {Log} from '../log/Log';
import {RedoItem, UndoItem, UndoRedoStack} from './UndoRedoStack';

class Undo implements UndoItem {
Expand All @@ -15,7 +15,7 @@ class Redo implements RedoItem {
}

export class SessionHistory {
constructor(public readonly log: PatchLog) {}
constructor(public readonly log: Log) {}

private readonly __onPatchRace = createRace();

Expand Down
6 changes: 3 additions & 3 deletions src/json-crdt/history/__tests__/LocalHistoryCrud.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {memfs} from 'memfs';
import {NodeCrud} from 'memfs/lib/node-to-crud';
import {Locks} from 'thingies/es2020/Locks';
import {LocalHistoryCrud} from '../LocalHistoryCrud';
import {PatchLog} from '../PatchLog';
import {Log} from '../../log/Log';
import {Model} from '../../model';

const setup = async () => {
Expand All @@ -25,7 +25,7 @@ test('can create a new document', async () => {
model.api.root({
foo: 'spam',
});
const log = PatchLog.fromNewModel(model);
const log = Log.fromNewModel(model);
const {id} = await local.create(['test'], log);
expect(typeof id).toBe('string');
expect(id.length > 6).toBe(true);
Expand All @@ -49,7 +49,7 @@ test('can delete a document', async () => {
model.api.root({
foo: 'spam',
});
const log = PatchLog.fromNewModel(model);
const log = Log.fromNewModel(model);
const {id} = await local.create(['test'], log);
await local.read(['test'], id);
await local.delete(['test'], id);
Expand Down
10 changes: 5 additions & 5 deletions src/json-crdt/history/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Patch} from '../../json-crdt-patch';
import type {PatchLog} from '../history/PatchLog';
import type {Log} from '../log/Log';
import type {Model} from '../model';

/**
Expand Down Expand Up @@ -37,16 +37,16 @@ export interface RemoteHistory<Cursor> {
}

export interface LocalHistory {
create(collection: string[], log: PatchLog): Promise<{id: string}>;
read(collection: string[], id: string): Promise<{log: PatchLog; cursor: string}>;
readHistory(collection: string[], id: string, cursor: string): Promise<{log: PatchLog; cursor: string}>;
create(collection: string[], log: Log): Promise<{id: string}>;
read(collection: string[], id: string): Promise<{log: Log; cursor: string}>;
readHistory(collection: string[], id: string, cursor: string): Promise<{log: Log; cursor: string}>;
update(collection: string[], id: string, patches: Patch[]): Promise<void>;
delete(collection: string[], id: string): Promise<void>;
}

export interface EditingSessionHistory {
load(id: string): Promise<Model>;
loadHistory(id: string): Promise<PatchLog>;
loadHistory(id: string): Promise<Log>;
undo(id: string): Promise<void>;
redo(id: string): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {Model} from '../model';
import {first, next} from '../../util/trees/util';
import type {Printable} from '../../util/print/types';

export class PatchLog implements Printable {
export class Log implements Printable {
/**
* Creates a `PatchLog` instance from a newly JSON CRDT model. Checks if
* the model API buffer has any initial operations applied, if yes, it
Expand All @@ -16,9 +16,9 @@ export class PatchLog implements Printable {
* `Model.withLogicalClock()` or `Model.withServerClock()`.
* @returns A new `PatchLog` instance.
*/
public static fromNewModel(model: Model<any>): PatchLog {
public static fromNewModel(model: Model<any>): Log {
const clock = model.clock.clone();
const log = new PatchLog(() => new Model(clock));
const log = new Log(() => new Model(clock));
const api = model.api;
if (api.builder.patch.ops.length) log.end.applyPatch(api.flush());
return log;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {Model} from '../../model';
import {PatchLog} from '../PatchLog';
import {Log} from '../Log';

const setup = (view: unknown) => {
const model = Model.withServerClock();
model.api.root(view);
const log = PatchLog.fromNewModel(model);
const log = Log.fromNewModel(model);
return {log};
};

Expand Down

0 comments on commit 50cc8d3

Please sign in to comment.