Skip to content

Commit

Permalink
refactor(Subscription): remove to use tryCatch
Browse files Browse the repository at this point in the history
This reverts commit 58817feaa485a976c1f6fa7a3f88213c22298c2e.
  • Loading branch information
tetsuharuohzeki committed Mar 18, 2016
1 parent a13b248 commit 53630c6
Showing 1 changed file with 40 additions and 23 deletions.
63 changes: 40 additions & 23 deletions src/Subscription.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {isArray} from './util/isArray';
import {isObject} from './util/isObject';
import {isFunction} from './util/isFunction';
import {tryCatch} from './util/tryCatch';
import {errorObject} from './util/errorObject';

export class Subscription {
public static EMPTY: Subscription = (function(empty: any){
Expand All @@ -26,19 +24,23 @@ export class Subscription {
}
this.isUnsubscribed = true;

let hasErrors = false;
let errors: any[] = [];
const result: {
hasErrors: boolean;
errors: any[];
} = {
hasErrors: false,
errors: [],
};

const { _unsubscribe, _subscriptions } = (<any> this);
const { _unsubscribe, _subscriptions }: {
_unsubscribe: () => void;
_subscriptions: Subscription[];
} = (<any> this);

(<any> this)._subscriptions = null;

if (isFunction(_unsubscribe)) {
let trial = tryCatch(_unsubscribe).call(this);
if (trial === errorObject) {
hasErrors = true;
errors.push(errorObject.e);
}
callPrivateUnsubscribe(this, result);
}

if (isArray(_subscriptions)) {
Expand All @@ -47,24 +49,15 @@ export class Subscription {
const len = _subscriptions.length;

while (++index < len) {
const sub = _subscriptions[index];
const sub: Subscription = _subscriptions[index];
if (isObject(sub)) {
let trial = tryCatch(sub.unsubscribe).call(sub);
if (trial === errorObject) {
hasErrors = true;
let err = errorObject.e;
if (err instanceof UnsubscriptionError) {
errors = errors.concat(err.errors);
} else {
errors.push(err);
}
}
callPublicUnsubscribe(sub, result);
}
}
}

if (hasErrors) {
throw new UnsubscriptionError(errors);
if (result.hasErrors) {
throw new UnsubscriptionError(result.errors);
}
}

Expand Down Expand Up @@ -126,4 +119,28 @@ export class UnsubscriptionError extends Error {
super('unsubscriptoin error(s)');
this.name = 'UnsubscriptionError';
}
}

function callPrivateUnsubscribe(sub: Subscription, result: { hasErrors: boolean; errors: any[]; }): void {
try {
(<any>sub)._unsubscribe();
}
catch (e) {
result.hasErrors = true;
result.errors.push(e);
}
}

function callPublicUnsubscribe(sub: Subscription, result: { hasErrors: boolean; errors: any[]; }): void {
try {
sub.unsubscribe();
}
catch (e) {
result.hasErrors = true;
if (e instanceof UnsubscriptionError) {
result.errors = result.errors.concat(e);
} else {
result.errors.push(e);
}
}
}

0 comments on commit 53630c6

Please sign in to comment.