Skip to content

Commit

Permalink
Fix reactiveArray.splice
Browse files Browse the repository at this point in the history
  • Loading branch information
mbeckem committed Aug 22, 2024
1 parent a4ec389 commit a617662
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
4 changes: 4 additions & 0 deletions packages/reactivity-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @conterra/reactivity-core

## v0.4.2

- Fix `reactiveArray.splice()`: new elements could not be inserted.

## v0.4.1

Improved `watch` ergonomics:
Expand Down
21 changes: 21 additions & 0 deletions packages/reactivity-core/collections/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,27 @@ describe("basic usage", () => {
expect(numbers).toEqual([3, 2, 1]);
});

it("removes elements using splice()", () => {
const array = reactiveArray([1, 2, 3]);
const removed = array.splice(1, 1);
expect(array.getItems()).toEqual([1, 3]);
expect(removed).toEqual([2]);
});

it("removes all elements using splice()", () => {
const array = reactiveArray([1, 2, 3]);
const removed = array.splice(0);
expect(array.getItems()).toEqual([]);
expect(removed).toEqual([1, 2, 3]);
});

it("supports adding elements using splice()", () => {
const array = reactiveArray([1]);
const removed = array.splice(1, 0, 2, 3);
expect(removed.length).toBe(0);
expect(array.getItems()).toEqual([1, 2, 3]);
});

it("supports iteration", () => {
const array = reactiveArray([1, 2, 3]);
expect(Array.from(array)).toMatchInlineSnapshot(`
Expand Down
13 changes: 7 additions & 6 deletions packages/reactivity-core/collections/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,16 +385,17 @@ class ReactiveArrayImpl<T> implements ReactiveArray<T> {
return cell!.value;
}

/* eslint-disable @typescript-eslint/no-explicit-any */
splice(...args: any[]): T[] {
const newItems: any[] | undefined = args[2];
const removedCells: Reactive<T>[] = (this.#items.splice as any)(...args);
if ((newItems != null && newItems.length !== 0) || removedCells.length !== 0) {
splice(start: number, deleteCount?: number, ...items: T[]): T[] {
const removedCells: Reactive<T>[] = this.#items.splice(
start,
deleteCount ?? this.#items.length,
...items.map((item) => reactive(item))
);
if ((items != null && items.length !== 0) || removedCells.length !== 0) {
this.#triggerStructuralChange();
}
return removedCells.map((cell) => cell.value);
}
/* eslint-enable @typescript-eslint/no-explicit-any */

sort(compare: (a: T, b: T) => number): void {
this.#items.sort((a, b) => compare(a.value, b.value));
Expand Down
2 changes: 1 addition & 1 deletion packages/reactivity-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@conterra/reactivity-core",
"type": "module",
"version": "0.4.1",
"version": "0.4.2",
"license": "Apache-2.0",
"description": "Framework agnostic library for building reactive applications.",
"repository": {
Expand Down

0 comments on commit a617662

Please sign in to comment.