-
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.
Subscribe & Presence Event Engine (#152)
feat(subscribe & presence): introducing Subscribe & Presence EventEngine
- Loading branch information
1 parent
b47b478
commit 649a9e9
Showing
79 changed files
with
10,244 additions
and
1,623 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Pod::Spec.new do |s| | ||
s.name = 'PubNubSwift' | ||
s.version = '6.2.3' | ||
s.version = '6.3.0' | ||
s.homepage = 'https://github.com/pubnub/swift' | ||
s.documentation_url = 'https://www.pubnub.com/docs/swift-native/pubnub-swift-sdk' | ||
s.authors = { 'PubNub, Inc.' => '[email protected]' } | ||
|
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> | ||
} |
Oops, something went wrong.