ts-schedule-decorators
v1.0.2
Published
Schedule decorator for TypeScript
Downloads
72
Maintainers
Readme
Schedule decorators for TypeScript
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() function internally, makes the code clearer and avoid code duplication.
Install
The easiest way is to install ts-schedule-decorators
as dependency
:
npm install ts-schedule-decorators --save
Usage
Schedule an instance method
Instance methods will be executed after instantiation.
@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
...
Schedule a static method
Static methods will be executed on declaration.
@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
...
Allow manual invocation
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
.
@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
Execute the method on the leading edge
If the option leading
is set to true
, then the method will be invoked on the leading edge as well.
@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
...
Stopping condition
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.
@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
...
License
Code licensed under MIT License.