/*!
 * Copyright (c) 2017-2018 by The Funfix Project Developers.
 * Some rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* @flow */

export interface ICancelable {
  cancel(): void;
}

declare export class Cancelable {
  cancel(): void;

  static of(cb: () => void): Cancelable;
  static empty(): Cancelable;
  static collection(...refs: Array<Cancelable>): Cancelable;
  static cancelAll(refs: Cancelable[]): void;
}

export interface IBoolCancelable extends ICancelable {
  isCanceled(): boolean;
}

declare export class BoolCancelable extends Cancelable {
  isCanceled(): boolean;

  static of(cb: () => void): BoolCancelable;
  static empty(): BoolCancelable;
  static alreadyCanceled(): BoolCancelable;
  static collection(...refs: Array<ICancelable>): BoolCancelable;
}

export interface IAssignCancelable extends IBoolCancelable {
  update(value: ICancelable): IAssignCancelable;
}

declare export class AssignCancelable extends BoolCancelable {
  update(value: ICancelable): this;

  static of(cb: () => void): AssignCancelable;
  static empty(): AssignCancelable;
  static alreadyCanceled(): AssignCancelable;
}

declare export class MultiAssignCancelable extends AssignCancelable {
  constructor(initial?: ICancelable): MultiAssignCancelable;
  update(value: ICancelable): this;
  collapse(): this;
  clear(): this;
  static of(cb: () => void): MultiAssignCancelable;
  static empty(): MultiAssignCancelable;
}

declare export class SerialCancelable extends AssignCancelable {
  constructor(initial?: ICancelable): SerialCancelable;
  update(value: ICancelable): this;
  static of(cb: () => void): SerialCancelable;
  static empty(): SerialCancelable;
}

declare export class SingleAssignCancelable extends AssignCancelable {
  constructor(): SingleAssignCancelable;
  update(value: ICancelable): this;
  static of(cb: () => void): SingleAssignCancelable;
  static empty(): SingleAssignCancelable;
}

declare export class StackedCancelable {
  constructor(initial?: ICancelable[]): StackedCancelable;

  isCanceled(): boolean;
  cancel(): void;

  push(value: ICancelable): this;
  pop(): ICancelable;

  static empty(): StackedCancelable;
  static collection(...refs: Array<ICancelable>): StackedCancelable;
}
