Skip to content

Commit

Permalink
slightly rework Subscription state
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Dec 5, 2021
1 parent bbddc65 commit 2e73413
Showing 1 changed file with 49 additions and 47 deletions.
96 changes: 49 additions & 47 deletions packages/core-js/modules/esnext.observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,47 +32,49 @@ var getSubscriptionInternalState = getterFor(SUBSCRIPTION);
var getSubscriptionObserverInternalState = getterFor(SUBSCRIPTION_OBSERVER);
var Array = global.Array;

var cleanupSubscription = function (subscriptionState) {
var cleanup = subscriptionState.cleanup;
if (cleanup) {
subscriptionState.cleanup = undefined;
try {
cleanup();
} catch (error) {
hostReportErrors(error);
}
}
};

var subscriptionClosed = function (subscriptionState) {
return subscriptionState.observer === undefined;
var SubscriptionState = function (observer) {
this.observer = anObject(observer);
this.cleanup = undefined;
this.subscriptionObserver = undefined;
};

var close = function (subscriptionState) {
var subscription = subscriptionState.facade;
if (!DESCRIPTORS) {
subscription.closed = true;
var subscriptionObserver = subscriptionState.subscriptionObserver;
if (subscriptionObserver) subscriptionObserver.closed = true;
} subscriptionState.observer = undefined;
SubscriptionState.prototype = {
type: SUBSCRIPTION,
clean: function () {
var cleanup = this.cleanup;
if (cleanup) {
this.cleanup = undefined;
try {
cleanup();
} catch (error) {
hostReportErrors(error);
}
}
},
close: function () {
if (!DESCRIPTORS) {
var subscription = this.facade;
var subscriptionObserver = this.subscriptionObserver;
subscription.closed = true;
if (subscriptionObserver) subscriptionObserver.closed = true;
} this.observer = undefined;
},
isClosed: function () {
return this.observer === undefined;
}
};

var Subscription = function (observer, subscriber) {
var subscriptionState = setInternalState(this, {
type: SUBSCRIPTION,
cleanup: undefined,
observer: anObject(observer),
subscriptionObserver: undefined
});
var subscriptionState = setInternalState(this, new SubscriptionState(observer));
var start;
if (!DESCRIPTORS) this.closed = false;
try {
if (start = getMethod(observer, 'start')) call(start, observer, this);
} catch (error) {
hostReportErrors(error);
}
if (subscriptionClosed(subscriptionState)) return;
var subscriptionObserver = subscriptionState.subscriptionObserver = new SubscriptionObserver(this);
if (subscriptionState.isClosed()) return;
var subscriptionObserver = subscriptionState.subscriptionObserver = new SubscriptionObserver(subscriptionState);
try {
var cleanup = subscriber(subscriptionObserver);
var subscription = cleanup;
Expand All @@ -82,38 +84,38 @@ var Subscription = function (observer, subscriber) {
} catch (error) {
subscriptionObserver.error(error);
return;
} if (subscriptionClosed(subscriptionState)) cleanupSubscription(subscriptionState);
} if (subscriptionState.isClosed()) subscriptionState.clean();
};

Subscription.prototype = redefineAll({}, {
unsubscribe: function unsubscribe() {
var subscriptionState = getSubscriptionInternalState(this);
if (!subscriptionClosed(subscriptionState)) {
close(subscriptionState);
cleanupSubscription(subscriptionState);
if (!subscriptionState.isClosed()) {
subscriptionState.close();
subscriptionState.clean();
}
}
});

if (DESCRIPTORS) defineProperty(Subscription.prototype, 'closed', {
configurable: true,
get: function () {
return subscriptionClosed(getSubscriptionInternalState(this));
return getSubscriptionInternalState(this).isClosed();
}
});

var SubscriptionObserver = function (subscription) {
var SubscriptionObserver = function (subscriptionState) {
setInternalState(this, {
type: SUBSCRIPTION_OBSERVER,
subscription: subscription
subscriptionState: subscriptionState
});
if (!DESCRIPTORS) this.closed = false;
};

SubscriptionObserver.prototype = redefineAll({}, {
next: function next(value) {
var subscriptionState = getSubscriptionInternalState(getSubscriptionObserverInternalState(this).subscription);
if (!subscriptionClosed(subscriptionState)) {
var subscriptionState = getSubscriptionObserverInternalState(this).subscriptionState;
if (!subscriptionState.isClosed()) {
var observer = subscriptionState.observer;
try {
var nextMethod = getMethod(observer, 'next');
Expand All @@ -124,38 +126,38 @@ SubscriptionObserver.prototype = redefineAll({}, {
}
},
error: function error(value) {
var subscriptionState = getSubscriptionInternalState(getSubscriptionObserverInternalState(this).subscription);
if (!subscriptionClosed(subscriptionState)) {
var subscriptionState = getSubscriptionObserverInternalState(this).subscriptionState;
if (!subscriptionState.isClosed()) {
var observer = subscriptionState.observer;
close(subscriptionState);
subscriptionState.close();
try {
var errorMethod = getMethod(observer, 'error');
if (errorMethod) call(errorMethod, observer, value);
else hostReportErrors(value);
} catch (err) {
hostReportErrors(err);
} cleanupSubscription(subscriptionState);
} subscriptionState.clean();
}
},
complete: function complete() {
var subscriptionState = getSubscriptionInternalState(getSubscriptionObserverInternalState(this).subscription);
if (!subscriptionClosed(subscriptionState)) {
var subscriptionState = getSubscriptionObserverInternalState(this).subscriptionState;
if (!subscriptionState.isClosed()) {
var observer = subscriptionState.observer;
close(subscriptionState);
subscriptionState.close();
try {
var completeMethod = getMethod(observer, 'complete');
if (completeMethod) call(completeMethod, observer);
} catch (error) {
hostReportErrors(error);
} cleanupSubscription(subscriptionState);
} subscriptionState.clean();
}
}
});

if (DESCRIPTORS) defineProperty(SubscriptionObserver.prototype, 'closed', {
configurable: true,
get: function () {
return subscriptionClosed(getSubscriptionInternalState(getSubscriptionObserverInternalState(this).subscription));
return getSubscriptionObserverInternalState(this).subscriptionState.isClosed();
}
});

Expand Down

0 comments on commit 2e73413

Please sign in to comment.