renamed WeakLink -> YWeakLink + YWeakLinkEvent
This commit is contained in:
		
							parent
							
								
									3f68e787de
								
							
						
					
					
						commit
						7b0d5fede5
					
				@ -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,
 | 
			
		||||
 | 
			
		||||
@ -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'
 | 
			
		||||
 | 
			
		||||
@ -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<any>} link
 | 
			
		||||
     * @param {YWeakLink<any>} 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<any>} parent
 | 
			
		||||
 * @param {number} index
 | 
			
		||||
 * @return {WeakLink<any>}
 | 
			
		||||
 * @return {YWeakLink<any>}
 | 
			
		||||
 */
 | 
			
		||||
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<any>} parent
 | 
			
		||||
 * @param {string} key
 | 
			
		||||
 * @return {WeakLink<any>|undefined}
 | 
			
		||||
 * @return {YWeakLink<any>|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
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@ -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'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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<any>} */ (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<any>} */ (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<any>} */ (value))
 | 
			
		||||
      case YWeakLink:
 | 
			
		||||
        content = new ContentLink(/** @type {YWeakLink<any>} */ (value))
 | 
			
		||||
        break;
 | 
			
		||||
      default:
 | 
			
		||||
        if (value instanceof AbstractType) {
 | 
			
		||||
 | 
			
		||||
@ -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<T>}
 | 
			
		||||
   * @return {YWeakLink<T>}
 | 
			
		||||
   */
 | 
			
		||||
  link(index) {
 | 
			
		||||
    if (this.doc !== null) {
 | 
			
		||||
 | 
			
		||||
@ -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<MapType>|undefined}
 | 
			
		||||
   * @return {YWeakLink<MapType>|undefined}
 | 
			
		||||
   */
 | 
			
		||||
  link(key) {
 | 
			
		||||
    return mapWeakLink(this, key)
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 * 
 | 
			
		||||
 * Weak link to another value stored somewhere in the document.
 | 
			
		||||
 */
 | 
			
		||||
export class WeakLink {
 | 
			
		||||
export class YWeakLink {
 | 
			
		||||
  /**
 | 
			
		||||
    * @param {ID} id
 | 
			
		||||
    * @param {Item|GC|null} item
 | 
			
		||||
@ -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'
 | 
			
		||||
 | 
			
		||||
@ -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<string, { action: 'add' | 'update' | 'delete', oldValue: any, newValue: any }>}
 | 
			
		||||
   */
 | 
			
		||||
  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 })
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user