-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4eeca92
commit eeb365b
Showing
8 changed files
with
260 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"object": { | ||
"pins": [ | ||
{ | ||
"package": "AsyncTimeSequences", | ||
"repositoryURL": "https://github.com/Henryforce/AsyncTimeSequences", | ||
"state": { | ||
"branch": null, | ||
"revision": "1dbdc4b6c888bd26ec15d5c279d7d0284fae422a", | ||
"version": "0.0.7" | ||
} | ||
} | ||
] | ||
}, | ||
"version": 1 | ||
} |
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
28 changes: 28 additions & 0 deletions
28
Sources/AsyncWebSocketClient/AsyncWebSocketClient+URLSessionWebSocketDelegate.swift
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,28 @@ | ||
// | ||
// AsyncWebSocketClient+URLSessionWebSocketDelegate.swift | ||
// AsyncWebSocketClient | ||
// | ||
// Created by Henry Javier Serrano Echeverria on 13/1/22. | ||
// | ||
|
||
import Foundation | ||
|
||
extension AsyncWebSocketClient: URLSessionWebSocketDelegate { | ||
nonisolated public func urlSession(_ session: URLSession, webSocketTask: URLSessionWebSocketTask, didOpenWithProtocol protocol: String?) { | ||
Task { | ||
await socketWasOpened() | ||
} | ||
} | ||
|
||
nonisolated public func urlSession(_ session: URLSession, webSocketTask: URLSessionWebSocketTask, didCloseWith closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?) { | ||
Task { | ||
await updateStream(with: .socketClosed(nil)) | ||
} | ||
} | ||
|
||
nonisolated public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { | ||
Task { | ||
await socketFailedToOpen() | ||
} | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
Sources/AsyncWebSocketClient/StreamGenerator/StreamGenerator.swift
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,65 @@ | ||
// | ||
// StreamGenerator.swift | ||
// | ||
// | ||
// Created by Henry Javier Serrano Echeverria on 13/1/22. | ||
// | ||
|
||
import Foundation | ||
|
||
// OPTIONAL TODO: move to a different package | ||
|
||
actor StreamGenerator<T> { | ||
var subscribers = [UUID: AsyncStream<T>.Continuation]() | ||
|
||
var value: T { _value } | ||
var _value: T { | ||
didSet { | ||
subscribers.values.forEach { $0.yield(value) } | ||
} | ||
} | ||
|
||
init(value: T) { | ||
self._value = value | ||
} | ||
|
||
func updateValue(_ value: T) { | ||
self._value = value | ||
} | ||
|
||
func subscribe() -> AsyncStream<T> { | ||
return AsyncStream { continuation in | ||
let uuid = UUID() | ||
subscribers[uuid] = continuation | ||
|
||
continuation.onTermination = { @Sendable _ in | ||
Task { [weak self] in | ||
await self?.removeSubscriber(with: uuid) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private func removeSubscriber(with uuid: UUID) { | ||
subscribers.removeValue(forKey: uuid) | ||
} | ||
|
||
deinit { | ||
for key in subscribers.keys { | ||
guard let subscriber = subscribers[key] else { continue } | ||
subscriber.finish() | ||
removeSubscriber(with: key) | ||
} | ||
} | ||
} | ||
|
||
//public protocol WriteStreamGenerator: Actor { | ||
// associatedtype Value | ||
// func updateValue(_ value: Value) | ||
//} | ||
// | ||
//public protocol ReadStreamGenerator: Actor { | ||
// associatedtype Value | ||
// var value: Value { get } | ||
// func subscribe() -> AsyncStream<Value> | ||
//} |
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
Oops, something went wrong.