1 | /**
|
---|
2 | * Copyright (c) 2014-present, Facebook, Inc.
|
---|
3 | *
|
---|
4 | * This source code is licensed under the MIT license found in the
|
---|
5 | * LICENSE file in the root directory of this source tree.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Cursor is expected to be required in a node or other CommonJS context:
|
---|
10 | *
|
---|
11 | * var Cursor = require('immutable/contrib/cursor');
|
---|
12 | *
|
---|
13 | * If you wish to use it in the browser, please check out Browserify or WebPack!
|
---|
14 | */
|
---|
15 |
|
---|
16 | var Immutable = require('../../');
|
---|
17 | var Iterable = Immutable.Iterable;
|
---|
18 | var Iterator = Iterable.Iterator;
|
---|
19 | var Seq = Immutable.Seq;
|
---|
20 | var Map = Immutable.Map;
|
---|
21 | var Record = Immutable.Record;
|
---|
22 |
|
---|
23 |
|
---|
24 | function cursorFrom(rootData, keyPath, onChange) {
|
---|
25 | if (arguments.length === 1) {
|
---|
26 | keyPath = [];
|
---|
27 | } else if (typeof keyPath === 'function') {
|
---|
28 | onChange = keyPath;
|
---|
29 | keyPath = [];
|
---|
30 | } else {
|
---|
31 | keyPath = valToKeyPath(keyPath);
|
---|
32 | }
|
---|
33 | return makeCursor(rootData, keyPath, onChange);
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | var KeyedCursorPrototype = Object.create(Seq.Keyed.prototype);
|
---|
38 | var IndexedCursorPrototype = Object.create(Seq.Indexed.prototype);
|
---|
39 |
|
---|
40 | function KeyedCursor(rootData, keyPath, onChange, size) {
|
---|
41 | this.size = size;
|
---|
42 | this._rootData = rootData;
|
---|
43 | this._keyPath = keyPath;
|
---|
44 | this._onChange = onChange;
|
---|
45 | }
|
---|
46 | KeyedCursorPrototype.constructor = KeyedCursor;
|
---|
47 |
|
---|
48 | function IndexedCursor(rootData, keyPath, onChange, size) {
|
---|
49 | this.size = size;
|
---|
50 | this._rootData = rootData;
|
---|
51 | this._keyPath = keyPath;
|
---|
52 | this._onChange = onChange;
|
---|
53 | }
|
---|
54 | IndexedCursorPrototype.constructor = IndexedCursor;
|
---|
55 |
|
---|
56 | KeyedCursorPrototype.toString = function() {
|
---|
57 | return this.__toString('Cursor {', '}');
|
---|
58 | }
|
---|
59 | IndexedCursorPrototype.toString = function() {
|
---|
60 | return this.__toString('Cursor [', ']');
|
---|
61 | }
|
---|
62 |
|
---|
63 | KeyedCursorPrototype.deref =
|
---|
64 | KeyedCursorPrototype.valueOf =
|
---|
65 | IndexedCursorPrototype.deref =
|
---|
66 | IndexedCursorPrototype.valueOf = function(notSetValue) {
|
---|
67 | return this._rootData.getIn(this._keyPath, notSetValue);
|
---|
68 | }
|
---|
69 |
|
---|
70 | KeyedCursorPrototype.get =
|
---|
71 | IndexedCursorPrototype.get = function(key, notSetValue) {
|
---|
72 | return this.getIn([key], notSetValue);
|
---|
73 | }
|
---|
74 |
|
---|
75 | KeyedCursorPrototype.getIn =
|
---|
76 | IndexedCursorPrototype.getIn = function(keyPath, notSetValue) {
|
---|
77 | keyPath = listToKeyPath(keyPath);
|
---|
78 | if (keyPath.length === 0) {
|
---|
79 | return this;
|
---|
80 | }
|
---|
81 | var value = this._rootData.getIn(newKeyPath(this._keyPath, keyPath), NOT_SET);
|
---|
82 | return value === NOT_SET ? notSetValue : wrappedValue(this, keyPath, value);
|
---|
83 | }
|
---|
84 |
|
---|
85 | IndexedCursorPrototype.set =
|
---|
86 | KeyedCursorPrototype.set = function(key, value) {
|
---|
87 | if(arguments.length === 1) {
|
---|
88 | return updateCursor(this, function() { return key; }, []);
|
---|
89 | } else {
|
---|
90 | return updateCursor(this, function (m) { return m.set(key, value); }, [key]);
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | IndexedCursorPrototype.push = function(/* values */) {
|
---|
95 | var args = arguments;
|
---|
96 | return updateCursor(this, function (m) {
|
---|
97 | return m.push.apply(m, args);
|
---|
98 | });
|
---|
99 | }
|
---|
100 |
|
---|
101 | IndexedCursorPrototype.pop = function() {
|
---|
102 | return updateCursor(this, function (m) {
|
---|
103 | return m.pop();
|
---|
104 | });
|
---|
105 | }
|
---|
106 |
|
---|
107 | IndexedCursorPrototype.unshift = function(/* values */) {
|
---|
108 | var args = arguments;
|
---|
109 | return updateCursor(this, function (m) {
|
---|
110 | return m.unshift.apply(m, args);
|
---|
111 | });
|
---|
112 | }
|
---|
113 |
|
---|
114 | IndexedCursorPrototype.shift = function() {
|
---|
115 | return updateCursor(this, function (m) {
|
---|
116 | return m.shift();
|
---|
117 | });
|
---|
118 | }
|
---|
119 |
|
---|
120 | IndexedCursorPrototype.setIn =
|
---|
121 | KeyedCursorPrototype.setIn = Map.prototype.setIn;
|
---|
122 |
|
---|
123 | KeyedCursorPrototype.remove =
|
---|
124 | KeyedCursorPrototype['delete'] =
|
---|
125 | IndexedCursorPrototype.remove =
|
---|
126 | IndexedCursorPrototype['delete'] = function(key) {
|
---|
127 | return updateCursor(this, function (m) { return m.remove(key); }, [key]);
|
---|
128 | }
|
---|
129 |
|
---|
130 | IndexedCursorPrototype.removeIn =
|
---|
131 | IndexedCursorPrototype.deleteIn =
|
---|
132 | KeyedCursorPrototype.removeIn =
|
---|
133 | KeyedCursorPrototype.deleteIn = Map.prototype.deleteIn;
|
---|
134 |
|
---|
135 | KeyedCursorPrototype.clear =
|
---|
136 | IndexedCursorPrototype.clear = function() {
|
---|
137 | return updateCursor(this, function (m) { return m.clear(); });
|
---|
138 | }
|
---|
139 |
|
---|
140 | IndexedCursorPrototype.update =
|
---|
141 | KeyedCursorPrototype.update = function(keyOrFn, notSetValue, updater) {
|
---|
142 | return arguments.length === 1 ?
|
---|
143 | updateCursor(this, keyOrFn) :
|
---|
144 | this.updateIn([keyOrFn], notSetValue, updater);
|
---|
145 | }
|
---|
146 |
|
---|
147 | IndexedCursorPrototype.updateIn =
|
---|
148 | KeyedCursorPrototype.updateIn = function(keyPath, notSetValue, updater) {
|
---|
149 | return updateCursor(this, function (m) {
|
---|
150 | return m.updateIn(keyPath, notSetValue, updater);
|
---|
151 | }, keyPath);
|
---|
152 | }
|
---|
153 |
|
---|
154 | IndexedCursorPrototype.merge =
|
---|
155 | KeyedCursorPrototype.merge = function(/*...iters*/) {
|
---|
156 | var args = arguments;
|
---|
157 | return updateCursor(this, function (m) {
|
---|
158 | return m.merge.apply(m, args);
|
---|
159 | });
|
---|
160 | }
|
---|
161 |
|
---|
162 | IndexedCursorPrototype.mergeWith =
|
---|
163 | KeyedCursorPrototype.mergeWith = function(merger/*, ...iters*/) {
|
---|
164 | var args = arguments;
|
---|
165 | return updateCursor(this, function (m) {
|
---|
166 | return m.mergeWith.apply(m, args);
|
---|
167 | });
|
---|
168 | }
|
---|
169 |
|
---|
170 | IndexedCursorPrototype.mergeIn =
|
---|
171 | KeyedCursorPrototype.mergeIn = Map.prototype.mergeIn;
|
---|
172 |
|
---|
173 | IndexedCursorPrototype.mergeDeep =
|
---|
174 | KeyedCursorPrototype.mergeDeep = function(/*...iters*/) {
|
---|
175 | var args = arguments;
|
---|
176 | return updateCursor(this, function (m) {
|
---|
177 | return m.mergeDeep.apply(m, args);
|
---|
178 | });
|
---|
179 | }
|
---|
180 |
|
---|
181 | IndexedCursorPrototype.mergeDeepWith =
|
---|
182 | KeyedCursorPrototype.mergeDeepWith = function(merger/*, ...iters*/) {
|
---|
183 | var args = arguments;
|
---|
184 | return updateCursor(this, function (m) {
|
---|
185 | return m.mergeDeepWith.apply(m, args);
|
---|
186 | });
|
---|
187 | }
|
---|
188 |
|
---|
189 | IndexedCursorPrototype.mergeDeepIn =
|
---|
190 | KeyedCursorPrototype.mergeDeepIn = Map.prototype.mergeDeepIn;
|
---|
191 |
|
---|
192 | KeyedCursorPrototype.withMutations =
|
---|
193 | IndexedCursorPrototype.withMutations = function(fn) {
|
---|
194 | return updateCursor(this, function (m) {
|
---|
195 | return (m || Map()).withMutations(fn);
|
---|
196 | });
|
---|
197 | }
|
---|
198 |
|
---|
199 | KeyedCursorPrototype.cursor =
|
---|
200 | IndexedCursorPrototype.cursor = function(subKeyPath) {
|
---|
201 | subKeyPath = valToKeyPath(subKeyPath);
|
---|
202 | return subKeyPath.length === 0 ? this : subCursor(this, subKeyPath);
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * All iterables need to implement __iterate
|
---|
207 | */
|
---|
208 | KeyedCursorPrototype.__iterate =
|
---|
209 | IndexedCursorPrototype.__iterate = function(fn, reverse) {
|
---|
210 | var cursor = this;
|
---|
211 | var deref = cursor.deref();
|
---|
212 | return deref && deref.__iterate ? deref.__iterate(
|
---|
213 | function (v, k) { return fn(wrappedValue(cursor, [k], v), k, cursor); },
|
---|
214 | reverse
|
---|
215 | ) : 0;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * All iterables need to implement __iterator
|
---|
220 | */
|
---|
221 | KeyedCursorPrototype.__iterator =
|
---|
222 | IndexedCursorPrototype.__iterator = function(type, reverse) {
|
---|
223 | var deref = this.deref();
|
---|
224 | var cursor = this;
|
---|
225 | var iterator = deref && deref.__iterator &&
|
---|
226 | deref.__iterator(Iterator.ENTRIES, reverse);
|
---|
227 | return new Iterator(function () {
|
---|
228 | if (!iterator) {
|
---|
229 | return { value: undefined, done: true };
|
---|
230 | }
|
---|
231 | var step = iterator.next();
|
---|
232 | if (step.done) {
|
---|
233 | return step;
|
---|
234 | }
|
---|
235 | var entry = step.value;
|
---|
236 | var k = entry[0];
|
---|
237 | var v = wrappedValue(cursor, [k], entry[1]);
|
---|
238 | return {
|
---|
239 | value: type === Iterator.KEYS ? k : type === Iterator.VALUES ? v : [k, v],
|
---|
240 | done: false
|
---|
241 | };
|
---|
242 | });
|
---|
243 | }
|
---|
244 |
|
---|
245 | KeyedCursor.prototype = KeyedCursorPrototype;
|
---|
246 | IndexedCursor.prototype = IndexedCursorPrototype;
|
---|
247 |
|
---|
248 |
|
---|
249 | var NOT_SET = {}; // Sentinel value
|
---|
250 |
|
---|
251 | function makeCursor(rootData, keyPath, onChange, value) {
|
---|
252 | if (arguments.length < 4) {
|
---|
253 | value = rootData.getIn(keyPath);
|
---|
254 | }
|
---|
255 | var size = value && value.size;
|
---|
256 | var CursorClass = Iterable.isIndexed(value) ? IndexedCursor : KeyedCursor;
|
---|
257 | var cursor = new CursorClass(rootData, keyPath, onChange, size);
|
---|
258 |
|
---|
259 | if (value instanceof Record) {
|
---|
260 | defineRecordProperties(cursor, value);
|
---|
261 | }
|
---|
262 |
|
---|
263 | return cursor;
|
---|
264 | }
|
---|
265 |
|
---|
266 | function defineRecordProperties(cursor, value) {
|
---|
267 | try {
|
---|
268 | value._keys.forEach(setProp.bind(undefined, cursor));
|
---|
269 | } catch (error) {
|
---|
270 | // Object.defineProperty failed. Probably IE8.
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | function setProp(prototype, name) {
|
---|
275 | Object.defineProperty(prototype, name, {
|
---|
276 | get: function() {
|
---|
277 | return this.get(name);
|
---|
278 | },
|
---|
279 | set: function(value) {
|
---|
280 | if (!this.__ownerID) {
|
---|
281 | throw new Error('Cannot set on an immutable record.');
|
---|
282 | }
|
---|
283 | }
|
---|
284 | });
|
---|
285 | }
|
---|
286 |
|
---|
287 | function wrappedValue(cursor, keyPath, value) {
|
---|
288 | return Iterable.isIterable(value) ? subCursor(cursor, keyPath, value) : value;
|
---|
289 | }
|
---|
290 |
|
---|
291 | function subCursor(cursor, keyPath, value) {
|
---|
292 | if (arguments.length < 3) {
|
---|
293 | return makeCursor( // call without value
|
---|
294 | cursor._rootData,
|
---|
295 | newKeyPath(cursor._keyPath, keyPath),
|
---|
296 | cursor._onChange
|
---|
297 | );
|
---|
298 | }
|
---|
299 | return makeCursor(
|
---|
300 | cursor._rootData,
|
---|
301 | newKeyPath(cursor._keyPath, keyPath),
|
---|
302 | cursor._onChange,
|
---|
303 | value
|
---|
304 | );
|
---|
305 | }
|
---|
306 |
|
---|
307 | function updateCursor(cursor, changeFn, changeKeyPath) {
|
---|
308 | var deepChange = arguments.length > 2;
|
---|
309 | var newRootData = cursor._rootData.updateIn(
|
---|
310 | cursor._keyPath,
|
---|
311 | deepChange ? Map() : undefined,
|
---|
312 | changeFn
|
---|
313 | );
|
---|
314 | var keyPath = cursor._keyPath || [];
|
---|
315 | var result = cursor._onChange && cursor._onChange.call(
|
---|
316 | undefined,
|
---|
317 | newRootData,
|
---|
318 | cursor._rootData,
|
---|
319 | deepChange ? newKeyPath(keyPath, changeKeyPath) : keyPath
|
---|
320 | );
|
---|
321 | if (result !== undefined) {
|
---|
322 | newRootData = result;
|
---|
323 | }
|
---|
324 | return makeCursor(newRootData, cursor._keyPath, cursor._onChange);
|
---|
325 | }
|
---|
326 |
|
---|
327 | function newKeyPath(head, tail) {
|
---|
328 | return head.concat(listToKeyPath(tail));
|
---|
329 | }
|
---|
330 |
|
---|
331 | function listToKeyPath(list) {
|
---|
332 | return Array.isArray(list) ? list : Immutable.Iterable(list).toArray();
|
---|
333 | }
|
---|
334 |
|
---|
335 | function valToKeyPath(val) {
|
---|
336 | return Array.isArray(val) ? val :
|
---|
337 | Iterable.isIterable(val) ? val.toArray() :
|
---|
338 | [val];
|
---|
339 | }
|
---|
340 |
|
---|
341 | exports.from = cursorFrom;
|
---|