| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 172× 1× 1× 34× 34× 1× 21× 21× 21× 36× 36× 36× 12× 12× 2× 10× 21× 36× 36× 36× 26× 1× 34× 1× 34× 21× 21× 13× 2× 2× 11× 11× 28× 28× 28× 26× 11× 11× 28× 28× 28× 28× 22× 6× 6× 28× 28× 11× 4× 4× 4× 2× 1× 17× 17× 21× 13× 17× 1× 1× 17× 1× | "use strict"; /* istanbul ignore next */ var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; var core_1 = require("./core"); var mobx_1 = require("mobx"); var arrayItemIdKey = "<id>"; function getArrayItemId(item, idKey) { return item && typeof item === "object" && item[idKey || arrayItemIdKey]; } exports.getArrayItemId = getArrayItemId; function setArrayItemId(item, id, idKey) { Eif (item && typeof item === "object") { item[idKey || arrayItemIdKey] = id; } } function setArrayItemIds(ar, idKey) { var nextId = 1; var usedIds = {}; // First pass - clear IDs that are duplicates for (var _i = 0, ar_1 = ar; _i < ar_1.length; _i++) { var item = ar_1[_i]; var id = getArrayItemId(item, idKey); if (id !== undefined) { nextId = typeof id === "number" ? Math.max(nextId, id + 1) : nextId; if (usedIds[id]) { setArrayItemId(item, undefined, idKey); } else { usedIds[id] = true; } } } // Second pass - allocate IDs for (var _a = 0, ar_2 = ar; _a < ar_2.length; _a++) { var item = ar_2[_a]; var id = getArrayItemId(item, idKey); if (id === undefined) { setArrayItemId(item, nextId++, idKey); } } } function saveItemWithId(item) { return __assign({}, core_1.save(item), (_a = {}, _a[arrayItemIdKey] = getArrayItemId(item), _a)); var _a; } function getArrayJsonComputed(ar, itemFactory, idKey) { return core_1.getOrCreateComputed(ar, "<json>", function () { return ({ get: function () { setArrayItemIds(ar, idKey); return ar.map(idKey ? core_1.save : saveItemWithId); }, set: function (data) { if (!core_1.isArray(data)) { ar.length = 0; // most likely schema has changed return; } // Build map of existing items by ID var existing = {}; for (var _i = 0, ar_3 = ar; _i < ar_3.length; _i++) { var item = ar_3[_i]; var id = getArrayItemId(item, idKey); if (id !== undefined && !existing[id]) { existing[id] = item; } } // Bring into line with supplied data ar.length = data.length; for (var i = 0; i < data.length; i++) { var itemJson = data[i]; var itemId = getArrayItemId(itemJson, idKey); // Reuse existing item with same id var item = existing[itemId]; if (item) { delete existing[itemId]; } else { item = itemFactory(); setArrayItemId(item, itemId, idKey); } core_1.load(item, itemJson); ar[i] = item; } // Dispose any items not reused for (var _a = 0, _b = Object.keys(existing); _a < _b.length; _a++) { var key = _b[_a]; var item = existing[key]; if (item.dispose) { item.dispose(); } } } }); }); } function array(factory, id) { var result = mobx_1.observable([]); Object.defineProperty(result, "json", { get: function () { return getArrayJsonComputed(this, factory, id).get(); }, set: function (data) { getArrayJsonComputed(this, factory, id).set(data); } }); return result; } exports.array = array; function arrayOf(ctor, id) { return array(function () { return new ctor(); }, id); } exports.arrayOf = arrayOf; //# sourceMappingURL=array.js.map |