|
| 1 | +import QueryResultSet from './query-result-set'; |
| 2 | +import AttributeJoinedData from '../attributes/attribute-joined-data'; |
| 3 | + |
| 4 | +// TODO: Make mutator methods QueryResultSet.join(), QueryResultSet.clip... |
| 5 | +export default class MutableQueryResultSet extends QueryResultSet { |
| 6 | + |
| 7 | + immutableClone() { |
| 8 | + const set = new QueryResultSet({ |
| 9 | + _ids: [].concat(this._ids), |
| 10 | + _modelsHash: Object.assign({}, this._modelsHash), |
| 11 | + _query: this._query, |
| 12 | + _offset: this._offset, |
| 13 | + }); |
| 14 | + Object.freeze(set._ids); |
| 15 | + Object.freeze(set._modelsHash); |
| 16 | + return set; |
| 17 | + } |
| 18 | + |
| 19 | + clipToRange(range) { |
| 20 | + if (range.isInfinite()) { |
| 21 | + return; |
| 22 | + } |
| 23 | + if (range.offset > this._offset) { |
| 24 | + this._ids = this._ids.slice(range.offset - this._offset); |
| 25 | + this._offset = range.offset; |
| 26 | + } |
| 27 | + |
| 28 | + const rangeEnd = range.offset + range.limit; |
| 29 | + const selfEnd = this._offset + this._ids.length; |
| 30 | + if (rangeEnd < selfEnd) { |
| 31 | + this._ids.length = Math.max(0, rangeEnd - this._offset); |
| 32 | + } |
| 33 | + |
| 34 | + const models = this.models(); |
| 35 | + this._modelsHash = {}; |
| 36 | + this._idToIndexHash = null; |
| 37 | + models.forEach((m) => this.updateModel(m)); |
| 38 | + } |
| 39 | + |
| 40 | + addModelsInRange(rangeModels, range) { |
| 41 | + this.addIdsInRange(rangeModels.map(m => m.id), range); |
| 42 | + rangeModels.forEach((m) => this.updateModel(m)); |
| 43 | + } |
| 44 | + |
| 45 | + addIdsInRange(rangeIds, range) { |
| 46 | + if ((this._offset === null) || range.isInfinite()) { |
| 47 | + this._ids = rangeIds; |
| 48 | + this._idToIndexHash = null; |
| 49 | + this._offset = range.offset; |
| 50 | + } else { |
| 51 | + const currentEnd = this._offset + this._ids.length; |
| 52 | + const rangeIdsEnd = range.offset + rangeIds.length; |
| 53 | + |
| 54 | + if (rangeIdsEnd < this._offset) { |
| 55 | + throw new Error(`addIdsInRange: You can only add adjacent values (${rangeIdsEnd} < ${this._offset})`) |
| 56 | + } |
| 57 | + if (range.offset > currentEnd) { |
| 58 | + throw new Error(`addIdsInRange: You can only add adjacent values (${range.offset} > ${currentEnd})`); |
| 59 | + } |
| 60 | + |
| 61 | + let existingBefore = [] |
| 62 | + if (range.offset > this._offset) { |
| 63 | + existingBefore = this._ids.slice(0, range.offset - this._offset); |
| 64 | + } |
| 65 | + |
| 66 | + let existingAfter = [] |
| 67 | + if ((rangeIds.length === range.limit) && (currentEnd > rangeIdsEnd)) { |
| 68 | + existingAfter = this._ids.slice(rangeIdsEnd - this._offset); |
| 69 | + } |
| 70 | + |
| 71 | + this._ids = [].concat(existingBefore, rangeIds, existingAfter); |
| 72 | + this._idToIndexHash = null; |
| 73 | + this._offset = Math.min(this._offset, range.offset); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + updateModel(item) { |
| 78 | + if (!item) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + // Sometimes the new copy of `item` doesn't contain the joined data present |
| 83 | + // in the old one, since it's not provided by default and may not have changed. |
| 84 | + // Make sure we never drop joined data by pulling it over. |
| 85 | + const existing = this._modelsHash[item.clientId]; |
| 86 | + if (existing) { |
| 87 | + const attrs = existing.constructor.attributes |
| 88 | + for (const key of Object.keys(attrs)) { |
| 89 | + const attr = attrs[key]; |
| 90 | + if ((attr instanceof AttributeJoinedData) && (item[attr.modelKey] === undefined)) { |
| 91 | + item[attr.modelKey] = existing[attr.modelKey]; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + this._modelsHash[item.clientId] = item; |
| 96 | + this._modelsHash[item.id] = item; |
| 97 | + this._idToIndexHash = null; |
| 98 | + } |
| 99 | + |
| 100 | + removeModelAtOffset(item, offset) { |
| 101 | + const idx = offset - this._offset; |
| 102 | + delete this._modelsHash[item.clientId]; |
| 103 | + delete this._modelsHash[item.id]; |
| 104 | + this._ids.splice(idx, 1); |
| 105 | + this._idToIndexHash = null; |
| 106 | + } |
| 107 | + |
| 108 | + setQuery(query) { |
| 109 | + this._query = query.clone(); |
| 110 | + this._query.finalize(); |
| 111 | + } |
| 112 | +} |
0 commit comments