Skip to content

Commit

Permalink
fix: avoid sharing values between instances with subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlafroscia committed Feb 7, 2019
1 parent 7ab8394 commit 9aab01e
Showing 1 changed file with 5 additions and 33 deletions.
38 changes: 5 additions & 33 deletions addon/decorators/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,39 +58,11 @@ export default function subscribe(observableKey) {

const SUBSCRIPTION = Symbol();

return ({ kind, descriptor, initializer, ...rest }) => {
assert("Must decorator a `field`", kind === "field");

delete descriptor.writable;

let lastValue,
hasEmittedValue = false;
return element => {
assert("Must decorator a `field`", element.kind === "field");

return {
...rest,
kind: "method",
descriptor: {
...descriptor,
get() {
if (hasEmittedValue) {
return lastValue;
}

if (initializer) {
return initializer();
}
},
set(value) {
hasEmittedValue = true;
lastValue = value;

// Ensure the Ember KVO knows that the subscription property
// has updated
if (this.notifyPropertyChange) {
this.notifyPropertyChange(rest.key);
}
}
},
...element,
finisher: klass => {
function resetSubscription() {
// Unsubscribe from the old observable
Expand All @@ -101,7 +73,7 @@ export default function subscribe(observableKey) {
// Set up the subscription to the new observable
const observable = get(this, observableKey);
this[SUBSCRIPTION] = observable.subscribe(value => {
set(this, rest.key, value);
set(this, element.key, value);
});
}
return class extends klass {
Expand All @@ -121,7 +93,7 @@ export default function subscribe(observableKey) {
}

this[SUBSCRIPTION] = observable.subscribe(value => {
set(this, rest.key, value);
set(this, element.key, value);
});
}

Expand Down

0 comments on commit 9aab01e

Please sign in to comment.