Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Commit b64d596

Browse files
Initial commit for Issue #541
1 parent 7bdc7ad commit b64d596

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Creates an Observable sequence from changes to an array using Array.observe.
3+
* @param {Array} array An array to observe changes.
4+
* @returns {Observable} An observable sequence containing changes to an array from Array.observe.
5+
*/
6+
Observable.ofArrayChanges = function(array) {
7+
if (!Array.isArray(array)) { throw new TypeError('Array.observe only accepts arrays.'); }
8+
if (typeof Array.observe !== 'function' && typeof Array.unobserve !== 'function') { throw new TypeError('Array.observe is not supported on your platform') }
9+
return new AnonymousObservable(function(observer) {
10+
function observerFn(changes) {
11+
for(var i = 0, len = changes.length; i < len; i++) {
12+
observer.onNext(changes[i]);
13+
}
14+
}
15+
16+
Array.observe(array, observerFn);
17+
18+
return function () {
19+
Array.unobserve(array, observerFn);
20+
};
21+
});
22+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Creates an Observable sequence from changes to an object using Object.observe.
3+
* @param {Object} obj An object to observe changes.
4+
* @returns {Observable} An observable sequence containing changes to an object from Object.observe.
5+
*/
6+
Observable.ofObjectChanges = function(obj) {
7+
if (obj == null) { throw new TypeError('object must not be null or undefined.'); }
8+
if (typeof Object.observe !== 'function' && typeof Object.unobserve !== 'function') { throw new TypeError('Array.observe is not supported on your platform') }
9+
return new AnonymousObservable(function(observer) {
10+
function observerFn(changes) {
11+
for(var i = 0, len = changes.length; i < len; i++) {
12+
observer.onNext(changes[i]);
13+
}
14+
}
15+
16+
Object.observe(obj, observerFn);
17+
18+
return function () {
19+
Object.unobserve(obj, observerFn);
20+
};
21+
});
22+
};

0 commit comments

Comments
 (0)