Skip to content

Commit 5ac5d10

Browse files
committed
[Enhancement]Async Store reducers
1 parent 65091a3 commit 5ac5d10

File tree

20 files changed

+92
-71
lines changed

20 files changed

+92
-71
lines changed

Sources/StreamVideo/Utils/AudioSession/AudioProcessing/Namespace/Reducers/AudioProcessingStore+DefaultReducer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension AudioProcessingStore.Namespace {
1717
file: StaticString,
1818
function: StaticString,
1919
line: UInt
20-
) throws -> AudioProcessingStore.Namespace.StoreState {
20+
) async throws -> AudioProcessingStore.Namespace.StoreState {
2121
var updatedState = state
2222

2323
switch action {

Sources/StreamVideo/Utils/AudioSession/AudioRecorder/Namespace/Reducers/StreamCallAudioRecorder+DefaultReducer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extension StreamCallAudioRecorder.Namespace {
4747
file: StaticString,
4848
function: StaticString,
4949
line: UInt
50-
) throws -> State {
50+
) async throws -> State {
5151
var updatedState = state
5252

5353
switch action {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Copyright © 2025 Stream.io Inc. All rights reserved.
3+
//
4+
5+
import Foundation
6+
7+
extension Sequence {
8+
9+
func asyncReduce<Result>(
10+
_ initialResult: Result,
11+
_ nextPartialResult: ((Result, Element) async throws -> Result)
12+
) async rethrows -> Result {
13+
var result = initialResult
14+
for element in self {
15+
result = try await nextPartialResult(result, element)
16+
}
17+
return result
18+
}
19+
}

Sources/StreamVideo/Utils/PermissionsStore/Namespace/Reducers/PermissionStore+DefaultReducer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension PermissionStore {
1616
file: StaticString,
1717
function: StaticString,
1818
line: UInt
19-
) throws -> PermissionStore.StoreState {
19+
) async throws -> PermissionStore.StoreState {
2020
var updatedState = state
2121

2222
switch action {

Sources/StreamVideo/Utils/Store/Reducer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class Reducer<Namespace: StoreNamespace>: @unchecked Sendable {
7676
file: StaticString,
7777
function: StaticString,
7878
line: UInt
79-
) throws -> Namespace.State {
79+
) async throws -> Namespace.State {
8080
state
8181
}
8282
}

Sources/StreamVideo/Utils/Store/StoreExecutor.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class StoreExecutor<Namespace: StoreNamespace>: @unchecked Sendable {
6363
file: StaticString,
6464
function: StaticString,
6565
line: UInt
66-
) async throws {
66+
) async throws -> Namespace.State {
6767
// Apply optional delay before processing action
6868
await action.applyDelayBeforeIfRequired()
6969

@@ -80,9 +80,9 @@ class StoreExecutor<Namespace: StoreNamespace>: @unchecked Sendable {
8080

8181
do {
8282
// Process action through all reducers sequentially
83-
let updatedState = try reducers
84-
.reduce(state) {
85-
try $1.reduce(
83+
let updatedState = try await reducers
84+
.asyncReduce(state) {
85+
try await $1.reduce(
8686
state: $0,
8787
action: action.wrappedValue,
8888
file: file,
@@ -106,6 +106,8 @@ class StoreExecutor<Namespace: StoreNamespace>: @unchecked Sendable {
106106

107107
// Apply optional delay after successful processing
108108
await action.applyDelayAfterIfRequired()
109+
110+
return updatedState
109111
} catch {
110112
// Log failure and rethrow
111113
logger.didFail(

Sources/StreamVideo/Utils/Store/StoreTask.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ final class StoreTask<Namespace: StoreNamespace>: Sendable {
6464
) async {
6565
resultSubject.send(.running)
6666
do {
67+
var workingState = state
6768
for action in actions {
68-
try await executor.run(
69+
workingState = try await executor.run(
6970
identifier: identifier,
70-
state: state,
71+
state: workingState,
7172
action: action,
7273
reducers: reducers,
7374
middleware: middleware,

StreamVideo.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@
504504
40A0E9602B88ABC80089E8D3 /* DemoBackgroundEffectSelector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40A0E95F2B88ABC80089E8D3 /* DemoBackgroundEffectSelector.swift */; };
505505
40A0E9622B88D3DC0089E8D3 /* UIInterfaceOrientation+CGOrientation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40A0E9612B88D3DC0089E8D3 /* UIInterfaceOrientation+CGOrientation.swift */; };
506506
40A0E9682B88E04D0089E8D3 /* CIImage_Resize_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40A0E9672B88E04D0089E8D3 /* CIImage_Resize_Tests.swift */; };
507+
40A0FFC02EA6418000F39D8F /* Sequence+AsyncReduce.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40A0FFBF2EA6418000F39D8F /* Sequence+AsyncReduce.swift */; };
507508
40A7C5B52E099B4600EEDF9C /* ParticipantEventResetAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40A7C5B22E099B1000EEDF9C /* ParticipantEventResetAdapter.swift */; };
508509
40A7C5B82E099D6200EEDF9C /* ParticipantEventResetAdapter_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40A7C5B72E099D6200EEDF9C /* ParticipantEventResetAdapter_Tests.swift */; };
509510
40A9416E2B4D959F006D6965 /* StreamPictureInPictureAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40A9416D2B4D959F006D6965 /* StreamPictureInPictureAdapter.swift */; };
@@ -2185,6 +2186,7 @@
21852186
40A0E95F2B88ABC80089E8D3 /* DemoBackgroundEffectSelector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoBackgroundEffectSelector.swift; sourceTree = "<group>"; };
21862187
40A0E9612B88D3DC0089E8D3 /* UIInterfaceOrientation+CGOrientation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIInterfaceOrientation+CGOrientation.swift"; sourceTree = "<group>"; };
21872188
40A0E9672B88E04D0089E8D3 /* CIImage_Resize_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CIImage_Resize_Tests.swift; sourceTree = "<group>"; };
2189+
40A0FFBF2EA6418000F39D8F /* Sequence+AsyncReduce.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Sequence+AsyncReduce.swift"; sourceTree = "<group>"; };
21882190
40A7C5B22E099B1000EEDF9C /* ParticipantEventResetAdapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParticipantEventResetAdapter.swift; sourceTree = "<group>"; };
21892191
40A7C5B72E099D6200EEDF9C /* ParticipantEventResetAdapter_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParticipantEventResetAdapter_Tests.swift; sourceTree = "<group>"; };
21902192
40A9416D2B4D959F006D6965 /* StreamPictureInPictureAdapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StreamPictureInPictureAdapter.swift; sourceTree = "<group>"; };
@@ -3753,6 +3755,7 @@
37533755
isa = PBXGroup;
37543756
children = (
37553757
4028FEAA2DC536DE001F9DC3 /* Date+millisecondsSince1970.swift */,
3758+
40A0FFBF2EA6418000F39D8F /* Sequence+AsyncReduce.swift */,
37563759
);
37573760
path = Foundation;
37583761
sourceTree = "<group>";
@@ -8599,6 +8602,7 @@
85998602
40C4E85D2E6999A500FC29BC /* RTCAudioStore+RestartAudioSession.swift in Sources */,
86008603
40BBC4BA2C627F83002AEF92 /* TrackEvent.swift in Sources */,
86018604
406128832CF33000007F5CDC /* SDPParser.swift in Sources */,
8605+
40A0FFC02EA6418000F39D8F /* Sequence+AsyncReduce.swift in Sources */,
86028606
84B9A56D29112F39004DE31A /* EndpointConfig.swift in Sources */,
86038607
405072672E5F4CF7003D2109 /* CallKitMissingPermissionPolicy+EndCall.swift in Sources */,
86048608
4039F0CF2D024DDF0078159E /* MediaTransceiverStorage.swift in Sources */,

StreamVideoSwiftUITests/CallViewModel_Tests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ final class CallViewModel_Tests: XCTestCase, @unchecked Sendable {
219219
}
220220

221221
func test_outgoingCall_callCreatedPriorToStarting_rejectedEventFromOneParticipantCallRemainsOngoing() async throws {
222-
LogConfig.level = .debug
223222
// Given
224223
let memberResponses = (participants + [thirdUser]).map {
225224
MemberResponse(

StreamVideoTests/Mock/Store/MockReducer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class MockReducer<Namespace: StoreNamespace>: Reducer<Namespace>, @uncheck
2020
file: StaticString,
2121
function: StaticString,
2222
line: UInt
23-
) throws -> Namespace.State {
23+
) async throws -> Namespace.State {
2424
inputs.append(.init(action: action, state: state))
2525
return state
2626
}

0 commit comments

Comments
 (0)