From cdddeed000afac8c8b7fd76fa9f61d5d932d63ac Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 25 Sep 2026 14:46:03 -0400 Subject: [PATCH 1/2] keepLatest: keep the initial value when it starts out loading If `when()` was true on the first run, keepLatest returned the value without saving it as `previous`. The next run with an empty value then returned undefined instead of the initial value. Co-Authored-By: Claude Opus 5.5 (1M context) --- reactiveweb/src/keep-latest.ts | 2 +- .../tests/utils/keep-latest/js-test.ts | 20 +++++++++++++++++++ .../tests/utils/keep-latest/js-test.ts | 20 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/reactiveweb/src/keep-latest.ts b/reactiveweb/src/keep-latest.ts index b2fb052e..b2f97ec0 100644 --- a/reactiveweb/src/keep-latest.ts +++ b/reactiveweb/src/keep-latest.ts @@ -107,7 +107,7 @@ export function keepLatest({ when, value: valueFn }: Options this.isLoading, + value: () => this.value, + }); + } + + const instance = new Test(); + + assert.strictEqual(instance.data, 3); + + instance.value = undefined; + + assert.strictEqual(instance.data, 3, 'returns the initial value while the value is empty'); + }); + test('it works with array values correctly', async function (assert) { class Test { @tracked isLoading = false; diff --git a/tests/vite-app/tests/utils/keep-latest/js-test.ts b/tests/vite-app/tests/utils/keep-latest/js-test.ts index 2bc2c9a5..5244725c 100644 --- a/tests/vite-app/tests/utils/keep-latest/js-test.ts +++ b/tests/vite-app/tests/utils/keep-latest/js-test.ts @@ -68,6 +68,26 @@ module('Utils | keepLatest | js', function (hooks) { assert.strictEqual(instance.data, 3); }); + test('it keeps the initial value when it starts out loading', async function (assert) { + class Test { + @tracked isLoading = true; + @tracked value?: number = 3; + + @use data = keepLatest({ + when: () => this.isLoading, + value: () => this.value, + }); + } + + const instance = new Test(); + + assert.strictEqual(instance.data, 3); + + instance.value = undefined; + + assert.strictEqual(instance.data, 3, 'returns the initial value while the value is empty'); + }); + test('it works with array values correctly', async function (assert) { class Test { @tracked isLoading = false; From 711c58c65321212663a0fb1ef5f07696cc6e008b Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 25 Sep 2026 14:51:25 -0400 Subject: [PATCH 2/2] Add a rendering test for the initial keepLatest value Co-Authored-By: Claude Opus 5.5 (1M context) --- .../utils/keep-latest/rendering-test.gts | 23 +++++++++++++++++++ .../utils/keep-latest/rendering-test.gts | 23 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/tests/test-app/tests/utils/keep-latest/rendering-test.gts b/tests/test-app/tests/utils/keep-latest/rendering-test.gts index f5695f29..68ad97a1 100644 --- a/tests/test-app/tests/utils/keep-latest/rendering-test.gts +++ b/tests/test-app/tests/utils/keep-latest/rendering-test.gts @@ -59,6 +59,29 @@ module('Utils | keepLatest | rendering', function (hooks) { assert.dom().hasText('2'); }); + test('it keeps the initial value when it starts out loading', async function (assert) { + class Test { + @tracked isLoading = true; + @tracked value?: number = 3; + + @use data = keepLatest({ + when: () => this.isLoading, + value: () => this.value, + }); + } + + const instance = new Test(); + + await render(); + + assert.dom().hasText('3'); + + instance.value = undefined; + await settled(); + + assert.dom().hasText('3', 'renders the initial value while the value is empty'); + }); + test('if the previous value is not empty, and the current value is empty', async function (assert) { class Test { @tracked x: number[] | number = []; diff --git a/tests/vite-app/tests/utils/keep-latest/rendering-test.gts b/tests/vite-app/tests/utils/keep-latest/rendering-test.gts index f5695f29..68ad97a1 100644 --- a/tests/vite-app/tests/utils/keep-latest/rendering-test.gts +++ b/tests/vite-app/tests/utils/keep-latest/rendering-test.gts @@ -59,6 +59,29 @@ module('Utils | keepLatest | rendering', function (hooks) { assert.dom().hasText('2'); }); + test('it keeps the initial value when it starts out loading', async function (assert) { + class Test { + @tracked isLoading = true; + @tracked value?: number = 3; + + @use data = keepLatest({ + when: () => this.isLoading, + value: () => this.value, + }); + } + + const instance = new Test(); + + await render(); + + assert.dom().hasText('3'); + + instance.value = undefined; + await settled(); + + assert.dom().hasText('3', 'renders the initial value while the value is empty'); + }); + test('if the previous value is not empty, and the current value is empty', async function (assert) { class Test { @tracked x: number[] | number = [];