Giter VIP home page Giter VIP logo

y-map's Introduction

Map Type for Yjs

Manage map-like data with this shareable map type. You can insert and delete objects in y-map. The objects must either be a custom types, or fulfill the following property: v equals JSON.parse(JSON.stringify(v)) (according to your definition of equality)

Use it!

Retrieve this with bower or npm.

Bower
bower install y-map --save
NPM
npm install y-map --save

Y.Map

Y.Map mimics the behaviour of a javascript Object. You can create, update, and remove properies on this type. Furthermore, you can observe changes on this type as you can observe changes on Javascript Objects with Object.observe - an ECMAScript 7 proposal which is likely to become accepted by the committee. Until then, we have our own implementation.

Reference
  • .get(key)
    • Retrieve the value for key
  • .set(key, value)
    • Set/update a property
    • You can also insert a type map.set(key, Y.Map)
    • If not a shared type, the value should fulfill the following property: value equals JSON.parse(JSON.stringify(value)) (according to your notion of equality)
  • .delete(key)
    • Delete a property
  • .keys()
    • Returns all keys for all values
  • .observe(observer)
    • The observer is called whenever something on this object changes. Throws add, update, and delete events
    • The event object has the following properties:
      • event.type The type of the event. "add" - a new key-value pair was added, "update" - an existing key-value pair was changed, or "delete" - a key-value pair was deleted)
      • event.name The key of the changed property
      • event.value If event type is either "update" or "add", this property defines the new value of the key-value pair
      • event.object The object on which the event occurred (The object on which .observe(..) was called)
  • .observeDeep(function observer(event){..})
    • Same as .observe, but catches events from all children (if they support .observeDeep)
    • event.path specifies the path of the change event
  • .observePath(path, observer) deprecated
    • path is an array of property keys
    • observer is when the value under the path is found
    • observer is called when the property under path is set, deleted, or updated
    • returns a function which, if called, removes the observer from the path
  • .unobserve(f)
    • Delete an observer

A note on intention preservation

When users create/update/delete the same property concurrently, only one change will prevail. Changes on different properties do not conflict with each other.

A note on time complexities

  • .get(key)
    • O(1)
  • .set(key, value)
    • O(1)
  • .delete(key)
    • O(1)
  • Apply a delete operation from another user
    • O(1)
  • Apply an update operation from another user (set/update a property)
    • Yjs does not transform against operations that do not conflict with each other.
    • An operation conflicts with another operation if it changes the same property.
    • Overall worst case complexety: O(|conflicts|^)

Changelog

10.0.0

  • inserting & retrieving types are synchronous operations
    • I.e. `y.share.map.get('some type') // => returns a type instead of a promise
  • relies on Yjs@^12.0.0

License

Yjs is licensed under the MIT License.

[email protected]

y-map's People

Contributors

dmonad avatar istvank avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

istvank calibr druuu

y-map's Issues

Observation Order Reversed when Set Offline

Data set by User 1 whilst connected to YJS is received by User 2 in order of being set, whereas data set whilst User 1 isn't connected, then a connection is re-established is received by User 2 in reverse order

User 2 Observation when User 1 sets whilst connected
ordered map

User 2 Observation when User 1 sets whilst offline, then reconnects
unordered

Support deleting multiple keys

Hi all,
Can we apply multiple deletions in one operation ?
If I understand the library right, when we make want to make a change, we broadcast this change to other peers and they will resolve the conflicts. If we can support this, we can send multiple ops in one message, this will give better performance.
Thanks.

How to Delete a key locally?

When using IPFS I want to unsubscribe the topic and .delete(key) locally. Not delete the key across the network.

How do I Achieve this?

Means to serialize or synchronize JSON data with y-map

I'm looking to keep a JSON array synchronized across clients using YJS and y-map fits the bill. However, in my application I would would prefer to keep the array type which the client directly manipulates and reads from as JSON since it is easier to plug in to other libraries.

From my understanding currently there is no way to 'synchronize' the y-map with a JSON array, is that correct? If so would something that does that be in the scope of this data type (i.e. a serialize/deserialize) or would a separate library be a better place?

get promise returns undefined

In my y-map array observer, let's call it observeMap(event), when calling event.object.get(event.name).then(function(map)) {...}, the value of map is undefined in the newest release. ๐Ÿ˜ข

dynamically set a nested property?

First off, thanks for making and maintaining this project, it seems great!

Given the following:

const Y = require('yjs')
newDoc = new Y.Doc()

doc = newDoc.getMap('patches') 

doc.set('patch', 
  {"people": 
    {"michael": 
      { "lastname": "smith",
        "height": "tall"
      },
    "sal": 
      { "lastname": "orange",
        "height": "small"
      }
    }
  })

In vanilla js, to dynamically update a property I could do:

let name = 'sal'
let prop = 'height'
people[name][height] = 'short'

How would I do this same operation in Yjs? I know that issue #11is similar, but does not discuss dealing with nested and dynamic properties.

Document observer event

Hey, thanks for the great work! However, I cannot find in your documentation how the event looks like that the observer gets. E.g., it was hard work to figure out that I can get the value by event.object.get(event.name, ...). And after implementing it like this, I found it out the hard way that it also works with event.value. ๐Ÿ’ƒ

event.value is undefined when you add a type to a YMap

The event fired when you add a new key to a YMap has a value key when you add a primitive value, but has undefined value when you add a Y type. Here are two tests to illustrate. The first one passes and the second one fails. I'm not sure what the intended behaviour is, but maybe this is an issue which is easily fixed (In the event handler, I just do event.value = event.object.get(event.name).

There's also a similar issue in YArray.toArray() where Y primitives appear as expected, but Y types show as undefined.

      it('event has correct value when setting a primitive on a YMap', async(function * (done) {
        var event
        yield flushAll()
        y1.observe(function (e) {
          event = e // just put it on event, should be thrown synchronously anyway
        })
        yield y1.set('stuff', 2)
        expect(event.value).toEqual(event.object.get(event.name))
        // delete
        y1.delete('stuff')
        done()
      }))
      it('event has correct value when setting a type on a YMap', async(function * (done) {
        var event
        yield flushAll()
        y1.observe(function (e) {
          event = e // just put it on event, should be thrown synchronously anyway
        })
        yield y1.set('stuff', Y.Map)
        expect(event.value).toEqual(event.object.get(event.name))
        // delete
        y1.delete('stuff')
        done()
      }))

.keys() shows non-existent keys after reload

When using websockets, after a reconnect the deleted nodes are still shown as entries when calling .keys().

Reproduce:

  1. Create several entries (custom types).
  2. Remove them
  3. Reload
  4. Entries still exist

Solution: Probably just check if ops are deleted before putting them in .opContents/.contents.

Detailed Error Info on Promises

When getting a store that doesn't exist, the promise throws a console error.
Being told why the error occurred may be useful as it will help with scenarios such as if doesn't exist, create.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.