-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding `.connnectionError` and removing `.connecting` and `.reconnecting` from `ConnectionStatus` * Deprecating `SubscriptionSession` and `SubscribeSessionFactory` * Limiting the scope of public structs/classes associated with the Subscribe loop that aren't part of any exposed public method/variable * Introducing Strategy pattern to handle both old & new Subscribe loop implementation * Removing local events emitted from Subscribe loop * Maintaining `state` parameter for /v2/subscribe and /v2/presence/heartbeat * Adding `enableEventEngine` and `maintainPresenceState` flags in `PubNubConfiguration` * Improved AutomaticRetry and making possible to retry other requests * Fixing unit & contract tests according to changes above
- Loading branch information
1 parent
b47b478
commit ee89cec
Showing
77 changed files
with
9,532 additions
and
1,236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,4 @@ SPEC CHECKSUMS: | |
|
||
PODFILE CHECKSUM: 61a40240486621bb01f596fdd5bc632504940fab | ||
|
||
COCOAPODS: 1.12.1 | ||
COCOAPODS: 1.14.3 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// | ||
// Dispatcher.swift | ||
// | ||
// Copyright (c) PubNub Inc. | ||
// All rights reserved. | ||
// | ||
// This source code is licensed under the license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - DispatcherListener | ||
|
||
struct DispatcherListener<Event> { | ||
let onAnyInvocationCompleted: (([Event]) -> Void) | ||
} | ||
|
||
// MARK: - Dispatcher | ||
|
||
protocol Dispatcher<Invocation, Event, Dependencies> { | ||
associatedtype Invocation: AnyEffectInvocation | ||
associatedtype Event | ||
associatedtype Dependencies | ||
|
||
func dispatch( | ||
invocations: [EffectInvocation<Invocation>], | ||
with dependencies: EventEngineDependencies<Dependencies>, | ||
notify listener: DispatcherListener<Event> | ||
) | ||
} | ||
|
||
// MARK: - EffectDispatcher | ||
|
||
class EffectDispatcher<Invocation: AnyEffectInvocation, Event, Dependencies>: Dispatcher { | ||
private let factory: any EffectHandlerFactory<Invocation, Event, Dependencies> | ||
private let effectsCache = EffectsCache<Event>() | ||
|
||
init(factory: some EffectHandlerFactory<Invocation, Event, Dependencies>) { | ||
self.factory = factory | ||
} | ||
|
||
func hasPendingInvocation(_ invocation: Invocation) -> Bool { | ||
effectsCache.hasPendingEffect(with: invocation.id) | ||
} | ||
|
||
func dispatch( | ||
invocations: [EffectInvocation<Invocation>], | ||
with dependencies: EventEngineDependencies<Dependencies>, | ||
notify listener: DispatcherListener<Event> | ||
) { | ||
invocations.forEach { | ||
switch $0 { | ||
case .managed(let invocation): | ||
executeEffect( | ||
effect: factory.effect(for: invocation, with: dependencies), | ||
storageId: invocation.id, | ||
notify: listener | ||
) | ||
case .regular(let invocation): | ||
executeEffect( | ||
effect: factory.effect(for: invocation, with: dependencies), | ||
storageId: UUID().uuidString, | ||
notify: listener | ||
) | ||
case .cancel(let cancelInvocation): | ||
effectsCache.getEffect(with: cancelInvocation.id)?.cancelTask() | ||
effectsCache.removeEffect(id: cancelInvocation.id) | ||
} | ||
} | ||
} | ||
|
||
private func executeEffect( | ||
effect: some EffectHandler<Event>, | ||
storageId id: String, | ||
notify listener: DispatcherListener<Event> | ||
) { | ||
effectsCache.put(effect: effect, with: id) | ||
effect.performTask { [weak effectsCache] results in | ||
effectsCache?.removeEffect(id: id) | ||
listener.onAnyInvocationCompleted(results) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - EffectsCache | ||
|
||
fileprivate class EffectsCache<Event> { | ||
private var managedEffects: Atomic<[String: EffectWrapper<Event>]> = Atomic([:]) | ||
|
||
func hasPendingEffect(with id: String) -> Bool { | ||
managedEffects.lockedRead { $0[id] } != nil | ||
} | ||
|
||
func put(effect: some EffectHandler<Event>, with id: String) { | ||
managedEffects.lockedWrite { $0[id] = EffectWrapper<Event>(id: id, effect: effect) } | ||
} | ||
|
||
func getEffect(with id: String) -> (any EffectHandler<Event>)? { | ||
managedEffects.lockedRead() { $0[id] }?.effect | ||
} | ||
|
||
func removeEffect(id: String) { | ||
managedEffects.lockedWrite { $0[id] = nil } | ||
} | ||
} | ||
|
||
// MARK: - EffectWrapper | ||
|
||
fileprivate struct EffectWrapper<Action> { | ||
let id: String | ||
let effect: any EffectHandler<Action> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// EffectHandler.swift | ||
// | ||
// Copyright (c) PubNub Inc. | ||
// All rights reserved. | ||
// | ||
// This source code is licensed under the license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
// | ||
import Foundation | ||
|
||
// MARK: - EffectHandlerFactory | ||
|
||
protocol EffectHandlerFactory<Invocation, Event, Dependencies> { | ||
associatedtype Invocation | ||
associatedtype Event | ||
associatedtype Dependencies | ||
|
||
func effect( | ||
for invocation: Invocation, | ||
with dependencies: EventEngineDependencies<Dependencies> | ||
) -> any EffectHandler<Event> | ||
} | ||
|
||
// MARK: - EffectHandler | ||
|
||
protocol EffectHandler<Event> { | ||
associatedtype Event | ||
|
||
func performTask(completionBlock: @escaping ([Event]) -> Void) | ||
func cancelTask() | ||
} | ||
|
||
extension EffectHandler { | ||
func cancelTask() {} | ||
} | ||
|
||
// MARK: - Delayed Effect Handler | ||
|
||
protocol DelayedEffectHandler: AnyObject, EffectHandler { | ||
var workItem: DispatchWorkItem? { get set } | ||
|
||
func delayInterval() -> TimeInterval? | ||
func onEarlyExit(notify completionBlock: @escaping ([Event]) -> Void) | ||
func onDelayExpired(notify completionBlock: @escaping ([Event]) -> Void) | ||
} | ||
|
||
extension DelayedEffectHandler { | ||
func performTask(completionBlock: @escaping ([Event]) -> Void) { | ||
guard let delay = delayInterval() else { | ||
onEarlyExit(notify: completionBlock); return | ||
} | ||
let workItem = DispatchWorkItem() { [weak self] in | ||
self?.onDelayExpired(notify: completionBlock) | ||
} | ||
DispatchQueue.global(qos: .default).asyncAfter( | ||
deadline: .now() + delay, | ||
execute: workItem | ||
) | ||
self.workItem = workItem | ||
} | ||
|
||
func cancelTask() { | ||
workItem?.cancel() | ||
} | ||
} |
Oops, something went wrong.