From 7b0d5fede57b5252bcdda4f06cc415abc7165e96 Mon Sep 17 00:00:00 2001 From: Bartosz Sypytkowski Date: Fri, 9 Jun 2023 13:57:06 +0200 Subject: [PATCH] renamed WeakLink -> YWeakLink + YWeakLinkEvent --- src/index.js | 3 ++- src/internals.js | 2 +- src/structs/ContentLink.js | 19 +++++++---------- src/structs/Item.js | 2 +- src/types/AbstractType.js | 10 ++++----- src/types/YArray.js | 4 ++-- src/types/YMap.js | 4 ++-- src/types/{WeakLink.js => YWeakLink.js} | 21 +++++++++++++++++-- tests/index.js | 2 +- ...akLinks.tests.js => y-weak-links.tests.js} | 14 +++++++------ 10 files changed, 48 insertions(+), 33 deletions(-) rename src/types/{WeakLink.js => YWeakLink.js} (57%) rename tests/{weakLinks.tests.js => y-weak-links.tests.js} (98%) diff --git a/src/index.js b/src/index.js index 3d9c535d..455690c9 100644 --- a/src/index.js +++ b/src/index.js @@ -10,11 +10,12 @@ export { YXmlHook as XmlHook, YXmlElement as XmlElement, YXmlFragment as XmlFragment, - WeakLink, + YWeakLink as WeakLink, YXmlEvent, YMapEvent, YArrayEvent, YTextEvent, + YWeakLinkEvent, YEvent, Item, AbstractStruct, diff --git a/src/internals.js b/src/internals.js index 4861c591..889c050b 100644 --- a/src/internals.js +++ b/src/internals.js @@ -27,7 +27,7 @@ export * from './types/YXmlElement.js' export * from './types/YXmlEvent.js' export * from './types/YXmlHook.js' export * from './types/YXmlText.js' -export * from './types/WeakLink.js' +export * from './types/YWeakLink.js' export * from './structs/AbstractStruct.js' export * from './structs/GC.js' diff --git a/src/structs/ContentLink.js b/src/structs/ContentLink.js index a8d5dcbe..25d6e6fb 100644 --- a/src/structs/ContentLink.js +++ b/src/structs/ContentLink.js @@ -1,13 +1,8 @@ import { decoding, encoding, error } from 'lib0' import { UpdateEncoderV1, UpdateEncoderV2, UpdateDecoderV1, UpdateDecoderV2, Transaction, Item, StructStore, // eslint-disable-line - WeakLink, - findRootTypeKey, - ID, - find, - ContentType, + YWeakLink, AbstractType, - findMarker, getItemCleanStart, createID, getItemCleanEnd @@ -15,7 +10,7 @@ import { export class ContentLink { /** - * @param {WeakLink} link + * @param {YWeakLink} link */ constructor (link) { this.link = link @@ -138,7 +133,7 @@ import { export const readContentWeakLink = decoder => { const flags = decoding.readUint8(decoder.restDecoder) const id = decoder.readLeftID() - return new ContentLink(new WeakLink(id, null)) + return new ContentLink(new YWeakLink(id, null)) } const lengthExceeded = error.create('Length exceeded!') @@ -149,7 +144,7 @@ const lengthExceeded = error.create('Length exceeded!') * @param {Transaction} transaction * @param {AbstractType} parent * @param {number} index - * @return {WeakLink} + * @return {YWeakLink} */ export const arrayWeakLink = (transaction, parent, index) => { let item = parent._start @@ -162,7 +157,7 @@ export const arrayWeakLink = (transaction, parent, index) => { if (item.length > 1) { item = getItemCleanEnd(transaction, transaction.doc.store, createID(item.id.client, item.id.clock + 1)) } - return new WeakLink(item.id, item) + return new YWeakLink(item.id, item) } index -= item.length } @@ -176,12 +171,12 @@ export const arrayWeakLink = (transaction, parent, index) => { * * @param {AbstractType} parent * @param {string} key - * @return {WeakLink|undefined} + * @return {YWeakLink|undefined} */ export const mapWeakLink = (parent, key) => { const item = parent._map.get(key) if (item !== undefined) { - return new WeakLink(item.id, item) + return new YWeakLink(item.id, item) } else { return undefined } diff --git a/src/structs/Item.js b/src/structs/Item.js index 65f0bfcb..f7e8658a 100644 --- a/src/structs/Item.js +++ b/src/structs/Item.js @@ -25,7 +25,7 @@ import { addChangedTypeToTransaction, isDeleted, StackItem, DeleteSet, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, ContentType, ContentDeleted, StructStore, ID, AbstractType, Transaction, // eslint-disable-line - WeakLink, + YWeakLink, ContentLink } from '../internals.js' diff --git a/src/types/AbstractType.js b/src/types/AbstractType.js index 4f9830c8..472297a6 100644 --- a/src/types/AbstractType.js +++ b/src/types/AbstractType.js @@ -11,7 +11,7 @@ import { ContentAny, ContentBinary, getItemCleanStart, - ContentDoc, YText, YArray, UpdateEncoderV1, UpdateEncoderV2, Doc, Snapshot, Transaction, EventHandler, YEvent, Item, WeakLink, ContentLink, // eslint-disable-line + ContentDoc, YText, YArray, UpdateEncoderV1, UpdateEncoderV2, Doc, Snapshot, Transaction, EventHandler, YEvent, Item, YWeakLink, ContentLink, // eslint-disable-line } from '../internals.js' import * as map from 'lib0/map' @@ -669,8 +669,8 @@ export const typeListInsertGenericsAfter = (transaction, parent, referenceItem, left = new Item(createID(ownClientId, getState(store, ownClientId)), left, left && left.lastId, right, right && right.id, parent, null, new ContentDoc(/** @type {Doc} */ (c))) left.integrate(transaction, 0) break - case WeakLink: - left = new Item(createID(ownClientId, getState(store, ownClientId)), left, left && left.lastId, right, right && right.id, parent, null, new ContentLink(/** @type {WeakLink} */ (c))) + case YWeakLink: + left = new Item(createID(ownClientId, getState(store, ownClientId)), left, left && left.lastId, right, right && right.id, parent, null, new ContentLink(/** @type {YWeakLink} */ (c))) left.integrate(transaction, 0) break default: @@ -855,8 +855,8 @@ export const typeMapSet = (transaction, parent, key, value) => { case Doc: content = new ContentDoc(/** @type {Doc} */ (value)) break - case WeakLink: - content = new ContentLink(/** @type {WeakLink} */ (value)) + case YWeakLink: + content = new ContentLink(/** @type {YWeakLink} */ (value)) break; default: if (value instanceof AbstractType) { diff --git a/src/types/YArray.js b/src/types/YArray.js index cfd5dac7..4e61e742 100644 --- a/src/types/YArray.js +++ b/src/types/YArray.js @@ -17,7 +17,7 @@ import { callTypeObservers, transact, ArraySearchMarker, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Transaction, Item, // eslint-disable-line - WeakLink, + YWeakLink, arrayWeakLink } from '../internals.js' import { typeListSlice } from './AbstractType.js' @@ -207,7 +207,7 @@ export class YArray extends AbstractType { * Returns the weak link to i-th element from a YArray. * * @param {number} index The index of the element to return from the YArray - * @return {WeakLink} + * @return {YWeakLink} */ link(index) { if (this.doc !== null) { diff --git a/src/types/YMap.js b/src/types/YMap.js index c86e8b3d..845e34ae 100644 --- a/src/types/YMap.js +++ b/src/types/YMap.js @@ -15,7 +15,7 @@ import { callTypeObservers, transact, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Transaction, Item, // eslint-disable-line - WeakLink, + YWeakLink, mapWeakLink } from '../internals.js' @@ -239,7 +239,7 @@ export class YMap extends AbstractType { * Returns a weak reference link to another element stored in the same document. * * @param {string} key - * @return {WeakLink|undefined} + * @return {YWeakLink|undefined} */ link(key) { return mapWeakLink(this, key) diff --git a/src/types/WeakLink.js b/src/types/YWeakLink.js similarity index 57% rename from src/types/WeakLink.js rename to src/types/YWeakLink.js index c1b25a18..978d920a 100644 --- a/src/types/WeakLink.js +++ b/src/types/YWeakLink.js @@ -1,11 +1,28 @@ -import { AbstractType, GC, ID, Item } from "yjs" +import { AbstractType, GC, ID, Item, Transaction, YEvent } from "yjs" + +/** + * @template T extends AbstractType + * @extends YEvent + * Event that describes the changes on a YMap. + */ +export class YWeakLinkEvent extends YEvent { + /** + * @param {YWeakLink} ylink The YWeakLink to which this event was propagated to. + * @param {Transaction} transaction + * @param {YEvent} source Source event that has been propagated to ylink. + */ + constructor (ylink, transaction, source) { + super(ylink, transaction) + this.source = source + } +} /** * @template T * * Weak link to another value stored somewhere in the document. */ -export class WeakLink { +export class YWeakLink { /** * @param {ID} id * @param {Item|GC|null} item diff --git a/tests/index.js b/tests/index.js index d002290f..ab596392 100644 --- a/tests/index.js +++ b/tests/index.js @@ -11,7 +11,7 @@ import * as doc from './doc.tests.js' import * as snapshot from './snapshot.tests.js' import * as updates from './updates.tests.js' import * as relativePositions from './relativePositions.tests.js' -import * as weakLinks from './weakLinks.tests.js' +import * as weakLinks from './y-weak-links.tests.js' import { runTests } from 'lib0/testing' import { isBrowser, isNode } from 'lib0/environment' diff --git a/tests/weakLinks.tests.js b/tests/y-weak-links.tests.js similarity index 98% rename from tests/weakLinks.tests.js rename to tests/y-weak-links.tests.js index 48981a3f..df7d8b91 100644 --- a/tests/weakLinks.tests.js +++ b/tests/y-weak-links.tests.js @@ -122,17 +122,19 @@ export const testObserveMapLinkArrayRemove = tc => { const doc = new Y.Doc() const map = doc.getMap('map') const array = doc.getArray('array') - /** - * @type {Map} - */ - let keys - map.observe((e) => keys = e.keys) array.insert(0, [1]) const link = array.link(0) map.set('key', link) + /** + * @type {any} + */ + let keys = null + map.observe((e) => { + console.log('map received event', e) + keys = e.keys + }) - keys = /** @type {any} */ (null) array.delete(0) t.compare(keys.get('key'), { action:'delete', oldValue: 1, newValue: null })