Skip to content

Commit 3752356

Browse files
committed
Cursor more explicitly supports Maps. Does not support scalars
1 parent 24e9861 commit 3752356

File tree

4 files changed

+111
-76
lines changed

4 files changed

+111
-76
lines changed

__tests__/Cursor.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,21 @@ describe('Cursor', () => {
6363
var data = Immutable.fromJS(json);
6464
var aCursor = data.cursor('a', onChange);
6565

66-
var deepCursor = aCursor.cursor(['b', 'c']);
67-
expect(deepCursor.deref()).toBe(1);
66+
var deepCursor = aCursor.cursor('b');
67+
expect(deepCursor.get('c')).toBe(1);
6868

6969
// cursor edits return new cursors:
70-
var newDeepCursor = deepCursor.update(x => x + 1);
71-
expect(newDeepCursor.deref()).toBe(2);
70+
var newDeepCursor = deepCursor.update('c', x => x + 1);
71+
expect(newDeepCursor.get('c')).toBe(2);
72+
7273
expect(onChange).lastCalledWith(
7374
Immutable.fromJS({a:{b:{c:2}}}),
7475
data,
7576
['a', 'b', 'c']
7677
);
7778

78-
var newestDeepCursor = newDeepCursor.update(x => x + 1);
79-
expect(newestDeepCursor.deref()).toBe(3);
79+
var newestDeepCursor = newDeepCursor.update('c', x => x + 1);
80+
expect(newestDeepCursor.get('c')).toBe(3);
8081
expect(onChange).lastCalledWith(
8182
Immutable.fromJS({a:{b:{c:3}}}),
8283
Immutable.fromJS({a:{b:{c:2}}}),
@@ -87,9 +88,9 @@ describe('Cursor', () => {
8788
expect(data.toJS()).toEqual(json);
8889

8990
// as is the original cursor.
90-
expect(deepCursor.deref()).toBe(1);
91-
var otherNewDeepCursor = deepCursor.update(x => x + 10);
92-
expect(otherNewDeepCursor.deref()).toBe(11);
91+
expect(deepCursor.get('c')).toBe(1);
92+
var otherNewDeepCursor = deepCursor.update('c', x => x + 10);
93+
expect(otherNewDeepCursor.get('c')).toBe(11);
9394
expect(onChange).lastCalledWith(
9495
Immutable.fromJS({a:{b:{c:11}}}),
9596
data,
@@ -174,7 +175,7 @@ describe('Cursor', () => {
174175

175176
it('can create sub-cursors', () => {
176177
var onChange = jest.genMockFunction();
177-
var data = Immutable.fromJS({a:{b:{c:1}}});
178+
var data = Immutable.fromJS(json);
178179

179180
var cursorA = data.cursor('a', onChange);
180181
var cursorAB = cursorA.cursor('b', onChange);

dist/Immutable.js

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,19 +2098,38 @@ var Cursor = function Cursor(rootData, keyPath, onChange, length) {
20982098
return m.remove(key);
20992099
}), key);
21002100
},
2101-
update: function(keyOrFn, notSetValue, updater) {
2102-
return arguments.length === 1 ? updateCursor(this, keyOrFn) : updateCursor(this, (function(map) {
2103-
return map.update(keyOrFn, notSetValue, updater);
2104-
}), keyOrFn);
2105-
},
21062101
updateIn: function(keyPath, notSetValue, updater) {
21072102
return updateCursor(this, (function(m) {
21082103
return m.updateIn(keyPath, notSetValue, updater);
21092104
}), keyPath);
21102105
},
2106+
merge: function() {
2107+
var args = arguments;
2108+
return updateCursor(this, (function(m) {
2109+
return m.merge.apply(m, args);
2110+
}));
2111+
},
2112+
mergeWith: function() {
2113+
var args = arguments;
2114+
return updateCursor(this, (function(m) {
2115+
return m.mergeWith.apply(m, args);
2116+
}));
2117+
},
2118+
mergeDeep: function() {
2119+
var args = arguments;
2120+
return updateCursor(this, (function(m) {
2121+
return m.mergeDeep.apply(m, args);
2122+
}));
2123+
},
2124+
mergeDeepWith: function() {
2125+
var args = arguments;
2126+
return updateCursor(this, (function(m) {
2127+
return m.mergeDeepWith.apply(m, args);
2128+
}));
2129+
},
21112130
clear: function() {
21122131
return updateCursor(this, (function(m) {
2113-
return m.clear();
2132+
return m && m.clear();
21142133
}));
21152134
},
21162135
cursor: function(maybeKeyPath, onChange) {
@@ -2125,17 +2144,17 @@ var Cursor = function Cursor(rootData, keyPath, onChange, length) {
21252144
},
21262145
withMutations: function(fn) {
21272146
return updateCursor(this, (function(m) {
2128-
return (m || Map.empty()).withMutations(fn);
2147+
return m.withMutations(fn);
21292148
}));
21302149
},
21312150
asMutable: function() {
21322151
return updateCursor(this, (function(m) {
2133-
return (m || Map.empty()).asMutable();
2152+
return m.asMutable();
21342153
}));
21352154
},
21362155
asImmutable: function() {
21372156
return updateCursor(this, (function(m) {
2138-
return (m || Map.empty()).asImmutable();
2157+
return m.asImmutable();
21392158
}));
21402159
},
21412160
__iterate: function(fn, reverse) {
@@ -2165,6 +2184,7 @@ var Cursor = function Cursor(rootData, keyPath, onChange, length) {
21652184
}
21662185
}, {}, Sequence);
21672186
Cursor.prototype[DELETE] = Cursor.prototype.remove;
2187+
Cursor.prototype.update = MapPrototype.update;
21682188
function makeCursor(rootData, keyPath, onChange, value) {
21692189
if (arguments.length < 4) {
21702190
value = rootData.getIn(keyPath);
@@ -2179,20 +2199,23 @@ function subCursor(cursor, key, value) {
21792199
return makeCursor(cursor._rootData, cursor._keyPath.concat(key), cursor._onChange, value);
21802200
}
21812201
function updateCursor(cursor, changeFn, changeKey) {
2182-
var updatingSelf = arguments.length < 3 || (Array.isArray(changeKey) && changeKey.length === 0);
21832202
var rootData = cursor._rootData;
2184-
var editPath = cursor._keyPath || [];
2185-
if (!updatingSelf) {
2203+
var keyPath = cursor._keyPath;
2204+
var onChange = cursor._onChange;
2205+
var editPath = keyPath || [];
2206+
if (arguments.length > 2) {
21862207
editPath = editPath.concat(changeKey);
21872208
}
2188-
var updateIn = applyUpdateIn(cursor._keyPath, updatingSelf ? undefined : Map.empty(), changeFn);
2209+
var updateIn = applyUpdateIn(keyPath, changeFn);
21892210
var newRootData = typeof rootData.deref === 'function' ? updateCursor(rootData, updateIn, editPath) : updateIn(rootData);
2190-
cursor._onChange && cursor._onChange(newRootData, rootData, editPath);
2191-
return makeCursor(newRootData, cursor._keyPath, cursor._onChange);
2211+
onChange && onChange(newRootData, rootData, editPath);
2212+
return makeCursor(newRootData, keyPath, onChange);
21922213
}
2193-
function applyUpdateIn(keyPath, notSetValue, changeFn) {
2214+
function applyUpdateIn(keyPath, changeFn) {
21942215
return function(collection) {
2195-
return collection.updateIn(keyPath, notSetValue, changeFn);
2216+
return collection.updateIn(keyPath, (function(prev) {
2217+
return changeFn(prev instanceof Map ? prev : Map.empty());
2218+
}));
21962219
};
21972220
}
21982221
var Vector = function Vector() {

0 commit comments

Comments
 (0)