renamed WeakLink -> YWeakLink + YWeakLinkEvent

This commit is contained in:
Bartosz Sypytkowski 2023-06-09 13:57:06 +02:00
parent c6640fae5b
commit 6f9db68f9a
10 changed files with 48 additions and 33 deletions

View File

@ -10,11 +10,12 @@ export {
YXmlHook as XmlHook, YXmlHook as XmlHook,
YXmlElement as XmlElement, YXmlElement as XmlElement,
YXmlFragment as XmlFragment, YXmlFragment as XmlFragment,
WeakLink, YWeakLink as WeakLink,
YXmlEvent, YXmlEvent,
YMapEvent, YMapEvent,
YArrayEvent, YArrayEvent,
YTextEvent, YTextEvent,
YWeakLinkEvent,
YEvent, YEvent,
Item, Item,
AbstractStruct, AbstractStruct,

View File

@ -27,7 +27,7 @@ export * from './types/YXmlElement.js'
export * from './types/YXmlEvent.js' export * from './types/YXmlEvent.js'
export * from './types/YXmlHook.js' export * from './types/YXmlHook.js'
export * from './types/YXmlText.js' export * from './types/YXmlText.js'
export * from './types/WeakLink.js' export * from './types/YWeakLink.js'
export * from './structs/AbstractStruct.js' export * from './structs/AbstractStruct.js'
export * from './structs/GC.js' export * from './structs/GC.js'

View File

@ -1,13 +1,8 @@
import { decoding, encoding, error } from 'lib0' import { decoding, encoding, error } from 'lib0'
import { import {
UpdateEncoderV1, UpdateEncoderV2, UpdateDecoderV1, UpdateDecoderV2, Transaction, Item, StructStore, // eslint-disable-line UpdateEncoderV1, UpdateEncoderV2, UpdateDecoderV1, UpdateDecoderV2, Transaction, Item, StructStore, // eslint-disable-line
WeakLink, YWeakLink,
findRootTypeKey,
ID,
find,
ContentType,
AbstractType, AbstractType,
findMarker,
getItemCleanStart, getItemCleanStart,
createID, createID,
getItemCleanEnd getItemCleanEnd
@ -15,7 +10,7 @@ import {
export class ContentLink { export class ContentLink {
/** /**
* @param {WeakLink<any>} link * @param {YWeakLink<any>} link
*/ */
constructor (link) { constructor (link) {
this.link = link this.link = link
@ -138,7 +133,7 @@ import {
export const readContentWeakLink = decoder => { export const readContentWeakLink = decoder => {
const flags = decoding.readUint8(decoder.restDecoder) const flags = decoding.readUint8(decoder.restDecoder)
const id = decoder.readLeftID() const id = decoder.readLeftID()
return new ContentLink(new WeakLink(id, null)) return new ContentLink(new YWeakLink(id, null))
} }
const lengthExceeded = error.create('Length exceeded!') const lengthExceeded = error.create('Length exceeded!')
@ -149,7 +144,7 @@ const lengthExceeded = error.create('Length exceeded!')
* @param {Transaction} transaction * @param {Transaction} transaction
* @param {AbstractType<any>} parent * @param {AbstractType<any>} parent
* @param {number} index * @param {number} index
* @return {WeakLink<any>} * @return {YWeakLink<any>}
*/ */
export const arrayWeakLink = (transaction, parent, index) => { export const arrayWeakLink = (transaction, parent, index) => {
let item = parent._start let item = parent._start
@ -162,7 +157,7 @@ export const arrayWeakLink = (transaction, parent, index) => {
if (item.length > 1) { if (item.length > 1) {
item = getItemCleanEnd(transaction, transaction.doc.store, createID(item.id.client, item.id.clock + 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 index -= item.length
} }
@ -176,12 +171,12 @@ export const arrayWeakLink = (transaction, parent, index) => {
* *
* @param {AbstractType<any>} parent * @param {AbstractType<any>} parent
* @param {string} key * @param {string} key
* @return {WeakLink<any>|undefined} * @return {YWeakLink<any>|undefined}
*/ */
export const mapWeakLink = (parent, key) => { export const mapWeakLink = (parent, key) => {
const item = parent._map.get(key) const item = parent._map.get(key)
if (item !== undefined) { if (item !== undefined) {
return new WeakLink(item.id, item) return new YWeakLink(item.id, item)
} else { } else {
return undefined return undefined
} }

View File

@ -25,7 +25,7 @@ import {
addChangedTypeToTransaction, addChangedTypeToTransaction,
isDeleted, isDeleted,
StackItem, DeleteSet, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, ContentType, ContentDeleted, StructStore, ID, AbstractType, Transaction, // eslint-disable-line StackItem, DeleteSet, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, ContentType, ContentDeleted, StructStore, ID, AbstractType, Transaction, // eslint-disable-line
WeakLink, YWeakLink,
ContentLink ContentLink
} from '../internals.js' } from '../internals.js'

View File

@ -11,7 +11,7 @@ import {
ContentAny, ContentAny,
ContentBinary, ContentBinary,
getItemCleanStart, 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' } from '../internals.js'
import * as map from 'lib0/map' 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 = 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) left.integrate(transaction, 0)
break break
case WeakLink: case YWeakLink:
left = new Item(createID(ownClientId, getState(store, ownClientId)), left, left && left.lastId, right, right && right.id, parent, null, new ContentLink(/** @type {WeakLink<any>} */ (c))) left = new Item(createID(ownClientId, getState(store, ownClientId)), left, left && left.lastId, right, right && right.id, parent, null, new ContentLink(/** @type {YWeakLink<any>} */ (c)))
left.integrate(transaction, 0) left.integrate(transaction, 0)
break break
default: default:
@ -855,8 +855,8 @@ export const typeMapSet = (transaction, parent, key, value) => {
case Doc: case Doc:
content = new ContentDoc(/** @type {Doc} */ (value)) content = new ContentDoc(/** @type {Doc} */ (value))
break break
case WeakLink: case YWeakLink:
content = new ContentLink(/** @type {WeakLink<any>} */ (value)) content = new ContentLink(/** @type {YWeakLink<any>} */ (value))
break; break;
default: default:
if (value instanceof AbstractType) { if (value instanceof AbstractType) {

View File

@ -17,7 +17,7 @@ import {
callTypeObservers, callTypeObservers,
transact, transact,
ArraySearchMarker, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Transaction, Item, // eslint-disable-line ArraySearchMarker, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Transaction, Item, // eslint-disable-line
WeakLink, YWeakLink,
arrayWeakLink arrayWeakLink
} from '../internals.js' } from '../internals.js'
import { typeListSlice } from './AbstractType.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. * Returns the weak link to i-th element from a YArray.
* *
* @param {number} index The index of the element to return from the YArray * @param {number} index The index of the element to return from the YArray
* @return {WeakLink<T>} * @return {YWeakLink<T>}
*/ */
link(index) { link(index) {
if (this.doc !== null) { if (this.doc !== null) {

View File

@ -15,7 +15,7 @@ import {
callTypeObservers, callTypeObservers,
transact, transact,
UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Transaction, Item, // eslint-disable-line UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Transaction, Item, // eslint-disable-line
WeakLink, YWeakLink,
mapWeakLink mapWeakLink
} from '../internals.js' } 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. * Returns a weak reference link to another element stored in the same document.
* *
* @param {string} key * @param {string} key
* @return {WeakLink<MapType>|undefined} * @return {YWeakLink<MapType>|undefined}
*/ */
link(key) { link(key) {
return mapWeakLink(this, key) return mapWeakLink(this, key)

View File

@ -1,11 +1,28 @@
import { AbstractType, GC, ID, Item } from "yjs" import { AbstractType, GC, ID, Item, Transaction, YEvent } from "yjs"
/**
* @template T extends AbstractType<any>
* @extends YEvent<any>
* Event that describes the changes on a YMap.
*/
export class YWeakLinkEvent extends YEvent {
/**
* @param {YWeakLink<T>} ylink The YWeakLink to which this event was propagated to.
* @param {Transaction} transaction
* @param {YEvent<any>} source Source event that has been propagated to ylink.
*/
constructor (ylink, transaction, source) {
super(ylink, transaction)
this.source = source
}
}
/** /**
* @template T * @template T
* *
* Weak link to another value stored somewhere in the document. * Weak link to another value stored somewhere in the document.
*/ */
export class WeakLink { export class YWeakLink {
/** /**
* @param {ID} id * @param {ID} id
* @param {Item|GC|null} item * @param {Item|GC|null} item

View File

@ -11,7 +11,7 @@ import * as doc from './doc.tests.js'
import * as snapshot from './snapshot.tests.js' import * as snapshot from './snapshot.tests.js'
import * as updates from './updates.tests.js' import * as updates from './updates.tests.js'
import * as relativePositions from './relativePositions.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 { runTests } from 'lib0/testing'
import { isBrowser, isNode } from 'lib0/environment' import { isBrowser, isNode } from 'lib0/environment'

View File

@ -122,17 +122,19 @@ export const testObserveMapLinkArrayRemove = tc => {
const doc = new Y.Doc() const doc = new Y.Doc()
const map = doc.getMap('map') const map = doc.getMap('map')
const array = doc.getArray('array') const array = doc.getArray('array')
/**
* @type {Map<string, { action: 'add' | 'update' | 'delete', oldValue: any, newValue: any }>}
*/
let keys
map.observe((e) => keys = e.keys)
array.insert(0, [1]) array.insert(0, [1])
const link = array.link(0) const link = array.link(0)
map.set('key', link) 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) array.delete(0)
t.compare(keys.get('key'), { action:'delete', oldValue: 1, newValue: null }) t.compare(keys.get('key'), { action:'delete', oldValue: 1, newValue: null })