Skip to content

Commit 8d55157

Browse files
committed
propagate channel buffer parameters to event channel invocations
1 parent 4eee2cd commit 8d55157

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

Sources/FlutterSwift/Channel/FlutterChannel.swift

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2023-2024 PADL Software Pty Ltd
2+
// Copyright (c) 2023-2025 PADL Software Pty Ltd
33
//
44
// Licensed under the Apache License, Version 2.0 (the License);
55
// you may not use this file except in compliance with the License.
@@ -39,6 +39,9 @@ public protocol FlutterChannel: AnyObject, Hashable, Equatable {
3939
var binaryMessenger: FlutterBinaryMessenger { get }
4040
var codec: FlutterMessageCodec { get }
4141
var priority: TaskPriority? { get }
42+
43+
func resizeChannelBuffer(_ newSize: Int) async throws
44+
func allowChannelBufferOverflow(_ allowed: Bool) async throws
4245
}
4346

4447
protocol _FlutterBinaryMessengerConnectionRepresentable: FlutterChannel {
@@ -92,25 +95,29 @@ func _allowChannelBufferOverflow(
9295
}
9396

9497
public extension FlutterChannel {
95-
func resizeChannelBuffer(_ newSize: Int) async throws {
98+
static func == (lhs: Self, rhs: Self) -> Bool {
99+
lhs.name == rhs.name
100+
}
101+
102+
func hash(into hasher: inout Hasher) {
103+
hasher.combine(name)
104+
}
105+
}
106+
107+
protocol _FlutterChannelDefaultBufferControl: FlutterChannel {}
108+
109+
extension _FlutterChannelDefaultBufferControl {
110+
public func resizeChannelBuffer(_ newSize: Int) async throws {
96111
try await _resizeChannelBuffer(binaryMessenger: binaryMessenger, on: name, newSize: newSize)
97112
}
98113

99-
func allowChannelBufferOverflow(_ allowed: Bool) async throws {
114+
public func allowChannelBufferOverflow(_ allowed: Bool) async throws {
100115
try await _allowChannelBufferOverflow(
101116
binaryMessenger: binaryMessenger,
102117
on: name,
103118
allowed: allowed
104119
)
105120
}
106-
107-
static func == (lhs: Self, rhs: Self) -> Bool {
108-
lhs.name == rhs.name
109-
}
110-
111-
func hash(into hasher: inout Hasher) {
112-
hasher.combine(name)
113-
}
114121
}
115122

116123
extension _FlutterBinaryMessengerConnectionRepresentable {

Sources/FlutterSwift/Channel/FlutterEventChannel.swift

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2023-2024 PADL Software Pty Ltd
2+
// Copyright (c) 2023-2025 PADL Software Pty Ltd
33
//
44
// Licensed under the Apache License, Version 2.0 (the License);
55
// you may not use this file except in compliance with the License.
@@ -32,7 +32,9 @@ public typealias FlutterEventStream<Event: Codable> = AnyAsyncSequence<Event?>
3232
/**
3333
* A channel for communicating with the Flutter side using event streams.
3434
*/
35-
public final class FlutterEventChannel: _FlutterBinaryMessengerConnectionRepresentable, Sendable {
35+
public final class FlutterEventChannel: _FlutterBinaryMessengerConnectionRepresentable,
36+
Sendable
37+
{
3638
public let name: String
3739
public let binaryMessenger: FlutterBinaryMessenger
3840
public let codec: FlutterMessageCodec
@@ -43,6 +45,10 @@ public final class FlutterEventChannel: _FlutterBinaryMessengerConnectionReprese
4345
private let _connection: ManagedAtomic<FlutterBinaryMessengerConnection>
4446
private let tasks: ManagedCriticalState<[String: EventStreamTask]>
4547

48+
// store these to propagate to separate channel invocations
49+
private var channelBufferSize = 1
50+
private var allowChannelBufferOverflow = false
51+
4652
var connection: FlutterBinaryMessengerConnection {
4753
get {
4854
_connection.load(ordering: .acquiring)
@@ -161,6 +167,19 @@ public final class FlutterEventChannel: _FlutterBinaryMessengerConnectionReprese
161167

162168
switch method.count > 1 ? String(method[0]) : call.method {
163169
case "listen":
170+
if !invocationID.isEmpty {
171+
try await _resizeChannelBuffer(
172+
binaryMessenger: binaryMessenger,
173+
on: name,
174+
newSize: channelBufferSize
175+
)
176+
try await _allowChannelBufferOverflow(
177+
binaryMessenger: binaryMessenger,
178+
on: name,
179+
allowed: allowChannelBufferOverflow
180+
)
181+
}
182+
164183
let stream = try await onListen(call.arguments)
165184
let task = EventStreamTask(priority: priority) {
166185
try? await self._run(for: stream, name: name)
@@ -220,4 +239,18 @@ public final class FlutterEventChannel: _FlutterBinaryMessengerConnectionReprese
220239
public var tasksCount: Int {
221240
tasks.withCriticalRegion { $0.count }
222241
}
242+
243+
public func resizeChannelBuffer(_ newSize: Int) async throws {
244+
try await _resizeChannelBuffer(binaryMessenger: binaryMessenger, on: name, newSize: newSize)
245+
channelBufferSize = newSize
246+
}
247+
248+
public func allowChannelBufferOverflow(_ allowed: Bool) async throws {
249+
try await _allowChannelBufferOverflow(
250+
binaryMessenger: binaryMessenger,
251+
on: name,
252+
allowed: allowed
253+
)
254+
allowChannelBufferOverflow = allowed
255+
}
223256
}

Sources/FlutterSwift/Channel/FlutterMethodChannel.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ extension FlutterMethodCall: Hashable where Arguments: Codable & Hashable {
7474
* A channel for communicating with the Flutter side using invocation of
7575
* asynchronous methods.
7676
*/
77-
public final class FlutterMethodChannel: _FlutterBinaryMessengerConnectionRepresentable, Sendable {
77+
public final class FlutterMethodChannel: _FlutterBinaryMessengerConnectionRepresentable,
78+
_FlutterChannelDefaultBufferControl, Sendable
79+
{
7880
public let name: String
7981
public let binaryMessenger: FlutterBinaryMessenger
8082
public let codec: FlutterMessageCodec

0 commit comments

Comments
 (0)