From 96c07bab1086510a55a004e9a1b90c30bc723a5c Mon Sep 17 00:00:00 2001 From: sangtran Date: Sat, 29 Aug 2026 01:08:28 +0700 Subject: feat: init base draft support window --- transport/channel.go | 23 +++++++++++++++++++ transport/local_channel.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 transport/channel.go create mode 100644 transport/local_channel.go (limited to 'transport') diff --git a/transport/channel.go b/transport/channel.go new file mode 100644 index 0000000..56d5f2b --- /dev/null +++ b/transport/channel.go @@ -0,0 +1,23 @@ +package transport + +import ( + "context" + + "flux/core" +) + +type Receiver[T any] interface { + // Receive returns the next record, or ok=false once the stream is done or + // ctx is cancelled. + Receive(ctx context.Context) (core.StreamRecord[T], bool) + Close() error +} + +// Emitter mirrors core.Emitter plus Close. It is declared separately because +// core cannot import transport without an import cycle. +type Emitter[T any] interface { + // Emit blocks until the record is handed off, or returns ctx.Err() if ctx + // is cancelled first. + Emit(ctx context.Context, record core.StreamRecord[T]) error + Close() error +} diff --git a/transport/local_channel.go b/transport/local_channel.go new file mode 100644 index 0000000..2fac029 --- /dev/null +++ b/transport/local_channel.go @@ -0,0 +1,56 @@ +package transport + +import ( + "context" + + "flux/core" +) + +type LocalReceiveChannel[T any] struct { + recordCh <-chan core.StreamRecord[T] +} + +func NewLocalReceiveChannel[T any](ch <-chan core.StreamRecord[T]) *LocalReceiveChannel[T] { + return &LocalReceiveChannel[T]{ + recordCh: ch, + } +} + +func (c *LocalReceiveChannel[T]) Receive(ctx context.Context) (core.StreamRecord[T], bool) { + select { + case val, ok := <-c.recordCh: + return val, ok + case <-ctx.Done(): + var zero core.StreamRecord[T] + return zero, false + } +} + +func (c *LocalReceiveChannel[T]) Close() error { + // The emitter will close + return nil +} + +type LocalEmitChannel[T any] struct { + recordCh chan<- core.StreamRecord[T] +} + +func NewLocalEmitChannel[T any](ch chan<- core.StreamRecord[T]) *LocalEmitChannel[T] { + return &LocalEmitChannel[T]{ + recordCh: ch, + } +} + +func (l *LocalEmitChannel[T]) Emit(ctx context.Context, c core.StreamRecord[T]) error { + select { + case l.recordCh <- c: + return nil + case <-ctx.Done(): + return ctx.Err() + } +} + +func (l *LocalEmitChannel[T]) Close() error { + close(l.recordCh) + return nil +} -- cgit v1.3.1