Skip to content

Commit b1a8719

Browse files
committed
generalize cursor construction, add updateIn to Cursor
1 parent 2ced269 commit b1a8719

File tree

5 files changed

+177
-151
lines changed

5 files changed

+177
-151
lines changed

dist/Immutable.js

Lines changed: 115 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,101 +1526,6 @@ function interposeFactory(sequence, separator) {
15261526
};
15271527
return interposedSequence;
15281528
}
1529-
var Cursor = function Cursor(rootData, keyPath, onChange, length) {
1530-
this.length = length;
1531-
this._rootData = rootData;
1532-
this._keyPath = keyPath;
1533-
this._onChange = onChange;
1534-
};
1535-
($traceurRuntime.createClass)(Cursor, {
1536-
equals: function(second) {
1537-
return is(this.deref(), second && (typeof second.deref === 'function' ? second.deref() : second));
1538-
},
1539-
deref: function(notSetValue) {
1540-
return this._rootData.getIn(this._keyPath, notSetValue);
1541-
},
1542-
get: function(key, notSetValue) {
1543-
if (Array.isArray(key) && key.length === 0) {
1544-
return this;
1545-
}
1546-
var value = this._rootData.getIn(this._keyPath.concat(key), NOT_SET);
1547-
return value === NOT_SET ? notSetValue : wrappedValue(this, key, value);
1548-
},
1549-
set: function(key, value) {
1550-
return updateCursor(this, (function(m) {
1551-
return m.set(key, value);
1552-
}), key);
1553-
},
1554-
remove: function(key) {
1555-
return updateCursor(this, (function(m) {
1556-
return m.remove(key);
1557-
}), key);
1558-
},
1559-
clear: function() {
1560-
return updateCursor(this, (function(m) {
1561-
return m.clear();
1562-
}));
1563-
},
1564-
update: function(keyOrFn, notSetValue, updater) {
1565-
return arguments.length === 1 ? updateCursor(this, keyOrFn) : updateCursor(this, (function(map) {
1566-
return map.update(keyOrFn, notSetValue, updater);
1567-
}), keyOrFn);
1568-
},
1569-
withMutations: function(fn) {
1570-
return updateCursor(this, (function(m) {
1571-
return (m || Map.empty()).withMutations(fn);
1572-
}));
1573-
},
1574-
cursor: function(subKey) {
1575-
return Array.isArray(subKey) && subKey.length === 0 ? this : subCursor(this, subKey);
1576-
},
1577-
__iterate: function(fn, reverse) {
1578-
var $__0 = this;
1579-
var deref = this.deref();
1580-
return deref && deref.__iterate ? deref.__iterate((function(v, k) {
1581-
return fn(wrappedValue($__0, k, v), k, $__0);
1582-
}), reverse) : 0;
1583-
},
1584-
__iterator: function(type, reverse) {
1585-
var $__0 = this;
1586-
var deref = this.deref();
1587-
var iterator = deref && deref.__iterator && deref.__iterator(ITERATE_ENTRIES, reverse);
1588-
return new Iterator((function() {
1589-
if (!iterator) {
1590-
return iteratorDone();
1591-
}
1592-
var step = iterator.next();
1593-
if (step.done) {
1594-
return step;
1595-
}
1596-
var entry = step.value;
1597-
var k = entry[0];
1598-
var v = entry[1];
1599-
return iteratorValue(type, k, wrappedValue($__0, k, v), step);
1600-
}));
1601-
}
1602-
}, {}, Sequence);
1603-
Cursor.prototype[DELETE] = Cursor.prototype.remove;
1604-
Cursor.prototype.getIn = Cursor.prototype.get;
1605-
function makeCursor(rootData, keyPath, onChange, value) {
1606-
if (arguments.length < 4) {
1607-
value = rootData.getIn(keyPath);
1608-
}
1609-
var length = value instanceof Sequence ? value.length : null;
1610-
return new Cursor(rootData, keyPath, onChange, length);
1611-
}
1612-
function wrappedValue(cursor, key, value) {
1613-
return value instanceof Sequence ? subCursor(cursor, key, value) : value;
1614-
}
1615-
function subCursor(cursor, key, value) {
1616-
return makeCursor(cursor._rootData, cursor._keyPath.concat(key), cursor._onChange, value);
1617-
}
1618-
function updateCursor(cursor, changeFn, changeKey) {
1619-
var newRootData = cursor._rootData.updateIn(cursor._keyPath, changeKey ? Map.empty() : undefined, changeFn);
1620-
var keyPath = cursor._keyPath || [];
1621-
cursor._onChange && cursor._onChange.call(undefined, newRootData, cursor._rootData, changeKey ? keyPath.concat(changeKey) : keyPath);
1622-
return makeCursor(newRootData, cursor._keyPath, cursor._onChange);
1623-
}
16241529
var Map = function Map(sequence) {
16251530
var map = $Map.empty();
16261531
return sequence ? sequence.constructor === $Map ? sequence : map.merge(sequence) : map;
@@ -1640,12 +1545,12 @@ var $Map = Map;
16401545
return updateMap(this, k, NOT_SET);
16411546
},
16421547
update: function(k, notSetValue, updater) {
1643-
return arguments.length === 1 ? this.updateIn([], null, k) : this.updateIn([k], notSetValue, updater);
1548+
return arguments.length === 1 ? this.updateIn([], undefined, k) : this.updateIn([k], notSetValue, updater);
16441549
},
16451550
updateIn: function(keyPath, notSetValue, updater) {
1646-
var $__14;
16471551
if (!updater) {
1648-
($__14 = [notSetValue, updater], updater = $__14[0], notSetValue = $__14[1], $__14);
1552+
updater = notSetValue;
1553+
notSetValue = undefined;
16491554
}
16501555
return updateInDeepMap(this, keyPath, notSetValue, updater, 0);
16511556
},
@@ -1680,15 +1585,8 @@ var $Map = Map;
16801585
seqs[$__5 - 1] = arguments[$__5];
16811586
return mergeIntoMapWith(this, deepMerger(merger), seqs);
16821587
},
1683-
cursor: function(keyPath, onChange) {
1684-
if (!onChange && typeof keyPath === 'function') {
1685-
onChange = keyPath;
1686-
keyPath = [];
1687-
} else if (arguments.length === 0) {
1688-
keyPath = [];
1689-
} else if (!Array.isArray(keyPath)) {
1690-
keyPath = [keyPath];
1691-
}
1588+
cursor: function(maybeKeyPath, onChange) {
1589+
var keyPath = arguments.length === 0 || typeof maybeKeyPath === 'function' && (onChange = maybeKeyPath) ? [] : Array.isArray(maybeKeyPath) ? maybeKeyPath : [maybeKeyPath];
16921590
return makeCursor(this, keyPath, onChange);
16931591
},
16941592
withMutations: function(fn) {
@@ -2167,6 +2065,116 @@ function spliceOut(array, idx, canEdit) {
21672065
var MAX_BITMAP_SIZE = SIZE / 2;
21682066
var MIN_ARRAY_SIZE = SIZE / 4;
21692067
var EMPTY_MAP;
2068+
var Cursor = function Cursor(rootData, keyPath, onChange, length) {
2069+
this.length = length;
2070+
this._rootData = rootData;
2071+
this._keyPath = keyPath;
2072+
this._onChange = onChange;
2073+
};
2074+
($traceurRuntime.createClass)(Cursor, {
2075+
equals: function(second) {
2076+
return is(this.deref(), second && (typeof second.deref === 'function' ? second.deref() : second));
2077+
},
2078+
deref: function(notSetValue) {
2079+
return this._rootData.getIn(this._keyPath, notSetValue);
2080+
},
2081+
get: function(key, notSetValue) {
2082+
return this.getIn([key], notSetValue);
2083+
},
2084+
getIn: function(searchKeyPath, notSetValue) {
2085+
if (!searchKeyPath || (Array.isArray(searchKeyPath) && searchKeyPath.length === 0)) {
2086+
return this;
2087+
}
2088+
var value = this._rootData.getIn(this._keyPath.concat(searchKeyPath), NOT_SET);
2089+
return value === NOT_SET ? notSetValue : wrappedValue(this, searchKeyPath, value);
2090+
},
2091+
set: function(key, value) {
2092+
return updateCursor(this, (function(m) {
2093+
return m.set(key, value);
2094+
}), key);
2095+
},
2096+
remove: function(key) {
2097+
return updateCursor(this, (function(m) {
2098+
return m.remove(key);
2099+
}), key);
2100+
},
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+
},
2106+
updateIn: function(keyPath, notSetValue, updater) {
2107+
return updateCursor(this, (function(m) {
2108+
return m.updateIn(keyPath, notSetValue, updater);
2109+
}), keyPath);
2110+
},
2111+
clear: function() {
2112+
return updateCursor(this, (function(m) {
2113+
return m.clear();
2114+
}));
2115+
},
2116+
cursor: function(maybeKeyPath, onChange) {
2117+
var keyPath = arguments.length === 0 || typeof maybeKeyPath === 'function' && (onChange = maybeKeyPath) ? [] : Array.isArray(maybeKeyPath) ? maybeKeyPath : [maybeKeyPath];
2118+
if (!onChange) {
2119+
if (keyPath.length === 0) {
2120+
return this;
2121+
}
2122+
return subCursor(this, keyPath);
2123+
}
2124+
return makeCursor(this, keyPath, onChange, this.deref().getIn(keyPath));
2125+
},
2126+
withMutations: function(fn) {
2127+
return updateCursor(this, (function(m) {
2128+
return (m || Map.empty()).withMutations(fn);
2129+
}));
2130+
},
2131+
__iterate: function(fn, reverse) {
2132+
var $__0 = this;
2133+
var deref = this.deref();
2134+
return deref && deref.__iterate ? deref.__iterate((function(v, k) {
2135+
return fn(wrappedValue($__0, k, v), k, $__0);
2136+
}), reverse) : 0;
2137+
},
2138+
__iterator: function(type, reverse) {
2139+
var $__0 = this;
2140+
var deref = this.deref();
2141+
var iterator = deref && deref.__iterator && deref.__iterator(ITERATE_ENTRIES, reverse);
2142+
return new Iterator((function() {
2143+
if (!iterator) {
2144+
return iteratorDone();
2145+
}
2146+
var step = iterator.next();
2147+
if (step.done) {
2148+
return step;
2149+
}
2150+
var entry = step.value;
2151+
var k = entry[0];
2152+
var v = entry[1];
2153+
return iteratorValue(type, k, wrappedValue($__0, k, v), step);
2154+
}));
2155+
}
2156+
}, {}, Sequence);
2157+
Cursor.prototype[DELETE] = Cursor.prototype.remove;
2158+
function makeCursor(rootData, keyPath, onChange, value) {
2159+
if (arguments.length < 4) {
2160+
value = rootData.getIn(keyPath);
2161+
}
2162+
var length = value instanceof Sequence ? value.length : null;
2163+
return new Cursor(rootData, keyPath, onChange, length);
2164+
}
2165+
function wrappedValue(cursor, key, value) {
2166+
return value instanceof Sequence ? subCursor(cursor, key, value) : value;
2167+
}
2168+
function subCursor(cursor, key, value) {
2169+
return makeCursor(cursor._rootData, cursor._keyPath.concat(key), cursor._onChange, value);
2170+
}
2171+
function updateCursor(cursor, changeFn, changeKey) {
2172+
var updatingSelf = arguments.length < 3 || (Array.isArray(changeKey) && changeKey.length === 0);
2173+
var newRootData = cursor._rootData.updateIn(cursor._keyPath, updatingSelf ? undefined : Map.empty(), changeFn);
2174+
var keyPath = cursor._keyPath || [];
2175+
cursor._onChange && cursor._onChange.call(undefined, newRootData, cursor._rootData, updatingSelf ? keyPath : keyPath.concat(changeKey));
2176+
return makeCursor(newRootData, cursor._keyPath, cursor._onChange);
2177+
}
21702178
var Vector = function Vector() {
21712179
for (var values = [],
21722180
$__6 = 0; $__6 < arguments.length; $__6++)

0 commit comments

Comments
 (0)