/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @flow strict */ // Checked by `pnpm run flow` (not shipped): the binding takes only a // document createYDoc made. import type {LexicalEditor} from 'lexical'; import type { YBindingOptions, YCollabConfig, YCollabInput, YDoc, YLayoutUpgradeHandle, } from '@lexical/y'; import {Doc} from '@y/y'; import { createYCollabConfig, createYDoc, createYServerLayoutUpgrade, YBinding, } from '@lexical/y'; import {createYDoc as createRelayDoc} from '@lexical/y/headless'; declare const editor: LexicalEditor; declare const state: Uint8Array; function configOf(doc: YDoc): YCollabConfig { return { captureTimeout: 500, convert: false, doc, root: null, schemaVersion: 1, selfCheck: null, selfCheckSample: null, synced: false, }; } const doc: YDoc = createYDoc(); const stored: YDoc = createYDoc(state, {gc: false}); const relay: YDoc = createRelayDoc(state); // Y.Doc's options pass through. const kept: YDoc = createYDoc(null, { gc: true, gcFilter: item => item.id.clock >= 0, guid: 'room', }); const relayKept: YDoc = createRelayDoc(state, { gcFilter: item => item.deleted === true, }); // $FlowExpectedError[incompatible-type] not an option of Y.Doc createYDoc(null, {cleanupFormatting: true}); // $FlowExpectedError[incompatible-type] gc is a boolean createRelayDoc(state, {gc: 'no'}); // A YDoc is a Y.Doc. const plain: Doc = doc; const binding = new YBinding(editor, stored, {onStatus: null, schemaVersion: 1}); // $FlowExpectedError[incompatible-type] onStatus is required new YBinding(editor, stored, {schemaVersion: 1}); // $FlowExpectedError[incompatible-type] options, with a schemaVersion, are required new YBinding(editor, stored); // $FlowExpectedError[incompatible-type] schemaVersion is required new YBinding(editor, stored, {onStatus: null, selfCheck: true}); const shared: YDoc = binding.doc; const config: YCollabConfig = configOf(relay); // $FlowExpectedError[incompatible-type] a plain Y.Doc may clean up formats new YBinding(editor, new Doc(), {onStatus: null, schemaVersion: 1}); // $FlowExpectedError[incompatible-type] a plain Y.Doc may clean up formats const forged: YDoc = new Doc(); // $FlowExpectedError[incompatible-type] a plain Y.Doc may clean up formats configOf(new Doc()); // What an app writes: doc, layoutUpgrade, schemaVersion, synced and // onYStatus, every other option optional. const input: YCollabInput = { doc, layoutUpgrade: null, onYStatus: null, schemaVersion: 1, synced: false, }; const tuned: YCollabInput = { captureTimeout: null, doc, layoutUpgrade: createYServerLayoutUpgrade(), onYStatus: status => {}, schemaVersion: 2, synced: true, }; const {schemaVersion: _version, ...noVersion} = input; const {layoutUpgrade: _upgrade, ...noUpgrade} = input; const {onYStatus: _status, ...noStatus} = input; const {synced: _synced, ...noSynced} = input; // $FlowExpectedError[incompatible-type] schemaVersion is required const unversioned: YCollabInput = noVersion; // $FlowExpectedError[incompatible-type] layoutUpgrade is required const undeclared: YCollabInput = noUpgrade; // $FlowExpectedError[incompatible-type] onYStatus is required const unwatched: YCollabInput = noStatus; // $FlowExpectedError[incompatible-type] synced is required const unsynced: YCollabInput = noSynced; // The factory: a handle or null. const configured = createYCollabConfig({ doc, layoutUpgrade: null, onYStatus: null, schemaVersion: 1, synced: false, }); const handshake = createYServerLayoutUpgrade().handshake({ generation: null, material: false, }); const seed: number = handshake.seed; // N2: the collab input accepts a field this release does not know (a later // option, set through a variable). declare const inputWithExtra: { doc: YDoc, layoutUpgrade: YLayoutUpgradeHandle | null, onYStatus: null, schemaVersion: number, synced: boolean, laterOption: boolean, }; const extended: YCollabInput = inputWithExtra; // F3: the binding options and the extension's configuration accept a field // a later release adds (every such field is optional), set through a // variable whose other fields have the same types. type B = YBindingOptions; declare const bindingOptionsWithExtra: { schemaVersion: B['schemaVersion'], root?: B['root'], captureTimeout?: B['captureTimeout'], onStatus: B['onStatus'], selfCheck?: B['selfCheck'], selfCheckSample?: B['selfCheckSample'], convert?: B['convert'], layoutUpgrade?: B['layoutUpgrade'], laterOption: boolean, }; const extendedBindingOptions: YBindingOptions = bindingOptionsWithExtra; type C = YCollabConfig; declare const configWithExtra: { doc: C['doc'], schemaVersion: C['schemaVersion'], root: C['root'], synced: C['synced'], captureTimeout: C['captureTimeout'], selfCheck: C['selfCheck'], selfCheckSample: C['selfCheckSample'], convert: C['convert'], layoutUpgrade?: C['layoutUpgrade'], onYStatus?: C['onYStatus'], laterOption: boolean, }; const extendedConfig: YCollabConfig = configWithExtra; // An options literal with a later field, as a caller writes it. const literalOptions: YBindingOptions = { laterOption: true, onStatus: null, schemaVersion: 1, }; // N3: the handle is opaque: only the factory makes one; its fields read. const realHandle: YLayoutUpgradeHandle = createYServerLayoutUpgrade(); const handleKind: string = realHandle.kind; // $FlowExpectedError[prop-missing] a handle has no target (removed before v1) void realHandle.target; // $FlowExpectedError[extra-arg] the factory takes no options createYServerLayoutUpgrade({target: 1}); // $FlowExpectedError[incompatible-type] a structural look-alike is not a handle const fakeHandle: YLayoutUpgradeHandle = { handshake: realHandle.handshake, kind: 'server', };