Skip to content

Consider adding tap back in #18

@mattmassicotte

Description

@mattmassicotte

Code looked like this:

extension DataChannel {
	/// Create a passthrough `DataChannel` that invokes a closure on read and write.
	public static func tap(
		channel: DataChannel,
		onRead: @Sendable @escaping (Data) async -> Void,
		onWrite: @Sendable @escaping (Data) async -> Void
	) -> DataChannel {

		let writeHandler: DataChannel.WriteHandler = {
			await onWrite($0)

			try await channel.writeHandler($0)
		}

		var iterator = channel.dataSequence.makeAsyncIterator()
		let dataStream = AsyncStream<Data> {
			let data = await iterator.next()

			if let data = data {
				await onRead(data)
			}

			return data
		}

		return DataChannel(writeHandler: writeHandler,
						   dataSequence: dataStream)
	}
}

The issue is iterator is not Sendable and that AsyncStream block is @Senable. This is trivial to "fix" with a nonisolated(unsafe), but I'm not 100% sure that is actually a safe thing to do and would require some thought.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions