Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

KDData: Proxy based realtime update object for kd.js #176

Merged
merged 22 commits into from
Aug 8, 2017
Merged

KDData: Proxy based realtime update object for kd.js #176

merged 22 commits into from
Aug 8, 2017

Conversation

gokmen
Copy link
Contributor

@gokmen gokmen commented Aug 4, 2017

KDData returns a Proxy object wrapped with KDEventEmitter which has get and set traps for sending update events for changed fields. If used as a data to any KDView it can work fine with realtime updates and pistachio templates.

A simple time example would be like;

  // Create a new kd.Data with initial data
  var date = new kd.Data({ now: new Date() })

  // Create a view pistachio and the data created above
  var main = new kd.View({ pistachio: "Now it's {strong{ #(now) }}, bye." }, date)

  // Update the data every second
  kd.utils.repeat(1000, () => date.now = new Date())

  main.appendToDomBody()

To make things simpler it takes only one argument which is the data itself, that can be an Array or an Object.

Todo

  • Handle Proxy requirement for not supported browsers
  • Update KDData.getEmitter to return correct emitter if it was used to wrap a KDObject

@gokmen gokmen requested review from sinan and usirin August 4, 2017 07:27
registerKDObjectInstance: -> KD.registerInstance @
setDataAt: (path, value) ->
@data = data
return @data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no data at this point, i am guessing this is a copy & paste issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgot to remove that, good catch 👍

this.__proxy__ = new Proxy data, proxyHandler this
this.__proxy__[key] = val for key, val of this when key isnt 'constructor'

return this.__proxy__
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first time i've seen something else being returned from a constructor, so not sure if this is gonna cause any problems. For example will the returned object from new kd.Data be extended from kd.EventEmitter?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am guessing the answer is yes, because of the line base.emit 'update', [ path ]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit trivial there, when you create a new KDData instance with provided data you're actually getting a Proxy object which has KDEventEmitter functionality on it. Which provides us to use it as a native data object to use with in KDViews.

@@ -0,0 +1,39 @@
KDEventEmitter = require './eventemitter'

module.exports = class KDData extends KDEventEmitter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to have more docs, and possibly tests covering this class, because I don't even understand the need of a Proxy object here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proxy object is required to achieve observe functionality over KDData I don't want to introduce specific setters to use on a KDData instance, instead we can use these instances as a native objects/arrays.

Copy link
Member

@sinan sinan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

setOptions:(@options = {})->
setOption: (option, value) ->
@options[option] = value
return @options
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return value changes here, we must make sure this doesn't break anything

Copy link
Member

@usirin usirin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@gokmen gokmen removed the wip label Aug 7, 2017
Copy link
Member

@sinan sinan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@gokmen gokmen merged commit 2ad8993 into master Aug 8, 2017
@gokmen gokmen deleted the kddata branch August 8, 2017 00:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants