UNPKG

16 kBJavaScriptView Raw
1import { createSelector } from '@ngrx/store';
2import { isDevMode } from '@angular/core';
3
4function getInitialEntityState() {
5 return {
6 ids: [],
7 entities: {},
8 };
9}
10function createInitialStateFactory() {
11 function getInitialState(additionalState = {}) {
12 return Object.assign(getInitialEntityState(), additionalState);
13 }
14 return { getInitialState };
15}
16
17function createSelectorsFactory() {
18 function getSelectors(selectState) {
19 const selectIds = (state) => state.ids;
20 const selectEntities = (state) => state.entities;
21 const selectAll = createSelector(selectIds, selectEntities, (ids, entities) => ids.map((id) => entities[id]));
22 const selectTotal = createSelector(selectIds, (ids) => ids.length);
23 if (!selectState) {
24 return {
25 selectIds,
26 selectEntities,
27 selectAll,
28 selectTotal,
29 };
30 }
31 return {
32 selectIds: createSelector(selectState, selectIds),
33 selectEntities: createSelector(selectState, selectEntities),
34 selectAll: createSelector(selectState, selectAll),
35 selectTotal: createSelector(selectState, selectTotal),
36 };
37 }
38 return { getSelectors };
39}
40
41var DidMutate;
42(function (DidMutate) {
43 DidMutate[DidMutate["EntitiesOnly"] = 0] = "EntitiesOnly";
44 DidMutate[DidMutate["Both"] = 1] = "Both";
45 DidMutate[DidMutate["None"] = 2] = "None";
46})(DidMutate || (DidMutate = {}));
47function createStateOperator(mutator) {
48 return function operation(arg, state) {
49 const clonedEntityState = {
50 ids: [...state.ids],
51 entities: Object.assign({}, state.entities),
52 };
53 const didMutate = mutator(arg, clonedEntityState);
54 if (didMutate === DidMutate.Both) {
55 return Object.assign({}, state, clonedEntityState);
56 }
57 if (didMutate === DidMutate.EntitiesOnly) {
58 return Object.assign(Object.assign({}, state), { entities: clonedEntityState.entities });
59 }
60 return state;
61 };
62}
63
64function selectIdValue(entity, selectId) {
65 const key = selectId(entity);
66 if (isDevMode() && key === undefined) {
67 console.warn('@ngrx/entity: The entity passed to the `selectId` implementation returned undefined.', 'You should probably provide your own `selectId` implementation.', 'The entity that was passed:', entity, 'The `selectId` implementation:', selectId.toString());
68 }
69 return key;
70}
71
72function createUnsortedStateAdapter(selectId) {
73 function addOneMutably(entity, state) {
74 const key = selectIdValue(entity, selectId);
75 if (key in state.entities) {
76 return DidMutate.None;
77 }
78 state.ids.push(key);
79 state.entities[key] = entity;
80 return DidMutate.Both;
81 }
82 function addManyMutably(entities, state) {
83 let didMutate = false;
84 for (const entity of entities) {
85 didMutate = addOneMutably(entity, state) !== DidMutate.None || didMutate;
86 }
87 return didMutate ? DidMutate.Both : DidMutate.None;
88 }
89 function setAllMutably(entities, state) {
90 state.ids = [];
91 state.entities = {};
92 addManyMutably(entities, state);
93 return DidMutate.Both;
94 }
95 function setOneMutably(entity, state) {
96 const key = selectIdValue(entity, selectId);
97 if (key in state.entities) {
98 state.entities[key] = entity;
99 return DidMutate.EntitiesOnly;
100 }
101 state.ids.push(key);
102 state.entities[key] = entity;
103 return DidMutate.Both;
104 }
105 function setManyMutably(entities, state) {
106 const didMutateSetOne = entities.map((entity) => setOneMutably(entity, state));
107 switch (true) {
108 case didMutateSetOne.some((didMutate) => didMutate === DidMutate.Both):
109 return DidMutate.Both;
110 case didMutateSetOne.some((didMutate) => didMutate === DidMutate.EntitiesOnly):
111 return DidMutate.EntitiesOnly;
112 default:
113 return DidMutate.None;
114 }
115 }
116 function removeOneMutably(key, state) {
117 return removeManyMutably([key], state);
118 }
119 function removeManyMutably(keysOrPredicate, state) {
120 const keys = keysOrPredicate instanceof Array
121 ? keysOrPredicate
122 : state.ids.filter((key) => keysOrPredicate(state.entities[key]));
123 const didMutate = keys
124 .filter((key) => key in state.entities)
125 .map((key) => delete state.entities[key]).length > 0;
126 if (didMutate) {
127 state.ids = state.ids.filter((id) => id in state.entities);
128 }
129 return didMutate ? DidMutate.Both : DidMutate.None;
130 }
131 function removeAll(state) {
132 return Object.assign({}, state, {
133 ids: [],
134 entities: {},
135 });
136 }
137 function takeNewKey(keys, update, state) {
138 const original = state.entities[update.id];
139 const updated = Object.assign({}, original, update.changes);
140 const newKey = selectIdValue(updated, selectId);
141 const hasNewKey = newKey !== update.id;
142 if (hasNewKey) {
143 keys[update.id] = newKey;
144 delete state.entities[update.id];
145 }
146 state.entities[newKey] = updated;
147 return hasNewKey;
148 }
149 function updateOneMutably(update, state) {
150 return updateManyMutably([update], state);
151 }
152 function updateManyMutably(updates, state) {
153 const newKeys = {};
154 updates = updates.filter((update) => update.id in state.entities);
155 const didMutateEntities = updates.length > 0;
156 if (didMutateEntities) {
157 const didMutateIds = updates.filter((update) => takeNewKey(newKeys, update, state)).length >
158 0;
159 if (didMutateIds) {
160 state.ids = state.ids.map((id) => newKeys[id] || id);
161 return DidMutate.Both;
162 }
163 else {
164 return DidMutate.EntitiesOnly;
165 }
166 }
167 return DidMutate.None;
168 }
169 function mapMutably(map, state) {
170 const changes = state.ids.reduce((changes, id) => {
171 const change = map(state.entities[id]);
172 if (change !== state.entities[id]) {
173 changes.push({ id, changes: change });
174 }
175 return changes;
176 }, []);
177 const updates = changes.filter(({ id }) => id in state.entities);
178 return updateManyMutably(updates, state);
179 }
180 function mapOneMutably({ map, id }, state) {
181 const entity = state.entities[id];
182 if (!entity) {
183 return DidMutate.None;
184 }
185 const updatedEntity = map(entity);
186 return updateOneMutably({
187 id: id,
188 changes: updatedEntity,
189 }, state);
190 }
191 function upsertOneMutably(entity, state) {
192 return upsertManyMutably([entity], state);
193 }
194 function upsertManyMutably(entities, state) {
195 const added = [];
196 const updated = [];
197 for (const entity of entities) {
198 const id = selectIdValue(entity, selectId);
199 if (id in state.entities) {
200 updated.push({ id, changes: entity });
201 }
202 else {
203 added.push(entity);
204 }
205 }
206 const didMutateByUpdated = updateManyMutably(updated, state);
207 const didMutateByAdded = addManyMutably(added, state);
208 switch (true) {
209 case didMutateByAdded === DidMutate.None &&
210 didMutateByUpdated === DidMutate.None:
211 return DidMutate.None;
212 case didMutateByAdded === DidMutate.Both ||
213 didMutateByUpdated === DidMutate.Both:
214 return DidMutate.Both;
215 default:
216 return DidMutate.EntitiesOnly;
217 }
218 }
219 return {
220 removeAll,
221 addOne: createStateOperator(addOneMutably),
222 addMany: createStateOperator(addManyMutably),
223 setAll: createStateOperator(setAllMutably),
224 setOne: createStateOperator(setOneMutably),
225 setMany: createStateOperator(setManyMutably),
226 updateOne: createStateOperator(updateOneMutably),
227 updateMany: createStateOperator(updateManyMutably),
228 upsertOne: createStateOperator(upsertOneMutably),
229 upsertMany: createStateOperator(upsertManyMutably),
230 removeOne: createStateOperator(removeOneMutably),
231 removeMany: createStateOperator(removeManyMutably),
232 map: createStateOperator(mapMutably),
233 mapOne: createStateOperator(mapOneMutably),
234 };
235}
236
237function createSortedStateAdapter(selectId, sort) {
238 const { removeOne, removeMany, removeAll } = createUnsortedStateAdapter(selectId);
239 function addOneMutably(entity, state) {
240 return addManyMutably([entity], state);
241 }
242 function addManyMutably(newModels, state) {
243 const models = newModels.filter((model) => !(selectIdValue(model, selectId) in state.entities));
244 if (models.length === 0) {
245 return DidMutate.None;
246 }
247 else {
248 merge(models, state);
249 return DidMutate.Both;
250 }
251 }
252 function setAllMutably(models, state) {
253 state.entities = {};
254 state.ids = [];
255 addManyMutably(models, state);
256 return DidMutate.Both;
257 }
258 function setOneMutably(entity, state) {
259 const id = selectIdValue(entity, selectId);
260 if (id in state.entities) {
261 state.ids = state.ids.filter((val) => val !== id);
262 merge([entity], state);
263 return DidMutate.Both;
264 }
265 else {
266 return addOneMutably(entity, state);
267 }
268 }
269 function setManyMutably(entities, state) {
270 const didMutateSetOne = entities.map((entity) => setOneMutably(entity, state));
271 switch (true) {
272 case didMutateSetOne.some((didMutate) => didMutate === DidMutate.Both):
273 return DidMutate.Both;
274 case didMutateSetOne.some((didMutate) => didMutate === DidMutate.EntitiesOnly):
275 return DidMutate.EntitiesOnly;
276 default:
277 return DidMutate.None;
278 }
279 }
280 function updateOneMutably(update, state) {
281 return updateManyMutably([update], state);
282 }
283 function takeUpdatedModel(models, update, state) {
284 if (!(update.id in state.entities)) {
285 return false;
286 }
287 const original = state.entities[update.id];
288 const updated = Object.assign({}, original, update.changes);
289 const newKey = selectIdValue(updated, selectId);
290 delete state.entities[update.id];
291 models.push(updated);
292 return newKey !== update.id;
293 }
294 function updateManyMutably(updates, state) {
295 const models = [];
296 const didMutateIds = updates.filter((update) => takeUpdatedModel(models, update, state))
297 .length > 0;
298 if (models.length === 0) {
299 return DidMutate.None;
300 }
301 else {
302 const originalIds = state.ids;
303 const updatedIndexes = [];
304 state.ids = state.ids.filter((id, index) => {
305 if (id in state.entities) {
306 return true;
307 }
308 else {
309 updatedIndexes.push(index);
310 return false;
311 }
312 });
313 merge(models, state);
314 if (!didMutateIds &&
315 updatedIndexes.every((i) => state.ids[i] === originalIds[i])) {
316 return DidMutate.EntitiesOnly;
317 }
318 else {
319 return DidMutate.Both;
320 }
321 }
322 }
323 function mapMutably(updatesOrMap, state) {
324 const updates = state.ids.reduce((changes, id) => {
325 const change = updatesOrMap(state.entities[id]);
326 if (change !== state.entities[id]) {
327 changes.push({ id, changes: change });
328 }
329 return changes;
330 }, []);
331 return updateManyMutably(updates, state);
332 }
333 function mapOneMutably({ map, id }, state) {
334 const entity = state.entities[id];
335 if (!entity) {
336 return DidMutate.None;
337 }
338 const updatedEntity = map(entity);
339 return updateOneMutably({
340 id: id,
341 changes: updatedEntity,
342 }, state);
343 }
344 function upsertOneMutably(entity, state) {
345 return upsertManyMutably([entity], state);
346 }
347 function upsertManyMutably(entities, state) {
348 const added = [];
349 const updated = [];
350 for (const entity of entities) {
351 const id = selectIdValue(entity, selectId);
352 if (id in state.entities) {
353 updated.push({ id, changes: entity });
354 }
355 else {
356 added.push(entity);
357 }
358 }
359 const didMutateByUpdated = updateManyMutably(updated, state);
360 const didMutateByAdded = addManyMutably(added, state);
361 switch (true) {
362 case didMutateByAdded === DidMutate.None &&
363 didMutateByUpdated === DidMutate.None:
364 return DidMutate.None;
365 case didMutateByAdded === DidMutate.Both ||
366 didMutateByUpdated === DidMutate.Both:
367 return DidMutate.Both;
368 default:
369 return DidMutate.EntitiesOnly;
370 }
371 }
372 function merge(models, state) {
373 models.sort(sort);
374 const ids = [];
375 let i = 0;
376 let j = 0;
377 while (i < models.length && j < state.ids.length) {
378 const model = models[i];
379 const modelId = selectIdValue(model, selectId);
380 const entityId = state.ids[j];
381 const entity = state.entities[entityId];
382 if (sort(model, entity) <= 0) {
383 ids.push(modelId);
384 i++;
385 }
386 else {
387 ids.push(entityId);
388 j++;
389 }
390 }
391 if (i < models.length) {
392 state.ids = ids.concat(models.slice(i).map(selectId));
393 }
394 else {
395 state.ids = ids.concat(state.ids.slice(j));
396 }
397 models.forEach((model, i) => {
398 state.entities[selectId(model)] = model;
399 });
400 }
401 return {
402 removeOne,
403 removeMany,
404 removeAll,
405 addOne: createStateOperator(addOneMutably),
406 updateOne: createStateOperator(updateOneMutably),
407 upsertOne: createStateOperator(upsertOneMutably),
408 setAll: createStateOperator(setAllMutably),
409 setOne: createStateOperator(setOneMutably),
410 setMany: createStateOperator(setManyMutably),
411 addMany: createStateOperator(addManyMutably),
412 updateMany: createStateOperator(updateManyMutably),
413 upsertMany: createStateOperator(upsertManyMutably),
414 map: createStateOperator(mapMutably),
415 mapOne: createStateOperator(mapOneMutably),
416 };
417}
418
419function createEntityAdapter(options = {}) {
420 var _a, _b;
421 const { selectId, sortComparer } = {
422 selectId: (_a = options.selectId) !== null && _a !== void 0 ? _a : ((entity) => entity.id),
423 sortComparer: (_b = options.sortComparer) !== null && _b !== void 0 ? _b : false,
424 };
425 const stateFactory = createInitialStateFactory();
426 const selectorsFactory = createSelectorsFactory();
427 const stateAdapter = sortComparer
428 ? createSortedStateAdapter(selectId, sortComparer)
429 : createUnsortedStateAdapter(selectId);
430 return Object.assign(Object.assign(Object.assign({ selectId,
431 sortComparer }, stateFactory), selectorsFactory), stateAdapter);
432}
433
434class Dictionary {
435}
436
437/**
438 * DO NOT EDIT
439 *
440 * This file is automatically generated at build
441 */
442
443/**
444 * Generated bundle index. Do not edit.
445 */
446
447export { Dictionary, createEntityAdapter };
448//# sourceMappingURL=ngrx-entity.mjs.map