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

Update rx.angular.js to use/work with RxJS 5 beta 8 #146

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = function (grunt) {
'src/headers/license.js',
'src/headers/intro.js',
'src/internal/trycatch.js',
'src/internal/util.js',
'src/module.js',
'src/factory.js',
'src/observeonscope.js',
Expand All @@ -53,6 +54,7 @@ module.exports = function (grunt) {
'src/headers/license.js',
'src/headers/intro.lite.js',
'src/internal/trycatch.js',
'src/internal/util.js',
'src/module.js',
'src/factory.js',
'src/observeonscope.js',
Expand All @@ -69,6 +71,7 @@ module.exports = function (grunt) {
'src/headers/license.js',
'src/headers/intro.lite.compat.js',
'src/internal/trycatch.js',
'src/internal/util.js',
'src/module.js',
'src/factory.js',
'src/observeonscope.js',
Expand Down
147 changes: 61 additions & 86 deletions dist/rx.angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ function thrower(e) {
throw e;
}

var RxNg = RxNg || {};

RxNg.inherits = function (child, parent) {
function __() { this.constructor = child; }
__.prototype = parent.prototype;
child.prototype = new __();
};

/**
* @ngdoc overview
* @name rx
Expand Down Expand Up @@ -89,53 +97,28 @@ function thrower(e) {
rxModule.factory('rx', ['$window', function($window) {
$window.Rx || ($window.Rx = Rx);

var CreateObservableFunction = (function(__super__) {
Rx.internals.inherits(CreateObservableFunction, __super__);
function CreateObservableFunction(self, name, fn) {
this._self = self;
this._name = name;
this._fn = fn;
__super__.call(this);
}

CreateObservableFunction.prototype.subscribeCore = function (o) {
var fn = this._fn;
this._self[this._name] = function () {
Rx.createObservableFunction = function (self, functionName, listener) {
var subscribeCore = function (o) {
self[functionName] = function () {
var len = arguments.length, args = new Array(len);
for (var i = 0; i < len; i++) { args[i] = arguments[i]; }

if (angular.isFunction(fn)) {
var result = tryCatch(fn).apply(this, args);
if (result === errorObj) { return o.onError(result.e); }
o.onNext(result);
if (angular.isFunction(listener)) {
var result = tryCatch(listener).apply(this, args);
if (result === errorObj) { return o.error(result.e); }
o.next(result);
} else if (args.length === 1) {
o.onNext(args[0]);
o.next(args[0]);
} else {
o.onNext(args);
o.next(args);
}
};

return new InnerDisposable(this._self, this._name);
};

function InnerDisposable(self, name) {
this._self = self;
this._name = name;
this.isDisposed = false;
}

InnerDisposable.prototype.dispose = function () {
if (!this.isDisposed) {
this.isDisposed = true;
delete this._self[this._name];
}
return function() {
delete self[functionName];
};
};

return CreateObservableFunction;
}(Rx.ObservableBase));

Rx.createObservableFunction = function (self, functionName, listener) {
return new CreateObservableFunction(self, functionName, listener).publish().refCount();
return Rx.Observable.create(subscribeCore).publish().refCount();
};

return $window.Rx;
Expand All @@ -160,7 +143,7 @@ function thrower(e) {
*/
rxModule.factory('observeOnScope', ['rx', function(rx) {
var ObserveOnScope = (function(__super__) {
rx.internals.inherits(ObserveOnScope, __super__);
RxNg.inherits(ObserveOnScope, __super__);
function ObserveOnScope(scope, expr, eq) {
this._scope = scope;
this._expr = expr;
Expand Down Expand Up @@ -209,7 +192,7 @@ function thrower(e) {
.takeWhile(function () {
return !$scope.$$destroyed;
})
.tap(
.do(
function (data){
($scope.$$phase || $scope.$root.$$phase) ?
onNext(data) :
Expand Down Expand Up @@ -267,14 +250,14 @@ function thrower(e) {
return rx.Observable.create(function (observer) {
// Create function to handle old and new Value
function listener (newValue, oldValue) {
observer.onNext({ oldValue: oldValue, newValue: newValue });
observer.next({ oldValue: oldValue, newValue: newValue });
}

// Returns function which disconnects the $watch expression
var disposable = rx.Disposable.create(scope.$watch(watchExpression, listener, objectEquality));
var disposable = new rx.Subscription(scope.$watch(watchExpression, listener, objectEquality));

scope.$on('$destroy', function(){
disposable.dispose();
disposable.unsubscribe();
});

return disposable;
Expand Down Expand Up @@ -319,7 +302,7 @@ function thrower(e) {
}

// Returns function which disconnects the $watch expression
var disposable = rx.Disposable.create(scope.$watchCollection(watchExpression, listener));
var disposable = new rx.Subscription(scope.$watchCollection(watchExpression, listener));

scope.$on('$destroy', function(){
disposable.dispose();
Expand Down Expand Up @@ -367,7 +350,7 @@ function thrower(e) {
}

// Returns function which disconnects the $watch expression
var disposable = rx.Disposable.create(scope.$watchGroup(watchExpressions, listener));
var disposable = new rx.Subscription(scope.$watchGroup(watchExpressions, listener));

scope.$on('$destroy', function(){
disposable.dispose();
Expand Down Expand Up @@ -415,17 +398,17 @@ function thrower(e) {
for (var i = 0; i < len; i++) { args[i] = arguments[i]; }
if (angular.isFunction(selector)) {
var result = tryCatch(selector).apply(null, args);
if (result === errorObj) { return observer.onError(result.e); }
observer.onNext(result);
if (result === errorObj) { return observer.error(result.e); }
observer.next(result);
} else if (args.length === 1) {
observer.onNext(args[0]);
observer.next(args[0]);
} else {
observer.onNext(args);
observer.next(args);
}
}

// Returns function which disconnects from the event binding
var disposable = rx.Disposable.create(scope.$on(eventName, listener));
var disposable = new rx.Subscription(scope.$on(eventName, listener));

scope.$on('$destroy', function(){ disposable.dispose(); });

Expand All @@ -448,7 +431,7 @@ function thrower(e) {
* @name rx.$rootScope.$createObservableFunction
*
* @description
* Provides a method to create obsersables from functions.
* Provides a method to create observables from functions.
*/
'$createObservableFunction': {
/**
Expand Down Expand Up @@ -495,7 +478,10 @@ function thrower(e) {
'$digestObservables': {
value: function(observables) {
var scope = this;
return rx.Observable.pairs(observables)
var keyValuePairs = Object.keys(observables).map(function(key) {
return [key, observables[key]]
});
return rx.Observable.from(keyValuePairs)
.flatMap(function(pair) {
return pair[1].digest(scope, pair[0])
.map(function(val) {
Expand Down Expand Up @@ -526,33 +512,8 @@ function thrower(e) {

rxModule.run(['$parse', function($parse) {

var DigestObservable = (function(__super__) {
Rx.internals.inherits(DigestObservable, __super__);
function DigestObservable(source, $scope, prop) {
this.source = source;
this.$scope = $scope;
this.prop = prop;
__super__.call(this);
}

DigestObservable.prototype.subscribeCore = function (o) {
var propSetter = $parse(this.prop).assign;
if (!propSetter) {
return o.onError(new Error('Property or expression is not assignable.'));
}

var m = new Rx.SingleAssignmentDisposable();
m.setDisposable(this.source.subscribe(new DigestObserver(o, this.$scope, propSetter)));
this.$scope.$on('$destroy', function () { m.dispose(); });

return m;
};

return DigestObservable;
}(Rx.ObservableBase));

var DigestObserver = (function(__super__) {
Rx.internals.inherits(DigestObserver, __super__);
RxNg.inherits(DigestObserver, __super__);
function DigestObserver(o, $scope, propSetter) {
this.o = o;
this.$scope = $scope;
Expand All @@ -569,16 +530,30 @@ function thrower(e) {
} else {
this.propSetter(this.$scope, x);
}
this.o.onNext(x);
this.o.next(x);
};
DigestObserver.prototype.error = function (e) { this.o.onError(e); };
DigestObserver.prototype.completed = function () { this.o.onCompleted(); };
DigestObserver.prototype.error = function (e) { this.o.error(e); };
DigestObserver.prototype.completed = function () { this.o.completed(); };

return DigestObserver;
}(Rx.internals.AbstractObserver));
}(Rx.Subscriber));

Rx.Observable.prototype.digest = function($scope, prop) {
return new DigestObservable(this, $scope, prop);
var self = this;

var subscribeCore = function (o) {
var propSetter = $parse(prop).assign;
if (!propSetter) {
return o.error(new Error('Property or expression is not assignable.'));
}

var m = self.subscribe(new DigestObserver(o, $scope, propSetter));
$scope.$on('$destroy', function () { m.unsubscribe(); });

return m;
};

return Rx.Observable.create(subscribeCore);
};
}]);

Expand All @@ -588,7 +563,7 @@ function thrower(e) {
__super__.call(this);
}

Rx.internals.inherits(ScopeScheduler, __super__);
RxNg.inherits(ScopeScheduler, __super__);

ScopeScheduler.prototype.schedule = function (state, action) {
if (this.$scope.$$destroyed) { return Rx.Disposable.empty; }
Expand Down Expand Up @@ -627,7 +602,7 @@ function thrower(e) {

return new Rx.BinaryDisposable(
sad,
Rx.Disposable.create(function () { clearTimeout(id); })
new Rx.Subscription(function () { clearTimeout(id); })
);
};

Expand All @@ -649,7 +624,7 @@ function thrower(e) {
}
}, period);

return Rx.Disposable.create(function () { clearInterval(id); });
return new Rx.Subscription(function () { clearInterval(id); });
};

return ScopeScheduler;
Expand Down
Loading