diff --git a/.gitignore b/.gitignore index d54e3689..786f7885 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ node_modules bower_components docs /y.* -/examples/*/index.dist.* +/examples_all/*/index.dist.* .vscode .yjsPersisted build diff --git a/.jsdoc.json b/.jsdoc.json index f5c7a0c3..f8638a27 100644 --- a/.jsdoc.json +++ b/.jsdoc.json @@ -5,7 +5,7 @@ "dictionaries": ["jsdoc"] }, "source": { - "include": ["./types", "./utils/UndoManager.js", "./utils/Y.js", "./provider", "./bindings"], + "include": ["./types", "./utils/UndoManager.mjs", "./utils/Y.mjs", "./provider", "./bindings"], "includePattern": ".js$" }, "plugins": [ diff --git a/README.new.md b/README.new.md new file mode 100644 index 00000000..b5589784 --- /dev/null +++ b/README.new.md @@ -0,0 +1,14 @@ + +### Typescript Declarations + +Until [this](https://github.com/Microsoft/TypeScript/issues/7546) is fixed, the only way to get type declarations is by adding Yjs to the list of checked files: + +```json +{ + "checkJs": true, + "include": [ + "./node_modules/yjs/" + ] + .. +} +``` diff --git a/bindings/dom.js b/bindings/dom.js deleted file mode 100644 index 4eb4d91f..00000000 --- a/bindings/dom.js +++ /dev/null @@ -1 +0,0 @@ -export * from './dom/DomBinding.js' diff --git a/bindings/dom.mjs b/bindings/dom.mjs new file mode 100644 index 00000000..d957a7ad --- /dev/null +++ b/bindings/dom.mjs @@ -0,0 +1 @@ +export * from './dom/DomBinding.mjs' diff --git a/bindings/dom/DomBinding.js b/bindings/dom/DomBinding.mjs similarity index 94% rename from bindings/dom/DomBinding.js rename to bindings/dom/DomBinding.mjs index 4c12cead..6490595b 100644 --- a/bindings/dom/DomBinding.js +++ b/bindings/dom/DomBinding.mjs @@ -4,14 +4,14 @@ /* global MutationObserver, getSelection */ -import { fromRelativePosition } from '../../utils/relativePosition.js' -import { createMutex } from '../../lib/mutex.js' -import { createAssociation, removeAssociation } from './util.js' -import { beforeTransactionSelectionFixer, afterTransactionSelectionFixer, getCurrentRelativeSelection } from './selection.js' -import { defaultFilter, applyFilterOnType } from './filter.js' -import { typeObserver } from './typeObserver.js' -import { domObserver } from './domObserver.js' -import { YXmlFragment } from '../../types/YXmlElement.js' // eslint-disable-line +import { fromRelativePosition } from '../../utils/relativePosition.mjs' +import { createMutex } from '../../lib/mutex.mjs' +import { createAssociation, removeAssociation } from './util.mjs' +import { beforeTransactionSelectionFixer, afterTransactionSelectionFixer, getCurrentRelativeSelection } from './selection.mjs' +import { defaultFilter, applyFilterOnType } from './filter.mjs' +import { typeObserver } from './typeObserver.mjs' +import { domObserver } from './domObserver.mjs' +import { YXmlFragment } from '../../types/YXmlElement.mjs' // eslint-disable-line /** * @callback DomFilter diff --git a/bindings/dom/domObserver.js b/bindings/dom/domObserver.mjs similarity index 96% rename from bindings/dom/domObserver.js rename to bindings/dom/domObserver.mjs index 38c073c7..ba3b3eb0 100644 --- a/bindings/dom/domObserver.js +++ b/bindings/dom/domObserver.mjs @@ -2,13 +2,13 @@ * @module bindings/dom */ -import { YXmlHook } from '../../types/YXmlHook.js' +import { YXmlHook } from '../../types/YXmlHook.mjs' import { iterateUntilUndeleted, removeAssociation, - insertNodeHelper } from './util.js' -import { simpleDiff } from '../../lib/diff.js' -import { YXmlFragment } from '../../types/YXmlElement.js' + insertNodeHelper } from './util.mjs' +import { simpleDiff } from '../../lib/diff.mjs' +import { YXmlFragment } from '../../types/YXmlElement.mjs' /** * 1. Check if any of the nodes was deleted diff --git a/bindings/dom/domToType.js b/bindings/dom/domToType.mjs similarity index 85% rename from bindings/dom/domToType.js rename to bindings/dom/domToType.mjs index d43b6ccd..79955c9f 100644 --- a/bindings/dom/domToType.js +++ b/bindings/dom/domToType.mjs @@ -3,12 +3,12 @@ */ /* eslint-env browser */ -import { YXmlText } from '../../types/YXmlText.js' -import { YXmlHook } from '../../types/YXmlHook.js' -import { YXmlElement } from '../../types/YXmlElement.js' -import { createAssociation, domsToTypes } from './util.js' -import { filterDomAttributes, defaultFilter } from './filter.js' -import { DomBinding } from './DomBinding.js' // eslint-disable-line +import { YXmlText } from '../../types/YXmlText.mjs' +import { YXmlHook } from '../../types/YXmlHook.mjs' +import { YXmlElement } from '../../types/YXmlElement.mjs' +import { createAssociation, domsToTypes } from './util.mjs' +import { filterDomAttributes, defaultFilter } from './filter.mjs' +import { DomBinding } from './DomBinding.mjs' // eslint-disable-line /** * @callback DomFilter diff --git a/bindings/dom/filter.js b/bindings/dom/filter.mjs similarity index 77% rename from bindings/dom/filter.js rename to bindings/dom/filter.mjs index d8d3ca45..124f2dbd 100644 --- a/bindings/dom/filter.js +++ b/bindings/dom/filter.mjs @@ -2,9 +2,10 @@ * @module bindings/dom */ -import { isParentOf } from '../../utils/isParentOf.js' -import * as Y from '../../index.js' -import { DomBinding } from './DomBinding.js' // eslint-disable-line +import { Y } from '../../utils/Y.mjs' // eslint-disable-line +import { YXmlElement, YXmlFragment } from '../../types/YXmlElement.mjs' // eslint-disable-line +import { isParentOf } from '../../utils/isParentOf.mjs' +import { DomBinding } from './DomBinding.mjs' // eslint-disable-line /** * Default filter method (does nothing). @@ -34,14 +35,14 @@ export const filterDomAttributes = (dom, filter) => { /** * Applies a filter on a type. * - * @param {Y.Y} y The Yjs instance. + * @param {Y} y The Yjs instance. * @param {DomBinding} binding The DOM binding instance that has the dom filter. - * @param {Y.XmlElement | Y.XmlFragment } type The type to apply the filter to. + * @param {YXmlElement | YXmlFragment } type The type to apply the filter to. * * @private */ export const applyFilterOnType = (y, binding, type) => { - if (isParentOf(binding.type, type) && type instanceof Y.XmlElement) { + if (isParentOf(binding.type, type) && type instanceof YXmlElement) { const nodeName = type.nodeName let attributes = new Map() if (type.getAttributes !== undefined) { diff --git a/bindings/dom/selection.js b/bindings/dom/selection.mjs similarity index 99% rename from bindings/dom/selection.js rename to bindings/dom/selection.mjs index 96bdf753..c8ce7fcd 100644 --- a/bindings/dom/selection.js +++ b/bindings/dom/selection.mjs @@ -4,7 +4,7 @@ /* globals getSelection */ -import { getRelativePosition } from '../../utils/relativePosition.js' +import { getRelativePosition } from '../../utils/relativePosition.mjs' let relativeSelection = null diff --git a/bindings/dom/typeObserver.js b/bindings/dom/typeObserver.mjs similarity index 95% rename from bindings/dom/typeObserver.js rename to bindings/dom/typeObserver.mjs index 5767a932..6ce5e21e 100644 --- a/bindings/dom/typeObserver.js +++ b/bindings/dom/typeObserver.mjs @@ -5,9 +5,9 @@ /* eslint-env browser */ /* global getSelection */ -import { YXmlText } from '../../types/YXmlText.js' -import { YXmlHook } from '../../types/YXmlHook.js' -import { removeDomChildrenUntilElementFound } from './util.js' +import { YXmlText } from '../../types/YXmlText.mjs' +import { YXmlHook } from '../../types/YXmlHook.mjs' +import { removeDomChildrenUntilElementFound } from './util.mjs' const findScrollReference = scrollingElement => { if (scrollingElement !== null) { diff --git a/bindings/dom/util.js b/bindings/dom/util.mjs similarity index 91% rename from bindings/dom/util.js rename to bindings/dom/util.mjs index e6c0d1ef..32f0d94d 100644 --- a/bindings/dom/util.js +++ b/bindings/dom/util.mjs @@ -2,11 +2,8 @@ * @module bindings/dom */ -import { domToType } from './domToType.js' -import { DomBinding } from './DomBinding.js' // eslint-disable-line -import { YXmlHook } from '../../types/YXmlHook.js' // eslint-disable-line -import { YXmlText } from '../../types/YXmlText.js' // eslint-disable-line -import { YXmlElement, YXmlFragment } from '../../types/YXmlElement.js' // eslint-disable-line +import { domToType } from './domToType.mjs' +import { DomBinding } from './DomBinding.mjs' // eslint-disable-line /** * Iterates items until an undeleted item is found. diff --git a/bindings/prosemirror.js b/bindings/prosemirror.mjs similarity index 90% rename from bindings/prosemirror.js rename to bindings/prosemirror.mjs index 8d73442f..c6bca5fa 100644 --- a/bindings/prosemirror.js +++ b/bindings/prosemirror.mjs @@ -2,17 +2,16 @@ * @module bindings/prosemirror */ -import { BindMapping } from '../utils/BindMapping.js' -import * as Y from '../index.js' -import { createMutex } from '../lib/mutex.js' +import { BindMapping } from '../utils/BindMapping.mjs' +import { YText } from '../types/YText.mjs' // eslint-disable-line +import { YXmlElement, YXmlFragment } from '../types/YXmlElement.mjs' // eslint-disable-line +import { createMutex } from '../lib/mutex.mjs' import * as PModel from 'prosemirror-model' -import { Plugin, PluginKey } from 'prosemirror-state' -import { Decoration, DecorationSet } from 'prosemirror-view' +import { EditorView, Decoration, DecorationSet } from 'prosemirror-view' // eslint-disable-line +import { Plugin, PluginKey, EditorState } from 'prosemirror-state' // eslint-disable-line /** - * @typedef {import('prosemirror-view').EditorView} EditorView - * @typedef {import('prosemirror-state').EditorState} EditorState - * @typedef {BindMapping} ProsemirrorMapping + * @typedef {BindMapping} ProsemirrorMapping */ /** @@ -26,7 +25,7 @@ export const prosemirrorPluginKey = new PluginKey('yjs') * This plugin listens to changes in prosemirror view and keeps yXmlState and view in sync. * * This plugin also keeps references to the type and the shared document so other plugins can access it. - * @param {Y.XmlFragment} yXmlFragment + * @param {YXmlFragment} yXmlFragment * @return {Plugin} Returns a prosemirror plugin that binds to this type */ export const prosemirrorPlugin = yXmlFragment => { @@ -132,7 +131,7 @@ export const cursorPlugin = new Plugin({ */ export class ProsemirrorBinding { /** - * @param {Y.XmlFragment} yXmlFragment The bind source + * @param {YXmlFragment} yXmlFragment The bind source * @param {EditorView} prosemirrorView The target binding */ constructor (yXmlFragment, prosemirrorView) { @@ -157,7 +156,7 @@ export class ProsemirrorBinding { let target = event.target if (target !== this.type) { do { - if (target.constructor === Y.XmlElement) { + if (target.constructor === YXmlElement) { createNodeFromYElement(target, this.prosemirrorView.state.schema, this.mapping) } target = target._parent @@ -204,7 +203,7 @@ export const createNodeIfNotExists = (el, schema, mapping) => { export const createNodeFromYElement = (el, schema, mapping) => { const children = [] el.toArray().forEach(type => { - if (type.constructor === Y.XmlElement) { + if (type.constructor === YXmlElement) { children.push(createNodeIfNotExists(type, schema, mapping)) } else { children.concat(createTextNodesFromYText(type, schema, mapping)).forEach(textchild => children.push(textchild)) @@ -243,17 +242,17 @@ export const createTextNodesFromYText = (text, schema, mapping) => { * @private * @param {PModel.Node} node * @param {ProsemirrorMapping} mapping - * @return {Y.XmlElement | Y.Text} + * @return {YXmlElement | YText} */ export const createTypeFromNode = (node, mapping) => { let type if (node.isText) { - type = new Y.Text() + type = new YText() const attrs = {} node.marks.forEach(mark => { attrs[mark.type.name] = mark.attrs }) type.insert(0, node.text, attrs) } else { - type = new Y.XmlElement(node.type.name) + type = new YXmlElement(node.type.name) for (let key in node.attrs) { type.setAttribute(key, node.attrs[key]) } @@ -265,7 +264,7 @@ export const createTypeFromNode = (node, mapping) => { /** * @private - * @param {Y.XmlFragment} yDomFragment + * @param {YXmlFragment} yDomFragment * @param {EditorState} state * @param {BindMapping} mapping */ diff --git a/bindings/quill.js b/bindings/quill.mjs similarity index 97% rename from bindings/quill.js rename to bindings/quill.mjs index 4cb99da7..47410172 100644 --- a/bindings/quill.js +++ b/bindings/quill.mjs @@ -2,7 +2,7 @@ * @module bindings/quill */ -import { createMutex } from '../lib/mutex.js' +import { createMutex } from '../lib/mutex.mjs' const typeObserver = function (event) { const quill = this.target diff --git a/bindings/textarea.js b/bindings/textarea.mjs similarity index 94% rename from bindings/textarea.js rename to bindings/textarea.mjs index 73c4c087..c0fc50fe 100644 --- a/bindings/textarea.js +++ b/bindings/textarea.mjs @@ -2,9 +2,9 @@ * @module bindings/textarea */ -import { simpleDiff } from '../lib/diff.js' -import { getRelativePosition, fromRelativePosition } from '../utils/relativePosition.js' -import { createMutex } from '../lib/mutex.js' +import { simpleDiff } from '../lib/diff.mjs' +import { getRelativePosition, fromRelativePosition } from '../utils/relativePosition.mjs' +import { createMutex } from '../lib/mutex.mjs' function typeObserver () { this._mutualExclude(() => { diff --git a/examples/prosemirror.html b/examples/prosemirror.html index f454a5ab..37271c36 100644 --- a/examples/prosemirror.html +++ b/examples/prosemirror.html @@ -51,8 +51,6 @@ diff --git a/examples/prosemirror.js b/examples/prosemirror.mjs similarity index 87% rename from examples/prosemirror.js rename to examples/prosemirror.mjs index 9b9c86b0..5edffaa7 100644 --- a/examples/prosemirror.js +++ b/examples/prosemirror.mjs @@ -1,6 +1,6 @@ -import * as Y from '../index.js' -import { prosemirrorPlugin, cursorPlugin } from '../bindings/prosemirror.js' -import { WebsocketProvider } from '../provider/websocket.js' +import * as Y from '../index.mjs' +import { WebsocketProvider } from '../provider/websocket.mjs' +import { prosemirrorPlugin, cursorPlugin } from '../bindings/prosemirror.mjs' import { EditorState } from 'prosemirror-state' import { EditorView } from 'prosemirror-view' diff --git a/examples/quill.js b/examples/quill.mjs similarity index 82% rename from examples/quill.js rename to examples/quill.mjs index 3aabff33..bac115cd 100644 --- a/examples/quill.js +++ b/examples/quill.mjs @@ -1,6 +1,7 @@ -import * as Y from '../index.js' -import { QuillBinding } from '../bindings/quill.js' -import { WebsocketProvider } from '../provider/websocket.js' +import * as Y from '../index.mjs' +import { WebsocketProvider } from '../provider/websocket.mjs' +import { QuillBinding } from '../bindings/quill.mjs' + import Quill from 'quill' const provider = new WebsocketProvider('ws://localhost:1234/') diff --git a/examples/textarea.html b/examples/textarea.html index f7c7989b..13c80650 100644 --- a/examples/textarea.html +++ b/examples/textarea.html @@ -46,15 +46,13 @@ - + diff --git a/examples_all/chat/index.html b/examples_all/chat/index.html index 228c7e75..df79fd54 100644 --- a/examples_all/chat/index.html +++ b/examples_all/chat/index.html @@ -13,7 +13,7 @@ - + diff --git a/examples_all/codemirror/index.html b/examples_all/codemirror/index.html index 2233240e..0a172f13 100644 --- a/examples_all/codemirror/index.html +++ b/examples_all/codemirror/index.html @@ -6,7 +6,7 @@
- + diff --git a/examples_all/drawing/index.html b/examples_all/drawing/index.html index a9dda2c8..837da40a 100644 --- a/examples_all/drawing/index.html +++ b/examples_all/drawing/index.html @@ -13,7 +13,7 @@ - + diff --git a/examples_all/html-editor-drawing-hook/index.html b/examples_all/html-editor-drawing-hook/index.html index 50166af5..291f6243 100644 --- a/examples_all/html-editor-drawing-hook/index.html +++ b/examples_all/html-editor-drawing-hook/index.html @@ -2,7 +2,7 @@ - + - + diff --git a/examples_all/jigsaw/index.html b/examples_all/jigsaw/index.html index 6030ca53..dd86e690 100644 --- a/examples_all/jigsaw/index.html +++ b/examples_all/jigsaw/index.html @@ -17,7 +17,7 @@ - + diff --git a/examples_all/monaco/index.html b/examples_all/monaco/index.html index c4ef9e71..5a11c216 100644 --- a/examples_all/monaco/index.html +++ b/examples_all/monaco/index.html @@ -14,7 +14,7 @@ } - + diff --git a/examples_all/notes/index.js b/examples_all/notes/index.js index 74f31c6e..56f19ffd 100644 --- a/examples_all/notes/index.js +++ b/examples_all/notes/index.js @@ -1,9 +1,9 @@ /* eslint-env browser */ -import { createYdbClient } from '../../YdbClient/index.js' -import Y from '../../src/Y.dist.js' -import * as ydb from '../../YdbClient/YdbClient.js' -import DomBinding from '../../bindings/DomBinding/DomBinding.js' +import { createYdbClient } from '../../YdbClient/index.mjs' +import Y from '../../src/Y.dist.mjs' +import * as ydb from '../../YdbClient/YdbClient.mjs' +import DomBinding from '../../bindings/DomBinding/DomBinding.mjs' const uuidv4 = () => 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => { const r = Math.random() * 16 | 0 diff --git a/index.js b/index.js deleted file mode 100644 index 7efbaa72..00000000 --- a/index.js +++ /dev/null @@ -1,52 +0,0 @@ - -import { Delete } from './structs/Delete.js' -import { ItemJSON } from './structs/ItemJSON.js' -import { ItemString } from './structs/ItemString.js' -import { ItemFormat } from './structs/ItemFormat.js' -import { ItemEmbed } from './structs/ItemEmbed.js' -import { GC } from './structs/GC.js' - -import { YArray } from './types/YArray.js' -import { YMap } from './types/YMap.js' -import { YText } from './types/YText.js' -import { YXmlText } from './types/YXmlText.js' -import { YXmlHook } from './types/YXmlHook.js' -import { YXmlElement, YXmlFragment } from './types/YXmlElement.js' - -import { registerStruct } from './utils/structReferences.js' - -export { Y } from './utils/Y.js' -export { UndoManager } from './utils/UndoManager.js' -export { Transaction } from './utils/Transaction.js' - -export { YArray as Array } from './types/YArray.js' -export { YMap as Map } from './types/YMap.js' -export { YText as Text } from './types/YText.js' -export { YXmlText as XmlText } from './types/YXmlText.js' -export { YXmlHook as XmlHook } from './types/YXmlHook.js' -export { YXmlElement as XmlElement, YXmlFragment as XmlFragment } from './types/YXmlElement.js' - -export { getRelativePosition, fromRelativePosition } from './utils/relativePosition.js' -export { registerStruct } from './utils/structReferences.js' -export * from './protocols/syncProtocol.js' -export * from './protocols/awarenessProtocol.js' -export * from './lib/encoding.js' -export * from './lib/decoding.js' -export * from './lib/mutex.js' - -export { WebsocketProvider } from './provider/websocket/WebSocketProvider.js' - -registerStruct(0, GC) -registerStruct(1, ItemJSON) -registerStruct(2, ItemString) -registerStruct(3, ItemFormat) -registerStruct(4, Delete) - -registerStruct(5, YArray) -registerStruct(6, YMap) -registerStruct(7, YText) -registerStruct(8, YXmlFragment) -registerStruct(9, YXmlElement) -registerStruct(10, YXmlText) -registerStruct(11, YXmlHook) -registerStruct(12, ItemEmbed) diff --git a/index.mjs b/index.mjs new file mode 100644 index 00000000..fefcc1e3 --- /dev/null +++ b/index.mjs @@ -0,0 +1,50 @@ + +import { Delete } from './structs/Delete.mjs' +import { ItemJSON } from './structs/ItemJSON.mjs' +import { ItemString } from './structs/ItemString.mjs' +import { ItemFormat } from './structs/ItemFormat.mjs' +import { ItemEmbed } from './structs/ItemEmbed.mjs' +import { GC } from './structs/GC.mjs' + +import { YArray } from './types/YArray.mjs' +import { YMap } from './types/YMap.mjs' +import { YText } from './types/YText.mjs' +import { YXmlText } from './types/YXmlText.mjs' +import { YXmlHook } from './types/YXmlHook.mjs' +import { YXmlElement, YXmlFragment } from './types/YXmlElement.mjs' + +import { registerStruct } from './utils/structReferences.mjs' + +export { Y } from './utils/Y.mjs' +export { UndoManager } from './utils/UndoManager.mjs' +export { Transaction } from './utils/Transaction.mjs' + +export { YArray as Array } from './types/YArray.mjs' +export { YMap as Map } from './types/YMap.mjs' +export { YText as Text } from './types/YText.mjs' +export { YXmlText as XmlText } from './types/YXmlText.mjs' +export { YXmlHook as XmlHook } from './types/YXmlHook.mjs' +export { YXmlElement as XmlElement, YXmlFragment as XmlFragment } from './types/YXmlElement.mjs' + +export { getRelativePosition, fromRelativePosition } from './utils/relativePosition.mjs' +export { registerStruct } from './utils/structReferences.mjs' +export * from './protocols/syncProtocol.mjs' +export * from './protocols/awarenessProtocol.mjs' +export * from './lib/encoding.mjs' +export * from './lib/decoding.mjs' +export * from './lib/mutex.mjs' + +registerStruct(0, GC) +registerStruct(1, ItemJSON) +registerStruct(2, ItemString) +registerStruct(3, ItemFormat) +registerStruct(4, Delete) + +registerStruct(5, YArray) +registerStruct(6, YMap) +registerStruct(7, YText) +registerStruct(8, YXmlFragment) +registerStruct(9, YXmlElement) +registerStruct(10, YXmlText) +registerStruct(11, YXmlHook) +registerStruct(12, ItemEmbed) diff --git a/lib/NamedEventHandler.js b/lib/NamedEventHandler.mjs similarity index 100% rename from lib/NamedEventHandler.js rename to lib/NamedEventHandler.mjs diff --git a/lib/Tree.js b/lib/Tree.mjs similarity index 100% rename from lib/Tree.js rename to lib/Tree.mjs diff --git a/lib/binary.js b/lib/binary.mjs similarity index 100% rename from lib/binary.js rename to lib/binary.mjs diff --git a/lib/decoding.js b/lib/decoding.mjs similarity index 99% rename from lib/decoding.js rename to lib/decoding.mjs index 384577cb..0d9a47d3 100644 --- a/lib/decoding.js +++ b/lib/decoding.mjs @@ -4,7 +4,7 @@ /* global Buffer */ -import * as globals from './globals.js' +import * as globals from './globals.mjs' /** * A Decoder handles the decoding of an ArrayBuffer. diff --git a/lib/diff.js b/lib/diff.mjs similarity index 100% rename from lib/diff.js rename to lib/diff.mjs diff --git a/lib/encoding.js b/lib/encoding.mjs similarity index 99% rename from lib/encoding.js rename to lib/encoding.mjs index 5de8d260..758ac881 100644 --- a/lib/encoding.js +++ b/lib/encoding.mjs @@ -1,7 +1,7 @@ /** * @module encoding */ -import * as globals from './globals.js' +import * as globals from './globals.mjs' const bits7 = 0b1111111 const bits8 = 0b11111111 diff --git a/lib/encoding.test.js b/lib/encoding.test.mjs similarity index 97% rename from lib/encoding.test.js rename to lib/encoding.test.mjs index b957707b..b74a4163 100644 --- a/lib/encoding.test.js +++ b/lib/encoding.test.mjs @@ -1,4 +1,4 @@ -import * as encoding from './encoding.js' +import * as encoding from './encoding.mjs' /** * Check if binary encoding is compatible with golang binary encoding - binary.PutVarUint. diff --git a/lib/globals.js b/lib/globals.mjs similarity index 100% rename from lib/globals.js rename to lib/globals.mjs diff --git a/lib/idb.js b/lib/idb.mjs similarity index 97% rename from lib/idb.js rename to lib/idb.mjs index 72a00fef..95e839f9 100644 --- a/lib/idb.js +++ b/lib/idb.mjs @@ -4,7 +4,7 @@ /* eslint-env browser */ -import * as globals from './globals.js' +import * as globals from './globals.mjs' /* * IDB Request to Promise transformer @@ -16,6 +16,8 @@ export const rtop = request => globals.createPromise((resolve, reject) => { }) /** + * @param {string} name + * @param {Function} initDB Called when the database is first created * @return {Promise} */ export const openDB = (name, initDB) => globals.createPromise((resolve, reject) => { diff --git a/lib/idb.test.js b/lib/idb.test.mjs similarity index 90% rename from lib/idb.test.js rename to lib/idb.test.mjs index e96a1352..6f2b9af3 100644 --- a/lib/idb.test.js +++ b/lib/idb.test.mjs @@ -1,6 +1,6 @@ -import * as test from './testing.js' -import * as idb from './idb.js' -import * as logging from './logging.js' +import * as test from './testing.mjs' +import * as idb from './idb.mjs' +import * as logging from './logging.mjs' const initTestDB = db => idb.createStores(db, [['test']]) const testDBName = 'idb-test' diff --git a/lib/logging.js b/lib/logging.mjs similarity index 93% rename from lib/logging.js rename to lib/logging.mjs index 6af7e49b..f6c56ca0 100644 --- a/lib/logging.js +++ b/lib/logging.mjs @@ -2,7 +2,7 @@ * @module logging */ -import * as globals from './globals.js' +import * as globals from './globals.mjs' let date = new Date().getTime() diff --git a/lib/math.js b/lib/math.mjs similarity index 100% rename from lib/math.js rename to lib/math.mjs diff --git a/lib/mutex.js b/lib/mutex.mjs similarity index 100% rename from lib/mutex.js rename to lib/mutex.mjs diff --git a/lib/number.js b/lib/number.mjs similarity index 100% rename from lib/number.js rename to lib/number.mjs diff --git a/lib/prng/PRNG/Mt19937.js b/lib/prng/PRNG/Mt19937.mjs similarity index 100% rename from lib/prng/PRNG/Mt19937.js rename to lib/prng/PRNG/Mt19937.mjs diff --git a/lib/prng/PRNG/PRNG.tests.js b/lib/prng/PRNG/PRNG.tests.mjs similarity index 87% rename from lib/prng/PRNG/PRNG.tests.js rename to lib/prng/PRNG/PRNG.tests.mjs index f524edd3..c680c347 100644 --- a/lib/prng/PRNG/PRNG.tests.js +++ b/lib/prng/PRNG/PRNG.tests.mjs @@ -2,10 +2,10 @@ * @module prng */ -import { Mt19937 } from './Mt19937.js' -import { Xoroshiro128plus } from './Xoroshiro128plus.js' -import { Xorshift32 } from './Xorshift32.js' -import * as time from '../../time.js' +import { Mt19937 } from './Mt19937.mjs' +import { Xoroshiro128plus } from './Xoroshiro128plus.mjs' +import { Xorshift32 } from './Xorshift32.mjs' +import * as time from '../../time.mjs' const DIAMETER = 300 const NUMBERS = 10000 diff --git a/lib/prng/PRNG/Xoroshiro128plus.js b/lib/prng/PRNG/Xoroshiro128plus.mjs similarity index 98% rename from lib/prng/PRNG/Xoroshiro128plus.js rename to lib/prng/PRNG/Xoroshiro128plus.mjs index 13b97156..ceea0169 100644 --- a/lib/prng/PRNG/Xoroshiro128plus.js +++ b/lib/prng/PRNG/Xoroshiro128plus.mjs @@ -2,7 +2,7 @@ * @module prng */ -import { Xorshift32 } from './Xorshift32.js' +import { Xorshift32 } from './Xorshift32.mjs' /** * This is a variant of xoroshiro128plus - the fastest full-period generator passing BigCrush without systematic failures. diff --git a/lib/prng/PRNG/Xorshift32.js b/lib/prng/PRNG/Xorshift32.mjs similarity index 100% rename from lib/prng/PRNG/Xorshift32.js rename to lib/prng/PRNG/Xorshift32.mjs diff --git a/lib/prng/prng.js b/lib/prng/prng.js index ae9e898e..c9c2e33b 100644 --- a/lib/prng/prng.js +++ b/lib/prng/prng.js @@ -2,12 +2,12 @@ * @module prng */ -import * as binary from '../binary.js' -import { fromCharCode, fromCodePoint } from '../string.js' -import { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } from '../number.js' -import * as math from '../math.js' +import * as binary from '../binary.mjs' +import { fromCharCode, fromCodePoint } from '../string.mjs' +import { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } from '../number.mjs' +import * as math from '../math.mjs' -import { Xoroshiro128plus as DefaultPRNG } from './PRNG/Xoroshiro128plus.js' +import { Xoroshiro128plus as DefaultPRNG } from './PRNG/Xoroshiro128plus.mjs' /** * Description of the function diff --git a/lib/prng/prng.mjs b/lib/prng/prng.mjs new file mode 100644 index 00000000..c9c2e33b --- /dev/null +++ b/lib/prng/prng.mjs @@ -0,0 +1,134 @@ +/** + * @module prng + */ + +import * as binary from '../binary.mjs' +import { fromCharCode, fromCodePoint } from '../string.mjs' +import { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } from '../number.mjs' +import * as math from '../math.mjs' + +import { Xoroshiro128plus as DefaultPRNG } from './PRNG/Xoroshiro128plus.mjs' + +/** + * Description of the function + * @callback generatorNext + * @return {number} A 32bit integer + */ + +/** + * A random type generator. + * + * @typedef {Object} PRNG + * @property {generatorNext} next Generate new number + */ + +/** + * Create a Xoroshiro128plus Pseudo-Random-Number-Generator. + * This is the fastest full-period generator passing BigCrush without systematic failures. + * But there are more PRNGs available in ./PRNG/. + * + * @param {number} seed A positive 32bit integer. Do not use negative numbers. + * @return {PRNG} + */ +export const createPRNG = seed => new DefaultPRNG(Math.floor(seed < 1 ? seed * binary.BITS32 : seed)) + +/** + * Generates a single random bool. + * + * @param {PRNG} gen A random number generator. + * @return {Boolean} A random boolean + */ +export const bool = gen => (gen.next() & 2) === 2 // brackets are non-optional! + +/** + * Generates a random integer with 53 bit resolution. + * + * @param {PRNG} gen A random number generator. + * @param {Number} [min = MIN_SAFE_INTEGER] The lower bound of the allowed return values (inclusive). + * @param {Number} [max = MAX_SAFE_INTEGER] The upper bound of the allowed return values (inclusive). + * @return {Number} A random integer on [min, max] + */ +export const int53 = (gen, min = MIN_SAFE_INTEGER, max = MAX_SAFE_INTEGER) => math.floor(real53(gen) * (max + 1 - min) + min) + +/** + * Generates a random integer with 32 bit resolution. + * + * @param {PRNG} gen A random number generator. + * @param {Number} [min = MIN_SAFE_INTEGER] The lower bound of the allowed return values (inclusive). + * @param {Number} [max = MAX_SAFE_INTEGER] The upper bound of the allowed return values (inclusive). + * @return {Number} A random integer on [min, max] + */ +export const int32 = (gen, min = MIN_SAFE_INTEGER, max = MAX_SAFE_INTEGER) => min + ((gen.next() >>> 0) % (max + 1 - min)) + +/** + * Generates a random real on [0, 1) with 32 bit resolution. + * + * @param {PRNG} gen A random number generator. + * @return {Number} A random real number on [0, 1). + */ +export const real32 = gen => (gen.next() >>> 0) / binary.BITS32 + +/** + * Generates a random real on [0, 1) with 53 bit resolution. + * + * @param {PRNG} gen A random number generator. + * @return {Number} A random real number on [0, 1). + */ +export const real53 = gen => (((gen.next() >>> 5) * binary.BIT26) + (gen.next() >>> 6)) / MAX_SAFE_INTEGER + +/** + * Generates a random character from char code 32 - 126. I.e. Characters, Numbers, special characters, and Space: + * + * (Space)!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[/]^_`abcdefghijklmnopqrstuvwxyz{|}~ + */ +export const char = gen => fromCharCode(int32(gen, 32, 126)) + +/** + * @param {PRNG} gen + * @return {string} A single letter (a-z) + */ +export const letter = gen => fromCharCode(int32(gen, 97, 122)) + +/** + * @param {PRNG} gen + * @return {string} A random word without spaces consisting of letters (a-z) + */ +export const word = gen => { + const len = int32(gen, 0, 20) + let str = '' + for (let i = 0; i < len; i++) { + str += letter(gen) + } + return str +} + +/** + * TODO: this function produces invalid runes. Does not cover all of utf16!! + */ +export const utf16Rune = gen => { + const codepoint = int32(gen, 0, 256) + return fromCodePoint(codepoint) +} + +/** + * @param {PRNG} gen + * @param {number} [maxlen = 20] + */ +export const utf16String = (gen, maxlen = 20) => { + const len = int32(gen, 0, maxlen) + let str = '' + for (let i = 0; i < len; i++) { + str += utf16Rune(gen) + } + return str +} + +/** + * Returns one element of a given array. + * + * @param {PRNG} gen A random number generator. + * @param {Array} array Non empty Array of possible values. + * @return {T} One of the values of the supplied Array. + * @template T + */ +export const oneOf = (gen, array) => array[int32(gen, 0, array.length - 1)] diff --git a/lib/prng/prng.test.js b/lib/prng/prng.test.mjs similarity index 88% rename from lib/prng/prng.test.js rename to lib/prng/prng.test.mjs index 00b5ee66..d56e0b49 100644 --- a/lib/prng/prng.test.js +++ b/lib/prng/prng.test.mjs @@ -4,14 +4,14 @@ /** *TODO: enable tests -import * as rt from '../rich-text/formatters.mjs' -import { test } from '../test/test.mjs' -import Xoroshiro128plus from './PRNG/Xoroshiro128plus.mjs' -import Xorshift32 from './PRNG/Xorshift32.mjs' -import MT19937 from './PRNG/Mt19937.mjs' -import { generateBool, generateInt, generateInt32, generateReal, generateChar } from './random.mjs' -import { MAX_SAFE_INTEGER } from '../number/constants.mjs' -import { BIT32 } from '../binary/constants.mjs' +import * as rt from '../rich-text/formatters.mjs'' +import { test } from '../test/test.mjs'' +import Xoroshiro128plus from './PRNG/Xoroshiro128plus.mjs'' +import Xorshift32 from './PRNG/Xorshift32.mjs'' +import MT19937 from './PRNG/Mt19937.mjs'' +import { generateBool, generateInt, generateInt32, generateReal, generateChar } from './random.mjs'' +import { MAX_SAFE_INTEGER } from '../number/constants.mjs'' +import { BIT32 } from '../binary/constants.mjs'' function init (Gen) { return { diff --git a/lib/random.js b/lib/random.mjs similarity index 100% rename from lib/random.js rename to lib/random.mjs diff --git a/lib/string.js b/lib/string.mjs similarity index 100% rename from lib/string.js rename to lib/string.mjs diff --git a/lib/testing.js b/lib/testing.mjs similarity index 92% rename from lib/testing.js rename to lib/testing.mjs index 43c85a2a..72da7864 100644 --- a/lib/testing.js +++ b/lib/testing.mjs @@ -2,8 +2,8 @@ * @module testing */ -import * as logging from './logging.js' -import { simpleDiff } from './diff.js' +import * as logging from './logging.mjs' +import { simpleDiff } from './diff.mjs' export const run = async (name, f) => { console.log(`%cStart:%c ${name}`, 'color:blue;', '') diff --git a/lib/time.js b/lib/time.mjs similarity index 100% rename from lib/time.js rename to lib/time.mjs diff --git a/now.json b/now.json new file mode 100644 index 00000000..64c043b7 --- /dev/null +++ b/now.json @@ -0,0 +1,7 @@ +{ + "name": "yjs-v13-72", + "engines": { + "node": "10.13.0" + }, + "start": "npm run ws-server" +} diff --git a/package.json b/package.json index 0cb031ad..7d8a4eab 100644 --- a/package.json +++ b/package.json @@ -1,29 +1,43 @@ { "name": "yjs", "version": "13.0.0-72", - "description": "A framework for real-time p2p shared editing on any data", - "main": "./build/yjs.js", - "module": "./index.js", + "description": "A ", + "module": "./index.mjs'", + "sideEffects": false, "scripts": { "test": "npm run lint", - "build": "rollup -c", + "build": "rm -rf build && rollup -c", "watch": "rollup -wc", "debug": "concurrently 'rollup -wc' 'cutest-serve build/y.test.js -o'", "lint": "standard **/*.js", - "docs": "rm -rf docs; ! jsdoc --configure ./.jsdoc.json --verbose --readme ./README.md --package ./package.json", + "docs": "rm -rf docs; jsdoc --configure ./.jsdoc.json --verbose --readme ./README.md --package ./package.json || true", "serve-docs": "npm run docs && serve ./docs/", - "postversion": "npm run build" + "postversion": "npm run build", + "websocket-server": "node --experimental-modules ./provider/websocket/server.mjs" }, "files": [ - "src/*", - ".esdoc.json", - ".jsdoc.json", + "bindings/*", "docs/*", - "build/*", + "examples/*", "lib/*", + "persistences/*", + "protocols/*", "provider/*", - "bindings/*" + "bindings/*", + "structs/*", + "tests/*", + "types/*", + "utils/*", + "index.js", + "README.md", + "LICENSE" ], + "dictionaries": { + "doc": "docs", + "example": "examples", + "test": "tests", + "lib": "./" + }, "bin": { "y-websockets": "provider/websocket/server.js" }, @@ -40,13 +54,7 @@ "url": "https://github.com/y-js/yjs.git" }, "keywords": [ - "Yjs", - "OT", - "Collaboration", - "Synchronization", - "ShareJS", - "Coweb", - "Concurrency" + "crdt" ], "author": "Kevin Jahns", "email": "kevin.jahns@rwth-aachen.de", diff --git a/persistences/FilePersistence.js b/persistences/FilePersistence.mjs similarity index 92% rename from persistences/FilePersistence.js rename to persistences/FilePersistence.mjs index 0c22ed43..f08a61bf 100644 --- a/persistences/FilePersistence.js +++ b/persistences/FilePersistence.mjs @@ -1,10 +1,10 @@ /* import fs from 'fs' import path from 'path' -import * as encoding from '../lib/encoding.js' -import * as decoding from '../lib/decoding.js' -import { createMutex } from '../lib/mutex.js' -import { encodeUpdate, encodeStructsDS, decodePersisted } from './decodePersisted.js' +import * as encoding from '../lib/encoding.mjs' +import * as decoding from '../lib/decoding.mjs' +import { createMutex } from '../lib/mutex.mjs' +import { encodeUpdate, encodeStructsDS, decodePersisted } from './decodePersisted.mjs' function createFilePath (persistence, roomName) { // TODO: filename checking! diff --git a/persistences/IndexedDBPersistence.js b/persistences/IndexedDBPersistence.mjs similarity index 98% rename from persistences/IndexedDBPersistence.js rename to persistences/IndexedDBPersistence.mjs index 78a2a955..b804211b 100644 --- a/persistences/IndexedDBPersistence.js +++ b/persistences/IndexedDBPersistence.mjs @@ -1,8 +1,8 @@ /* -import { Y } from '../utils/Y.js' -import { createMutex } from '../lib/mutex.js' -import { decodePersisted, encodeStructsDS, encodeUpdate, PERSIST_STRUCTS_DS, PERSIST_UPDATE } from './decodePersisted.js' +import { Y } from '../utils/Y.mjs' +import { createMutex } from '../lib/mutex.mjs' +import { decodePersisted, encodeStructsDS, encodeUpdate, PERSIST_STRUCTS_DS, PERSIST_UPDATE } from './decodePersisted.mjs' function rtop (request) { return new Promise(function (resolve, reject) { diff --git a/persistences/decodePersisted.js b/persistences/decodePersisted.mjs similarity index 93% rename from persistences/decodePersisted.js rename to persistences/decodePersisted.mjs index 20d92320..080ab36b 100644 --- a/persistences/decodePersisted.js +++ b/persistences/decodePersisted.mjs @@ -1,7 +1,7 @@ /* -import { integrateRemoteStructs } from '../MessageHandler/integrateRemoteStructs.js' -import { writeStructs } from '../MessageHandler/syncStep1.js' -import { writeDeleteSet, readDeleteSet } from '../MessageHandler/deleteSet.js' +import { integrateRemoteStructs } from '../MessageHandler/integrateRemoteStructs.mjs' +import { writeStructs } from '../MessageHandler/syncStep1.mjs' +import { writeDeleteSet, readDeleteSet } from '../MessageHandler/deleteSet.mjs' export const PERSIST_UPDATE = 0 /** diff --git a/protocols/awarenessProtocol.js b/protocols/awarenessProtocol.mjs similarity index 94% rename from protocols/awarenessProtocol.js rename to protocols/awarenessProtocol.mjs index a2ca0c80..e0a2887a 100644 --- a/protocols/awarenessProtocol.js +++ b/protocols/awarenessProtocol.mjs @@ -2,9 +2,9 @@ * @module awareness-protocol */ -import * as encoding from '../lib/encoding.js' -import * as decoding from '../lib/decoding.js' -import { Y } from '../utils/Y.js' // eslint-disable-line +import * as encoding from '../lib/encoding.mjs' +import * as decoding from '../lib/decoding.mjs' +import { Y } from '../utils/Y.mjs' // eslint-disable-line const messageUsersStateChanged = 0 diff --git a/protocols/syncProtocol.js b/protocols/syncProtocol.mjs similarity index 97% rename from protocols/syncProtocol.js rename to protocols/syncProtocol.mjs index d8b63e87..49888b39 100644 --- a/protocols/syncProtocol.js +++ b/protocols/syncProtocol.mjs @@ -2,14 +2,14 @@ * @module sync-protocol */ -import * as encoding from '../lib/encoding.js' -import * as decoding from '../lib/decoding.js' -import * as ID from '../utils/ID.js' -import { getStruct } from '../utils/structReferences.js' -import { deleteItemRange } from '../structs/Delete.js' -import { integrateRemoteStruct } from '../utils/integrateRemoteStructs.js' -import { Y } from '../utils/Y.js' // eslint-disable-line -import { Item } from '../structs/Item.js' +import * as encoding from '../lib/encoding.mjs' +import * as decoding from '../lib/decoding.mjs' +import * as ID from '../utils/ID.mjs' +import { getStruct } from '../utils/structReferences.mjs' +import { deleteItemRange } from '../utils/structManipulation.mjs' +import { integrateRemoteStruct } from '../utils/integrateRemoteStructs.mjs' +import { Y } from '../utils/Y.mjs' // eslint-disable-line +import { Item } from '../structs/Item.mjs' /** * @typedef {Map} StateSet @@ -83,7 +83,7 @@ export const writeDeleteSet = (encoder, y) => { const gc = n.gc if (currentUser !== user) { numberOfUsers++ - // a new user was foundimport { StateSet } from '../Store/StateStore.js' // eslint-disable-line + // a new user was foundimport { StateSet } from '../Store/StateStore.mjs' // eslint-disable-line if (currentUser !== null) { // happens on first iteration encoding.setUint32(encoder, lastLenPos, currentLength) diff --git a/provider/websocket.js b/provider/websocket.js deleted file mode 100644 index 03a23052..00000000 --- a/provider/websocket.js +++ /dev/null @@ -1,5 +0,0 @@ -/** - * @module provider/websocket - */ - -export * from './websocket/WebSocketProvider.js' diff --git a/provider/websocket.mjs b/provider/websocket.mjs new file mode 100644 index 00000000..c4684770 --- /dev/null +++ b/provider/websocket.mjs @@ -0,0 +1,5 @@ +/** + * @module provider/websocket + */ + +export * from './websocket/WebSocketProvider.mjs' diff --git a/provider/websocket/WebSocketProvider.js b/provider/websocket/WebSocketProvider.mjs similarity index 97% rename from provider/websocket/WebSocketProvider.js rename to provider/websocket/WebSocketProvider.mjs index 68f94e7a..2283a1b4 100644 --- a/provider/websocket/WebSocketProvider.js +++ b/provider/websocket/WebSocketProvider.mjs @@ -4,8 +4,8 @@ /* eslint-env browser */ -import * as Y from '../../index.js' -export * from '../../index.js' +import * as Y from '../../index.mjs' +export * from '../../index.mjs' const messageSync = 0 const messageAwareness = 1 diff --git a/provider/websocket/server.js b/provider/websocket/server.mjs similarity index 97% rename from provider/websocket/server.js rename to provider/websocket/server.mjs index c14d4b4b..37d756ee 100644 --- a/provider/websocket/server.js +++ b/provider/websocket/server.mjs @@ -2,8 +2,9 @@ * @module provider/websocket */ -const Y = require('../../build/yjs.umd.js') -const WebSocket = require('ws') +import * as Y from '../../index.mjs' +import WebSocket from 'ws' + const wss = new WebSocket.Server({ port: 1234 }) const docs = new Map() diff --git a/provider/ydb/NamedEventHandler.js b/provider/ydb/NamedEventHandler.mjs similarity index 91% rename from provider/ydb/NamedEventHandler.js rename to provider/ydb/NamedEventHandler.mjs index 7474dfad..75460d68 100644 --- a/provider/ydb/NamedEventHandler.js +++ b/provider/ydb/NamedEventHandler.mjs @@ -2,7 +2,7 @@ * @module provider/ydb */ -import * as globals from './globals.js' +import * as globals from './globals.mjs' export const Class = class NamedEventHandler { constructor () { diff --git a/provider/ydb/YdbClient.js b/provider/ydb/YdbClient.mjs similarity index 92% rename from provider/ydb/YdbClient.js rename to provider/ydb/YdbClient.mjs index 39da5cf6..1660cd18 100644 --- a/provider/ydb/YdbClient.js +++ b/provider/ydb/YdbClient.mjs @@ -3,19 +3,19 @@ */ /* eslint-env browser */ -import * as idbactions from './idbactions.js' -import * as globals from '../../lib/globals.js' -import * as message from './message.js' -import * as bc from './broadcastchannel.js' -import * as encoding from '../../lib/encoding.js' -import * as logging from '../../lib/logging.js' -import * as idb from '../../lib/idb.js' -import * as decoding from '../../lib/decoding.js' -import { Y } from '../../utils/Y.js' -import { integrateRemoteStruct } from '../MessageHandler/integrateRemoteStructs.js' -import { createMutualExclude } from '../../lib/mutualExclude.js' +import * as idbactions from './idbactions.mjs' +import * as globals from '../../lib/globals.mjs' +import * as message from './message.mjs' +import * as bc from './broadcastchannel.mjs' +import * as encoding from '../../lib/encoding.mjs' +import * as logging from '../../lib/logging.mjs' +import * as idb from '../../lib/idb.mjs' +import * as decoding from '../../lib/decoding.mjs' +import { Y } from '../../utils/Y.mjs' +import { integrateRemoteStruct } from '../MessageHandler/integrateRemoteStructs.mjs' +import { createMutualExclude } from '../../lib/mutualExclude.mjs' -import * as NamedEventHandler from './NamedEventHandler.js' +import * as NamedEventHandler from './NamedEventHandler.mjs' /** * @typedef RoomState diff --git a/provider/ydb/YdbClient.test.js b/provider/ydb/YdbClient.test.mjs similarity index 92% rename from provider/ydb/YdbClient.test.js rename to provider/ydb/YdbClient.test.mjs index 9468e811..327945cd 100644 --- a/provider/ydb/YdbClient.test.js +++ b/provider/ydb/YdbClient.test.mjs @@ -4,11 +4,11 @@ /* eslint-env browser */ -import * as test from './test.js' -import * as ydbClient from './YdbClient.js' -import * as globals from './globals.js' -import * as idbactions from './idbactions.js' -import * as logging from './logging.js' +import * as test from './test.mjs' +import * as ydbClient from './YdbClient.mjs' +import * as globals from './globals.mjs' +import * as idbactions from './idbactions.mjs' +import * as logging from './logging.mjs' const wsUrl = 'ws://127.0.0.1:8899/ws' const testRoom = 'testroom' diff --git a/provider/ydb/broadcastchannel.js b/provider/ydb/broadcastchannel.mjs similarity index 97% rename from provider/ydb/broadcastchannel.js rename to provider/ydb/broadcastchannel.mjs index 5badfd4e..61fb429d 100644 --- a/provider/ydb/broadcastchannel.js +++ b/provider/ydb/broadcastchannel.mjs @@ -4,10 +4,10 @@ /* eslint-env browser */ -import * as decoding from '../../lib/decoding.js' -import * as encoding from '../../lib/encoding.js' -import * as globals from '../../lib/globals.js' -import * as NamedEventHandler from './NamedEventHandler.js' +import * as decoding from '../../lib/decoding.mjs' +import * as encoding from '../../lib/encoding.mjs' +import * as globals from '../../lib/globals.mjs' +import * as NamedEventHandler from './NamedEventHandler.mjs' const bc = new BroadcastChannel('ydb-client') /** @@ -15,7 +15,8 @@ const bc = new BroadcastChannel('ydb-client') */ const datasubs = globals.createMap() /** - * @type {Set} Set of Ydb instances + * Set of Ydb instances + * @type {Set} */ const ydbinstances = globals.createSet() diff --git a/provider/ydb/idbactions.js b/provider/ydb/idbactions.mjs similarity index 97% rename from provider/ydb/idbactions.js rename to provider/ydb/idbactions.mjs index fb65c6f9..7c4111d2 100644 --- a/provider/ydb/idbactions.js +++ b/provider/ydb/idbactions.mjs @@ -33,11 +33,11 @@ * - A client may update a room when the room is in either US or Co */ -import * as encoding from '../../lib/encoding.js' -import * as decoding from '../../lib/decoding.js' -import * as idb from '../../lib/idb.js' -import * as globals from '../../lib/globals.js' -import * as message from './message.js' +import * as encoding from '../../lib/encoding.mjs' +import * as decoding from '../../lib/decoding.mjs' +import * as idb from '../../lib/idb.mjs' +import * as globals from '../../lib/globals.mjs' +import * as message from './message.mjs' /** * Get 'client-unconfirmed' store from transaction @@ -68,10 +68,16 @@ const getStoreUS = t => idb.getStore(t, STORE_US) /** * @param {string} room * @param {number} offset - * @return {[string, number]} + * @return {HUTableKey} */ const encodeHUKey = (room, offset) => [room, offset] +/** + * Array of length 2: [string, number] + * @typedef HUTableKey + * @type {any} + */ + /** * @typedef RoomAndOffset * @type {Object} @@ -80,7 +86,7 @@ const encodeHUKey = (room, offset) => [room, offset] */ /** - * @param {[string, number]} key + * @param {HUTableKey} key * @return {RoomAndOffset} */ const decodeHUKey = key => { diff --git a/provider/ydb/idbactions.test.js b/provider/ydb/idbactions.test.mjs similarity index 86% rename from provider/ydb/idbactions.test.js rename to provider/ydb/idbactions.test.mjs index 41cf61c3..50d0ce0f 100644 --- a/provider/ydb/idbactions.test.js +++ b/provider/ydb/idbactions.test.mjs @@ -1,6 +1,6 @@ -import * as globals from '../../lib/globals.js' -import * as idbactions from './idbactions.js' -import * as test from '../../lib/testing.js' +import * as globals from '../../lib/globals.mjs' +import * as idbactions from './idbactions.mjs' +import * as test from '../../lib/testing.mjs' idbactions.deleteDB().then(() => idbactions.openDB()).then(db => { test.run('update lifetime 1', async (testname) => { diff --git a/provider/ydb/index.js b/provider/ydb/index.mjs similarity index 78% rename from provider/ydb/index.js rename to provider/ydb/index.mjs index 12cb6b17..d8361b70 100644 --- a/provider/ydb/index.js +++ b/provider/ydb/index.mjs @@ -2,7 +2,7 @@ * @module provider/ydb */ -import * as ydbclient from './YdbClient.js' +import * as ydbclient from './YdbClient.mjs' /** * @param {string} url diff --git a/provider/ydb/message.js b/provider/ydb/message.mjs similarity index 94% rename from provider/ydb/message.js rename to provider/ydb/message.mjs index 86af478b..8467924a 100644 --- a/provider/ydb/message.js +++ b/provider/ydb/message.mjs @@ -2,11 +2,11 @@ * @module provider/ydb */ -import * as encoding from './encoding.js' -import * as decoding from './decoding.js' -import * as idbactions from './idbactions.js' -import * as logging from './logging.js' -import * as bc from './broadcastchannel.js' +import * as encoding from './encoding.mjs' +import * as decoding from './decoding.mjs' +import * as idbactions from './idbactions.mjs' +import * as logging from './logging.mjs' +import * as bc from './broadcastchannel.mjs' /* make sure to update message.go in ydb when updating these values.. */ export const MESSAGE_UPDATE = 0 // TODO: rename host_unconfirmed? diff --git a/rollup.config.js b/rollup.config.js index cc132536..14b14d12 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -4,17 +4,17 @@ import babel from 'rollup-plugin-babel' import uglify from 'rollup-plugin-uglify-es' export default [{ - input: './index.js', - output: { + input: './index.mjs', + output: [{ name: 'Y', - file: 'build/yjs.umd.js', - format: 'umd', + file: 'build/yjs.js', + format: 'cjs', sourcemap: true - } + }] }, { - input: 'tests/index.js', + input: 'tests/index.mjs', output: { - file: 'build/y.test.js', + file: 'build/y.test.mjs', format: 'iife', name: 'ytests', sourcemap: true @@ -27,7 +27,7 @@ export default [{ commonjs() ] }, { - input: './examples/prosemirror.js', + input: './examples/prosemirror.mjs', output: { name: 'prosemirror', file: 'examples/build/prosemirror.js', @@ -44,7 +44,7 @@ export default [{ uglify() ] }, { - input: './examples/textarea.js', + input: './examples/textarea.mjs', output: { name: 'textarea', file: 'examples/build/textarea.js', @@ -52,16 +52,10 @@ export default [{ sourcemap: true }, plugins: [ - nodeResolve({ - sourcemap: true, - module: true - }), - commonjs(), - babel(), uglify() ] }, { - input: './examples/quill.js', + input: './examples/quill.mjs', output: { name: 'textarea', file: 'examples/build/quill.js', diff --git a/structs/Delete.js b/structs/Delete.mjs similarity index 64% rename from structs/Delete.js rename to structs/Delete.mjs index d0bc3aca..bf93c3c8 100644 --- a/structs/Delete.js +++ b/structs/Delete.mjs @@ -2,47 +2,15 @@ * @module structs */ -import { getStructReference } from '../utils/structReferences.js' -import * as ID from '../utils/ID.js' -import { stringifyID } from '../protocols/syncProtocol.js' -import { writeStructToTransaction } from '../utils/Transaction.js' -import * as decoding from '../lib/decoding.js' -import * as encoding from '../lib/encoding.js' -import { Item } from './Item.js' // eslint-disable-line -import { Y } from '../utils/Y.js' // eslint-disable-line - -/** - * @private - * Delete all items in an ID-range. - * Does not create delete operations! - * TODO: implement getItemCleanStartNode for better performance (only one lookup). - */ -export const deleteItemRange = (y, user, clock, range, gcChildren) => { - let item = y.os.getItemCleanStart(ID.createID(user, clock)) - if (item !== null) { - if (!item._deleted) { - item._splitAt(y, range) - item._delete(y, false, true) - } - let itemLen = item._length - range -= itemLen - clock += itemLen - if (range > 0) { - let node = y.os.findNode(ID.createID(user, clock)) - while (node !== null && node.val !== null && range > 0 && node.val._id.equals(ID.createID(user, clock))) { - const nodeVal = node.val - if (!nodeVal._deleted) { - nodeVal._splitAt(y, range) - nodeVal._delete(y, false, gcChildren) - } - const nodeLen = nodeVal._length - range -= nodeLen - clock += nodeLen - node = node.next() - } - } - } -} +import { getStructReference } from '../utils/structReferences.mjs' +import * as ID from '../utils/ID.mjs' +import { stringifyID } from '../protocols/syncProtocol.mjs' +import { writeStructToTransaction } from '../utils/Transaction.mjs' +import * as decoding from '../lib/decoding.mjs' +import * as encoding from '../lib/encoding.mjs' +import { Item } from './Item.mjs' // eslint-disable-line +import { Y } from '../utils/Y.mjs' // eslint-disable-line +import { deleteItemRange } from '../utils/structManipulation.mjs' /** * @private diff --git a/structs/GC.js b/structs/GC.mjs similarity index 88% rename from structs/GC.js rename to structs/GC.mjs index 55c94db7..1d0f8797 100644 --- a/structs/GC.js +++ b/structs/GC.mjs @@ -2,12 +2,12 @@ * @module structs */ -import { getStructReference } from '../utils/structReferences.js' -import * as ID from '../utils/ID.js' -import { writeStructToTransaction } from '../utils/Transaction.js' -import * as decoding from '../lib/decoding.js' -import * as encoding from '../lib/encoding.js' -import { Y } from '../utils/Y.js' // eslint-disable-line +import { getStructReference } from '../utils/structReferences.mjs' +import * as ID from '../utils/ID.mjs' +import { writeStructToTransaction } from '../utils/Transaction.mjs' +import * as decoding from '../lib/decoding.mjs' +import * as encoding from '../lib/encoding.mjs' +import { Y } from '../utils/Y.mjs' // eslint-disable-line // TODO should have the same base class as Item export class GC { diff --git a/structs/Item.js b/structs/Item.mjs similarity index 97% rename from structs/Item.js rename to structs/Item.mjs index 7ea72def..d50bccc4 100644 --- a/structs/Item.js +++ b/structs/Item.mjs @@ -2,15 +2,15 @@ * @module structs */ -import { getStructReference } from '../utils/structReferences.js' -import * as ID from '../utils/ID.js' -import { Delete } from './Delete.js' -import { transactionTypeChanged, writeStructToTransaction } from '../utils/Transaction.js' -import { GC } from './GC.js' -import * as encoding from '../lib/encoding.js' -import * as decoding from '../lib/decoding.js' -import { Y } from '../utils/Y.js' -import { Type } from './Type.js' // eslint-disable-line +import { getStructReference } from '../utils/structReferences.mjs' +import * as ID from '../utils/ID.mjs' +import { Delete } from './Delete.mjs' +import { transactionTypeChanged, writeStructToTransaction } from '../utils/Transaction.mjs' +import { GC } from './GC.mjs' +import * as encoding from '../lib/encoding.mjs' +import * as decoding from '../lib/decoding.mjs' +import { Y } from '../utils/Y.mjs' +import { Type } from './Type.mjs' // eslint-disable-line /** * @private diff --git a/structs/ItemEmbed.js b/structs/ItemEmbed.mjs similarity index 79% rename from structs/ItemEmbed.js rename to structs/ItemEmbed.mjs index 52974acd..04dd9290 100644 --- a/structs/ItemEmbed.js +++ b/structs/ItemEmbed.mjs @@ -2,11 +2,11 @@ * @module structs */ -import { Item } from './Item.js' -import { logItemHelper } from '../protocols/syncProtocol.js' -import * as encoding from '../lib/encoding.js' -import * as decoding from '../lib/decoding.js' -import { Y } from '../utils/Y.js' // eslint-disable-line +import { Item } from './Item.mjs' +import { logItemHelper } from '../protocols/syncProtocol.mjs' +import * as encoding from '../lib/encoding.mjs' +import * as decoding from '../lib/decoding.mjs' +import { Y } from '../utils/Y.mjs' // eslint-disable-line export class ItemEmbed extends Item { constructor () { diff --git a/structs/ItemFormat.js b/structs/ItemFormat.mjs similarity index 82% rename from structs/ItemFormat.js rename to structs/ItemFormat.mjs index f002c7bb..074bdd2a 100644 --- a/structs/ItemFormat.js +++ b/structs/ItemFormat.mjs @@ -2,11 +2,11 @@ * @module structs */ -import { Item } from './Item.js' -import { logItemHelper } from '../protocols/syncProtocol.js' -import * as encoding from '../lib/encoding.js' -import * as decoding from '../lib/decoding.js' -import { Y } from '../utils/Y.js' // eslint-disable-line +import { Item } from './Item.mjs' +import { logItemHelper } from '../protocols/syncProtocol.mjs' +import * as encoding from '../lib/encoding.mjs' +import * as decoding from '../lib/decoding.mjs' +import { Y } from '../utils/Y.mjs' // eslint-disable-line export class ItemFormat extends Item { constructor () { diff --git a/structs/ItemJSON.js b/structs/ItemJSON.mjs similarity index 87% rename from structs/ItemJSON.js rename to structs/ItemJSON.mjs index 1d5036a8..78062273 100644 --- a/structs/ItemJSON.js +++ b/structs/ItemJSON.mjs @@ -2,11 +2,11 @@ * @module structs */ -import { Item, splitHelper } from './Item.js' -import { logItemHelper } from '../protocols/syncProtocol.js' -import * as encoding from '../lib/encoding.js' -import * as decoding from '../lib/decoding.js' -import { Y } from '../utils/Y.js' // eslint-disable-line +import { Item, splitHelper } from './Item.mjs' +import { logItemHelper } from '../protocols/syncProtocol.mjs' +import * as encoding from '../lib/encoding.mjs' +import * as decoding from '../lib/decoding.mjs' +import { Y } from '../utils/Y.mjs' // eslint-disable-line export class ItemJSON extends Item { constructor () { diff --git a/structs/ItemString.js b/structs/ItemString.mjs similarity index 82% rename from structs/ItemString.js rename to structs/ItemString.mjs index ed6a0b1f..43fa558c 100644 --- a/structs/ItemString.js +++ b/structs/ItemString.mjs @@ -2,11 +2,11 @@ * @module structs */ -import { Item, splitHelper } from './Item.js' -import { logItemHelper } from '../protocols/syncProtocol.js' -import * as encoding from '../lib/encoding.js' -import * as decoding from '../lib/decoding.js' -import { Y } from '../utils/Y.js' // eslint-disable-line +import { Item, splitHelper } from './Item.mjs' +import { logItemHelper } from '../protocols/syncProtocol.mjs' +import * as encoding from '../lib/encoding.mjs' +import * as decoding from '../lib/decoding.mjs' +import { Y } from '../utils/Y.mjs' // eslint-disable-line export class ItemString extends Item { constructor () { diff --git a/structs/Type.js b/structs/Type.mjs similarity index 96% rename from structs/Type.js rename to structs/Type.mjs index 7e8fe15d..fff8d407 100644 --- a/structs/Type.js +++ b/structs/Type.mjs @@ -2,11 +2,11 @@ * @module structs */ -import { Item } from './Item.js' -import { EventHandler } from '../utils/EventHandler.js' -import { createID } from '../utils/ID.js' -import { YEvent } from '../utils/YEvent.js' -import { Y } from '../utils/Y.js' // eslint-disable-line +import { Item } from './Item.mjs' +import { EventHandler } from '../utils/EventHandler.mjs' +import { createID } from '../utils/ID.mjs' +import { YEvent } from '../utils/YEvent.mjs' +import { Y } from '../utils/Y.mjs' // eslint-disable-line // restructure children as if they were inserted one after another const integrateChildren = (y, start) => { diff --git a/tests/DeleteStore.tests.js b/tests/DeleteStore.tests.mjs similarity index 94% rename from tests/DeleteStore.tests.js rename to tests/DeleteStore.tests.mjs index 2317587e..406c469f 100644 --- a/tests/DeleteStore.tests.js +++ b/tests/DeleteStore.tests.mjs @@ -1,7 +1,7 @@ import { test } from 'cutest' -import * as random from '../lib/prng/prng.js' -import { DeleteStore } from '../utils/DeleteStore.js' -import * as ID from '../utils/ID.js' +import * as random from '../lib/prng/prng.mjs' +import { DeleteStore } from '../utils/DeleteStore.mjs' +import * as ID from '../utils/ID.mjs' /** * Converts a DS to an array of length 10. diff --git a/tests/diff.tests.js b/tests/diff.tests.mjs similarity index 92% rename from tests/diff.tests.js rename to tests/diff.tests.mjs index f8e91f6b..60a0067c 100644 --- a/tests/diff.tests.js +++ b/tests/diff.tests.mjs @@ -1,6 +1,6 @@ import { test } from 'cutest' -import { simpleDiff } from '../lib/diff.js' -import * as random from '../lib/prng/prng.js' +import { simpleDiff } from '../lib/diff.mjs' +import * as random from '../lib/prng/prng.mjs' function runDiffTest (t, a, b, expected) { let result = simpleDiff(a, b) diff --git a/tests/encode-decode.tests.js b/tests/encode-decode.tests.mjs similarity index 94% rename from tests/encode-decode.tests.js rename to tests/encode-decode.tests.mjs index e6fc7b17..740472ff 100644 --- a/tests/encode-decode.tests.js +++ b/tests/encode-decode.tests.mjs @@ -1,8 +1,8 @@ import { test } from 'cutest' -import { generateRandomUint32 } from '../utils/generateRandomUint32.js' -import * as encoding from '../lib/encoding.js' -import * as decoding from '../lib/decoding.js' -import * as random from '../lib/prng/prng.js' +import { generateRandomUint32 } from '../utils/generateRandomUint32.mjs' +import * as encoding from '../lib/encoding.mjs' +import * as decoding from '../lib/decoding.mjs' +import * as random from '../lib/prng/prng.mjs' function testEncoding (t, write, read, val) { let encoder = encoding.createEncoder() diff --git a/tests/helper.js b/tests/helper.mjs similarity index 95% rename from tests/helper.js rename to tests/helper.mjs index 6e3d35ed..a8a18073 100644 --- a/tests/helper.js +++ b/tests/helper.mjs @@ -1,19 +1,19 @@ -import * as Y from '../index.js' -import { ItemJSON } from '../structs/ItemJSON.js' -import { ItemString } from '../structs/ItemString.js' -import { defragmentItemContent } from '../utils/defragmentItemContent.js' +import * as Y from '../index.mjs' +import { ItemJSON } from '../structs/ItemJSON.mjs' +import { ItemString } from '../structs/ItemString.mjs' +import { defragmentItemContent } from '../utils/defragmentItemContent.mjs' import Quill from 'quill' -import { GC } from '../structs/GC.js' -import * as random from '../lib/prng/prng.js' -import * as syncProtocol from '../protocols/syncProtocol.js' -import * as encoding from '../lib/encoding.js' -import * as decoding from '../lib/decoding.js' -import { createMutex } from '../lib/mutex.js' -import { QuillBinding } from '../bindings/quill.js' -import { DomBinding } from '../bindings/dom/DomBinding.js' +import { GC } from '../structs/GC.mjs' +import * as random from '../lib/prng/prng.mjs' +import * as syncProtocol from '../protocols/syncProtocol.mjs' +import * as encoding from '../lib/encoding.mjs' +import * as decoding from '../lib/decoding.mjs' +import { createMutex } from '../lib/mutex.mjs' +import { QuillBinding } from '../bindings/quill.mjs' +import { DomBinding } from '../bindings/dom/DomBinding.mjs' -export * from '../index.js' +export * from '../index.mjs' /** * @param {TestYInstance} y diff --git a/tests/index.js b/tests/index.js deleted file mode 100644 index 8f30d9ae..00000000 --- a/tests/index.js +++ /dev/null @@ -1,9 +0,0 @@ -// TODO: include all tests -import './red-black-tree.js' -import './y-array.tests.js' -import './y-text.tests.js' -import './y-map.tests.js' -import './y-xml.tests.js' -import './encode-decode.tests.js' -import './diff.tests.js' -import './prosemirror.test.js' diff --git a/tests/index.mjs b/tests/index.mjs new file mode 100644 index 00000000..a9058701 --- /dev/null +++ b/tests/index.mjs @@ -0,0 +1,9 @@ +// TODO: include all tests +import './red-black-tree.mjs' +import './y-array.tests.mjs' +import './y-text.tests.mjs' +import './y-map.tests.mjs' +import './y-xml.tests.mjs' +import './encode-decode.tests.mjs' +import './diff.tests.mjs' +import './prosemirror.test.mjs' diff --git a/tests/prosemirror.test.js b/tests/prosemirror.test.mjs similarity index 88% rename from tests/prosemirror.test.js rename to tests/prosemirror.test.mjs index 7a005207..f24a2b59 100644 --- a/tests/prosemirror.test.js +++ b/tests/prosemirror.test.mjs @@ -1,8 +1,8 @@ import { test } from 'cutest' -import * as random from '../lib/prng/prng.js' -import * as Y from '../index.js' +import * as random from '../lib/prng/prng.mjs' +import * as Y from '../index.mjs' -import { prosemirrorPlugin } from '../bindings/prosemirror.js' +import { prosemirrorPlugin } from '../bindings/prosemirror.mjs' import {EditorState} from 'prosemirror-state' import {EditorView} from 'prosemirror-view' import {schema} from 'prosemirror-schema-basic' diff --git a/tests/red-black-tree.js b/tests/red-black-tree.mjs similarity index 97% rename from tests/red-black-tree.js rename to tests/red-black-tree.mjs index 857550f0..e46c7694 100644 --- a/tests/red-black-tree.js +++ b/tests/red-black-tree.mjs @@ -1,7 +1,7 @@ -import { Tree as RedBlackTree } from '../lib/Tree.js' -import * as ID from '../utils/ID.js' +import { Tree as RedBlackTree } from '../lib/Tree.mjs' +import * as ID from '../utils/ID.mjs' import { test, proxyConsole } from 'cutest' -import * as random from '../lib/prng/prng.js' +import * as random from '../lib/prng/prng.mjs' proxyConsole() diff --git a/tests/y-array.tests.js b/tests/y-array.tests.mjs similarity index 99% rename from tests/y-array.tests.js rename to tests/y-array.tests.mjs index b59d96f8..199f2591 100644 --- a/tests/y-array.tests.js +++ b/tests/y-array.tests.mjs @@ -1,7 +1,7 @@ -import { initArrays, compareUsers, applyRandomTests } from './helper.js' -import * as Y from '../index.js' +import { initArrays, compareUsers, applyRandomTests } from './helper.mjs' +import * as Y from '../index.mjs' import { test, proxyConsole } from 'cutest' -import * as random from '../lib/prng/prng.js' +import * as random from '../lib/prng/prng.mjs' proxyConsole() test('basic spec', async function array0 (t) { diff --git a/tests/y-map.tests.js b/tests/y-map.tests.mjs similarity index 99% rename from tests/y-map.tests.js rename to tests/y-map.tests.mjs index 3e9fefb5..65230f96 100644 --- a/tests/y-map.tests.js +++ b/tests/y-map.tests.mjs @@ -1,7 +1,7 @@ -import { initArrays, compareUsers, applyRandomTests } from './helper.js' -import * as Y from '../index.js' +import { initArrays, compareUsers, applyRandomTests } from './helper.mjs' +import * as Y from '../index.mjs' import { test, proxyConsole } from 'cutest' -import * as random from '../lib/prng/prng.js' +import * as random from '../lib/prng/prng.mjs' proxyConsole() diff --git a/tests/y-text.tests.js b/tests/y-text.tests.mjs similarity index 98% rename from tests/y-text.tests.js rename to tests/y-text.tests.mjs index 118c8087..c1f8a8f7 100644 --- a/tests/y-text.tests.js +++ b/tests/y-text.tests.mjs @@ -1,4 +1,4 @@ -import { initArrays, compareUsers } from './helper.js' +import { initArrays, compareUsers } from './helper.mjs' import { test, proxyConsole } from 'cutest' proxyConsole() diff --git a/tests/y-xml.tests.js b/tests/y-xml.tests.mjs similarity index 99% rename from tests/y-xml.tests.js rename to tests/y-xml.tests.mjs index f963cd6a..4193bc5b 100644 --- a/tests/y-xml.tests.js +++ b/tests/y-xml.tests.mjs @@ -1,7 +1,7 @@ -import { initArrays, compareUsers, applyRandomTests } from './helper.js' +import { initArrays, compareUsers, applyRandomTests } from './helper.mjs' import { test } from 'cutest' -import * as Y from '../index.js' -import * as random from '../lib/prng/prng.js' +import * as Y from '../index.mjs' +import * as random from '../lib/prng/prng.mjs' test('set property', async function xml0 (t) { var { testConnector, users, xml0, xml1 } = await initArrays(t, { users: 2 }) diff --git a/types/YArray.js b/types/YArray.mjs similarity index 96% rename from types/YArray.js rename to types/YArray.mjs index 0a760981..3cfdae59 100644 --- a/types/YArray.js +++ b/types/YArray.mjs @@ -2,13 +2,13 @@ * @module types */ -import { Type } from '../structs/Type.js' -import { ItemJSON } from '../structs/ItemJSON.js' -import { ItemString } from '../structs/ItemString.js' -import { stringifyItemID, logItemHelper } from '../protocols/syncProtocol.js' -import { YEvent } from '../utils/YEvent.js' -import { Transaction } from '../utils/Transaction.js' // eslint-disable-line -import { Item } from '../structs/Item.js' // eslint-disable-line +import { Type } from '../structs/Type.mjs' +import { ItemJSON } from '../structs/ItemJSON.mjs' +import { ItemString } from '../structs/ItemString.mjs' +import { stringifyItemID, logItemHelper } from '../protocols/syncProtocol.mjs' +import { YEvent } from '../utils/YEvent.mjs' +import { Transaction } from '../utils/Transaction.mjs' // eslint-disable-line +import { Item } from '../structs/Item.mjs' // eslint-disable-line /** * Event that describes the changes on a YArray @@ -73,8 +73,9 @@ export class YArrayEvent extends YEvent { */ export class YArray extends Type { /** - * @private * Creates YArray Event and calls observers. + * + * @private */ _callObserver (transaction, parentSubs, remote) { this._callEventHandler(transaction, new YArrayEvent(this, remote, transaction)) @@ -243,9 +244,9 @@ export class YArray extends Type { } /** - * @private * Inserts content after an element container. * + * @private * @param {Item} left The element container to use as a reference. * @param {Array} content The Array of content to insert (see {@see insert}) */ diff --git a/types/YMap.js b/types/YMap.mjs similarity index 93% rename from types/YMap.js rename to types/YMap.mjs index 74a22568..1ef79f53 100644 --- a/types/YMap.js +++ b/types/YMap.mjs @@ -2,11 +2,11 @@ * @module types */ -import { Item } from '../structs/Item.js' -import { Type } from '../structs/Type.js' -import { ItemJSON } from '../structs/ItemJSON.js' -import { logItemHelper } from '../protocols/syncProtocol.js' -import { YEvent } from '../utils/YEvent.js' +import { Item } from '../structs/Item.mjs' +import { Type } from '../structs/Type.mjs' +import { ItemJSON } from '../structs/ItemJSON.mjs' +import { logItemHelper } from '../protocols/syncProtocol.mjs' +import { YEvent } from '../utils/YEvent.mjs' /** * Event that describes the changes on a YMap. @@ -29,8 +29,9 @@ export class YMapEvent extends YEvent { */ export class YMap extends Type { /** - * @private * Creates YMap Event and calls observers. + * + * @private */ _callObserver (transaction, parentSubs, remote) { this._callEventHandler(transaction, new YMapEvent(this, parentSubs, remote)) diff --git a/types/YText.js b/types/YText.mjs similarity index 98% rename from types/YText.js rename to types/YText.mjs index 9dbacd3c..7283e305 100644 --- a/types/YText.js +++ b/types/YText.mjs @@ -2,11 +2,11 @@ * @module types */ -import { ItemEmbed } from '../structs/ItemEmbed.js' -import { ItemString } from '../structs/ItemString.js' -import { ItemFormat } from '../structs/ItemFormat.js' -import { logItemHelper } from '../protocols/syncProtocol.js' -import { YArrayEvent, YArray } from './YArray.js' +import { ItemEmbed } from '../structs/ItemEmbed.mjs' +import { ItemString } from '../structs/ItemString.mjs' +import { ItemFormat } from '../structs/ItemFormat.mjs' +import { logItemHelper } from '../protocols/syncProtocol.mjs' +import { YArrayEvent, YArray } from './YArray.mjs' /** * @private @@ -464,7 +464,7 @@ class YTextEvent extends YArrayEvent { */ export class YText extends YArray { /** - * @param {String} string The initial value of the YText. + * @param {String} [string] The initial value of the YText. */ constructor (string) { super() @@ -477,8 +477,9 @@ export class YText extends YArray { } /** - * @private * Creates YMap Event and calls observers. + * + * @private */ _callObserver (transaction, parentSubs, remote) { this._callEventHandler(transaction, new YTextEvent(this, remote, transaction)) diff --git a/types/YXmlElement.js b/types/YXmlElement.mjs similarity index 94% rename from types/YXmlElement.js rename to types/YXmlElement.mjs index 75165d2e..4b6a8535 100644 --- a/types/YXmlElement.js +++ b/types/YXmlElement.mjs @@ -2,16 +2,16 @@ * @module types */ -import { YMap } from './YMap.js' -import { createAssociation } from '../bindings/dom/util.js' -import * as encoding from '../lib/encoding.js' -import * as decoding from '../lib/decoding.js' -import { Y } from '../utils/Y.js' // eslint-disable-line -import { DomBinding } from '../bindings/dom/DomBinding.js' // eslint-disable-line -import { YXmlTreeWalker } from './YXmlTreeWalker.js' -import { YArray } from './YArray.js' -import { YXmlEvent } from './YXmlEvent.js' -import { logItemHelper } from '../protocols/syncProtocol.js' +import { YMap } from './YMap.mjs' +import { createAssociation } from '../bindings/dom/util.mjs' +import * as encoding from '../lib/encoding.mjs' +import * as decoding from '../lib/decoding.mjs' +import { Y } from '../utils/Y.mjs' // eslint-disable-line +import { DomBinding } from '../bindings/dom/DomBinding.mjs' // eslint-disable-line +import { YXmlTreeWalker } from './YXmlTreeWalker.mjs' +import { YArray } from './YArray.mjs' +import { YXmlEvent } from './YXmlEvent.mjs' +import { logItemHelper } from '../protocols/syncProtocol.mjs' /** * Dom filter function. @@ -182,8 +182,9 @@ export class YXmlElement extends YXmlFragment { } /** - * @private * Creates an Item with the same effect as this Item (without position effect) + * + * @private */ _copy () { let struct = super._copy() @@ -192,11 +193,11 @@ export class YXmlElement extends YXmlFragment { } /** - * @private * Read the next Item in a Decoder and fill this Item with the read data. * * This is called when data is received from a remote peer. * + * @private * @param {Y} y The Yjs instance that this Item belongs to. * @param {decoding.Decoder} decoder The decoder object to read data from. */ @@ -212,9 +213,8 @@ export class YXmlElement extends YXmlFragment { * * This is called when this Item is sent to a remote peer. * - * @param {encoding.Encoder} encoder The encoder to write data to. - * * @private + * @param {encoding.Encoder} encoder The encoder to write data to. */ _toBinary (encoder) { super._toBinary(encoder) @@ -231,9 +231,8 @@ export class YXmlElement extends YXmlFragment { * * Checks for nodeName * * Sets domFilter * - * @param {Y} y The Yjs instance - * * @private + * @param {Y} y The Yjs instance */ _integrate (y) { if (this.nodeName === null) { diff --git a/types/YXmlEvent.js b/types/YXmlEvent.mjs similarity index 87% rename from types/YXmlEvent.js rename to types/YXmlEvent.mjs index e41d9f26..e8bb46b4 100644 --- a/types/YXmlEvent.js +++ b/types/YXmlEvent.mjs @@ -2,10 +2,10 @@ * @module types */ -import { YEvent } from '../utils/YEvent.js' +import { YEvent } from '../utils/YEvent.mjs' -import { Type } from '../structs/Type.js' // eslint-disable-line -import { Transaction } from '../utils/Transaction.js' // eslint-disable-line +import { Type } from '../structs/Type.mjs' // eslint-disable-line +import { Transaction } from '../utils/Transaction.mjs' // eslint-disable-line /** * An Event that describes changes on a YXml Element or Yxml Fragment diff --git a/types/YXmlHook.js b/types/YXmlHook.mjs similarity index 89% rename from types/YXmlHook.js rename to types/YXmlHook.mjs index ad45f1eb..5b259960 100644 --- a/types/YXmlHook.js +++ b/types/YXmlHook.mjs @@ -2,13 +2,13 @@ * @module types */ -import { YMap } from './YMap.js' -import { createAssociation } from '../bindings/dom/util.js' -import * as encoding from '../lib/encoding.js' -import * as decoding from '../lib/decoding.js' +import { YMap } from './YMap.mjs' +import { createAssociation } from '../bindings/dom/util.mjs' +import * as encoding from '../lib/encoding.mjs' +import * as decoding from '../lib/decoding.mjs' -import { DomBinding } from '../bindings/dom/DomBinding.js' // eslint-disable-line -import { Y } from '../utils/Y.js' // eslint-disable-line +import { DomBinding } from '../bindings/dom/DomBinding.mjs' // eslint-disable-line +import { Y } from '../utils/Y.mjs' // eslint-disable-line /** * You can manage binding to a custom type with YXmlHook. diff --git a/types/YXmlText.js b/types/YXmlText.mjs similarity index 87% rename from types/YXmlText.js rename to types/YXmlText.mjs index a4b5780e..efce5573 100644 --- a/types/YXmlText.js +++ b/types/YXmlText.mjs @@ -2,10 +2,10 @@ * @module types */ -import { YText } from './YText.js' -import { createAssociation } from '../bindings/dom/util.js' -import { Y } from '../index.js' // eslint-disable-line -import { DomBinding } from '../bindings/dom/DomBinding.js' // eslint-disable-line +import { YText } from './YText.mjs' +import { createAssociation } from '../bindings/dom/util.mjs' +import { Y } from '../utils/Y.mjs' // eslint-disable-line +import { DomBinding } from '../bindings/dom/DomBinding.mjs' // eslint-disable-line /** * Represents text in a Dom Element. In the future this type will also handle diff --git a/types/YXmlTreeWalker.js b/types/YXmlTreeWalker.mjs similarity index 95% rename from types/YXmlTreeWalker.js rename to types/YXmlTreeWalker.mjs index 605266c7..848fa18d 100644 --- a/types/YXmlTreeWalker.js +++ b/types/YXmlTreeWalker.mjs @@ -2,7 +2,7 @@ * @module types */ -import { YXmlElement, YXmlFragment } from './YXmlElement.js' // eslint-disable-line +import { YXmlElement, YXmlFragment } from './YXmlElement.mjs' // eslint-disable-line /** * Define the elements to which a set of CSS queries apply. diff --git a/utils/BindMapping.js b/utils/BindMapping.mjs similarity index 100% rename from utils/BindMapping.js rename to utils/BindMapping.mjs diff --git a/utils/DeleteStore.js b/utils/DeleteStore.mjs similarity index 97% rename from utils/DeleteStore.js rename to utils/DeleteStore.mjs index 00329b89..864b52b2 100644 --- a/utils/DeleteStore.js +++ b/utils/DeleteStore.mjs @@ -2,8 +2,8 @@ * @module utils */ -import { Tree } from '../lib/Tree.js' -import * as ID from './ID.js' +import { Tree } from '../lib/Tree.mjs' +import * as ID from './ID.mjs' class DSNode { constructor (id, len, gc) { diff --git a/utils/EventHandler.js b/utils/EventHandler.mjs similarity index 100% rename from utils/EventHandler.js rename to utils/EventHandler.mjs diff --git a/utils/ID.js b/utils/ID.mjs similarity index 93% rename from utils/ID.js rename to utils/ID.mjs index cd421309..1bc1a658 100644 --- a/utils/ID.js +++ b/utils/ID.mjs @@ -2,9 +2,9 @@ * @module utils */ -import { getStructReference } from './structReferences.js' -import * as decoding from '../lib/decoding.js' -import * as encoding from '../lib/encoding.js' +import { getStructReference } from './structReferences.mjs' +import * as decoding from '../lib/decoding.mjs' +import * as encoding from '../lib/encoding.mjs' export class ID { constructor (user, clock) { diff --git a/utils/OperationStore.js b/utils/OperationStore.mjs similarity index 93% rename from utils/OperationStore.js rename to utils/OperationStore.mjs index d473dd19..45b1e6e3 100644 --- a/utils/OperationStore.js +++ b/utils/OperationStore.mjs @@ -2,11 +2,11 @@ * @module utils */ -import { Tree } from '../lib/Tree.js' -import * as ID from '../utils/ID.js' -import { getStruct } from '../utils/structReferences.js' -import { stringifyID, stringifyItemID } from '../protocols/syncProtocol.js' -import { GC } from '../structs/GC.js' +import { Tree } from '../lib/Tree.mjs' +import * as ID from '../utils/ID.mjs' +import { getStruct } from '../utils/structReferences.mjs' +import { stringifyID, stringifyItemID } from '../protocols/syncProtocol.mjs' +import { GC } from '../structs/GC.mjs' export class OperationStore extends Tree { constructor (y) { diff --git a/utils/StateStore.js b/utils/StateStore.mjs similarity index 96% rename from utils/StateStore.js rename to utils/StateStore.mjs index f739b5f5..b6024483 100644 --- a/utils/StateStore.js +++ b/utils/StateStore.mjs @@ -2,7 +2,7 @@ * @module utils */ -import * as ID from '../utils/ID.js' +import * as ID from '../utils/ID.mjs' /** * @typedef {Map} StateSet diff --git a/utils/Transaction.js b/utils/Transaction.mjs similarity index 89% rename from utils/Transaction.js rename to utils/Transaction.mjs index ce34d5f0..67b2b793 100644 --- a/utils/Transaction.js +++ b/utils/Transaction.mjs @@ -2,11 +2,11 @@ * @module utils */ -import * as encoding from '../lib/encoding.js' -import { Y } from '../utils/Y.js' // eslint-disable-line -import { Type } from '../structs/Type.js' // eslint-disable-line -import { Item } from '../structs/Item.js' // eslint-disable-line -import { YEvent } from './YEvent.js' // eslint-disable-line +import * as encoding from '../lib/encoding.mjs' +import { Y } from '../utils/Y.mjs' // eslint-disable-line +import { Type } from '../structs/Type.mjs' // eslint-disable-line +import { Item } from '../structs/Item.mjs' // eslint-disable-line +import { YEvent } from './YEvent.mjs' // eslint-disable-line /** * A transaction is created for every change on the Yjs model. It is possible * to bundle changes on the Yjs model in a single transaction to diff --git a/utils/UndoManager.js b/utils/UndoManager.mjs similarity index 98% rename from utils/UndoManager.js rename to utils/UndoManager.mjs index 4eae011c..8a9c73e7 100644 --- a/utils/UndoManager.js +++ b/utils/UndoManager.mjs @@ -1,6 +1,6 @@ -import * as ID from './ID.js' -import { isParentOf } from './isParentOf.js' +import * as ID from './ID.mjs' +import { isParentOf } from './isParentOf.mjs' class ReverseOperation { constructor (y, transaction, bindingInfos) { diff --git a/utils/Y.js b/utils/Y.mjs similarity index 90% rename from utils/Y.js rename to utils/Y.mjs index 0208a0c8..7e6e96e9 100644 --- a/utils/Y.js +++ b/utils/Y.mjs @@ -1,15 +1,15 @@ -import { DeleteStore } from './DeleteStore.js' -import { OperationStore } from './OperationStore.js' -import { StateStore } from './StateStore.js' -import { generateRandomUint32 } from './generateRandomUint32.js' -import { createRootID } from './ID.js' -import { NamedEventHandler } from '../lib/NamedEventHandler.js' -import { Transaction } from './Transaction.js' -import * as encoding from '../lib/encoding.js' -import * as message from '../protocols/syncProtocol.js' -import { integrateRemoteStructs } from './integrateRemoteStructs.js' -import { Type } from '../structs/Type.js' // eslint-disable-line -import { Decoder } from '../lib/decoding.js' // eslint-disable-line +import { DeleteStore } from './DeleteStore.mjs' +import { OperationStore } from './OperationStore.mjs' +import { StateStore } from './StateStore.mjs' +import { generateRandomUint32 } from './generateRandomUint32.mjs' +import { createRootID } from './ID.mjs' +import { NamedEventHandler } from '../lib/NamedEventHandler.mjs' +import { Transaction } from './Transaction.mjs' +import * as encoding from '../lib/encoding.mjs' +import * as message from '../protocols/syncProtocol.mjs' +import { integrateRemoteStructs } from './integrateRemoteStructs.mjs' +import { Type } from '../structs/Type.mjs' // eslint-disable-line +import { Decoder } from '../lib/decoding.mjs' // eslint-disable-line /** * Anything that can be encoded with `JSON.stringify` and can be decoded with @@ -135,16 +135,18 @@ export class Y extends NamedEventHandler { } /** - * @private * Fake _start for root properties (y.set('name', type)) + * + * @private */ get _start () { return null } /** - * @private * Fake _start for root properties (y.set('name', type)) + * + * @private */ set _start (start) {} diff --git a/utils/YEvent.js b/utils/YEvent.mjs similarity index 92% rename from utils/YEvent.js rename to utils/YEvent.mjs index 78bccb26..dc8c65dd 100644 --- a/utils/YEvent.js +++ b/utils/YEvent.mjs @@ -2,7 +2,7 @@ * @module utils */ -import { Type } from '../structs/Type.js' // eslint-disable-line +import { Type } from '../structs/Type.mjs' // eslint-disable-line /** * YEvent describes the changes on a YType. diff --git a/utils/defragmentItemContent.js b/utils/defragmentItemContent.mjs similarity index 91% rename from utils/defragmentItemContent.js rename to utils/defragmentItemContent.mjs index 19c57b84..d4240fd7 100644 --- a/utils/defragmentItemContent.js +++ b/utils/defragmentItemContent.mjs @@ -3,9 +3,9 @@ * @module utils */ -import * as ID from '../utils/ID.js' -import { ItemJSON } from '../structs/ItemJSON.js' -import { ItemString } from '../structs/ItemString.js' +import * as ID from '../utils/ID.mjs' +import { ItemJSON } from '../structs/ItemJSON.mjs' +import { ItemString } from '../structs/ItemString.mjs' /** * Try to merge all items in os with their successors. diff --git a/utils/generateRandomUint32.js b/utils/generateRandomUint32.mjs similarity index 100% rename from utils/generateRandomUint32.js rename to utils/generateRandomUint32.mjs diff --git a/utils/integrateRemoteStructs.js b/utils/integrateRemoteStructs.mjs similarity index 93% rename from utils/integrateRemoteStructs.js rename to utils/integrateRemoteStructs.mjs index 265cad2f..77a0fb44 100644 --- a/utils/integrateRemoteStructs.js +++ b/utils/integrateRemoteStructs.mjs @@ -2,11 +2,11 @@ * @module utils */ -import { getStruct } from '../utils/structReferences.js' -import * as decoding from '../lib/decoding.js' -import { GC } from '../structs/GC.js' -import { Y } from '../utils/Y.js' // eslint-disable-line -import { Item } from '../structs/Item.js' // eslint-disable-line +import { getStruct } from '../utils/structReferences.mjs' +import * as decoding from '../lib/decoding.mjs' +import { GC } from '../structs/GC.mjs' +import { Y } from '../utils/Y.mjs' // eslint-disable-line +import { Item } from '../structs/Item.mjs' // eslint-disable-line class MissingEntry { constructor (decoder, missing, struct) { diff --git a/utils/isParentOf.js b/utils/isParentOf.mjs similarity index 76% rename from utils/isParentOf.js rename to utils/isParentOf.mjs index d4833209..1357d66d 100644 --- a/utils/isParentOf.js +++ b/utils/isParentOf.mjs @@ -2,8 +2,8 @@ * @module utils */ -import { Y } from '../utils/Y.js' // eslint-disable-line -import { Type } from '../structs/Type.js' // eslint-disable-line +import { Y } from '../utils/Y.mjs' // eslint-disable-line +import { Type } from '../structs/Type.mjs' // eslint-disable-line /** * Check if `parent` is a parent of `child`. diff --git a/utils/relativePosition.js b/utils/relativePosition.mjs similarity index 97% rename from utils/relativePosition.js rename to utils/relativePosition.mjs index 8f15b2e1..51d2ccbd 100644 --- a/utils/relativePosition.js +++ b/utils/relativePosition.mjs @@ -2,8 +2,8 @@ * @module utils */ -import * as ID from './ID.js' -import { GC } from '../structs/GC.js' +import * as ID from './ID.mjs' +import { GC } from '../structs/GC.mjs' // TODO: Implement function to describe ranges diff --git a/utils/structManipulation.mjs b/utils/structManipulation.mjs new file mode 100644 index 00000000..bff1e477 --- /dev/null +++ b/utils/structManipulation.mjs @@ -0,0 +1,35 @@ + +import * as ID from '../utils/ID.mjs' + +/** + * @private + * Delete all items in an ID-range. + * Does not create delete operations! + * TODO: implement getItemCleanStartNode for better performance (only one lookup). + */ +export const deleteItemRange = (y, user, clock, range, gcChildren) => { + let item = y.os.getItemCleanStart(ID.createID(user, clock)) + if (item !== null) { + if (!item._deleted) { + item._splitAt(y, range) + item._delete(y, false, true) + } + let itemLen = item._length + range -= itemLen + clock += itemLen + if (range > 0) { + let node = y.os.findNode(ID.createID(user, clock)) + while (node !== null && node.val !== null && range > 0 && node.val._id.equals(ID.createID(user, clock))) { + const nodeVal = node.val + if (!nodeVal._deleted) { + nodeVal._splitAt(y, range) + nodeVal._delete(y, false, gcChildren) + } + const nodeLen = nodeVal._length + range -= nodeLen + clock += nodeLen + node = node.next() + } + } + } +} diff --git a/utils/structReferences.js b/utils/structReferences.mjs similarity index 100% rename from utils/structReferences.js rename to utils/structReferences.mjs