Closed
Bug 898394
Opened 11 years ago
Closed 11 years ago
Implement an Object Emitter
Categories
(DevTools :: General, defect)
Tracking
(Not tracked)
RESOLVED
FIXED
Firefox 25
People
(Reporter: paul, Assigned: paul)
References
Details
Attachments
(2 files, 2 obsolete files)
20.80 KB,
patch
|
vporof
:
review+
|
Details | Diff | Splinter Review |
7.24 KB,
patch
|
Details | Diff | Splinter Review |
The idea is to have a JSON like object that would emit a "set" event every time one of its property is modified, like a data store. This is useful for the App Manager, where we will be holding a JSON like object that will contain data about all the connected devices. We want any service to be able to use this object and be notified in case of changes.
Comment 1•11 years ago
|
||
DUP of bug 800355 ? (Object.observe will be the standard way to do what you want) Meanwhile, you use Object.watch [1] or proxies [2] I think. [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/watch [2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
Comment 2•11 years ago
|
||
(In reply to David Bruant from comment #1) Strongly agree with using Object.observe or Object.watch.
Assignee | ||
Comment 3•11 years ago
|
||
Proxy. Based on Benvie's code. Test are coming (I hit a clobber in the middle of my work).
Comment 4•11 years ago
|
||
Nit: function isObject(value) { let type = typeof value; return type == "object" ? value !== null : type == "function"; } can be replaced by: function isObject(value) { return Object(value) === value; }
Assignee | ||
Comment 5•11 years ago
|
||
with basic tests.
Attachment #781672 -
Attachment is obsolete: true
Attachment #781672 -
Flags: review?(vporof)
Attachment #781687 -
Flags: review?(vporof)
Comment 6•11 years ago
|
||
Comment on attachment 781687 [details] [diff] [review] patch v0.2 Review of attachment 781687 [details] [diff] [review]: ----------------------------------------------------------------- r+ with the comments below addressed. ::: browser/devtools/shared/event-emitter.js @@ +190,5 @@ > + return [this._wrappers.get(value), this._paths.get(value)]; > + }, > + get: function(target, key) { > + let value = target[key]; > + let [wrapped, path] = this.wrap(target, key, value); Isn't this.wrap going to throw if you'll try to get a value that hasn't been set yet? Please add tests for "get" as well, handling both primitives and objects. @@ +199,5 @@ > + let [wrapped, path] = this.unwrap(target, key, value); > + target[key] = value; > + this._emitter.emit("set", path, value); > + }, > + getOwnPropertyDescriptor: function(target, key) { Please test this as well. @@ +217,5 @@ > + } > + } > + return desc; > + }, > + defineProperty: function(target, key, desc) { Ditto. @@ +231,5 @@ > + [desc.set] = this.unwrap(target, "set "+key, desc.set); > + } > + Object.defineProperty(target, desc); > + } > + } You should also handle |enumerate| and |iterate| if you want to avoid getting wrapped values in those cases. Do you want to? What do you think?
Attachment #781687 -
Flags: review?(vporof) → review+
Assignee | ||
Comment 7•11 years ago
|
||
Thanks for the review. I'll address these comments soon.
> You should also handle |enumerate| and |iterate| if you want to avoid getting
> wrapped values in those cases. Do you want to? What do you think?
I might add that.
Comment 8•11 years ago
|
||
(In reply to Victor Porof [:vp] from comment #2) > (In reply to David Bruant from comment #1) > > Strongly agree with using Object.observe or Object.watch. Object.observe is of course much preferable to this, but implementation is probably far off since no one has even begun work on it and it seems like it'd take a lot of work to implement.
Comment 9•11 years ago
|
||
(In reply to Brandon Benvie [:bbenvie] from comment #8) > (In reply to Victor Porof [:vp] from comment #2) > > (In reply to David Bruant from comment #1) > > > > Strongly agree with using Object.observe or Object.watch. > > Object.observe is of course much preferable to this, but implementation is > probably far off since no one has even begun work on it and it seems like > it'd take a lot of work to implement. Agreed.
Comment 10•11 years ago
|
||
(In reply to Brandon Benvie [:bbenvie] from comment #8) > (In reply to Victor Porof [:vp] from comment #2) > > (In reply to David Bruant from comment #1) > > > > Strongly agree with using Object.observe or Object.watch. > > Object.observe is of course much preferable to this, but implementation is > probably far off since no one has even begun work on it and it seems like > it'd take a lot of work to implement. but maybe not to polyfill using proxies [1][2]. I wonder how far away the polyfill is from the latest spec draft. Tests for the polyfill could feed the standard tests and the native implementation tests. Your code wouldn't need to change the day the standard version comes along (unless major changes to the spec happens, but this seems unlikely) [1] https://mail.mozilla.org/pipermail/es-discuss/2012-December/026772.html [2] https://github.com/tvcutsem/harmony-reflect/blob/master/examples/observer.js
Comment 11•11 years ago
|
||
(In reply to David Bruant from comment #10) > (In reply to Brandon Benvie [:bbenvie] from comment #8) > > (In reply to Victor Porof [:vp] from comment #2) > > > (In reply to David Bruant from comment #1) > > > > > > Strongly agree with using Object.observe or Object.watch. > > > > Object.observe is of course much preferable to this, but implementation is > > probably far off since no one has even begun work on it and it seems like > > it'd take a lot of work to implement. > but maybe not to polyfill using proxies [1][2]. I wonder how far away the > polyfill is from the latest spec draft. > Tests for the polyfill could feed the standard tests and the native > implementation tests. Your code wouldn't need to change the day the standard > version comes along (unless major changes to the spec happens, but this > seems unlikely) I was thinking about that as well. I think you could probably implement the whole spec using proxies. Hell, you could pretty nearly implement the whole spec using dirty checking. It seems like it might be worth doing.
Assignee | ||
Comment 12•11 years ago
|
||
. Renamed ObjectEmitter to ObservableObject . Moved in its own file . Better test coverage . Fixed defineProperty
Assignee | ||
Comment 13•11 years ago
|
||
Fixed the way event-emitter.js is imported.
Attachment #783029 -
Attachment is obsolete: true
Assignee | ||
Comment 14•11 years ago
|
||
https://tbpl.mozilla.org/?tree=Try&rev=ed12defccb13
Assignee | ||
Updated•11 years ago
|
Whiteboard: [land-in-fx-team]
Assignee | ||
Comment 15•11 years ago
|
||
https://hg.mozilla.org/integration/fx-team/rev/8ead44298a4c
Whiteboard: [land-in-fx-team] → [fixed-in-fx-team]
Comment 16•11 years ago
|
||
https://hg.mozilla.org/mozilla-central/rev/8ead44298a4c
Status: ASSIGNED → RESOLVED
Closed: 11 years ago
Flags: in-testsuite+
Resolution: --- → FIXED
Whiteboard: [fixed-in-fx-team]
Target Milestone: --- → Firefox 25
Updated•6 years ago
|
Product: Firefox → DevTools
You need to log in
before you can comment on or make changes to this bug.
Description
•