Skip to content

Instantly share code, notes, and snippets.

View SergeyPetrachkov's full-sized avatar
:octocat:

Sergey Petrachkov SergeyPetrachkov

:octocat:
View GitHub Profile
@SergeyPetrachkov
SergeyPetrachkov / AsyncWaiter.swift
Created May 9, 2022 09:18 — forked from krzyzanowskim/AsyncWaiter.swift
Synchronously (well) wait for async Task value update
/// Wait for async operation to return value and call callback with the value
/// This class is intended to workaround/simplify async/await + actors isolation
/// https://twitter.com/krzyzanowskim/status/1523233140914876416
private class AsyncWaiter<T> {
var didReceiveValue: Bool = false
let value: (T) -> Void
let operation: () async throws -> T
init(_ value: @escaping (T) -> Void, operation: @escaping () async throws -> T) {
self.value = value
import Foundation
class Debounce {
private let queue: DispatchQueue
private let delay: Double
private var workItem: DispatchWorkItem?
private var cancelBlock: (() -> Void)?
init(queue: DispatchQueue, delay: Double) {
self.queue = queue
import Foundation
class Throttle {
private let queue: DispatchQueue
private let delay: Double
private var delayedBlock: (() -> Void)?
private var cancelBlock: (() -> Void)?
private var timer: DispatchSourceTimer?
private var isReady = true
private var hasDelayedBlock: Bool { delayedBlock != nil }