ICancelable represents a one-time idempotent action that can be
used to cancel async computations, or to release resources that
active data sources are holding.
It is similar in spirit to java.io.Closeable, but without the I/O
focus, or to IDisposable in Microsoft .NET.
// Scheduling execution with a 10 seconds delayconst ref = setTimeout(() =>console.log("Hello1"), 10000)
const task = Cancelable.of(() => clearTimeout(ref))
// If we change our mind
task.cancel()
In case some API requires the return of an ICancelable reference,
but there isn't anything that can be canceled, then
Cancelable.empty can be used to return a reusable reference
that doesn't do anything when canceled.
const task = Cancelable.empty()
// It's a no-op, doesn't do anything
task.cancel()
Implementation sample:
class MyCancelable implements ICancelable {
// Idempotency guardprivate _isCanceled: boolean = false
cancel() {
// We need an idempotency guarantee, any side-effects// need to happen only onceif (!this._isCanceled) {
this._isCanceled = trueconsole.log("Was canceled!")
}
}
}
ICancelable
represents a one-time idempotent action that can be used to cancel async computations, or to release resources that active data sources are holding.It is similar in spirit to
java.io.Closeable
, but without the I/O focus, or toIDisposable
in Microsoft .NET.// Scheduling execution with a 10 seconds delay const ref = setTimeout(() => console.log("Hello1"), 10000) const task = Cancelable.of(() => clearTimeout(ref)) // If we change our mind task.cancel()
In case some API requires the return of an
ICancelable
reference, but there isn't anything that can be canceled, then Cancelable.empty can be used to return a reusable reference that doesn't do anything when canceled.const task = Cancelable.empty() // It's a no-op, doesn't do anything task.cancel()
Implementation sample:
class MyCancelable implements ICancelable { // Idempotency guard private _isCanceled: boolean = false cancel() { // We need an idempotency guarantee, any side-effects // need to happen only once if (!this._isCanceled) { this._isCanceled = true console.log("Was canceled!") } } }