|
| 1 | +// |
| 2 | +// Dispatcher.swift |
| 3 | +// |
| 4 | +// Copyright (c) PubNub Inc. |
| 5 | +// All rights reserved. |
| 6 | +// |
| 7 | +// This source code is licensed under the license found in the |
| 8 | +// LICENSE file in the root directory of this source tree. |
| 9 | +// |
| 10 | + |
| 11 | +import Foundation |
| 12 | + |
| 13 | +// MARK: - DispatcherListener |
| 14 | + |
| 15 | +struct DispatcherListener<Event> { |
| 16 | + let onAnyInvocationCompleted: (([Event]) -> Void) |
| 17 | +} |
| 18 | + |
| 19 | +// MARK: - Dispatcher |
| 20 | + |
| 21 | +protocol Dispatcher<Invocation, Event, Dependencies> { |
| 22 | + associatedtype Invocation: AnyEffectInvocation |
| 23 | + associatedtype Event |
| 24 | + associatedtype Dependencies |
| 25 | + |
| 26 | + func dispatch( |
| 27 | + invocations: [EffectInvocation<Invocation>], |
| 28 | + with dependencies: EventEngineDependencies<Dependencies>, |
| 29 | + notify listener: DispatcherListener<Event> |
| 30 | + ) |
| 31 | +} |
| 32 | + |
| 33 | +// MARK: - EffectDispatcher |
| 34 | + |
| 35 | +class EffectDispatcher<Invocation: AnyEffectInvocation, Event, Dependencies>: Dispatcher { |
| 36 | + private let factory: any EffectHandlerFactory<Invocation, Event, Dependencies> |
| 37 | + private let effectsCache = EffectsCache<Event>() |
| 38 | + |
| 39 | + init(factory: some EffectHandlerFactory<Invocation, Event, Dependencies>) { |
| 40 | + self.factory = factory |
| 41 | + } |
| 42 | + |
| 43 | + func hasPendingInvocation(_ invocation: Invocation) -> Bool { |
| 44 | + effectsCache.hasPendingEffect(with: invocation.id) |
| 45 | + } |
| 46 | + |
| 47 | + func dispatch( |
| 48 | + invocations: [EffectInvocation<Invocation>], |
| 49 | + with dependencies: EventEngineDependencies<Dependencies>, |
| 50 | + notify listener: DispatcherListener<Event> |
| 51 | + ) { |
| 52 | + invocations.forEach { |
| 53 | + switch $0 { |
| 54 | + case .managed(let invocation): |
| 55 | + executeEffect( |
| 56 | + effect: factory.effect(for: invocation, with: dependencies), |
| 57 | + storageId: invocation.id, |
| 58 | + notify: listener |
| 59 | + ) |
| 60 | + case .regular(let invocation): |
| 61 | + executeEffect( |
| 62 | + effect: factory.effect(for: invocation, with: dependencies), |
| 63 | + storageId: UUID().uuidString, |
| 64 | + notify: listener |
| 65 | + ) |
| 66 | + case .cancel(let cancelInvocation): |
| 67 | + effectsCache.getEffect(with: cancelInvocation.id)?.cancelTask() |
| 68 | + effectsCache.removeEffect(id: cancelInvocation.id) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private func executeEffect( |
| 74 | + effect: some EffectHandler<Event>, |
| 75 | + storageId id: String, |
| 76 | + notify listener: DispatcherListener<Event> |
| 77 | + ) { |
| 78 | + effectsCache.put(effect: effect, with: id) |
| 79 | + effect.performTask { [weak effectsCache] results in |
| 80 | + effectsCache?.removeEffect(id: id) |
| 81 | + listener.onAnyInvocationCompleted(results) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// MARK: - EffectsCache |
| 87 | + |
| 88 | +fileprivate class EffectsCache<Event> { |
| 89 | + private var managedEffects: Atomic<[String: EffectWrapper<Event>]> = Atomic([:]) |
| 90 | + |
| 91 | + func hasPendingEffect(with id: String) -> Bool { |
| 92 | + managedEffects.lockedRead { $0[id] } != nil |
| 93 | + } |
| 94 | + |
| 95 | + func put(effect: some EffectHandler<Event>, with id: String) { |
| 96 | + managedEffects.lockedWrite { $0[id] = EffectWrapper<Event>(id: id, effect: effect) } |
| 97 | + } |
| 98 | + |
| 99 | + func getEffect(with id: String) -> (any EffectHandler<Event>)? { |
| 100 | + managedEffects.lockedRead() { $0[id] }?.effect |
| 101 | + } |
| 102 | + |
| 103 | + func removeEffect(id: String) { |
| 104 | + managedEffects.lockedWrite { $0[id] = nil } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// MARK: - EffectWrapper |
| 109 | + |
| 110 | +fileprivate struct EffectWrapper<Action> { |
| 111 | + let id: String |
| 112 | + let effect: any EffectHandler<Action> |
| 113 | +} |
0 commit comments