refactor and remove dependency circles
This commit is contained in:
parent
4c1798e5fa
commit
585265e9a5
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,4 +1,4 @@
|
|||||||
node_modules
|
node_modules
|
||||||
dist
|
dist
|
||||||
.vscode
|
.vscode
|
||||||
./docs
|
docs
|
||||||
|
@ -15,8 +15,8 @@ const customModules = new Set([
|
|||||||
* @type {Set<any>}
|
* @type {Set<any>}
|
||||||
*/
|
*/
|
||||||
const customLibModules = new Set([
|
const customLibModules = new Set([
|
||||||
// 'funlib',
|
'lib0',
|
||||||
// 'y-protocols'
|
'y-protocols'
|
||||||
])
|
])
|
||||||
const debugResolve = {
|
const debugResolve = {
|
||||||
resolveId (importee) {
|
resolveId (importee) {
|
||||||
|
@ -26,3 +26,7 @@ export { DeleteStore, DSNode } from './utils/DeleteStore.js'
|
|||||||
export { deleteItemRange } from './utils/structManipulation.js'
|
export { deleteItemRange } from './utils/structManipulation.js'
|
||||||
export { integrateRemoteStruct, integrateRemoteStructs } from './utils/integrateRemoteStructs.js'
|
export { integrateRemoteStruct, integrateRemoteStructs } from './utils/integrateRemoteStructs.js'
|
||||||
export { isParentOf } from './utils/isParentOf.js'
|
export { isParentOf } from './utils/isParentOf.js'
|
||||||
|
|
||||||
|
export { stringifyItemID } from './utils/structStringify.js'
|
||||||
|
|
||||||
|
export * from './utils/structReferences.js'
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
* @module structs
|
* @module structs
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { getStructReference } from 'y-protocols/sync.js'
|
import { getStructReference } from '../utils/structReferences.js'
|
||||||
import * as ID from '../utils/ID.js'
|
import * as ID from '../utils/ID.js'
|
||||||
import { writeStructToTransaction } from '../utils/structEncoding.js'
|
import { writeStructToTransaction } from '../utils/structEncoding.js'
|
||||||
import * as decoding from 'lib0/decoding.js'
|
import * as decoding from 'lib0/decoding.js'
|
||||||
@ -10,7 +10,7 @@ import * as encoding from 'lib0/encoding.js'
|
|||||||
// import { Item } from './Item.js' // eslint-disable-line
|
// import { Item } from './Item.js' // eslint-disable-line
|
||||||
// import { Y } from '../utils/Y.js' // eslint-disable-line
|
// import { Y } from '../utils/Y.js' // eslint-disable-line
|
||||||
import { deleteItemRange } from '../utils/structManipulation.js'
|
import { deleteItemRange } from '../utils/structManipulation.js'
|
||||||
import * as stringify from 'y-protocols/utils/structStringify.js'
|
import * as stringify from '../utils/structStringify.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
* @module structs
|
* @module structs
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { getStructReference } from 'y-protocols/sync.js'
|
import { getStructReference } from '../utils/structReferences.js'
|
||||||
import * as ID from '../utils/ID.js'
|
import * as ID from '../utils/ID.js'
|
||||||
import { writeStructToTransaction } from '../utils/structEncoding.js'
|
import { writeStructToTransaction } from '../utils/structEncoding.js'
|
||||||
import * as decoding from 'lib0/decoding.js'
|
import * as decoding from 'lib0/decoding.js'
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
* @module structs
|
* @module structs
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { getStructReference } from 'y-protocols/sync.js'
|
import { getStructReference } from '../utils/structReferences.js'
|
||||||
import * as ID from '../utils/ID.js'
|
import * as ID from '../utils/ID.js'
|
||||||
import { Delete } from './Delete.js'
|
import { Delete } from './Delete.js'
|
||||||
import { writeStructToTransaction } from '../utils/structEncoding.js'
|
import { writeStructToTransaction } from '../utils/structEncoding.js'
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
// TODO: ItemBinary should be able to merge with right (similar to other items). Or the other items (ItemJSON) should not be able to merge - extra byte + consistency
|
// TODO: ItemBinary should be able to merge with right (similar to other items). Or the other items (ItemJSON) should not be able to merge - extra byte + consistency
|
||||||
|
|
||||||
import { Item } from './Item.js'
|
import { Item } from './Item.js'
|
||||||
import * as stringify from 'y-protocols/utils/structStringify.js'
|
import * as stringify from '../utils/structStringify.js'
|
||||||
import * as encoding from 'lib0/encoding.js'
|
import * as encoding from 'lib0/encoding.js'
|
||||||
import * as decoding from 'lib0/decoding.js'
|
import * as decoding from 'lib0/decoding.js'
|
||||||
import { Y } from '../utils/Y.js' // eslint-disable-line
|
import { Y } from '../utils/Y.js' // eslint-disable-line
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Item } from './Item.js'
|
import { Item } from './Item.js'
|
||||||
import * as stringify from 'y-protocols/utils/structStringify.js'
|
import * as stringify from '../utils/structStringify.js'
|
||||||
import * as encoding from 'lib0/encoding.js'
|
import * as encoding from 'lib0/encoding.js'
|
||||||
import * as decoding from 'lib0/decoding.js'
|
import * as decoding from 'lib0/decoding.js'
|
||||||
import { Y } from '../utils/Y.js' // eslint-disable-line
|
import { Y } from '../utils/Y.js' // eslint-disable-line
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Item } from './Item.js'
|
import { Item } from './Item.js'
|
||||||
import * as stringify from 'y-protocols/utils/structStringify.js'
|
import * as stringify from '../utils/structStringify.js'
|
||||||
import * as encoding from 'lib0/encoding.js'
|
import * as encoding from 'lib0/encoding.js'
|
||||||
import * as decoding from 'lib0/decoding.js'
|
import * as decoding from 'lib0/decoding.js'
|
||||||
import { Y } from '../utils/Y.js' // eslint-disable-line
|
import { Y } from '../utils/Y.js' // eslint-disable-line
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Item, splitHelper } from './Item.js'
|
import { Item, splitHelper } from './Item.js'
|
||||||
import * as stringify from 'y-protocols/utils/structStringify.js'
|
import * as stringify from '../utils/structStringify.js'
|
||||||
import * as encoding from 'lib0/encoding.js'
|
import * as encoding from 'lib0/encoding.js'
|
||||||
import * as decoding from 'lib0/decoding.js'
|
import * as decoding from 'lib0/decoding.js'
|
||||||
import { Y } from '../utils/Y.js' // eslint-disable-line
|
import { Y } from '../utils/Y.js' // eslint-disable-line
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Item, splitHelper } from './Item.js'
|
import { Item, splitHelper } from './Item.js'
|
||||||
import * as stringify from 'y-protocols/utils/structStringify.js'
|
import * as stringify from '../utils/structStringify.js'
|
||||||
import * as encoding from 'lib0/encoding.js'
|
import * as encoding from 'lib0/encoding.js'
|
||||||
import * as decoding from 'lib0/decoding.js'
|
import * as decoding from 'lib0/decoding.js'
|
||||||
import { Y } from '../utils/Y.js' // eslint-disable-line
|
import { Y } from '../utils/Y.js' // eslint-disable-line
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
import { Type } from '../structs/Type.js'
|
import { Type } from '../structs/Type.js'
|
||||||
import { ItemJSON } from '../structs/ItemJSON.js'
|
import { ItemJSON } from '../structs/ItemJSON.js'
|
||||||
import { ItemString } from '../structs/ItemString.js'
|
import { ItemString } from '../structs/ItemString.js'
|
||||||
import * as stringify from 'y-protocols/utils/structStringify.js'
|
import * as stringify from '../utils/structStringify.js'
|
||||||
import { YEvent } from '../utils/YEvent.js'
|
import { YEvent } from '../utils/YEvent.js'
|
||||||
import { Transaction } from '../utils/Transaction.js' // eslint-disable-line
|
import { Transaction } from '../utils/Transaction.js' // eslint-disable-line
|
||||||
import { Item } from '../structs/Item.js' // eslint-disable-line
|
import { Item } from '../structs/Item.js' // eslint-disable-line
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
import { Item } from '../structs/Item.js'
|
import { Item } from '../structs/Item.js'
|
||||||
import { Type } from '../structs/Type.js'
|
import { Type } from '../structs/Type.js'
|
||||||
import { ItemJSON } from '../structs/ItemJSON.js'
|
import { ItemJSON } from '../structs/ItemJSON.js'
|
||||||
import * as stringify from 'y-protocols/utils/structStringify.js'
|
import * as stringify from '../utils/structStringify.js'
|
||||||
import { YEvent } from '../utils/YEvent.js'
|
import { YEvent } from '../utils/YEvent.js'
|
||||||
import { ItemBinary } from '../structs/ItemBinary.js'
|
import { ItemBinary } from '../structs/ItemBinary.js'
|
||||||
import { isVisible } from '../utils/snapshot.js'
|
import { isVisible } from '../utils/snapshot.js'
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
import { ItemEmbed } from '../structs/ItemEmbed.js'
|
import { ItemEmbed } from '../structs/ItemEmbed.js'
|
||||||
import { ItemString } from '../structs/ItemString.js'
|
import { ItemString } from '../structs/ItemString.js'
|
||||||
import { ItemFormat } from '../structs/ItemFormat.js'
|
import { ItemFormat } from '../structs/ItemFormat.js'
|
||||||
import * as stringify from 'y-protocols/utils/structStringify.js'
|
import * as stringify from '../utils/structStringify.js'
|
||||||
import { YArrayEvent, YArray } from './YArray.js'
|
import { YArrayEvent, YArray } from './YArray.js'
|
||||||
import { isVisible } from '../utils/snapshot.js'
|
import { isVisible } from '../utils/snapshot.js'
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ import * as decoding from 'lib0/decoding.js'
|
|||||||
import { Y } from '../utils/Y.js' // eslint-disable-line
|
import { Y } from '../utils/Y.js' // eslint-disable-line
|
||||||
import { YArray } from './YArray.js'
|
import { YArray } from './YArray.js'
|
||||||
import { YXmlEvent } from './YXmlEvent.js'
|
import { YXmlEvent } from './YXmlEvent.js'
|
||||||
import * as stringify from 'y-protocols/utils/structStringify.js'
|
import * as stringify from '../utils/structStringify.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define the elements to which a set of CSS queries apply.
|
* Define the elements to which a set of CSS queries apply.
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
* @module utils
|
* @module utils
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { getStructReference } from 'y-protocols/sync.js'
|
import { getStructReference } from '../utils/structReferences.js'
|
||||||
import * as decoding from 'lib0/decoding.js'
|
import * as decoding from 'lib0/decoding.js'
|
||||||
import * as encoding from 'lib0/encoding.js'
|
import * as encoding from 'lib0/encoding.js'
|
||||||
|
|
||||||
@ -38,10 +38,14 @@ export const createID = (user, clock) => new ID(user, clock)
|
|||||||
export const RootFakeUserID = 0xFFFFFF
|
export const RootFakeUserID = 0xFFFFFF
|
||||||
|
|
||||||
export class RootID {
|
export class RootID {
|
||||||
|
/**
|
||||||
|
* @param {string} name
|
||||||
|
* @param {Function?} typeConstructor
|
||||||
|
*/
|
||||||
constructor (name, typeConstructor) {
|
constructor (name, typeConstructor) {
|
||||||
this.user = RootFakeUserID
|
this.user = RootFakeUserID
|
||||||
this.name = name
|
this.name = name
|
||||||
this.type = getStructReference(typeConstructor)
|
this.type = typeConstructor === null ? null : getStructReference(typeConstructor)
|
||||||
}
|
}
|
||||||
equals (id) {
|
equals (id) {
|
||||||
return id !== null && id.user === this.user && id.name === this.name && id.type === this.type
|
return id !== null && id.user === this.user && id.name === this.name && id.type === this.type
|
||||||
@ -70,7 +74,7 @@ export class RootID {
|
|||||||
* y.define('name', Y.Array) // name, and typeConstructor
|
* y.define('name', Y.Array) // name, and typeConstructor
|
||||||
*
|
*
|
||||||
* @param {string} name
|
* @param {string} name
|
||||||
* @param {Function} typeConstructor must be defined in structReferences
|
* @param {Function?} typeConstructor must be defined in structReferences
|
||||||
*/
|
*/
|
||||||
export const createRootID = (name, typeConstructor) => new RootID(name, typeConstructor)
|
export const createRootID = (name, typeConstructor) => new RootID(name, typeConstructor)
|
||||||
|
|
||||||
|
@ -4,9 +4,9 @@
|
|||||||
|
|
||||||
import { Tree } from 'lib0/tree.js'
|
import { Tree } from 'lib0/tree.js'
|
||||||
import * as ID from '../utils/ID.js'
|
import * as ID from '../utils/ID.js'
|
||||||
import { getStruct } from 'y-protocols/sync.js'
|
import { getStruct } from './structReferences.js'
|
||||||
import { GC } from '../structs/GC.js'
|
import { GC } from '../structs/GC.js'
|
||||||
import * as stringify from 'y-protocols/utils/structStringify.js'
|
import * as stringify from './structStringify.js'
|
||||||
|
|
||||||
export class OperationStore extends Tree {
|
export class OperationStore extends Tree {
|
||||||
constructor (y) {
|
constructor (y) {
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
* @module utils
|
* @module utils
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { getStruct } from 'y-protocols/sync.js'
|
import { getStruct } from '../utils/structReferences.js'
|
||||||
import * as decoding from 'lib0/decoding.js'
|
import * as decoding from 'lib0/decoding.js'
|
||||||
import { GC } from '../structs/GC.js'
|
import { GC } from '../structs/GC.js'
|
||||||
import { Y } from '../utils/Y.js' // eslint-disable-line
|
import { Y } from '../utils/Y.js' // eslint-disable-line
|
||||||
|
35
src/utils/structReferences.js
Normal file
35
src/utils/structReferences.js
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
|
||||||
|
/**
|
||||||
|
* @module utils
|
||||||
|
*/
|
||||||
|
|
||||||
|
const structs = new Map()
|
||||||
|
const references = new Map()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a new Yjs types. The same type must be defined with the same
|
||||||
|
* reference on all clients!
|
||||||
|
*
|
||||||
|
* @param {Number} reference
|
||||||
|
* @param {Function} structConstructor
|
||||||
|
*
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
export const registerStruct = (reference, structConstructor) => {
|
||||||
|
structs.set(reference, structConstructor)
|
||||||
|
references.set(structConstructor, reference)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
export const getStruct = (reference) => {
|
||||||
|
return structs.get(reference)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
export const getStructReference = (typeConstructor) => {
|
||||||
|
return references.get(typeConstructor)
|
||||||
|
}
|
46
src/utils/structStringify.js
Normal file
46
src/utils/structStringify.js
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
|
||||||
|
import * as ID from './ID.js'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stringify an item id.
|
||||||
|
*
|
||||||
|
* @param {ID.ID | ID.RootID} id
|
||||||
|
* @return {string}
|
||||||
|
*/
|
||||||
|
export const stringifyID = id => id instanceof ID.ID ? `(${id.user},${id.clock})` : `(${id.name},${id.type})`
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stringify an item as ID. HHere, an item could also be a Yjs instance (e.g. item._parent).
|
||||||
|
*
|
||||||
|
* @param {Y.Item | Y.Y | null} item
|
||||||
|
* @return {string}
|
||||||
|
*/
|
||||||
|
export const stringifyItemID = item => {
|
||||||
|
let result
|
||||||
|
if (item === null) {
|
||||||
|
result = '()'
|
||||||
|
} else if (item._id != null) {
|
||||||
|
result = stringifyID(item._id)
|
||||||
|
} else {
|
||||||
|
// must be a Yjs instance
|
||||||
|
// Don't include Y in this module, so we prevent circular dependencies.
|
||||||
|
result = 'y'
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper utility to convert an item to a readable format.
|
||||||
|
*
|
||||||
|
* @param {String} name The name of the item class (YText, ItemString, ..).
|
||||||
|
* @param {Y.Item} item The item instance.
|
||||||
|
* @param {String} [append] Additional information to append to the returned
|
||||||
|
* string.
|
||||||
|
* @return {String} A readable string that represents the item object.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
export const logItemHelper = (name, item, append) => {
|
||||||
|
const left = item._left !== null ? stringifyID(item._left._lastId) : '()'
|
||||||
|
const origin = item._origin !== null ? stringifyID(item._origin._lastId) : '()'
|
||||||
|
return `${name}(id:${stringifyItemID(item)},left:${left},origin:${origin},right:${stringifyItemID(item._right)},parent:${stringifyItemID(item._parent)},parentSub:${item._parentSub}${append !== undefined ? ' - ' + append : ''})`
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user