-
-
Notifications
You must be signed in to change notification settings - Fork 123
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
Showing
9 changed files
with
125 additions
and
86 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
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,52 @@ | ||
// | ||
// SharedStream.swift | ||
// | ||
// | ||
// Created by Guilherme Souza on 12/01/24. | ||
// | ||
|
||
import ConcurrencyExtras | ||
import Foundation | ||
|
||
final class SharedStream<Element>: Sendable where Element: Sendable { | ||
private let storage = LockIsolated<[UUID: AsyncStream<Element>.Continuation]>([:]) | ||
private let _value: LockIsolated<Element> | ||
|
||
var lastElement: Element { _value.value } | ||
|
||
init(initialElement: Element) { | ||
_value = LockIsolated(initialElement) | ||
} | ||
|
||
func makeStream() -> AsyncStream<Element> { | ||
let (stream, continuation) = AsyncStream<Element>.makeStream() | ||
let id = UUID() | ||
|
||
continuation.onTermination = { _ in | ||
self.storage.withValue { | ||
$0[id] = nil | ||
} | ||
} | ||
|
||
storage.withValue { | ||
$0[id] = continuation | ||
} | ||
|
||
continuation.yield(lastElement) | ||
|
||
return stream | ||
} | ||
|
||
func yield(_ value: Element) { | ||
_value.setValue(value) | ||
for continuation in storage.value.values { | ||
continuation.yield(value) | ||
} | ||
} | ||
|
||
func finish() { | ||
for continuation in storage.value.values { | ||
continuation.finish() | ||
} | ||
} | ||
} |
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// StreamManagerTests.swift | ||
// | ||
// | ||
// Created by Guilherme Souza on 18/01/24. | ||
// | ||
|
||
import Foundation | ||
@testable import Realtime | ||
import XCTest | ||
|
||
final class StreamManagerTests: XCTestCase { | ||
func testYieldInitialValue() async { | ||
let manager = SharedStream(initialElement: 0) | ||
|
||
let value = await manager.makeStream().first(where: { _ in true }) | ||
XCTAssertEqual(value, 0) | ||
} | ||
} |