-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathRecord.js
269 lines (236 loc) · 7.74 KB
/
Record.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { toJS } from './toJS';
import { KeyedCollection } from './Collection';
import { keyedSeqFromValue } from './Seq';
import { List } from './List';
import { ITERATE_ENTRIES, ITERATOR_SYMBOL } from './Iterator';
import { isRecord, IS_RECORD_SYMBOL } from './predicates/isRecord';
import { CollectionPrototype } from './CollectionImpl';
import { DELETE } from './TrieUtils';
import { getIn } from './methods/getIn';
import { setIn } from './methods/setIn';
import { deleteIn } from './methods/deleteIn';
import { update } from './methods/update';
import { updateIn } from './methods/updateIn';
import { merge, mergeWith } from './methods/merge';
import { mergeDeep, mergeDeepWith } from './methods/mergeDeep';
import { mergeIn } from './methods/mergeIn';
import { mergeDeepIn } from './methods/mergeDeepIn';
import { withMutations } from './methods/withMutations';
import { asMutable } from './methods/asMutable';
import { asImmutable } from './methods/asImmutable';
import invariant from './utils/invariant';
import quoteString from './utils/quoteString';
import { isImmutable } from './predicates/isImmutable';
function throwOnInvalidDefaultValues(defaultValues) {
if (isRecord(defaultValues)) {
throw new Error(
'Can not call `Record` with an immutable Record as default values. Use a plain javascript object instead.'
);
}
if (isImmutable(defaultValues)) {
throw new Error(
'Can not call `Record` with an immutable Collection as default values. Use a plain javascript object instead.'
);
}
if (defaultValues === null || typeof defaultValues !== 'object') {
throw new Error(
'Can not call `Record` with a non-object as default values. Use a plain javascript object instead.'
);
}
}
export class Record {
constructor(defaultValues, name) {
let hasInitialized;
throwOnInvalidDefaultValues(defaultValues);
const RecordType = function Record(values) {
if (values instanceof RecordType) {
return values;
}
if (!(this instanceof RecordType)) {
return new RecordType(values);
}
if (!hasInitialized) {
hasInitialized = true;
const keys = Object.keys(defaultValues);
const indices = (RecordTypePrototype._indices = {});
// Deprecated: left to attempt not to break any external code which
// relies on a ._name property existing on record instances.
// Use Record.getDescriptiveName() instead
RecordTypePrototype._name = name;
RecordTypePrototype._keys = keys;
RecordTypePrototype._defaultValues = defaultValues;
for (let i = 0; i < keys.length; i++) {
const propName = keys[i];
indices[propName] = i;
if (RecordTypePrototype[propName]) {
/* eslint-disable no-console */
// eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
typeof console === 'object' &&
console.warn &&
console.warn(
'Cannot define ' +
recordName(this) +
' with property "' +
propName +
'" since that property name is part of the Record API.'
);
/* eslint-enable no-console */
} else {
setProp(RecordTypePrototype, propName);
}
}
}
this.__ownerID = undefined;
this._values = List().withMutations((l) => {
l.setSize(this._keys.length);
KeyedCollection(values).forEach((v, k) => {
l.set(this._indices[k], v === this._defaultValues[k] ? undefined : v);
});
});
return this;
};
const RecordTypePrototype = (RecordType.prototype =
Object.create(RecordPrototype));
RecordTypePrototype.constructor = RecordType;
if (name) {
RecordType.displayName = name;
}
// eslint-disable-next-line no-constructor-return
return RecordType;
}
toString() {
let str = recordName(this) + ' { ';
const keys = this._keys;
let k;
for (let i = 0, l = keys.length; i !== l; i++) {
k = keys[i];
str += (i ? ', ' : '') + k + ': ' + quoteString(this.get(k));
}
return str + ' }';
}
equals(other) {
return (
this === other ||
(isRecord(other) && recordSeq(this).equals(recordSeq(other)))
);
}
hashCode() {
return recordSeq(this).hashCode();
}
// @pragma Access
has(k) {
return this._indices.hasOwnProperty(k);
}
get(k, notSetValue) {
if (!this.has(k)) {
return notSetValue;
}
const index = this._indices[k];
const value = this._values.get(index);
return value === undefined ? this._defaultValues[k] : value;
}
// @pragma Modification
set(k, v) {
if (this.has(k)) {
const newValues = this._values.set(
this._indices[k],
v === this._defaultValues[k] ? undefined : v
);
if (newValues !== this._values && !this.__ownerID) {
return makeRecord(this, newValues);
}
}
return this;
}
remove(k) {
return this.set(k);
}
clear() {
const newValues = this._values.clear().setSize(this._keys.length);
return this.__ownerID ? this : makeRecord(this, newValues);
}
wasAltered() {
return this._values.wasAltered();
}
toSeq() {
return recordSeq(this);
}
toJS() {
return toJS(this);
}
entries() {
return this.__iterator(ITERATE_ENTRIES);
}
__iterator(type, reverse) {
return recordSeq(this).__iterator(type, reverse);
}
__iterate(fn, reverse) {
return recordSeq(this).__iterate(fn, reverse);
}
__ensureOwner(ownerID) {
if (ownerID === this.__ownerID) {
return this;
}
const newValues = this._values.__ensureOwner(ownerID);
if (!ownerID) {
this.__ownerID = ownerID;
this._values = newValues;
return this;
}
return makeRecord(this, newValues, ownerID);
}
}
Record.isRecord = isRecord;
Record.getDescriptiveName = recordName;
const RecordPrototype = Record.prototype;
RecordPrototype[IS_RECORD_SYMBOL] = true;
RecordPrototype[DELETE] = RecordPrototype.remove;
RecordPrototype.deleteIn = RecordPrototype.removeIn = deleteIn;
RecordPrototype.getIn = getIn;
RecordPrototype.hasIn = CollectionPrototype.hasIn;
RecordPrototype.merge = merge;
RecordPrototype.mergeWith = mergeWith;
RecordPrototype.mergeIn = mergeIn;
RecordPrototype.mergeDeep = mergeDeep;
RecordPrototype.mergeDeepWith = mergeDeepWith;
RecordPrototype.mergeDeepIn = mergeDeepIn;
RecordPrototype.setIn = setIn;
RecordPrototype.update = update;
RecordPrototype.updateIn = updateIn;
RecordPrototype.withMutations = withMutations;
RecordPrototype.asMutable = asMutable;
RecordPrototype.asImmutable = asImmutable;
RecordPrototype[ITERATOR_SYMBOL] = RecordPrototype.entries;
RecordPrototype.toJSON = RecordPrototype.toObject =
CollectionPrototype.toObject;
RecordPrototype.inspect = RecordPrototype.toSource = function () {
return this.toString();
};
function makeRecord(likeRecord, values, ownerID) {
const record = Object.create(Object.getPrototypeOf(likeRecord));
record._values = values;
record.__ownerID = ownerID;
return record;
}
function recordName(record) {
return record.constructor.displayName || record.constructor.name || 'Record';
}
function recordSeq(record) {
return keyedSeqFromValue(record._keys.map((k) => [k, record.get(k)]));
}
function setProp(prototype, name) {
try {
Object.defineProperty(prototype, name, {
get: function () {
return this.get(name);
},
set: function (value) {
invariant(this.__ownerID, 'Cannot set on an immutable record.');
this.set(name, value);
},
});
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- TODO enable eslint here
} catch (error) {
// Object.defineProperty failed. Probably IE8.
}
}