mirror of
https://github.com/yjs/yjs.git
synced 2025-12-25 16:09:30 +01:00
added support for Object.observe in supported browsers
This commit is contained in:
@@ -130,11 +130,12 @@ module.exports = (HB)->
|
||||
super()
|
||||
|
||||
#
|
||||
# Transform this to a Json and loose all the sharing-abilities (the new object will be a deep clone)!
|
||||
# Transform this to a Json. If your browser supports Object.observe it will be transformed automatically when a change arrives.
|
||||
# Otherwise you will loose all the sharing-abilities (the new object will be a deep clone)!
|
||||
# @return {Json}
|
||||
#
|
||||
toJson: ()->
|
||||
if not @bound_json?
|
||||
if not @bound_json? or not Object.observe?
|
||||
val = @val()
|
||||
json = {}
|
||||
for name, o of val
|
||||
@@ -149,6 +150,37 @@ module.exports = (HB)->
|
||||
else
|
||||
json[name] = o
|
||||
@bound_json = json
|
||||
if Object.observe?
|
||||
that = @
|
||||
Object.observe @bound_json, (events)->
|
||||
for event in events
|
||||
if not event.changed_by? and (event.type is "add" or event.type = "update")
|
||||
# this event is not created by Yatta.
|
||||
that.val(event.name, event.object[event.name])
|
||||
that.on 'change', (event_name, property_name, op)->
|
||||
if this is that and op.creator isnt HB.getUserId()
|
||||
notifier = Object.getNotifier(that.bound_json)
|
||||
oldVal = that.bound_json[property_name]
|
||||
if oldVal?
|
||||
notifier.performChange 'update', ()->
|
||||
that.bound_json[property_name] = that.val(property_name)
|
||||
, that.bound_json
|
||||
notifier.notify
|
||||
object: that.bound_json
|
||||
type: 'update'
|
||||
name: property_name
|
||||
oldValue: oldVal
|
||||
changed_by: op.creator
|
||||
else
|
||||
notifier.performChange 'add', ()->
|
||||
that.bound_json[property_name] = that.val(property_name)
|
||||
, that.bound_json
|
||||
notifier.notify
|
||||
object: that.bound_json
|
||||
type: 'add'
|
||||
name: property_name
|
||||
oldValue: oldVal
|
||||
changed_by: op.creator
|
||||
@bound_json
|
||||
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user