Skip to content

Instantly share code, notes, and snippets.

@dimorphic
Created February 10, 2017 11:12
Show Gist options
  • Select an option

  • Save dimorphic/7b0fa6cab383989f7b5023d3d71efda9 to your computer and use it in GitHub Desktop.

Select an option

Save dimorphic/7b0fa6cab383989f7b5023d3d71efda9 to your computer and use it in GitHub Desktop.
once decorator
import {Subscriber} from "rxjs";
export function Once(milliseconds: number = 0) {
return function (target, key, descriptor) {
var originalMethod = descriptor.value;
descriptor.value = function (...args) {
var sub = originalMethod.apply(this, args);
setTimeout(() => {
if (sub instanceof Subscriber) {
sub.unsubscribe();
} else if (sub instanceof Function) {
sub()
} else if (sub === null) {
} else {
throw new Error('@Once did not receive something to unsubscribe from, did you forget to return an Observable maybe?');
}
}, milliseconds);
};
return descriptor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment