CoScope

public final class CoScope
extension CoScope: CoCancellable

The holder of uncompleted CoCancellable and coroutines.

CoScope helps to manage lifecycle of coroutines and CoCancellable, like CoFuture and CoChannel. It keeps weak references on inner objects and cancels them on cancel() or deinit. All completed objects are automaticaly removed from scope.

Note

CoScope keeps weak references.
let scope = CoScope()

let future = makeSomeFuture().added(to: scope)

queue.startCoroutine(in: scope) {
    . . . some code . . .
    let result = try future.await()
    . . . some code . . .
}

let future2 = queue.coroutineFuture {
    try Coroutine.delay(.seconds(5)) // imitate some work
    return 5
}.added(to: scope)

//cancel all added futures and coroutines
scope.cancel()
  • Initializes a scope.

    Declaration

    Swift

    public init()
  • Adds weak referance of CoCancellable to be canceled when the scope is being canceled or deinited.

    Declaration

    Swift

    public func add(_ item: CoCancellable)

    Parameters

    item

    CoCancellable to add.

  • Returns true if the scope is empty (contains no CoCancellable).

    Declaration

    Swift

    public var isEmpty: Bool { get }

cancel

  • Returns true if the scope is canceled.

    Declaration

    Swift

    public var isCanceled: Bool { get }
  • Cancels the scope and all CoCancellable that it contains.

    Declaration

    Swift

    public func cancel()
  • Adds an observer callback that is called when the CoScope is canceled or deinited.

    Declaration

    Swift

    public func whenComplete(_ callback: @escaping () -> Void)

    Parameters

    callback

    The callback that is called when the scope is canceled or deinited.