Skip to content

Commit

Permalink
fix: internal protections on class
Browse files Browse the repository at this point in the history
  • Loading branch information
rishi8094 committed Sep 11, 2022
1 parent 0e1ee3f commit d702056
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 60 deletions.
46 changes: 24 additions & 22 deletions Sources/LeapEdge/Utils/Emitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,36 @@

import Foundation

public final class Emitter {
fileprivate var registry = [ObjectIdentifier: ContiguousArray<UnsafeListener>]()

public func emit<Message>(_ message: Message) {
if let listeners = registry[ObjectIdentifier(Message.self)] {
for listener in listeners {
unsafeDowncast(listener, to: Listener<Message>.self).content(message)
extension LeapEdge {
final class Emitter {
fileprivate var registry = [ObjectIdentifier: ContiguousArray<UnsafeListener>]()

func emit<Message>(_ message: Message) {
if let listeners = registry[ObjectIdentifier(Message.self)] {
for listener in listeners {
unsafeDowncast(listener, to: Listener<Message>.self).content(message)
}
}
}
}

@discardableResult
public func when<Message>(_ listener: @escaping (Message)->Void) -> Listener<Message> {
let id = ObjectIdentifier(Message.self)
let handler = Listener(content: listener)
if registry.keys.contains(id) {
registry[id]!.append(handler)
} else {
registry[id] = [handler]
@discardableResult
func when<Message>(_ listener: @escaping (Message)->Void) -> Listener<Message> {
let id = ObjectIdentifier(Message.self)
let handler = Listener(content: listener)
if registry.keys.contains(id) {
registry[id]!.append(handler)
} else {
registry[id] = [handler]
}
return handler
}
return handler
}
}

public class UnsafeListener {}
public class UnsafeListener {}

public class Listener<Message>: UnsafeListener {
public let content: (Message)->Void
public class Listener<Message>: UnsafeListener {
let content: (Message)->Void

init(content: @escaping (Message)->Void) {self.content = content}
init(content: @escaping (Message)->Void) {self.content = content}
}
}
20 changes: 11 additions & 9 deletions Sources/LeapEdge/Utils/Partial.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@

import Foundation

public struct Partial<Wrapped> {
private var values: [PartialKeyPath<Wrapped>: Any] = [:]

subscript<ValueType>(key: KeyPath<Wrapped, ValueType>) -> ValueType? {
get {
return values[key] as? ValueType
}
set {
values[key] = newValue
extension LeapEdge {
public struct Partial<Wrapped> {
private var values: [PartialKeyPath<Wrapped>: Any] = [:]

subscript<ValueType>(key: KeyPath<Wrapped, ValueType>) -> ValueType? {
get {
return values[key] as? ValueType
}
set {
values[key] = newValue
}
}
}
}
58 changes: 30 additions & 28 deletions Sources/LeapEdge/Utils/Throttler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,36 @@

import Foundation

class Throttler {

private var workItem: DispatchWorkItem = DispatchWorkItem(block: {})
private var previousRun: Date = Date.distantPast
private let queue: DispatchQueue
private let minimumDelay: TimeInterval

init(minimumDelay: TimeInterval, queue: DispatchQueue = DispatchQueue.main) {
self.minimumDelay = minimumDelay
self.queue = queue
}

func throttle(_ block: @escaping () -> Void) {
// Cancel any existing work item if it has not yet executed
workItem.cancel()

// Re-assign workItem with the new block task, resetting the previousRun time when it executes
workItem = DispatchWorkItem() {
[weak self] in
self?.previousRun = Date()
block()
extension LeapEdge {
final class Throttler {

private var workItem: DispatchWorkItem = DispatchWorkItem(block: {})
private var previousRun: Date = Date.distantPast
private let queue: DispatchQueue
private let minimumDelay: TimeInterval

init(minimumDelay: TimeInterval, queue: DispatchQueue = DispatchQueue.main) {
self.minimumDelay = minimumDelay
self.queue = queue
}

func throttle(_ block: @escaping () -> Void) {
// Cancel any existing work item if it has not yet executed
workItem.cancel()

// Re-assign workItem with the new block task, resetting the previousRun time when it executes
workItem = DispatchWorkItem() {
[weak self] in
self?.previousRun = Date()
block()
}

// If the time since the previous run is more than the required minimum delay
// => execute the workItem immediately
// else
// => delay the workItem execution by the minimum delay time
let delay = previousRun.timeIntervalSinceNow > minimumDelay ? 0 : minimumDelay
queue.asyncAfter(deadline: .now() + Double(delay), execute: workItem)
}

// If the time since the previous run is more than the required minimum delay
// => execute the workItem immediately
// else
// => delay the workItem execution by the minimum delay time
let delay = previousRun.timeIntervalSinceNow > minimumDelay ? 0 : minimumDelay
queue.asyncAfter(deadline: .now() + Double(delay), execute: workItem)
}
}
2 changes: 1 addition & 1 deletion Tests/LeapEdgeTests/LeapEdgeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import XCTest

final class LeapEdgeTests: XCTestCase {
func testConnection() throws {
var options: Partial<LeapEdge.InitOptions> = .init()
var options: LeapEdge.Partial<LeapEdge.InitOptions> = .init()
options[\.debug] = false
let leap = LeapEdge(auth: .init(token: nil, projectId: "test"), opts: options)
leap.connect()
Expand Down

0 comments on commit d702056

Please sign in to comment.