add internals file and use it to organize imports

This commit is contained in:
Kevin Jahns
2019-04-04 19:35:38 +02:00
parent 8dbd2c4696
commit 30bf3742c9
38 changed files with 378 additions and 260 deletions

View File

@@ -1,10 +1,13 @@
import {
getItemRange,
StructStore, Transaction, ID // eslint-disable-line
} from '../internals.js'
import * as math from 'lib0/math.js'
import * as map from 'lib0/map.js'
import * as encoding from 'lib0/encoding.js'
import * as decoding from 'lib0/decoding.js'
import * as math from 'lib0/math.js'
import { StructStore, getItemRange } from './StructStore.js' // eslint-disable-line
import { Transaction } from './Transaction.js' // eslint-disable-line
import { ID } from './ID.js' // eslint-disable-line
class DeleteItem {
/**

View File

@@ -1,5 +1,3 @@
import { Transaction } from './Transaction.js' // eslint-disable-line
import { YEvent } from './YEvent.js' // eslint-disable-line
import * as f from 'lib0/function.js'
/**
@@ -20,7 +18,7 @@ export class EventHandler {
* @template ARG0,ARG1
* @returns {EventHandler<ARG0,ARG1>}
*/
export const create = () => new EventHandler()
export const createEventHandler = () => new EventHandler()
/**
* Adds an event listener that is called when
@@ -30,7 +28,7 @@ export const create = () => new EventHandler()
* @param {EventHandler<ARG0,ARG1>} eventHandler
* @param {function(ARG0,ARG1):void} f The event handler.
*/
export const addEventListener = (eventHandler, f) =>
export const addEventHandlerListener = (eventHandler, f) =>
eventHandler.l.push(f)
/**
@@ -41,7 +39,7 @@ export const addEventListener = (eventHandler, f) =>
* @param {function(ARG0,ARG1):void} f The event handler that was added with
* {@link EventHandler#addEventListener}
*/
export const removeEventListener = (eventHandler, f) => {
export const removeEventHandlerListener = (eventHandler, f) => {
eventHandler.l = eventHandler.l.filter(g => f !== g)
}
@@ -50,7 +48,7 @@ export const removeEventListener = (eventHandler, f) => {
* @template ARG0,ARG1
* @param {EventHandler<ARG0,ARG1>} eventHandler
*/
export const removeAllEventListeners = eventHandler => {
export const removeAllEventHandlerListeners = eventHandler => {
eventHandler.l.length = 0
}
@@ -62,5 +60,5 @@ export const removeAllEventListeners = eventHandler => {
* @param {EventHandler<ARG0,ARG1>} eventHandler
* @param {[ARG0,ARG1]} args
*/
export const callEventListeners = (eventHandler, args) =>
export const callEventHandlerListeners = (eventHandler, args) =>
f.callAll(eventHandler.l, args)

View File

@@ -2,10 +2,11 @@
* @module utils
*/
import { AbstractType } from '../internals' // eslint-disable-line
import * as decoding from 'lib0/decoding.js'
import * as encoding from 'lib0/encoding.js'
import * as error from 'lib0/error.js'
import { AbstractType } from '../types/AbstractType.js' // eslint-disable-line
export class ID {
/**

View File

@@ -1,5 +1,9 @@
import { DeleteSet, isDeleted } from './DeleteSet'
import { AbstractItem } from '../structs/AbstractItem.js' // eslint-disable-line
import {
DeleteSet,
isDeleted,
AbstractItem // eslint-disable-line
} from '../internals.js'
export class Snapshot {
/**

View File

@@ -1,8 +1,8 @@
import { AbstractStruct } from '../structs/AbstractStruct.js' // eslint-disable-line
import { AbstractItem } from '../structs/AbstractItem.js' // eslint-disable-line
import { ItemType } from '../structs/ItemType.js' // eslint-disable-line
import { ID } from './ID.js' // eslint-disable-line
import { Transaction } from './Transaction.js' // eslint-disable-line
import {
Transaction, ID, ItemType, AbstractItem, AbstractStruct // eslint-disable-line
} from '../internals.js'
import * as map from 'lib0/map.js'
import * as math from 'lib0/math.js'
import * as error from 'lib0/error.js'

View File

@@ -2,16 +2,17 @@
* @module utils
*/
import {
getState,
createID,
writeStructsFromTransaction,
writeDeleteSet,
DeleteSet,
sortAndMergeDeleteSet,
AbstractType, AbstractItem, YEvent, ItemType, Y // eslint-disable-line
} from '../internals.js'
import * as encoding from 'lib0/encoding.js'
import { AbstractType } from '../types/AbstractType.js' // eslint-disable-line
import { AbstractItem } from '../structs/AbstractItem.js' // eslint-disable-line
import { Y } from './Y.js' // eslint-disable-line
import { YEvent } from './YEvent.js' // eslint-disable-line
import { ItemType } from '../structs/ItemType.js' // eslint-disable-line
import { writeStructsFromTransaction } from './structEncoding.js'
import { createID } from './ID.js' // eslint-disable-line
import { writeDeleteSet, DeleteSet, sortAndMergeDeleteSet } from './DeleteSet.js'
import { getState } from './StructStore.js'
/**
* A transaction is created for every change on the Yjs model. It is possible

View File

@@ -1,15 +1,17 @@
// @ts-nocheck
import * as ID from './ID.js'
import { isParentOf } from './isParentOf.js'
import {
isParentOf,
createID
} from '../internals.js'
class ReverseOperation {
constructor (y, transaction, bindingInfos) {
this.created = new Date()
const beforeState = transaction.beforeState
if (beforeState.has(y.userID)) {
this.toState = ID.createID(y.userID, y.ss.getState(y.userID) - 1)
this.fromState = ID.createID(y.userID, beforeState.get(y.userID))
this.toState = createID(y.userID, y.ss.getState(y.userID) - 1)
this.fromState = createID(y.userID, beforeState.get(y.userID))
} else {
this.toState = null
this.fromState = null
@@ -51,7 +53,7 @@ function applyReverseOperation (y, scope, reverseBuffer) {
const redoitems = new Set()
for (let del of undoOp.deletedStructs) {
const fromState = del.from
const toState = ID.createID(fromState.user, fromState.clock + del.len - 1)
const toState = createID(fromState.user, fromState.clock + del.len - 1)
y.os.getItemCleanStart(fromState)
y.os.getItemCleanEnd(toState)
y.os.iterate(fromState, toState, op => {

View File

@@ -1,21 +1,25 @@
import { StructStore, findIndexSS } from './StructStore.js'
import { } from './StructStore.js'
import {
callEventHandlerListeners,
sortAndMergeDeleteSet,
StructStore,
findIndexSS,
Transaction,
AbstractType,
AbstractItem,
YArray,
YText,
YMap,
YXmlFragment,
YEvent, ItemDeleted, GC, AbstractStruct // eslint-disable-line
} from '../internals.js'
import { Observable } from 'lib0/observable.js'
import * as error from 'lib0/error.js'
import * as random from 'lib0/random.js'
import * as map from 'lib0/map.js'
import { Observable } from 'lib0/observable.js'
import { Transaction } from './Transaction.js'
import { AbstractStruct, AbstractRef } from '../structs/AbstractStruct.js' // eslint-disable-line
import { AbstractType } from '../types/AbstractType.js'
import { AbstractItem } from '../structs/AbstractItem.js'
import { sortAndMergeDeleteSet } from './DeleteSet.js'
import * as math from 'lib0/math.js'
import { GC } from '../structs/GC.js' // eslint-disable-line
import { ItemDeleted } from '../structs/ItemDeleted.js' // eslint-disable-line
import { YArray } from '../types/YArray.js'
import { YText } from '../types/YText.js'
import { YMap } from '../types/YMap.js'
import { YXmlFragment } from '../types/YXmlElement.js'
import { YEvent } from './YEvent.js' // eslint-disable-line
import * as eventHandler from './EventHandler.js'
/**
* A Yjs instance handles the state of shared data.
@@ -34,14 +38,6 @@ export class Y extends Observable {
*/
this.share = new Map()
this.store = new StructStore()
/**
* @type {Map<number, Map<number, AbstractRef>>}
*/
this._missingStructs = new Map()
/**
* @type {Array<AbstractStruct>}
*/
this._readyToIntegrate = []
/**
* @type {Transaction | null}
*/
@@ -54,7 +50,7 @@ export class Y extends Observable {
get transaction () {
const t = this._transaction
if (t === null) {
throw new Error('All changes must happen inside a transaction')
throw error.create('All changes must happen inside a transaction')
}
return t
}
@@ -98,7 +94,7 @@ export class Y extends Observable {
})
// we don't need to check for events.length
// because we know it has at least one element
eventHandler.callEventListeners(type._dEH, [events, transaction])
callEventHandlerListeners(type._dEH, [events, transaction])
})
// when all changes & events are processed, emit afterTransaction event
this.emit('afterTransaction', [this, transaction])

View File

@@ -1,8 +1,8 @@
import { AbstractItem } from '../structs/AbstractItem.js' // eslint-disable-line
import { AbstractType } from '../types/AbstractType.js' // eslint-disable-line
import { Transaction } from './Transaction.js' // eslint-disable-line
import { AbstractStruct } from '../structs/AbstractStruct.js' // eslint-disable-line
import { isDeleted } from './DeleteSet.js'
import {
isDeleted,
AbstractItem, AbstractType, Transaction, AbstractStruct // eslint-disable-line
} from '../internals.js'
/**
* @module utils

View File

@@ -2,8 +2,7 @@
* @module utils
*/
import { Y } from '../utils/Y.js' // eslint-disable-line
import { AbstractType } from '../types/AbstractType.js' // eslint-disable-line
import { AbstractType } from '../internals.js' // eslint-disable-line
/**
* Check if `parent` is a parent of `child`.

View File

@@ -1,15 +1,20 @@
/**
* @module utils
*/
import * as ID from './ID.js'
import { AbstractType } from '../types/AbstractType.js' // eslint-disable-line
import { AbstractItem } from '../structs/AbstractItem.js' // eslint-disable-line
import {
find,
exists,
getItemType,
createID,
writeID,
readID,
compareIDs,
findRootTypeKey,
AbstractItem,
ID, StructStore, Y, AbstractType // eslint-disable-line
} from '../internals.js'
import * as encoding from 'lib0/encoding.js'
import * as decoding from 'lib0/decoding.js'
import * as error from 'lib0/error.js'
import { find, exists, getItemType, StructStore } from './StructStore.js' // eslint-disable-line
import { Y } from './Y.js' // eslint-disable-line
/**
* A relative position that is based on the Yjs model. In contrast to an
@@ -38,13 +43,13 @@ import { Y } from './Y.js' // eslint-disable-line
*/
export class RelativePosition {
/**
* @param {ID.ID|null} type
* @param {ID|null} type
* @param {string|null} tname
* @param {ID.ID|null} item
* @param {ID|null} item
*/
constructor (type, tname, item) {
/**
* @type {ID.ID|null}
* @type {ID|null}
*/
this.type = type
/**
@@ -52,7 +57,7 @@ export class RelativePosition {
*/
this.tname = tname
/**
* @type {ID.ID | null}
* @type {ID | null}
*/
this.item = item
}
@@ -83,13 +88,13 @@ export const createAbsolutePosition = (type, offset) => new AbsolutePosition(typ
/**
* @param {AbstractType<any>} type
* @param {ID.ID|null} item
* @param {ID|null} item
*/
export const createRelativePosition = (type, item) => {
let typeid = null
let tname = null
if (type._item === null) {
tname = ID.findRootTypeKey(type)
tname = findRootTypeKey(type)
} else {
typeid = type._item.id
}
@@ -109,7 +114,7 @@ export const createRelativePositionByOffset = (type, offset) => {
if (!t.deleted && t.countable) {
if (t.length > offset) {
// case 1: found position somewhere in the linked list
return createRelativePosition(type, ID.createID(t.id.client, t.id.clock + offset))
return createRelativePosition(type, createID(t.id.client, t.id.clock + offset))
}
offset -= t.length
}
@@ -126,7 +131,7 @@ export const writeRelativePosition = (encoder, rpos) => {
const { type, tname, item } = rpos
if (item !== null) {
encoding.writeVarUint(encoder, 0)
ID.writeID(encoder, item)
writeID(encoder, item)
} else if (tname !== null) {
// case 2: found position at the end of the list and type is stored in y.share
encoding.writeUint8(encoder, 1)
@@ -134,7 +139,7 @@ export const writeRelativePosition = (encoder, rpos) => {
} else if (type !== null) {
// case 3: found position at the end of the list and type is attached to an item
encoding.writeUint8(encoder, 2)
ID.writeID(encoder, type)
writeID(encoder, type)
} else {
throw error.unexpectedCase()
}
@@ -154,7 +159,7 @@ export const readRelativePosition = (decoder, y, store) => {
switch (decoding.readVarUint(decoder)) {
case 0:
// case 1: found position somewhere in the linked list
itemID = ID.readID(decoder)
itemID = readID(decoder)
break
case 1:
// case 2: found position at the end of the list and type is stored in y.share
@@ -162,7 +167,7 @@ export const readRelativePosition = (decoder, y, store) => {
break
case 2: {
// case 3: found position at the end of the list and type is attached to an item
type = ID.readID(decoder)
type = readID(decoder)
}
}
return new RelativePosition(type, tname, itemID)
@@ -231,7 +236,7 @@ export const toRelativePosition = (apos, y) => {
while (n !== null) {
if (!n.deleted && n.countable) {
if (n.length > offset) {
return createRelativePosition(type, ID.createID(n.id.client, n.id.clock + offset))
return createRelativePosition(type, createID(n.id.client, n.id.clock + offset))
}
offset -= n.length
}
@@ -247,8 +252,8 @@ export const toRelativePosition = (apos, y) => {
*/
export const compareRelativePositions = (a, b) => a === b || (
a !== null && b !== null && (
(a.item !== null && b.item !== null && ID.compareIDs(a.item, b.item)) ||
(a.item !== null && b.item !== null && compareIDs(a.item, b.item)) ||
(a.tname !== null && a.tname === b.tname) ||
(a.type !== null && b.type !== null && ID.compareIDs(a.type, b.type))
(a.type !== null && b.type !== null && compareIDs(a.type, b.type))
)
)

View File

@@ -1,20 +1,26 @@
import {
findIndexSS,
exists,
ItemBinaryRef,
GCRef,
ItemDeletedRef,
ItemEmbedRef,
ItemFormatRef,
ItemJSONRef,
ItemStringRef,
ItemTypeRef,
writeID,
createID,
readID,
Transaction, AbstractStruct, AbstractRef, StructStore, ID // eslint-disable-line
} from '../internals.js'
import * as encoding from 'lib0/encoding.js'
import * as decoding from 'lib0/decoding.js'
import * as map from 'lib0/map.js'
import { AbstractStruct, AbstractRef } from '../structs/AbstractStruct.js' // eslint-disable-line
import * as binary from 'lib0/binary.js'
import { Transaction } from './Transaction.js' // eslint-disable-line
import { findIndexSS, exists, StructStore } from './StructStore.js' // eslint-disable-line
import { writeID, createID, readID, ID } from './ID.js' // eslint-disable-line
import * as iterator from 'lib0/iterator.js'
import { ItemBinaryRef } from '../structs/ItemBinary.js'
import { GCRef } from '../structs/GC.js'
import { ItemDeletedRef } from '../structs/ItemDeleted.js'
import { ItemEmbedRef } from '../structs/ItemEmbed.js'
import { ItemFormatRef } from '../structs/ItemFormat.js'
import { ItemJSONRef } from '../structs/ItemJSON.js'
import { ItemStringRef } from '../structs/ItemString.js'
import { ItemTypeRef } from '../structs/ItemType.js'
/**
* @typedef {Map<number, number>} StateMap