зеркало из https://github.com/mozilla/gecko-dev.git
Bug 901517 - Observable Object: use EventEmitter.decorate and fix array issue. r=poirot.alex
This commit is contained in:
Родитель
0f07087e67
Коммит
aeea62bf3a
|
@ -1,4 +1,4 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
/* this source code form is subject to the terms of the mozilla public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
|
@ -28,21 +28,24 @@
|
|||
*
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
const EventEmitter = require("devtools/shared/event-emitter");
|
||||
|
||||
function ObservableObject(object = {}) {
|
||||
EventEmitter.decorate(this);
|
||||
let handler = new Handler(this);
|
||||
this.object = new Proxy(object, handler);
|
||||
handler._wrappers.set(this.object, object);
|
||||
handler._paths.set(object, []);
|
||||
}
|
||||
|
||||
exports.ObservableObject = ObservableObject;
|
||||
module.exports = ObservableObject;
|
||||
|
||||
ObservableObject.prototype = new EventEmitter();
|
||||
|
||||
function isObject(value) {
|
||||
return Object(value) === value;
|
||||
function isObject(x) {
|
||||
if (typeof x === "object")
|
||||
return x !== null;
|
||||
return typeof x === "function";
|
||||
}
|
||||
|
||||
function Handler(emitter) {
|
||||
|
@ -71,7 +74,7 @@ Handler.prototype = {
|
|||
if (!isObject(value) || !this._wrappers.has(value)) {
|
||||
return [value, this._paths.get(target).concat(key)];
|
||||
}
|
||||
return [this._wrappers.get(value), this._paths.get(value)];
|
||||
return [this._wrappers.get(value), this._paths.get(target).concat(key)];
|
||||
},
|
||||
get: function(target, key) {
|
||||
let value = target[key];
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
function test() {
|
||||
let tmp = {};
|
||||
Cu.import("resource://gre/modules/devtools/Loader.jsm", tmp);
|
||||
let {ObservableObject} = tmp.devtools.require("devtools/shared/observable-object");
|
||||
let ObservableObject = tmp.devtools.require("devtools/shared/observable-object");
|
||||
|
||||
let rawObject = {};
|
||||
let oe = new ObservableObject(rawObject);
|
||||
|
@ -40,10 +40,15 @@ function test() {
|
|||
{type: "get", path: "foo", value: [{a:42}]},
|
||||
{type: "get", path: "foo.0", value: {a:42}},
|
||||
{type: "set", path: "foo.0.a", value: 2},
|
||||
{type: "get", path: "foo", value: [{a:2}]},
|
||||
{type: "get", path: "bar", value: {}},
|
||||
{type: "set", path: "foo.1", value: {}},
|
||||
];
|
||||
|
||||
function callback(event, path, value) {
|
||||
oe.off("get", callback);
|
||||
ok(event, "event defined");
|
||||
ok(path, "path defined");
|
||||
let e = expected[index];
|
||||
is(event, e.type, "[" + index + "] Right event received");
|
||||
is(path.join("."), e.path, "[" + index + "] Path valid");
|
||||
|
@ -51,9 +56,6 @@ function test() {
|
|||
index++;
|
||||
areObjectsSynced();
|
||||
oe.on("get", callback);
|
||||
if (index == expected.length) {
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
oe.on("set", callback);
|
||||
|
@ -72,4 +74,9 @@ function test() {
|
|||
oe.object.foo = [{a:42}];
|
||||
oe.object.foo[0].a;
|
||||
oe.object.foo[0].a = 2;
|
||||
oe.object.foo[1] = oe.object.bar;
|
||||
|
||||
is(index, expected.length, "Event count is right");
|
||||
|
||||
finish();
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче