|
| 1 | +import {FanOutUnsubscribe} from 'thingies/es2020/fanout'; |
| 2 | +import {ITimestampStruct, Patch, compare} from '../../json-crdt-patch'; |
| 3 | +import {printTree} from '../../util/print/printTree'; |
| 4 | +import {AvlMap} from '../../util/trees/avl/AvlMap'; |
| 5 | +import {Model} from '../model'; |
| 6 | +import {first, next} from '../../util/trees/util'; |
| 7 | +import type {Printable} from '../../util/print/types'; |
| 8 | + |
| 9 | +export class PatchLog implements Printable { |
| 10 | + /** |
| 11 | + * Creates a `PatchLog` instance from a newly JSON CRDT model. Checks if |
| 12 | + * the model API buffer has any initial operations applied, if yes, it |
| 13 | + * uses them to create the initial state of the log. |
| 14 | + * |
| 15 | + * @param model A new JSON CRDT model, just created with |
| 16 | + * `Model.withLogicalClock()` or `Model.withServerClock()`. |
| 17 | + * @returns A new `PatchLog` instance. |
| 18 | + */ |
| 19 | + public static fromNewModel(model: Model<any>): PatchLog { |
| 20 | + const clock = model.clock.clone(); |
| 21 | + const log = new PatchLog(() => new Model(clock)); |
| 22 | + const api = model.api; |
| 23 | + if (api.builder.patch.ops.length) log.end.applyPatch(api.flush()); |
| 24 | + return log; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Model factory function that creates a new JSON CRDT model instance, which |
| 29 | + * is used as the starting point of the log. It is called every time a new |
| 30 | + * model is needed to replay the log. |
| 31 | + * |
| 32 | + * @readonly Internally this function may be updated, but externally it is |
| 33 | + * read-only. |
| 34 | + */ |
| 35 | + public start: () => Model; |
| 36 | + |
| 37 | + /** |
| 38 | + * The end of the log, the current state of the document. It is the model |
| 39 | + * instance that is used to apply new patches to the log. |
| 40 | + * |
| 41 | + * @readonly |
| 42 | + */ |
| 43 | + public readonly end: Model; |
| 44 | + |
| 45 | + /** |
| 46 | + * The patches in the log, stored in an AVL tree for efficient replaying. The |
| 47 | + * collection of patches which are applied to the `start()` model to reach |
| 48 | + * the `end` model. |
| 49 | + * |
| 50 | + * @readonly |
| 51 | + */ |
| 52 | + public readonly patches = new AvlMap<ITimestampStruct, Patch>(compare); |
| 53 | + |
| 54 | + private __onPatch: FanOutUnsubscribe; |
| 55 | + private __onFlush: FanOutUnsubscribe; |
| 56 | + |
| 57 | + constructor(start: () => Model) { |
| 58 | + this.start = start; |
| 59 | + const end = (this.end = start()); |
| 60 | + const onPatch = (patch: Patch) => { |
| 61 | + const id = patch.getId(); |
| 62 | + if (!id) return; |
| 63 | + this.patches.set(id, patch); |
| 64 | + }; |
| 65 | + const api = end.api; |
| 66 | + this.__onPatch = api.onPatch.listen(onPatch); |
| 67 | + this.__onFlush = api.onFlush.listen(onPatch); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Call this method to destroy the `PatchLog` instance. It unsubscribes patch |
| 72 | + * and flush listeners from the `end` model and clears the patch log. |
| 73 | + */ |
| 74 | + public destroy() { |
| 75 | + this.__onPatch(); |
| 76 | + this.__onFlush(); |
| 77 | + this.patches.clear(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Creates a new model instance using the `start()` factory function and |
| 82 | + * replays all patches in the log to reach the current state of the document. |
| 83 | + * |
| 84 | + * @returns A new model instance with all patches replayed. |
| 85 | + */ |
| 86 | + public replayToEnd(): Model { |
| 87 | + const clone = this.start().clone(); |
| 88 | + for (let node = first(this.patches.root); node; node = next(node)) clone.applyPatch(node.v); |
| 89 | + return clone; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Replays the patch log until a specified timestamp, including the patch |
| 94 | + * at the given timestamp. The model returned is a new instance of `start()` |
| 95 | + * with patches replayed up to the given timestamp. |
| 96 | + * |
| 97 | + * @param ts Timestamp ID of the patch to replay to. |
| 98 | + * @returns A new model instance with patches replayed up to the given timestamp. |
| 99 | + */ |
| 100 | + public replayTo(ts: ITimestampStruct): Model { |
| 101 | + const clone = this.start().clone(); |
| 102 | + for (let node = first(this.patches.root); node && compare(ts, node.k) >= 0; node = next(node)) |
| 103 | + clone.applyPatch(node.v); |
| 104 | + return clone; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Advance the start of the log to a specified timestamp, excluding the patch |
| 109 | + * at the given timestamp. This method removes all patches from the log that |
| 110 | + * are older than the given timestamp and updates the `start()` factory |
| 111 | + * function to replay the log from the new start. |
| 112 | + * |
| 113 | + * @param ts Timestamp ID of the patch to advance to. |
| 114 | + */ |
| 115 | + public advanceTo(ts: ITimestampStruct): void { |
| 116 | + const newStartPatches: Patch[] = []; |
| 117 | + let node = first(this.patches.root); |
| 118 | + for (; node && compare(ts, node.k) >= 0; node = next(node)) newStartPatches.push(node.v); |
| 119 | + for (const patch of newStartPatches) this.patches.del(patch.getId()!); |
| 120 | + const oldStart = this.start; |
| 121 | + this.start = (): Model => { |
| 122 | + const model = oldStart(); |
| 123 | + for (const patch of newStartPatches) model.applyPatch(patch); |
| 124 | + return model; |
| 125 | + }; |
| 126 | + } |
| 127 | + |
| 128 | + // ---------------------------------------------------------------- Printable |
| 129 | + |
| 130 | + public toString(tab?: string) { |
| 131 | + const patches: Patch[] = []; |
| 132 | + this.patches.forEach(({v}) => patches.push(v)); |
| 133 | + return ( |
| 134 | + `log` + |
| 135 | + printTree(tab, [ |
| 136 | + (tab) => `start` + printTree(tab, [(tab) => this.start().toString(tab)]), |
| 137 | + () => '', |
| 138 | + (tab) => |
| 139 | + 'history' + |
| 140 | + printTree( |
| 141 | + tab, |
| 142 | + patches.map((patch, i) => (tab) => `${i}: ${patch.toString(tab)}`), |
| 143 | + ), |
| 144 | + () => '', |
| 145 | + (tab) => `end` + printTree(tab, [(tab) => this.end.toString(tab)]), |
| 146 | + ]) |
| 147 | + ); |
| 148 | + } |
| 149 | +} |
0 commit comments