How to use the yjs.Map function in yjs

To help you get started, we’ve selected a few yjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github citrusbyte / thicket / packages / thicket-webapp / src / database / index.js View on Github external
y.connector.whenSynced(() => {
                setTimeout(() => {
                  this._syncing = false
                  this.emit(`synced-${communityId}`)
                }, 1000)
              })
            }
          })
        }

        const options = yConfig(node, communityId)
        options.connector = { ...options.connector, onSubscribed }
        const y = new Y(`thicket:${communityId}`, options, persistence)
        y.share.publications = y.define('publications', Y.Array)
        y.share.metadata = y.define('map', Y.Map)
        y.share.publicationsMetadata = y.define('map', Y.Map)
        y.share.nicknames = y.define('map', Y.Map)

        // updates to the community metadata (eg change community title)
        y.share.metadata.observe(({ value, type }) => {
          if (value && type !== 'delete') {
            if (!this._syncing) {
              this.emit(`update-${communityId}`, value)
            }
          }
        })
        // updates to the publications (eg new publication)
        y.share.publications.observe(async ({ type, values }) => {
          if (!this._syncing) {
            this.emit(`update-${communityId}-publications`, await this.publicationsGetAll(communityId))
          }
          // async remove local blocks
github citrusbyte / thicket / packages / thicket-webapp / src / database / index.js View on Github external
this.emit(`syncing-${communityId}`)
              y.connector.whenSynced(() => {
                setTimeout(() => {
                  this._syncing = false
                  this.emit(`synced-${communityId}`)
                }, 1000)
              })
            }
          })
        }

        const options = yConfig(node, communityId)
        options.connector = { ...options.connector, onSubscribed }
        const y = new Y(`thicket:${communityId}`, options, persistence)
        y.share.publications = y.define('publications', Y.Array)
        y.share.metadata = y.define('map', Y.Map)
        y.share.publicationsMetadata = y.define('map', Y.Map)
        y.share.nicknames = y.define('map', Y.Map)

        // updates to the community metadata (eg change community title)
        y.share.metadata.observe(({ value, type }) => {
          if (value && type !== 'delete') {
            if (!this._syncing) {
              this.emit(`update-${communityId}`, value)
            }
          }
        })
        // updates to the publications (eg new publication)
        y.share.publications.observe(async ({ type, values }) => {
          if (!this._syncing) {
            this.emit(`update-${communityId}-publications`, await this.publicationsGetAll(communityId))
          }
github ipfs-shipyard / peer-pad-core / src / backend / index.js View on Github external
this.auth.on('change', (peerId, newCapabilities) => {
      let capabilities = this.crdt.share.access.get(peerId)
      if (!capabilities) {
        this.crdt.share.access.set(peerId, Y.Map)
        capabilities = this.crdt.share.access.get(peerId)
      }
      if (newCapabilities) {
        Object.keys(newCapabilities).forEach((capabilityName, hasPermission) => {
          if (capabilities.get(capabilityName) !== newCapabilities[capabilityName]) {
            capabilities.set(capabilityName, hasPermission)
          }
        })
      } else {
        capabilities.delete(peerId)
      }
    })
github sambapos / pmpos3 / src / store / configureProtocol.ts View on Github external
yclient(Y);
    yindexeddb(Y);
    const persistence = new Y.IndexedDB();

    let y = new Y(
        networkName, {
            connector: {
                name: 'websockets-client',
                url: 'https://my-websockets-server.herokuapp.com/'
            }
        },
        persistence);

    let chatprotocol = y.define('chat', Y.Array);
    let commitProtocol = y.define('commits', Y.Array);
    let configProtocol = y.define('config', Y.Map);

    dispatchCommitProtocol(dispatch, commitProtocol);
    dispatchConfigProtocol(dispatch, configProtocol);

    configProtocol.observe(event => {
        dispatchConfigEvent(dispatch, event.target);
    });

    commitProtocol.observe(event => {
        let elements: any[] = Array.from(event.addedElements);
        let commits = elements.reduce(
            (r: any[], e) => {
                r.push(...e._content);
                return r;
            },
            []);