source: node_modules/immutable/contrib/cursor/index.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 8.9 KB
Line 
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
16var Immutable = require('../../');
17var Iterable = Immutable.Iterable;
18var Iterator = Iterable.Iterator;
19var Seq = Immutable.Seq;
20var Map = Immutable.Map;
21var Record = Immutable.Record;
22
23
24function 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
37var KeyedCursorPrototype = Object.create(Seq.Keyed.prototype);
38var IndexedCursorPrototype = Object.create(Seq.Indexed.prototype);
39
40function KeyedCursor(rootData, keyPath, onChange, size) {
41 this.size = size;
42 this._rootData = rootData;
43 this._keyPath = keyPath;
44 this._onChange = onChange;
45}
46KeyedCursorPrototype.constructor = KeyedCursor;
47
48function IndexedCursor(rootData, keyPath, onChange, size) {
49 this.size = size;
50 this._rootData = rootData;
51 this._keyPath = keyPath;
52 this._onChange = onChange;
53}
54IndexedCursorPrototype.constructor = IndexedCursor;
55
56KeyedCursorPrototype.toString = function() {
57 return this.__toString('Cursor {', '}');
58}
59IndexedCursorPrototype.toString = function() {
60 return this.__toString('Cursor [', ']');
61}
62
63KeyedCursorPrototype.deref =
64KeyedCursorPrototype.valueOf =
65IndexedCursorPrototype.deref =
66IndexedCursorPrototype.valueOf = function(notSetValue) {
67 return this._rootData.getIn(this._keyPath, notSetValue);
68}
69
70KeyedCursorPrototype.get =
71IndexedCursorPrototype.get = function(key, notSetValue) {
72 return this.getIn([key], notSetValue);
73}
74
75KeyedCursorPrototype.getIn =
76IndexedCursorPrototype.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
85IndexedCursorPrototype.set =
86KeyedCursorPrototype.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
94IndexedCursorPrototype.push = function(/* values */) {
95 var args = arguments;
96 return updateCursor(this, function (m) {
97 return m.push.apply(m, args);
98 });
99}
100
101IndexedCursorPrototype.pop = function() {
102 return updateCursor(this, function (m) {
103 return m.pop();
104 });
105}
106
107IndexedCursorPrototype.unshift = function(/* values */) {
108 var args = arguments;
109 return updateCursor(this, function (m) {
110 return m.unshift.apply(m, args);
111 });
112}
113
114IndexedCursorPrototype.shift = function() {
115 return updateCursor(this, function (m) {
116 return m.shift();
117 });
118}
119
120IndexedCursorPrototype.setIn =
121KeyedCursorPrototype.setIn = Map.prototype.setIn;
122
123KeyedCursorPrototype.remove =
124KeyedCursorPrototype['delete'] =
125IndexedCursorPrototype.remove =
126IndexedCursorPrototype['delete'] = function(key) {
127 return updateCursor(this, function (m) { return m.remove(key); }, [key]);
128}
129
130IndexedCursorPrototype.removeIn =
131IndexedCursorPrototype.deleteIn =
132KeyedCursorPrototype.removeIn =
133KeyedCursorPrototype.deleteIn = Map.prototype.deleteIn;
134
135KeyedCursorPrototype.clear =
136IndexedCursorPrototype.clear = function() {
137 return updateCursor(this, function (m) { return m.clear(); });
138}
139
140IndexedCursorPrototype.update =
141KeyedCursorPrototype.update = function(keyOrFn, notSetValue, updater) {
142 return arguments.length === 1 ?
143 updateCursor(this, keyOrFn) :
144 this.updateIn([keyOrFn], notSetValue, updater);
145}
146
147IndexedCursorPrototype.updateIn =
148KeyedCursorPrototype.updateIn = function(keyPath, notSetValue, updater) {
149 return updateCursor(this, function (m) {
150 return m.updateIn(keyPath, notSetValue, updater);
151 }, keyPath);
152}
153
154IndexedCursorPrototype.merge =
155KeyedCursorPrototype.merge = function(/*...iters*/) {
156 var args = arguments;
157 return updateCursor(this, function (m) {
158 return m.merge.apply(m, args);
159 });
160}
161
162IndexedCursorPrototype.mergeWith =
163KeyedCursorPrototype.mergeWith = function(merger/*, ...iters*/) {
164 var args = arguments;
165 return updateCursor(this, function (m) {
166 return m.mergeWith.apply(m, args);
167 });
168}
169
170IndexedCursorPrototype.mergeIn =
171KeyedCursorPrototype.mergeIn = Map.prototype.mergeIn;
172
173IndexedCursorPrototype.mergeDeep =
174KeyedCursorPrototype.mergeDeep = function(/*...iters*/) {
175 var args = arguments;
176 return updateCursor(this, function (m) {
177 return m.mergeDeep.apply(m, args);
178 });
179}
180
181IndexedCursorPrototype.mergeDeepWith =
182KeyedCursorPrototype.mergeDeepWith = function(merger/*, ...iters*/) {
183 var args = arguments;
184 return updateCursor(this, function (m) {
185 return m.mergeDeepWith.apply(m, args);
186 });
187}
188
189IndexedCursorPrototype.mergeDeepIn =
190KeyedCursorPrototype.mergeDeepIn = Map.prototype.mergeDeepIn;
191
192KeyedCursorPrototype.withMutations =
193IndexedCursorPrototype.withMutations = function(fn) {
194 return updateCursor(this, function (m) {
195 return (m || Map()).withMutations(fn);
196 });
197}
198
199KeyedCursorPrototype.cursor =
200IndexedCursorPrototype.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 */
208KeyedCursorPrototype.__iterate =
209IndexedCursorPrototype.__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 */
221KeyedCursorPrototype.__iterator =
222IndexedCursorPrototype.__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
245KeyedCursor.prototype = KeyedCursorPrototype;
246IndexedCursor.prototype = IndexedCursorPrototype;
247
248
249var NOT_SET = {}; // Sentinel value
250
251function 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
266function 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
274function 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
287function wrappedValue(cursor, keyPath, value) {
288 return Iterable.isIterable(value) ? subCursor(cursor, keyPath, value) : value;
289}
290
291function 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
307function 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
327function newKeyPath(head, tail) {
328 return head.concat(listToKeyPath(tail));
329}
330
331function listToKeyPath(list) {
332 return Array.isArray(list) ? list : Immutable.Iterable(list).toArray();
333}
334
335function valToKeyPath(val) {
336 return Array.isArray(val) ? val :
337 Iterable.isIterable(val) ? val.toArray() :
338 [val];
339}
340
341exports.from = cursorFrom;
Note: See TracBrowser for help on using the repository browser.