ts-schedule-decorators
Version:
Schedule decorator for TypeScript
137 lines (114 loc) • 3.98 kB
Markdown
[](https://travis-ci.org/Boulangerie/ts-schedule-decorators)
[](https://coveralls.io/github/Boulangerie/ts-schedule-decorators)
[](https://www.npmjs.org/package/ts-schedule-decorators)
[](http://npm-stat.com/charts.html?package=ts-schedule-decorators&from=2016-01-09)
[](https://david-dm.org/Boulangerie/ts-schedule-decorators)
[](https://david-dm.org/Boulangerie/ts-schedule-decorators)
[](https://www.npmjs.org/package/ts-schedule-decorators)
The schedule decorators library provides a simple, ES5+ compatible, lightweight and universal decorator **to easily make recurrent function calls**.
The decorator uses the standard [window.setInterval()](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval) function internally, makes the code clearer and avoid code duplication.
The easiest way is to install `ts-schedule-decorators` as `dependency`:
```sh
npm install ts-schedule-decorators --save
```
Instance methods will be executed after instantiation.
```ts
@Schedulable()
class Cat {
@Interval(1000)
public meow() {
console.log('Meeeooow')
}
}
const garfield = new Cat()
// Displays:
// 2017-01-09 12:12:35 Meeeooow
// 2017-01-09 12:12:36 Meeeooow
// 2017-01-09 12:12:37 Meeeooow
...
```
Static methods will be executed on declaration.
```ts
@Schedulable()
class Cat {
@Interval(1000)
public static miaow() {
console.log('Miaaooow')
}
}
// Displays:
// 2017-01-09 12:16:41 Miaaooow
// 2017-01-09 12:16:42 Miaaooow
// 2017-01-09 12:16:43 Miaaooow
...
```
By default and to be safe, an interval method cannot be invoked manually. However, you can avoid this behavior by setting the `protectOriginal` option to `false`.
```ts
@Schedulable()
class Cat {
@Interval(1000)
public static miaow() {
console.log('Miaaooow')
}
}
Cat.miaow()
// Displays:
// Uncaught Error: interval method cannot be invoked
@Schedulable()
class Cat {
@Interval(1000, { protectOriginal: false })
public static miaow() {
console.log('Miaaooow')
}
}
Cat.miaow()
// Displays:
// 2017-01-09 12:55:33 Miaaooow
// 2017-01-09 12:55:34 Miaaooow
// 2017-01-09 12:55:35 Miaaooow
// 2017-01-09 12:55:36 Miaaooow
```
If the option `leading` is set to `true`, then the method will be invoked on the leading edge as well.
```ts
@Schedulable()
class Cat {
@Interval(1000, { leading: true })
public static miaow() {
console.log('Miaaooow')
}
}
// Displays:
// 2017-01-09 12:18:18 Miaaooow
// 2017-01-09 12:18:19 Miaaooow
// 2017-01-09 12:18:20 Miaaooow
// 2017-01-09 12:18:21 Miaaooow
...
```
The execution of the method can be stopped providing a function to the `stop` option. If the invokation of this function returns `true`, then the interval is cleared.
```ts
@Schedulable()
class Cat {
public static stopIt: boolean = false
@Interval(1000, { stop: self => return self.stopIt })
public static miaow() {
console.log('Miaaooow')
Cat.stopIt = true
}
}
// Displays:
// 2017-01-09 12:18:18 Miaaooow
// 2017-01-09 12:18:19 Miaaooow
// 2017-01-09 12:18:20 Miaaooow
// 2017-01-09 12:18:21 Miaaooow
...
```
Code licensed under [MIT License](LICENSE).