Skip to content

Instantly share code, notes, and snippets.

@WouterSpaak
Created November 9, 2018 13:46
Show Gist options
  • Save WouterSpaak/2fb26c5b5f9e21c5c387a250f2a3b4bf to your computer and use it in GitHub Desktop.
Save WouterSpaak/2fb26c5b5f9e21c5c387a250f2a3b4bf to your computer and use it in GitHub Desktop.
// Creating an internal subject that will recieve values from
// Angular calling the shadowed method with new values.
const subject = new Subject<any>();
// We need a super secret string to monkey patch a new method onto
// the target class.
const newKey = `__keyForObservableHostListener__${key}`;
Object.defineProperties(target, {
[newKey]: {
// here, we just next() the recieved values into the internal subject.
value: (val: any) => subject.next(val),
configurable: false,
enumerable: false,
writable: false
},
// And here, we're assigning our internal to the original key.
[key]: {
get() {
if (operator) {
return subject.pipe(operator);
} else {
return subject.asObservable();
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment