Skip to content

Commit

Permalink
migrate to ObservableV2
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Feb 29, 2024
1 parent 53173a9 commit 541306b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 40 deletions.
6 changes: 3 additions & 3 deletions src/utils/AbstractConnector.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Observable } from 'lib0/observable'
import { ObservableV2 } from 'lib0/observable'

import {
Doc // eslint-disable-line
Expand All @@ -10,9 +10,9 @@ import {
* @note This interface is experimental and it is not advised to actually inherit this class.
* It just serves as typing information.
*
* @extends {Observable<any>}
* @extends {ObservableV2<any>}
*/
export class AbstractConnector extends Observable {
export class AbstractConnector extends ObservableV2 {
/**
* @param {Doc} ydoc
* @param {any} awareness
Expand Down
43 changes: 22 additions & 21 deletions src/utils/Doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
ContentDoc, Item, Transaction, YEvent // eslint-disable-line
} from '../internals.js'

import { Observable } from 'lib0/observable'
import { ObservableV2 } from 'lib0/observable'
import * as random from 'lib0/random'
import * as map from 'lib0/map'
import * as array from 'lib0/array'
Expand All @@ -33,11 +33,27 @@ export const generateNewClientId = random.uint32
* @property {boolean} [DocOpts.shouldLoad] Whether the document should be synced by the provider now. This is toggled to true when you call ydoc.load()
*/

/**
* @typedef {Object} DocEvents
* @property {function(Doc):void} DocEvents.destroy
* @property {function(Doc):void} DocEvents.load
* @property {function(boolean, Doc):void} DocEvents.sync
* @property {function(Uint8Array, any, Doc, Transaction):void} DocEvents.update
* @property {function(Uint8Array, any, Doc, Transaction):void} DocEvents.updateV2
* @property {function(Doc):void} DocEvents.beforeAllTransactions
* @property {function(Transaction, Doc):void} DocEvents.beforeTransaction
* @property {function(Transaction, Doc):void} DocEvents.beforeObserverCalls
* @property {function(Transaction, Doc):void} DocEvents.afterTransaction
* @property {function(Transaction, Doc):void} DocEvents.afterTransactionCleanup
* @property {function(Doc, Array<Transaction>):void} DocEvents.afterAllTransactions
* @property {function({ loaded: Set<Doc>, added: Set<Doc>, removed: Set<Doc> }, Doc, Transaction):void} DocEvents.subdocs
*/

/**
* A Yjs instance handles the state of shared data.
* @extends Observable<string>
* @extends ObservableV2<DocEvents>
*/
export class Doc extends Observable {
export class Doc extends ObservableV2 {
/**
* @param {DocOpts} opts configuration
*/
Expand Down Expand Up @@ -115,7 +131,7 @@ export class Doc extends Observable {
}
this.isSynced = isSynced === undefined || isSynced === true
if (this.isSynced && !this.isLoaded) {
this.emit('load', [])
this.emit('load', [this])
}
})
/**
Expand Down Expand Up @@ -321,24 +337,9 @@ export class Doc extends Observable {
transaction.subdocsRemoved.add(this)
}, null, true)
}
this.emit('destroyed', [true])
// @ts-ignore
this.emit('destroyed', [true]) // DEPRECATED!
this.emit('destroy', [this])
super.destroy()
}

/**
* @param {string} eventName
* @param {function(...any):any} f
*/
on (eventName, f) {
super.on(eventName, f)
}

/**
* @param {string} eventName
* @param {function} f
*/
off (eventName, f) {
super.off(eventName, f)
}
}
23 changes: 17 additions & 6 deletions src/utils/UndoManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import {
getItemCleanStart,
isDeleted,
addToDeleteSet,
Transaction, Doc, Item, GC, DeleteSet, AbstractType // eslint-disable-line
YEvent, Transaction, Doc, Item, GC, DeleteSet, AbstractType // eslint-disable-line
} from '../internals.js'

import * as time from 'lib0/time'
import * as array from 'lib0/array'
import * as logging from 'lib0/logging'
import { Observable } from 'lib0/observable'
import { ObservableV2 } from 'lib0/observable'

export class StackItem {
/**
Expand Down Expand Up @@ -48,7 +48,7 @@ const clearUndoManagerStackItem = (tr, um, stackItem) => {
/**
* @param {UndoManager} undoManager
* @param {Array<StackItem>} stack
* @param {string} eventType
* @param {'undo'|'redo'} eventType
* @return {StackItem?}
*/
const popStackItem = (undoManager, stack, eventType) => {
Expand Down Expand Up @@ -120,7 +120,7 @@ const popStackItem = (undoManager, stack, eventType) => {
}, undoManager)
if (undoManager.currStackItem != null) {
const changedParentTypes = _tr.changedParentTypes
undoManager.emit('stack-item-popped', [{ stackItem: undoManager.currStackItem, type: eventType, changedParentTypes }, undoManager])
undoManager.emit('stack-item-popped', [{ stackItem: undoManager.currStackItem, type: eventType, changedParentTypes, origin: undoManager }, undoManager])
undoManager.currStackItem = null
}
return undoManager.currStackItem
Expand All @@ -139,16 +139,24 @@ const popStackItem = (undoManager, stack, eventType) => {
* @property {Doc} [doc] The document that this UndoManager operates on. Only needed if typeScope is empty.
*/

/**
* @typedef {Object} StackItemEvent
* @property {StackItem} StackItemEvent.stackItem
* @property {any} StackItemEvent.origin
* @property {'undo'|'redo'} StackItemEvent.type
* @property {Map<AbstractType<YEvent<any>>,Array<YEvent<any>>>} StackItemEvent.changedParentTypes
*/

/**
* Fires 'stack-item-added' event when a stack item was added to either the undo- or
* the redo-stack. You may store additional stack information via the
* metadata property on `event.stackItem.meta` (it is a `Map` of metadata properties).
* Fires 'stack-item-popped' event when a stack item was popped from either the
* undo- or the redo-stack. You may restore the saved stack information from `event.stackItem.meta`.
*
* @extends {Observable<'stack-item-added'|'stack-item-popped'|'stack-cleared'|'stack-item-updated'>}
* @extends {ObservableV2<{'stack-item-added':function(StackItemEvent, UndoManager):void, 'stack-item-popped': function(StackItemEvent, UndoManager):void, 'stack-cleared': function({ undoStackCleared: boolean, redoStackCleared: boolean }):void, 'stack-item-updated': function(StackItemEvent, UndoManager):void }>}
*/
export class UndoManager extends Observable {
export class UndoManager extends ObservableV2 {
/**
* @param {AbstractType<any>|Array<AbstractType<any>>} typeScope Accepts either a single type, or an array of types
* @param {UndoManagerOptions} options
Expand Down Expand Up @@ -246,6 +254,9 @@ export class UndoManager extends Observable {
keepItem(item, true)
}
})
/**
* @type {[StackItemEvent, UndoManager]}
*/
const changeEvent = [{ stackItem: stack[stack.length - 1], origin: transaction.origin, type: undoing ? 'redo' : 'undo', changedParentTypes: transaction.changedParentTypes }, this]
if (didAdd) {
this.emit('stack-item-added', changeEvent)
Expand Down
4 changes: 2 additions & 2 deletions tests/testHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const encV1 = {
mergeUpdates: Y.mergeUpdates,
applyUpdate: Y.applyUpdate,
logUpdate: Y.logUpdate,
updateEventName: 'update',
updateEventName: /** @type {'update'} */ ('update'),
diffUpdate: Y.diffUpdate
}

Expand All @@ -43,7 +43,7 @@ export const encV2 = {
mergeUpdates: Y.mergeUpdatesV2,
applyUpdate: Y.applyUpdateV2,
logUpdate: Y.logUpdateV2,
updateEventName: 'updateV2',
updateEventName: /** @type {'updateV2'} */ ('updateV2'),
diffUpdate: Y.diffUpdateV2
}

Expand Down
16 changes: 8 additions & 8 deletions tests/updates.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import * as object from 'lib0/object'
* @property {function(Uint8Array):{from:Map<number,number>,to:Map<number,number>}} Enc.parseUpdateMeta
* @property {function(Y.Doc):Uint8Array} Enc.encodeStateVector
* @property {function(Uint8Array):Uint8Array} Enc.encodeStateVectorFromUpdate
* @property {string} Enc.updateEventName
* @property {'update'|'updateV2'} Enc.updateEventName
* @property {string} Enc.description
* @property {function(Uint8Array, Uint8Array):Uint8Array} Enc.diffUpdate
*/
Expand Down Expand Up @@ -169,7 +169,7 @@ const checkUpdateCases = (ydoc, updates, enc, hasDeletes) => {
// t.info('Target State: ')
// enc.logUpdate(targetState)

cases.forEach((mergedUpdates, i) => {
cases.forEach((mergedUpdates) => {
// t.info('State Case $' + i + ':')
// enc.logUpdate(updates)
const merged = new Y.Doc({ gc: false })
Expand Down Expand Up @@ -218,10 +218,10 @@ const checkUpdateCases = (ydoc, updates, enc, hasDeletes) => {
}

/**
* @param {t.TestCase} tc
* @param {t.TestCase} _tc
*/
export const testMergeUpdates1 = tc => {
encoders.forEach((enc, i) => {
export const testMergeUpdates1 = _tc => {
encoders.forEach((enc) => {
t.info(`Using encoder: ${enc.description}`)
const ydoc = new Y.Doc({ gc: false })
const updates = /** @type {Array<Uint8Array>} */ ([])
Expand Down Expand Up @@ -299,16 +299,16 @@ export const testMergePendingUpdates = tc => {
Y.applyUpdate(yDoc5, update4)
Y.applyUpdate(yDoc5, serverUpdates[4])
// @ts-ignore
const update5 = Y.encodeStateAsUpdate(yDoc5) // eslint-disable-line
const _update5 = Y.encodeStateAsUpdate(yDoc5) // eslint-disable-line

const yText5 = yDoc5.getText('textBlock')
t.compareStrings(yText5.toString(), 'nenor')
}

/**
* @param {t.TestCase} tc
* @param {t.TestCase} _tc
*/
export const testObfuscateUpdates = tc => {
export const testObfuscateUpdates = _tc => {
const ydoc = new Y.Doc()
const ytext = ydoc.getText('text')
const ymap = ydoc.getMap('map')
Expand Down

0 comments on commit 541306b

Please sign in to comment.