From 7b4db77c4feac45b38cb707c7054ef202e76dbd2 Mon Sep 17 00:00:00 2001 From: Wang Guan Date: Wed, 8 May 2024 02:21:40 +0900 Subject: [PATCH 1/4] lazyThenable: simplify type & add executed() --- src/concurrency/lazy-thenable.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/concurrency/lazy-thenable.ts b/src/concurrency/lazy-thenable.ts index d1954ed..6e1912a 100644 --- a/src/concurrency/lazy-thenable.ts +++ b/src/concurrency/lazy-thenable.ts @@ -3,14 +3,18 @@ * @param io * @return */ -export function lazyThenable(io: () => T): PromiseLike> { +export function lazyThenable(io: () => T): LazyThenable> { let r: null | Promise> = null; return { - then( - onfulfilled?: ((value: Awaited) => PromiseLike | TResult1) | undefined | null, - onrejected?: ((reason: any) => PromiseLike | TResult2) | undefined | null, - ): PromiseLike { + get executed(): boolean { + return r !== null; + }, + then(onfulfilled, onrejected): PromiseLike { return (r ??= Promise.resolve(io())).then(onfulfilled, onrejected); }, }; } + +interface LazyThenable extends PromiseLike { + get executed(): boolean; +} From d1bfc2787666509e0f009e67520bba736c33c99e Mon Sep 17 00:00:00 2001 From: Wang Guan Date: Sat, 11 May 2024 01:58:06 +0900 Subject: [PATCH 2/4] test --- src/concurrency/lazy-thenable.spec.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/concurrency/lazy-thenable.spec.ts b/src/concurrency/lazy-thenable.spec.ts index 41d6463..2f747b8 100644 --- a/src/concurrency/lazy-thenable.spec.ts +++ b/src/concurrency/lazy-thenable.spec.ts @@ -7,10 +7,13 @@ describe(lazyThenable, () => { const lazy1 = lazyThenable(async () => ++called); expect(called).toEqual(0); + expect(lazy1.executed).toBe(false) const converted = Promise.resolve(lazy1); expect(called).toEqual(0); + expect(lazy1.executed).toBe(true) await wait(0); + expect(lazy1.executed).toBe(true) expect(called).toEqual(1); expect(await converted).toEqual(1); expect(await lazy1).toEqual(1); From faa41ca7f971f2039693d8a9e91328d0957568de Mon Sep 17 00:00:00 2001 From: Wang Guan Date: Sat, 11 May 2024 02:02:01 +0900 Subject: [PATCH 3/4] make linter happy --- src/concurrency/lazy-thenable.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/concurrency/lazy-thenable.spec.ts b/src/concurrency/lazy-thenable.spec.ts index 2f747b8..19bb97d 100644 --- a/src/concurrency/lazy-thenable.spec.ts +++ b/src/concurrency/lazy-thenable.spec.ts @@ -7,13 +7,13 @@ describe(lazyThenable, () => { const lazy1 = lazyThenable(async () => ++called); expect(called).toEqual(0); - expect(lazy1.executed).toBe(false) + expect(lazy1.executed).toBe(false); const converted = Promise.resolve(lazy1); expect(called).toEqual(0); - expect(lazy1.executed).toBe(true) + expect(lazy1.executed).toBe(true); await wait(0); - expect(lazy1.executed).toBe(true) + expect(lazy1.executed).toBe(true); expect(called).toEqual(1); expect(await converted).toEqual(1); expect(await lazy1).toEqual(1); From c2682e22eed6eb1121e1d2ab9b211e31ebc926d8 Mon Sep 17 00:00:00 2001 From: Wang Guan Date: Sat, 11 May 2024 02:08:43 +0900 Subject: [PATCH 4/4] fix --- src/concurrency/lazy-thenable.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/concurrency/lazy-thenable.spec.ts b/src/concurrency/lazy-thenable.spec.ts index 19bb97d..6d05f47 100644 --- a/src/concurrency/lazy-thenable.spec.ts +++ b/src/concurrency/lazy-thenable.spec.ts @@ -11,7 +11,7 @@ describe(lazyThenable, () => { const converted = Promise.resolve(lazy1); expect(called).toEqual(0); - expect(lazy1.executed).toBe(true); + expect(lazy1.executed).toBe(false); await wait(0); expect(lazy1.executed).toBe(true); expect(called).toEqual(1);