Permalink
Browse files

Renamed property change listener interfaces

  • Loading branch information...
1 parent 0acb00d commit ec628c8e66ef4eb872a18f3d47d2facc34ba9755 @kriskowal committed Nov 7, 2012
View
61 README.md
@@ -829,29 +829,24 @@ with method calls like `array.push`. All methods of a watched array
support change dispatch. In addition, arrays have a `set` method to
make setting the value at a particular index observable.
-- **PropertyChanges.addPropertyChangeListener(object, key, listener,
- before)**
-- **PropertyChanges.removePropertyChangeListener(object, key,
- listener, before)**
-- **PropertyChanges.dispatchPropertyChange(object, key, value,
- before)**
-- **PropertyChanges.addBeforePropertyChangeListener(object, key,
- listener)**
-- **PropertyChanges.removeBeforePropertyChangeListener(object, key,
- listener)**
-- **PropertyChanges.dispatchBeforePropertyChange(object, key, value)**
-- **PropertyChanges.getPropertyChangeDescriptor(object, key)**
+- PropertyChanges.**add**OwnPropertyChange**Listener**(object, key, listener, before)
+- PropertyChanges.**remove**OwnPropertyChange**Listener**(object, key, listener, before)
+- PropertyChanges.**dispatch**OwnPropertyChange(object, key, value, before)
+- PropertyChanges.add**Before**OwnPropertyChangeListener(object, key, listener)
+- PropertyChanges.remove**Before**OwnPropertyChangeListener(object, key, listener)
+- PropertyChanges.dispatch**Before**OwnPropertyChange(object, key, value)
+- PropertyChanges.**get**OwnPropertyChange**Descriptor**(object, key)
All of these functions delegate to methods of the same name if one
exists on the object.
-- **object.addPropertyChangeListener(key, listener, before)**
-- **object.removePropertyChangeListener(key, listener, before)**
-- **object.dispatchPropertyChange(key, value)**
-- **object.addBeforePropertyChangeListener(key, listener)**
-- **object.removeBeforePropertyChangeListener(key, listener)**
-- **object.dispatchBeforePropertyChange(key, value)**
-- **object.getPropertyChangeDescriptor(key)**
+- object.**add**OwnPropertyChange**Listener**(key, listener, before)
+- object.**remove**OwnPropertyChange**Listener**(key, listener, before)
+- object.**dispatch**OwnPropertyChange(key, value, before)
+- object.add**Before**OwnPropertyChangeListener(key, listener)
+- object.remove**Before**OwnPropertyChangeListener(key, listener)
+- object.dispatch**Before**OwnPropertyChange(key, value)
+- object.**get**OwnPropertyChange**Descriptor**(key)
Additionally, `PropertyChanges.prototype` can be **mixed into** other
types of objects to support the property change dispatch interface. All
@@ -870,13 +865,13 @@ or updates for any item in a map data structure.
With the `listen/array-changes` module required, `Array` can also
dispatch map changes for the values at each index.
-- **collection.addMapChangeListener(listener)**
-- **collection.removeMapChangeListener(listener)**
-- **collection.dispatchMapChange(key, value)**
-- **collection.addBeforeMapChangeListener(listener)**
-- **collection.removeBeforeMapChangeListener(listener)**
-- **collection.dispatchBeforeMapChange(key, value)**
-- **collection.getMapChangeDescriptor()**
+- collection.**add**MapChangeListener(listener, before)
+- collection.**remove**MapChangeListener(listener, before)
+- collection.**dispatch**MapChange(key, value, before)
+- collection.add**Before**MapChangeListener(listener)
+- collection.remove**Before**MapChangeListener(listener)
+- collection.dispatch**Before**MapChange(key, value)
+- collection.**get**MapChange**Descriptor**()
The **listener** for a map change receives the `value`, `key`, and
collection `object` as arguments, the same pattern as a `forEach` or
@@ -896,13 +891,13 @@ A range change listener receives notifications when a range of values at
a particular position is added, removed, or replaced within an ordered
collection.
-- **collection.addRangeChangeListener(listener)**
-- **collection.removeRangeChangeListener(listener)**
-- **collection.dispatchRangeChange(plus, minus, index)**
-- **collection.addBeforeRangeChangeListener(listener)**
-- **collection.removeBeforeRangeChangeListener(listener)**
-- **collection.dispatchBeforeRangeChange(plus, minus, index)**
-- **collection.getRangeChangeDescriptor()**
+- collection.**add**RangeChange**Listener**(listener, before)
+- collection.**remove**RangeChange**Listener**(listener, before)
+- collection.**dispatch**RangeChange(plus, minus, index, before)
+- collection.add**Before**RangeChange**Listener**(listener)
+- collection.remove**Before**RangeChange**Listener**(listener)
+- collection.dispatch**Before**RangeChange(plus, minus, index)
+- collection.**get**RangeChange**Descriptor**()
The **listener** for a range change is a function that accepts `plus`,
`minus`, and `index` arguments. `plus` and `minus` are the values that
View
4 demo/observable-array-demo.js
@@ -3,11 +3,11 @@ require("../listen/array-changes");
var array = [];
-array.addPropertyChangeListener("length", function (length) {
+array.addOwnPropertyChangeListener("length", function (length) {
console.log("changed", length);
});
-array.addPropertyChangeListener(0, function (value) {
+array.addOwnPropertyChangeListener(0, function (value) {
console.log("array[0] changed to", value);
});
View
24 listen/array-changes.js
@@ -63,7 +63,7 @@ var observableArrayProperties = {
// dispatch before change events
this.dispatchBeforeRangeChange(this, this, 0);
for (var i = 0; i < this.length; i++) {
- PropertyChanges.dispatchBeforePropertyChange(this, i, this[i]);
+ PropertyChanges.dispatchBeforeOwnPropertyChange(this, i, this[i]);
this.dispatchBeforeMapChange(i, this[i]);
}
@@ -72,7 +72,7 @@ var observableArrayProperties = {
// dispatch after change events
for (var i = 0; i < this.length; i++) {
- PropertyChanges.dispatchPropertyChange(this, i, this[i]);
+ PropertyChanges.dispatchOwnPropertyChange(this, i, this[i]);
this.dispatchMapChange(i, this[i]);
}
this.dispatchRangeChange(this, this, 0);
@@ -89,7 +89,7 @@ var observableArrayProperties = {
// dispatch before change events
this.dispatchBeforeRangeChange(this, this, 0);
for (var i = 0; i < this.length; i++) {
- PropertyChanges.dispatchBeforePropertyChange(this, i, this[i]);
+ PropertyChanges.dispatchBeforeOwnPropertyChange(this, i, this[i]);
this.dispatchBeforeMapChange(i, this[i]);
}
@@ -98,7 +98,7 @@ var observableArrayProperties = {
// dispatch after change events
for (var i = 0; i < this.length; i++) {
- PropertyChanges.dispatchPropertyChange(this, i, this[i]);
+ PropertyChanges.dispatchOwnPropertyChange(this, i, this[i]);
this.dispatchMapChange(i, this[i]);
}
this.dispatchRangeChange(this, this, 0);
@@ -120,20 +120,20 @@ var observableArrayProperties = {
// dispatch before change events
if (diff) {
- PropertyChanges.dispatchBeforePropertyChange(this, "length", this.length);
+ PropertyChanges.dispatchBeforeOwnPropertyChange(this, "length", this.length);
}
this.dispatchBeforeRangeChange(plus, minus, start);
if (diff === 0) { // substring replacement
for (var i = start; i < start + plus.length; i++) {
- PropertyChanges.dispatchBeforePropertyChange(this, i, this[i]);
+ PropertyChanges.dispatchBeforeOwnPropertyChange(this, i, this[i]);
this.dispatchBeforeMapChange(i, this[i]);
}
- } else if (PropertyChanges.hasPropertyChangeDescriptor(this)) {
+ } else if (PropertyChanges.hasOwnPropertyChangeDescriptor(this)) {
// all subsequent values changed or shifted.
// avoid (longest - start) long walks if there are no
// registered descriptors.
for (var i = start; i < longest; i++) {
- PropertyChanges.dispatchBeforePropertyChange(this, i, this[i]);
+ PropertyChanges.dispatchBeforeOwnPropertyChange(this, i, this[i]);
this.dispatchBeforeMapChange(i, this[i]);
}
}
@@ -144,21 +144,21 @@ var observableArrayProperties = {
// dispatch after change events
if (diff === 0) { // substring replacement
for (var i = start; i < start + plus.length; i++) {
- PropertyChanges.dispatchPropertyChange(this, i, this[i]);
+ PropertyChanges.dispatchOwnPropertyChange(this, i, this[i]);
this.dispatchMapChange(i, this[i]);
}
- } else if (PropertyChanges.hasPropertyChangeDescriptor(this)) {
+ } else if (PropertyChanges.hasOwnPropertyChangeDescriptor(this)) {
// all subsequent values changed or shifted.
// avoid (longest - start) long walks if there are no
// registered descriptors.
for (var i = start; i < longest; i++) {
- PropertyChanges.dispatchPropertyChange(this, i, this[i]);
+ PropertyChanges.dispatchOwnPropertyChange(this, i, this[i]);
this.dispatchMapChange(i, this[i]);
}
}
this.dispatchRangeChange(plus, minus, start);
if (diff) {
- PropertyChanges.dispatchPropertyChange(this, "length", this.length);
+ PropertyChanges.dispatchOwnPropertyChange(this, "length", this.length);
}
return result;
View
90 listen/property-changes.js
@@ -34,7 +34,7 @@ var propertyChangeDescriptors = new WeakMap();
// book-keeping is probably not warranted since it would be rare for an
// observed object to no longer be observed unless it was about to be disposed
// of or reused as an observable. The only benefit would be in avoiding bulk
-// calls to dispatchPropertyChange events on objects that have no listeners.
+// calls to dispatchOwnPropertyChange events on objects that have no listeners.
/*
To observe shallow property changes for a particular key of a particular
@@ -60,7 +60,7 @@ function PropertyChanges() {
throw new Error("This is an abstract interface. Mix it. Don't construct it");
}
-PropertyChanges.prototype.getPropertyChangeDescriptor = function (key) {
+PropertyChanges.prototype.getOwnPropertyChangeDescriptor = function (key) {
if (!propertyChangeDescriptors.has(this)) {
propertyChangeDescriptors.set(this, {});
}
@@ -74,7 +74,7 @@ PropertyChanges.prototype.getPropertyChangeDescriptor = function (key) {
return objectPropertyChangeDescriptors[key];
};
-PropertyChanges.prototype.hasPropertyChangeDescriptor = function (key) {
+PropertyChanges.prototype.hasOwnPropertyChangeDescriptor = function (key) {
if (!propertyChangeDescriptors.has(this)) {
return false;
}
@@ -88,12 +88,12 @@ PropertyChanges.prototype.hasPropertyChangeDescriptor = function (key) {
return true;
};
-PropertyChanges.prototype.addPropertyChangeListener = function (key, listener, beforeChange) {
+PropertyChanges.prototype.addOwnPropertyChangeListener = function (key, listener, beforeChange) {
if (this.makeObservable && !this.isObservable) {
this.makeObservable(); // particularly for observable arrays, for
// their length property
}
- var descriptor = PropertyChanges.getPropertyChangeDescriptor(this, key);
+ var descriptor = PropertyChanges.getOwnPropertyChangeDescriptor(this, key);
var listeners;
if (beforeChange) {
listeners = descriptor.willChangeListeners;
@@ -104,12 +104,12 @@ PropertyChanges.prototype.addPropertyChangeListener = function (key, listener, b
listeners.push(listener);
};
-PropertyChanges.prototype.addBeforePropertyChangeListener = function (key, listener) {
- return PropertyChanges.addPropertyChangeListener(this, key, listener, true);
+PropertyChanges.prototype.addBeforeOwnPropertyChangeListener = function (key, listener) {
+ return PropertyChanges.addOwnPropertyChangeListener(this, key, listener, true);
};
-PropertyChanges.prototype.removePropertyChangeListener = function (key, listener, beforeChange) {
- var descriptor = PropertyChanges.getPropertyChangeDescriptor(this, key);
+PropertyChanges.prototype.removeOwnPropertyChangeListener = function (key, listener, beforeChange) {
+ var descriptor = PropertyChanges.getOwnPropertyChangeDescriptor(this, key);
var listeners;
if (beforeChange) {
@@ -129,12 +129,12 @@ PropertyChanges.prototype.removePropertyChangeListener = function (key, listener
}
};
-PropertyChanges.prototype.removeBeforePropertyChangeListener = function (key, listener) {
- return PropertyChanges.removePropertyChangeListener(this, key, listener, true);
+PropertyChanges.prototype.removeBeforeOwnPropertyChangeListener = function (key, listener) {
+ return PropertyChanges.removeOwnPropertyChangeListener(this, key, listener, true);
};
-PropertyChanges.prototype.dispatchPropertyChange = function (key, value, beforeChange) {
- var descriptor = PropertyChanges.getPropertyChangeDescriptor(this, key);
+PropertyChanges.prototype.dispatchOwnPropertyChange = function (key, value, beforeChange) {
+ var descriptor = PropertyChanges.getOwnPropertyChangeDescriptor(this, key);
var listeners;
if (beforeChange) {
@@ -164,8 +164,8 @@ PropertyChanges.prototype.dispatchPropertyChange = function (key, value, beforeC
}, this);
};
-PropertyChanges.prototype.dispatchBeforePropertyChange = function (key, listener) {
- return PropertyChanges.dispatchPropertyChange(this, key, listener, true);
+PropertyChanges.prototype.dispatchBeforeOwnPropertyChange = function (key, listener) {
+ return PropertyChanges.dispatchOwnPropertyChange(this, key, listener, true);
};
PropertyChanges.prototype.makePropertyObservable = function (key) {
@@ -245,9 +245,9 @@ PropertyChanges.prototype.makePropertyObservable = function (key) {
if (value === overriddenDescriptor.value) {
return value;
}
- PropertyChanges.dispatchBeforePropertyChange(this, key, overriddenDescriptor.value);
+ PropertyChanges.dispatchBeforeOwnPropertyChange(this, key, overriddenDescriptor.value);
overriddenDescriptor.value = value;
- PropertyChanges.dispatchPropertyChange(this, key, value);
+ PropertyChanges.dispatchOwnPropertyChange(this, key, value);
return value;
},
enumerable: overriddenDescriptor.enumerable,
@@ -270,7 +270,7 @@ PropertyChanges.prototype.makePropertyObservable = function (key) {
if (value === formerValue) {
return value;
}
- PropertyChanges.dispatchBeforePropertyChange(this, key, formerValue);
+ PropertyChanges.dispatchBeforeOwnPropertyChange(this, key, formerValue);
// call through to actual setter
if (overriddenDescriptor.set) {
overriddenDescriptor.set.apply(this, arguments)
@@ -282,7 +282,7 @@ PropertyChanges.prototype.makePropertyObservable = function (key) {
}
// dispatch the new value: the given value if there is
// no getter, or the actual value if there is one
- PropertyChanges.dispatchPropertyChange(this, key, value);
+ PropertyChanges.dispatchOwnPropertyChange(this, key, value);
return value;
},
enumerable: overriddenDescriptor.enumerable,
@@ -319,56 +319,56 @@ PropertyChanges.prototype.makePropertyUnobservable = function (key) {
// constructor functions
-PropertyChanges.getPropertyChangeDescriptor = function (object, key) {
- if (object.getPropertyChangeDescriptor) {
- return object.getPropertyChangeDescriptor(key);
+PropertyChanges.getOwnPropertyChangeDescriptor = function (object, key) {
+ if (object.getOwnPropertyChangeDescriptor) {
+ return object.getOwnPropertyChangeDescriptor(key);
} else {
- return PropertyChanges.prototype.getPropertyChangeDescriptor.call(object, key);
+ return PropertyChanges.prototype.getOwnPropertyChangeDescriptor.call(object, key);
}
};
-PropertyChanges.hasPropertyChangeDescriptor = function (object, key) {
- if (object.hasPropertyChangeDescriptor) {
- return object.hasPropertyChangeDescriptor(key);
+PropertyChanges.hasOwnPropertyChangeDescriptor = function (object, key) {
+ if (object.hasOwnPropertyChangeDescriptor) {
+ return object.hasOwnPropertyChangeDescriptor(key);
} else {
- return PropertyChanges.prototype.hasPropertyChangeDescriptor.call(object, key);
+ return PropertyChanges.prototype.hasOwnPropertyChangeDescriptor.call(object, key);
}
};
-PropertyChanges.addPropertyChangeListener = function (object, key, listener, beforeChange) {
- if (object.addPropertyChangeListener) {
- return object.addPropertyChangeListener(key, listener, beforeChange);
+PropertyChanges.addOwnPropertyChangeListener = function (object, key, listener, beforeChange) {
+ if (object.addOwnPropertyChangeListener) {
+ return object.addOwnPropertyChangeListener(key, listener, beforeChange);
} else {
- return PropertyChanges.prototype.addPropertyChangeListener.call(object, key, listener, beforeChange);
+ return PropertyChanges.prototype.addOwnPropertyChangeListener.call(object, key, listener, beforeChange);
}
};
-PropertyChanges.removePropertyChangeListener = function (object, key, listener, beforeChange) {
- if (object.removePropertyChangeListener) {
- return object.removePropertyChangeListener(key, listener, beforeChange);
+PropertyChanges.removeOwnPropertyChangeListener = function (object, key, listener, beforeChange) {
+ if (object.removeOwnPropertyChangeListener) {
+ return object.removeOwnPropertyChangeListener(key, listener, beforeChange);
} else {
- return PropertyChanges.prototype.removePropertyChangeListener.call(object, key, listener, beforeChange);
+ return PropertyChanges.prototype.removeOwnPropertyChangeListener.call(object, key, listener, beforeChange);
}
};
-PropertyChanges.dispatchPropertyChange = function (object, key, value, beforeChange) {
- if (object.dispatchPropertyChange) {
- return object.dispatchPropertyChange(key, value, beforeChange);
+PropertyChanges.dispatchOwnPropertyChange = function (object, key, value, beforeChange) {
+ if (object.dispatchOwnPropertyChange) {
+ return object.dispatchOwnPropertyChange(key, value, beforeChange);
} else {
- return PropertyChanges.prototype.dispatchPropertyChange.call(object, key, value, beforeChange);
+ return PropertyChanges.prototype.dispatchOwnPropertyChange.call(object, key, value, beforeChange);
}
};
-PropertyChanges.addBeforePropertyChangeListener = function (object, key, listener) {
- return PropertyChanges.addPropertyChangeListener(object, key, listener, true);
+PropertyChanges.addBeforeOwnPropertyChangeListener = function (object, key, listener) {
+ return PropertyChanges.addOwnPropertyChangeListener(object, key, listener, true);
};
-PropertyChanges.removeBeforePropertyChangeListener = function (object, key, listener) {
- return PropertyChanges.removePropertyChangeListener(object, key, listener, true);
+PropertyChanges.removeBeforeOwnPropertyChangeListener = function (object, key, listener) {
+ return PropertyChanges.removeOwnPropertyChangeListener(object, key, listener, true);
};
-PropertyChanges.dispatchBeforePropertyChange = function (object, key, value) {
- return PropertyChanges.dispatchPropertyChange(object, key, value, true);
+PropertyChanges.dispatchBeforeOwnPropertyChange = function (object, key, value) {
+ return PropertyChanges.dispatchOwnPropertyChange(object, key, value, true);
};
PropertyChanges.makePropertyObservable = function (object, key) {
View
12 spec/listen/array-changes-spec.js
@@ -11,11 +11,11 @@ describe("Array change dispatch", function () {
it("set up listeners", function () {
- array.addBeforePropertyChangeListener("length", function (length) {
+ array.addBeforeOwnPropertyChangeListener("length", function (length) {
spy("length change from", length);
});
- array.addPropertyChangeListener("length", function (length) {
+ array.addOwnPropertyChangeListener("length", function (length) {
spy("length change to", length);
});
@@ -322,12 +322,12 @@ describe("Array change dispatch", function () {
// mute all listeners
- var descriptor = array.getPropertyChangeDescriptor('length');
+ var descriptor = array.getOwnPropertyChangeDescriptor('length');
descriptor.willChangeListeners.forEach(function (listener) {
- array.removeBeforePropertyChangeListener('length', listener);
+ array.removeBeforeOwnPropertyChangeListener('length', listener);
});
descriptor.changeListeners.forEach(function (listener) {
- array.removePropertyChangeListener('length', listener);
+ array.removeOwnPropertyChangeListener('length', listener);
});
var descriptor = array.getRangeChangeDescriptor();
@@ -379,7 +379,7 @@ describe("Array change dispatch", function () {
it("observes length changes on arrays that are not otherwised observed", function () {
var array = [1, 2, 3];
var spy = jasmine.createSpy();
- array.addPropertyChangeListener("length", spy);
+ array.addOwnPropertyChangeListener("length", spy);
array.push(4);
expect(spy).toHaveBeenCalled();
});
View
16 spec/listen/property-changes-spec.js
@@ -12,10 +12,10 @@ describe("PropertyChanges", function () {
it("observes setter on object", function () {
spy = jasmine.createSpy();
var object = {};
- PropertyChanges.addBeforePropertyChangeListener(object, 'x', function (value, key) {
+ PropertyChanges.addBeforeOwnPropertyChangeListener(object, 'x', function (value, key) {
spy('from', value, key);
});
- PropertyChanges.addPropertyChangeListener(object, 'x', function (value, key) {
+ PropertyChanges.addOwnPropertyChangeListener(object, 'x', function (value, key) {
spy('to', value, key);
});
object.x = 10;
@@ -44,10 +44,10 @@ describe("PropertyChanges", function () {
configurable: true
}
});
- PropertyChanges.addBeforePropertyChangeListener(object, 'x', function (value, key) {
+ PropertyChanges.addBeforeOwnPropertyChangeListener(object, 'x', function (value, key) {
spy('from', value, key);
});
- PropertyChanges.addPropertyChangeListener(object, 'x', function (value, key) {
+ PropertyChanges.addOwnPropertyChangeListener(object, 'x', function (value, key) {
spy('to', value, key);
});
object.x = 10;
@@ -61,10 +61,10 @@ describe("PropertyChanges", function () {
it("handles cyclic own property change listeners", function () {
var a = {};
var b = {};
- PropertyChanges.addPropertyChangeListener(a, 'foo', function (value) {
+ PropertyChanges.addOwnPropertyChangeListener(a, 'foo', function (value) {
b.bar = value;
});
- PropertyChanges.addPropertyChangeListener(b, 'bar', function (value) {
+ PropertyChanges.addOwnPropertyChangeListener(b, 'bar', function (value) {
a.foo = value;
});
a.foo = 10;
@@ -79,7 +79,7 @@ describe("PropertyChanges", function () {
}
};
spyOn(object, "handlePropertyChange").andCallThrough();
- PropertyChanges.addPropertyChangeListener(object, "foo", object);
+ PropertyChanges.addOwnPropertyChangeListener(object, "foo", object);
object.foo = 10;
expect(object.handlePropertyChange).toHaveBeenCalled();
});
@@ -91,7 +91,7 @@ describe("PropertyChanges", function () {
}
};
spyOn(object, "handleFooChange").andCallThrough();
- PropertyChanges.addPropertyChangeListener(object, "foo", object);
+ PropertyChanges.addOwnPropertyChangeListener(object, "foo", object);
object.foo = 10;
expect(object.handleFooChange).toHaveBeenCalled();
});

0 comments on commit ec628c8

Please sign in to comment.