|
| 1 | +import {Model} from "../model"; |
| 2 | +import {PatchLog} from "./PatchLog"; |
| 3 | +import {FileModelEncoding} from "./constants"; |
| 4 | +import {Encoder as SidecarEncoder} from '../codec/sidecar/binary/Encoder'; |
| 5 | +import {Encoder as StructuralEncoderCompact} from '../codec/structural/compact/Encoder'; |
| 6 | +import {Encoder as StructuralEncoderVerbose} from '../codec/structural/verbose/Encoder'; |
| 7 | +import {encode as encodeCompact} from '../../json-crdt-patch/codec/compact/encode'; |
| 8 | +import {encode as encodeVerbose} from '../../json-crdt-patch/codec/verbose/encode'; |
| 9 | +import type * as types from "./types"; |
| 10 | + |
| 11 | +export class File { |
| 12 | + public static fromModel(model: Model): File { |
| 13 | + return new File(model, PatchLog.fromModel(model)); |
| 14 | + } |
| 15 | + |
| 16 | + constructor( |
| 17 | + public readonly model: Model, |
| 18 | + public readonly history: PatchLog, |
| 19 | + ) {} |
| 20 | + |
| 21 | + public serialize(params: types.FileSerializeParams = {}): types.FileWriteSequence { |
| 22 | + const view = this.model.view(); |
| 23 | + const metadata: types.FileMetadata = [ |
| 24 | + {}, |
| 25 | + FileModelEncoding.SidecarBinary, |
| 26 | + ]; |
| 27 | + let model: Uint8Array | unknown | null = null; |
| 28 | + const modelFormat = params.model ?? 'sidecar'; |
| 29 | + switch (modelFormat) { |
| 30 | + case 'sidecar': { |
| 31 | + metadata[1] = FileModelEncoding.SidecarBinary; |
| 32 | + const encoder = new SidecarEncoder(); |
| 33 | + const [, uint8] = encoder.encode(this.model); |
| 34 | + model = uint8; |
| 35 | + break; |
| 36 | + } |
| 37 | + case 'binary': { |
| 38 | + metadata[1] = FileModelEncoding.StructuralBinary; |
| 39 | + model = this.model.toBinary(); |
| 40 | + break; |
| 41 | + } |
| 42 | + case 'compact': { |
| 43 | + metadata[1] = FileModelEncoding.StructuralCompact; |
| 44 | + model = new StructuralEncoderCompact().encode(this.model); |
| 45 | + break; |
| 46 | + } |
| 47 | + case 'verbose': { |
| 48 | + metadata[1] = FileModelEncoding.StructuralVerbose; |
| 49 | + model = new StructuralEncoderVerbose().encode(this.model); |
| 50 | + break; |
| 51 | + } |
| 52 | + default: |
| 53 | + throw new Error(`Invalid model format: ${modelFormat}`); |
| 54 | + } |
| 55 | + const history: types.FileWriteSequenceHistory = [ |
| 56 | + null, |
| 57 | + [], |
| 58 | + ]; |
| 59 | + const patchFormat = params.history ?? 'binary'; |
| 60 | + switch (patchFormat) { |
| 61 | + case 'binary': { |
| 62 | + history[0] = this.history.start.toBinary(); |
| 63 | + this.history.patches.forEach(({v}) => { |
| 64 | + history[1].push(v.toBinary()); |
| 65 | + }); |
| 66 | + break; |
| 67 | + } |
| 68 | + case 'compact': { |
| 69 | + history[0] = new StructuralEncoderCompact().encode(this.history.start); |
| 70 | + this.history.patches.forEach(({v}) => { |
| 71 | + history[1].push(encodeCompact(v)); |
| 72 | + }); |
| 73 | + break; |
| 74 | + } |
| 75 | + case 'verbose': { |
| 76 | + history[0] = new StructuralEncoderVerbose().encode(this.history.start); |
| 77 | + this.history.patches.forEach(({v}) => { |
| 78 | + history[1].push(encodeVerbose(v)); |
| 79 | + }); |
| 80 | + break; |
| 81 | + } |
| 82 | + default: |
| 83 | + throw new Error(`Invalid history format: ${patchFormat}`); |
| 84 | + } |
| 85 | + return [ |
| 86 | + view, |
| 87 | + metadata, |
| 88 | + model, |
| 89 | + history, |
| 90 | + ]; |
| 91 | + } |
| 92 | +} |
0 commit comments