Created
February 10, 2017 11:12
-
-
Save dimorphic/7b0fa6cab383989f7b5023d3d71efda9 to your computer and use it in GitHub Desktop.
once decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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