Options
All
  • Public
  • Public/Protected
  • All
Menu

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 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!")
    }
  }
}

Hierarchy

Implemented by

Index

Methods

Methods

cancel

  • cancel(): void

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc