Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | 156x 156x 156x 156x 156x 156x 156x 11886x 11886x 11886x 11886x 11886x 11886x 11886x 11886x 11886x 11886x 11886x 11886x 11886x 11886x 11886x 11886x 11886x 11886x 11886x 49731x 49669x 28473x 28448x 25408x 15064x 15030x 3513x 3513x 1852x 1852x 10584x 7002x 294x 155x 8x 24x 21x 74x 5229x 8x 1483x 11238x 11238x 11238x 11238x 45068x 45068x 45068x 45068x 45068x 358953x 45068x 45068x 45068x 536571x 536571x 536571x 536571x 4x 11886x 98x 97x 97x 97x 97x 85x 97x 10747x 10747x 10747x 10747x 11886x 11886x 11886x | import {FanOut} from 'thingies/lib/fanout'; import {VecNode, ConNode, ObjNode, ArrNode, BinNode, StrNode, ValNode} from '../../nodes'; import {type ApiPath, ArrApi, BinApi, ConApi, type NodeApi, ObjApi, StrApi, VecApi, ValApi} from './nodes'; import type {Patch} from '../../../json-crdt-patch/Patch'; import {PatchBuilder} from '../../../json-crdt-patch/PatchBuilder'; import type {SyncStore} from '../../../util/events/sync-store'; import {MergeFanOut, MicrotaskBufferFanOut} from './fanout'; import {ExtNode} from '../../extensions/ExtNode'; import type {Model} from '../Model'; import type {JsonNode, JsonNodeView} from '../../nodes'; /** * Local changes API for a JSON CRDT model. This class is the main entry point * for executing local user actions on a JSON CRDT document. * * @category Local API */ export class ModelApi<N extends JsonNode = JsonNode> implements SyncStore<JsonNodeView<N>> { /** * Patch builder for the local changes. */ public builder: PatchBuilder; /** * Index of the next operation in builder's patch to be committed locally. * * @ignore */ public next: number = 0; /** Emitted before the model is reset, using the `.reset()` method. */ public readonly onBeforeReset = new FanOut<void>(); /** Emitted after the model is reset, using the `.reset()` method. */ public readonly onReset = new FanOut<void>(); /** Emitted before a patch is applied using `model.applyPatch()`. */ public readonly onBeforePatch = new FanOut<Patch>(); /** Emitted after a patch is applied using `model.applyPatch()`. */ public readonly onPatch = new FanOut<Patch>(); /** Emitted before local changes through `model.api` are applied. */ public readonly onBeforeLocalChange = new FanOut<number>(); /** Emitted after local changes through `model.api` are applied. */ public readonly onLocalChange = new FanOut<number>(); /** * Emitted after local changes through `model.api` are applied. Same as * `.onLocalChange`, but this event buffered withing a microtask. */ public readonly onLocalChanges = new MicrotaskBufferFanOut<number>(this.onLocalChange); /** Emitted before a transaction is started. */ public readonly onBeforeTransaction = new FanOut<void>(); /** Emitted after transaction completes. */ public readonly onTransaction = new FanOut<void>(); /** Emitted when the model changes. Combines `onReset`, `onPatch` and `onLocalChange`. */ public readonly onChange = new MergeFanOut<number | Patch | undefined>([ this.onReset, this.onPatch, this.onLocalChange, ]); /** Emitted when the model changes. Same as `.onChange`, but this event is emitted once per microtask. */ public readonly onChanges = new MicrotaskBufferFanOut<number | Patch | undefined>(this.onChange); /** Emitted when the `model.api` builder change buffer is flushed. */ public readonly onFlush = new FanOut<Patch>(); /** * @param model Model instance on which the API operates. */ constructor(public readonly model: Model<N>) { this.builder = new PatchBuilder(model.clock); model.onbeforereset = () => this.onBeforeReset.emit(); model.onreset = () => this.onReset.emit(); model.onbeforepatch = (patch) => this.onBeforePatch.emit(patch); model.onpatch = (patch) => this.onPatch.emit(patch); } /** * Returns a local change API for the given node. If an instance already * exists, returns the existing instance. */ public wrap(node: ValNode): ValApi; public wrap(node: StrNode<any>): StrApi; public wrap(node: BinNode): BinApi; public wrap(node: ArrNode): ArrApi; public wrap(node: ObjNode): ObjApi; public wrap(node: ConNode): ConApi; public wrap(node: VecNode): VecApi; public wrap(node: JsonNode): NodeApi; public wrap(node: ExtNode<any, any>): NodeApi; public wrap(node: JsonNode) { if (node instanceof ValNode) return node.api || (node.api = new ValApi(node, this)); else if (node instanceof StrNode) return node.api || (node.api = new StrApi(node, this)); else if (node instanceof BinNode) return node.api || (node.api = new BinApi(node, this)); else if (node instanceof ArrNode) return node.api || (node.api = new ArrApi(node, this)); else if (node instanceof ObjNode) return node.api || (node.api = new ObjApi(node, this)); else if (node instanceof ConNode) return node.api || (node.api = new ConApi(node, this)); else if (node instanceof VecNode) return node.api || (node.api = new VecApi(node, this)); else if (node instanceof ExtNode) { if (node.api) return node.api; const extension = this.model.ext.get(node.extId)!; return (node.api = new extension.Api(node, this)); } else Ethrow new Error('UNKNOWN_NODE'); } /** * Local changes API for the root node. */ public get r() { return new ValApi(this.model.root, this); } /** @ignore */ public get node() { return this.r.get(); } /** * Traverses the model starting from the root node and returns a local * changes API for a node at the given path. * * @param path Path at which to locate a node. * @returns A local changes API for a node at the given path. */ public in(path?: ApiPath) { return this.r.in(path); } /** * Locates a JSON CRDT node, throws an error if the node doesn't exist. * * @param path Path at which to locate a node. * @returns A JSON CRDT node. */ public find(path?: ApiPath) { return this.node.find(path); } /** * Locates a `con` node and returns a local changes API for it. If the node * doesn't exist or the node at the path is not a `con` node, throws an error. * * @todo Rename to `con`. * * @param path Path at which to locate a node. * @returns A local changes API for a `con` node. */ public con(path?: ApiPath) { return this.node.con(path); } /** * Locates a `val` node and returns a local changes API for it. If the node * doesn't exist or the node at the path is not a `val` node, throws an error. * * @param path Path at which to locate a node. * @returns A local changes API for a `val` node. */ public val(path?: ApiPath) { return this.node.val(path); } /** * Locates a `vec` node and returns a local changes API for it. If the node * doesn't exist or the node at the path is not a `vec` node, throws an error. * * @param path Path at which to locate a node. * @returns A local changes API for a `vec` node. */ public vec(path?: ApiPath) { return this.node.vec(path); } /** * Locates an `obj` node and returns a local changes API for it. If the node * doesn't exist or the node at the path is not an `obj` node, throws an error. * * @param path Path at which to locate a node. * @returns A local changes API for an `obj` node. */ public obj(path?: ApiPath) { return this.node.obj(path); } /** * Locates a `str` node and returns a local changes API for it. If the node * doesn't exist or the node at the path is not a `str` node, throws an error. * * @param path Path at which to locate a node. * @returns A local changes API for a `str` node. */ public str(path?: ApiPath) { return this.node.str(path); } /** * Locates a `bin` node and returns a local changes API for it. If the node * doesn't exist or the node at the path is not a `bin` node, throws an error. * * @param path Path at which to locate a node. * @returns A local changes API for a `bin` node. */ public bin(path?: ApiPath) { return this.node.bin(path); } /** * Locates an `arr` node and returns a local changes API for it. If the node * doesn't exist or the node at the path is not an `arr` node, throws an error. * * @param path Path at which to locate a node. * @returns A local changes API for an `arr` node. */ public arr(path?: ApiPath) { return this.node.arr(path); } /** * Given a JSON/CBOR value, constructs CRDT nodes recursively out of it and * sets the root node of the model to the constructed nodes. * * @param json JSON/CBOR value to set as the view of the model. * @returns Reference to itself. */ public root(json: unknown): this { const builder = this.builder; builder.root(builder.json(json)); this.apply(); return this; } /** * Apply locally any operations from the `.builder`, which haven't been * applied yet. */ public apply() { const ops = this.builder.patch.ops; const length = ops.length; const model = this.model; const from = this.next; this.onBeforeLocalChange.emit(from); for (let i = this.next; i < length; i++) model.applyOperation(ops[i]); this.next = length; model.tick++; this.onLocalChange.emit(from); } /** * Advance patch pointer to the end without applying the operations. With the * idea that they have already been applied locally. * * You need to manually call `this.onBeforeLocalChange.emit(this.next)` before * calling this method. * * @ignore */ public advance() { const from = this.next; this.next = this.builder.patch.ops.length; this.model.tick++; this.onLocalChange.emit(from); } /** * Returns the view of the model. * * @returns JSON/CBOR of the model. */ public view() { return this.model.view(); } private inTx = false; public transaction(callback: () => void) { if (this.inTx) callback(); else { this.inTx = true; try { this.onBeforeTransaction.emit(); callback(); this.onTransaction.emit(); } finally { this.inTx = false; } } } /** * Flushes the builder and returns a patch. * * @returns A JSON CRDT patch. * @todo Make this return undefined if there are no operations in the builder. */ public flush(): Patch { const patch = this.builder.flush(); this.next = 0; if (patch.ops.length) this.onFlush.emit(patch); return patch; } public stopAutoFlush?: () => void = undefined; /** * Begins to automatically flush buffered operations into patches, grouping * operations by microtasks or by transactions. To capture the patch, listen * to the `.onFlush` event. * * @returns Callback to stop auto flushing. */ public autoFlush(): () => void { const drain = () => this.builder.patch.ops.length && this.flush(); const onLocalChangesUnsubscribe = this.onLocalChanges.listen(drain); const onBeforeTransactionUnsubscribe = this.onBeforeTransaction.listen(drain); const onTransactionUnsubscribe = this.onTransaction.listen(drain); return (this.stopAutoFlush = () => { this.stopAutoFlush = undefined; onLocalChangesUnsubscribe(); onBeforeTransactionUnsubscribe(); onTransactionUnsubscribe(); }); } // ---------------------------------------------------------------- SyncStore public readonly subscribe = (callback: () => void) => this.onChanges.listen(() => callback()); public readonly getSnapshot = () => this.view() as any; } |