From c5b4831b61bf6a0d78a9d6fc14e15b064655fbe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E5=8F=AF?= Date: Thu, 1 Aug 2024 11:41:53 +0800 Subject: [PATCH] chore: improve if condition --- README.md | 2 -- dataloader.go | 14 +++++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8e9384b..1abc418 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,6 @@ API Design func New[K comparable, V any](loader Loader[K, V], options ...Option) Interface[K, V] type Interface[K comparable, V any] interface { - // Go loads a single key asynchronously - Go(context.Context, K) <-chan Result[V] // Load loads a single key Load(context.Context, K) Result[V] // LoadMany loads multiple keys diff --git a/dataloader.go b/dataloader.go index e691824..0f8ac5b 100644 --- a/dataloader.go +++ b/dataloader.go @@ -132,13 +132,13 @@ func (d *dataLoader[K, V]) goLoad(ctx context.Context, key K) <-chan Result[V] { // If there are no keys in the current batch, schedule a new batch timer d.stopSchedule = make(chan struct{}) go d.scheduleBatch(ctx, d.stopSchedule) - } - - // Check if the key is in flight - if chs, ok := d.chs[key]; ok { - d.chs[key] = append(chs, ch) - d.mu.Unlock() - return ch + } else { + // Check if the key is in flight + if chs, ok := d.chs[key]; ok { + d.chs[key] = append(chs, ch) + d.mu.Unlock() + return ch + } } // Add the key and channel to the current batch