Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aeb2172

Browse filesBrowse files
authored
Fix issue where refs can recursively collide (immutable-js#1598)
Fixes immutable-js#1560
1 parent bcd1732 commit aeb2172
Copy full SHA for aeb2172

File tree

Expand file treeCollapse file tree

4 files changed

+50
-15
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+50
-15
lines changed

‎__tests__/Set.ts

Copy file name to clipboardExpand all lines: __tests__/Set.ts
+39-1Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
///<reference path='../resources/jest.d.ts'/>
99

1010
declare var Symbol: any;
11-
import { is, List, Map, OrderedSet, Seq, Set } from '../';
11+
import { fromJS, is, List, Map, OrderedSet, Seq, Set } from '../';
1212

1313
describe('Set', () => {
1414
it('accepts array of values', () => {
@@ -304,4 +304,42 @@ describe('Set', () => {
304304
expect(set.count(x => x % 2 === 0)).toEqual(2);
305305
expect(set.count(x => true)).toEqual(5);
306306
});
307+
308+
describe('"size" should correctly reflect the number of elements in a Set', () => {
309+
describe('deduplicating custom classes that invoke fromJS() as part of equality check', () => {
310+
class Entity {
311+
constructor(entityId, entityKey) {
312+
this.entityId = entityId;
313+
this.entityKey = entityKey;
314+
}
315+
asImmutable() {
316+
return fromJS({
317+
entityId: this.entityId,
318+
entityKey: this.entityKey,
319+
});
320+
}
321+
valueOf() {
322+
return this.asImmutable().toString();
323+
}
324+
}
325+
it('with mutations', () => {
326+
const testSet = Set().withMutations(mutableSet => {
327+
mutableSet.add(new Entity('hello', 'world'));
328+
mutableSet.add(new Entity('testing', 'immutable'));
329+
mutableSet.add(new Entity('hello', 'world'));
330+
});
331+
expect(testSet.size).toEqual(2);
332+
});
333+
it('without mutations', () => {
334+
const testSet0 = Set();
335+
const testSet1 = testSet0.add(new Entity('hello', 'world'));
336+
const testSet2 = testSet1.add(new Entity('testing', 'immutable'));
337+
const testSet3 = testSet2.add(new Entity('hello', 'world'));
338+
expect(testSet0.size).toEqual(0);
339+
expect(testSet1.size).toEqual(1);
340+
expect(testSet2.size).toEqual(2);
341+
expect(testSet3.size).toEqual(2);
342+
});
343+
});
344+
});
307345
});

‎src/List.js

Copy file name to clipboardExpand all lines: src/List.js
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
SHIFT,
1111
SIZE,
1212
MASK,
13-
DID_ALTER,
1413
OwnerID,
1514
MakeRef,
1615
SetRef,
@@ -441,7 +440,7 @@ function updateList(list, index, value) {
441440

442441
let newTail = list._tail;
443442
let newRoot = list._root;
444-
const didAlter = MakeRef(DID_ALTER);
443+
const didAlter = MakeRef();
445444
if (index >= getTailOffset(list._capacity)) {
446445
newTail = updateVNode(newTail, list.__ownerID, 0, index, value, didAlter);
447446
} else {
@@ -500,7 +499,9 @@ function updateVNode(node, ownerID, level, index, value, didAlter) {
500499
return node;
501500
}
502501

503-
SetRef(didAlter);
502+
if (didAlter) {
503+
SetRef(didAlter);
504+
}
504505

505506
newNode = editableVNode(node, ownerID);
506507
if (value === undefined && idx === newNode.array.length - 1) {

‎src/Map.js

Copy file name to clipboardExpand all lines: src/Map.js
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import {
1414
SIZE,
1515
MASK,
1616
NOT_SET,
17-
CHANGE_LENGTH,
18-
DID_ALTER,
1917
OwnerID,
2018
MakeRef,
2119
SetRef,
@@ -656,8 +654,8 @@ function updateMap(map, k, v) {
656654
newSize = 1;
657655
newRoot = new ArrayMapNode(map.__ownerID, [[k, v]]);
658656
} else {
659-
const didChangeSize = MakeRef(CHANGE_LENGTH);
660-
const didAlter = MakeRef(DID_ALTER);
657+
const didChangeSize = MakeRef();
658+
const didAlter = MakeRef();
661659
newRoot = updateNode(
662660
map._root,
663661
map.__ownerID,

‎src/TrieUtils.js

Copy file name to clipboardExpand all lines: src/TrieUtils.js
+5-7Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ export const MASK = SIZE - 1;
1818
export const NOT_SET = {};
1919

2020
// Boolean references, Rough equivalent of `bool &`.
21-
export const CHANGE_LENGTH = { value: false };
22-
export const DID_ALTER = { value: false };
23-
24-
export function MakeRef(ref) {
25-
ref.value = false;
26-
return ref;
21+
export function MakeRef() {
22+
return { value: false };
2723
}
2824

2925
export function SetRef(ref) {
30-
ref && (ref.value = true);
26+
if (ref) {
27+
ref.value = true;
28+
}
3129
}
3230

3331
// A function which returns a value representing an "owner" for transient writes

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.