Skip to content

Commit

Permalink
refactor: simplify computedArray getter
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Dec 30, 2024
1 parent c199506 commit e7ca90f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/unstable/computedArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { computed, ISignal } from '../index.js';

export function computedArray<I, O>(
arr: ISignal<I[]>,
getGetter: (item: ISignal<I>, index: number) => () => O
getter: (item: I, index: number) => O
) {
const length = computed(() => arr.get().length);
const keys = computed(
Expand All @@ -20,7 +20,7 @@ export function computedArray<I, O>(
while (array.length < length.get()) {
const index = array.length;
const item = computed(() => arr.get()[index]);
array.push(computed(getGetter(item, index)));
array.push(computed(() => getter(item.get(), index)));
}
if (array.length > length.get()) {
array.length = length.get();
Expand Down
26 changes: 11 additions & 15 deletions tests/computedArray.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { effect, signal } from './api';
test('should get updated item value', () => {
const src = signal([1]);
const arr = computedArray(src, item => {
return () => item.get() + 1;
return item + 1;
});
expect(arr[0]).toBe(2);
});
Expand All @@ -14,7 +14,7 @@ test('should watch item value change', () => {
const spy = vi.fn();
const src = signal([1]);
const arr = computedArray(src, item => {
return () => item.get() + 1;
return item + 1;
});
effect(() => {
spy();
Expand All @@ -29,7 +29,7 @@ test('should not trigger if item value did not change', () => {
const spy = vi.fn();
const src = signal([1]);
const arr = computedArray(src, item => {
return () => item.get() + 1;
return item + 1;
});
effect(() => {
spy();
Expand All @@ -44,12 +44,10 @@ test('should not trigger first item computed if source item did not change', ()
const spy = vi.fn();
const src = signal([1]);
const arr = computedArray(src, (item, i) => {
return () => {
if (i === 0) {
spy();
}
return item.get() + 1;
};
if (i === 0) {
spy();
}
return item + 1;
});
effect(() => arr[0]);
expect(spy).toHaveBeenCalledTimes(1);
Expand All @@ -63,7 +61,7 @@ test('should watch length change', () => {
const spy = vi.fn();
const src = signal([1]);
const arr = computedArray(src, item => {
return () => item.get() + 1;
return item + 1;
});
effect(() => {
spy();
Expand All @@ -80,7 +78,7 @@ test('should watch item remove', () => {
const spy = vi.fn();
const src = signal([1, 2]);
const arr = computedArray(src, item => {
return () => item.get() + 1;
return item + 1;
});
effect(() => {
spy();
Expand All @@ -97,10 +95,8 @@ test('should only trigger access items', () => {
const spy = vi.fn();
const src = signal([1, 2, 3, 4]);
const arr = computedArray(src, item => {
return () => {
spy();
return item.get() + 1;
};
spy();
return item + 1;
});
effect(() => {
arr[0];
Expand Down

0 comments on commit e7ca90f

Please sign in to comment.