Skip to content

Try to fix issue with empty list that is a singleton #2004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion __tests__/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ describe('List', () => {
it('chained mutations does not result in new empty list instance', () => {
const v1 = List(['x']);
const v2 = v1.withMutations(v => v.push('y').pop().pop());
expect(v2).toBe(List());
expect(v2).toEqual(List());
});

it('calling `clear` and `setSize` should set all items to undefined', () => {
Expand Down Expand Up @@ -927,6 +927,37 @@ describe('List', () => {
expect(isNaNValue(list.get(0))).toBe(true);
});

it('return a new emptyList if the emptyList has been mutated #2003', () => {
const emptyList = List();

const nonEmptyList = emptyList.withMutations(l => {
l.setSize(1);
l.set(0, 'a');
});

expect(nonEmptyList.size).toBe(1);
expect(nonEmptyList).toEqual(List.of('a'));
expect(emptyList.size).toBe(0);

const mutableList = emptyList.asMutable();

mutableList.setSize(1);
mutableList.set(0, 'b');

expect(mutableList.size).toBe(1);
expect(mutableList).toEqual(List.of('b'));

expect(emptyList.size).toBe(0);
expect(List().size).toBe(0);
});

it('Mutating empty list with a JS API should not mutate new instances', () => {
Object.assign(List(), List([1, 2]));

expect(List().size).toBe(0);
expect(List().toArray()).toEqual([]);
});

// Note: NaN is the only value not equal to itself. The isNaN() built-in
// function returns true for any non-numeric value, not just the NaN value.
function isNaNValue(value) {
Expand Down
3 changes: 1 addition & 2 deletions src/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,8 @@ function makeList(origin, capacity, level, root, tail, ownerID, hash) {
return list;
}

let EMPTY_LIST;
export function emptyList() {
return EMPTY_LIST || (EMPTY_LIST = makeList(0, 0, SHIFT));
return makeList(0, 0, SHIFT);
}

function updateList(list, index, value) {
Expand Down