-
Notifications
You must be signed in to change notification settings - Fork 7
/
map.js
320 lines (291 loc) · 8.22 KB
/
map.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"use strict";
var Construct = require("can-construct");
var define = require("can-define");
var defineHelpers = require("../define-helpers/define-helpers");
var ObservationRecorder = require("can-observation-recorder");
var ns = require("can-namespace");
var canLog = require("can-log");
var canLogDev = require("can-log/dev/dev");
var canReflect = require("can-reflect");
var canSymbol = require("can-symbol");
var queues = require("can-queues");
var addTypeEvents = require("can-event-queue/type/type");
var keysForDefinition = function(definitions) {
var keys = [];
for(var prop in definitions) {
var definition = definitions[prop];
if(typeof definition !== "object" || ("serialize" in definition ? !!definition.serialize : !definition.get)) {
keys.push(prop);
}
}
return keys;
};
function assign(source) {
queues.batch.start();
canReflect.assignMap(this, source || {});
queues.batch.stop();
}
function update(source) {
queues.batch.start();
canReflect.updateMap(this, source || {});
queues.batch.stop();
}
function assignDeep(source){
queues.batch.start();
// TODO: we should probably just throw an error instead of cleaning
canReflect.assignDeepMap(this, source || {});
queues.batch.stop();
}
function updateDeep(source){
queues.batch.start();
// TODO: we should probably just throw an error instead of cleaning
canReflect.updateDeepMap(this, source || {});
queues.batch.stop();
}
function setKeyValue(key, value) {
var defined = defineHelpers.defineExpando(this, key, value);
if(!defined) {
this[key] = value;
}
}
function getKeyValue(key) {
var value = this[key];
if(value !== undefined || key in this || Object.isSealed(this)) {
return value;
} else {
ObservationRecorder.add(this, key);
return this[key];
}
}
var getSchemaSymbol = canSymbol.for("can.getSchema");
function getSchema() {
var def = this.prototype._define;
var definitions = def ? def.definitions : {};
var schema = {
type: "map",
identity: [],
keys: {}
};
return define.updateSchemaKeys(schema, definitions);
}
var DefineMap = Construct.extend("DefineMap",{
setup: function(base){
var key,
prototype = this.prototype;
if(DefineMap) {
// we have already created
var result = define(prototype, prototype, base.prototype._define);
define.makeDefineInstanceKey(this, result);
addTypeEvents(this);
for(key in DefineMap.prototype) {
define.defineConfigurableAndNotEnumerable(prototype, key, prototype[key]);
}
define.defineConfigurableAndNotEnumerable(prototype, "setup", function(props){
define.setup.call(
this,
props || {},
this.constructor.seal
);
});
var _computedGetter = Object.getOwnPropertyDescriptor(prototype, "_computed").get;
Object.defineProperty(prototype, "_computed", {
configurable: true,
enumerable: false,
get: function(){
if(this === prototype) {
return;
}
return _computedGetter.call(this, arguments);
}
});
} else {
for(key in prototype) {
define.defineConfigurableAndNotEnumerable(prototype, key, prototype[key]);
}
}
define.defineConfigurableAndNotEnumerable(prototype, "constructor", this);
this[getSchemaSymbol] = getSchema;
}
},{
// setup for only dynamic DefineMap instances
setup: function(props, sealed){
if(!this._define) {
Object.defineProperty(this,"_define",{
enumerable: false,
value: {
definitions: {}
}
});
Object.defineProperty(this,"_data",{
enumerable: false,
value: {}
});
}
define.setup.call(
this,
props || {},
sealed === true
);
},
get: function(prop){
if(prop) {
return getKeyValue.call(this, prop);
} else {
return canReflect.unwrap(this, Map);
}
},
set: function(prop, value){
if(typeof prop === "object") {
//!steal-remove-start
if(process.env.NODE_ENV !== 'production') {
canLogDev.warn('can-define/map/map.prototype.set is deprecated; please use can-define/map/map.prototype.assign or can-define/map/map.prototype.update instead');
}
//!steal-remove-end
if(value === true) {
updateDeep.call(this, prop);
} else {
assignDeep.call(this, prop);
}
} else {
setKeyValue.call(this, prop, value);
}
return this;
},
assignDeep: function(prop) {
assignDeep.call(this, prop);
return this;
},
updateDeep: function(prop) {
updateDeep.call(this, prop);
return this;
},
assign: function(prop) {
assign.call(this, prop);
return this;
},
update: function(prop) {
update.call(this, prop);
return this;
},
serialize: function () {
return canReflect.serialize(this, Map);
},
deleteKey: defineHelpers.deleteKey,
forEach: (function(){
var forEach = function(list, cb, thisarg){
return canReflect.eachKey(list, cb, thisarg);
},
noObserve = ObservationRecorder.ignore(forEach);
return function(cb, thisarg, observe) {
return observe === false ? noObserve(this, cb, thisarg) : forEach(this, cb, thisarg);
};
})(),
"*": {
type: define.types.observable
}
});
var defineMapProto = {
// -type-
"can.isMapLike": true,
"can.isListLike": false,
"can.isValueLike": false,
// -get/set-
"can.getKeyValue": getKeyValue,
"can.setKeyValue": setKeyValue,
"can.deleteKeyValue": defineHelpers.deleteKey,
// -shape
"can.getOwnKeys": function() {
var keys = canReflect.getOwnEnumerableKeys(this);
if(this._computed) {
var computedKeys = canReflect.getOwnKeys(this._computed);
var key;
for (var i=0; i<computedKeys.length; i++) {
key = computedKeys[i];
if (keys.indexOf(key) < 0) {
keys.push(key);
}
}
}
return keys;
},
"can.getOwnEnumerableKeys": function(){
ObservationRecorder.add(this, 'can.keys');
ObservationRecorder.add(Object.getPrototypeOf(this), 'can.keys');
return keysForDefinition(this._define.definitions).concat(keysForDefinition(this._instanceDefinitions) );
},
"can.hasOwnKey": function(key) {
return Object.hasOwnProperty.call(this._define.definitions, key);
},
"can.hasKey": function(key) {
return !!this._define.definitions[key];
},
// -shape get/set-
"can.assignDeep": assignDeep,
"can.updateDeep": updateDeep,
"can.unwrap": defineHelpers.reflectUnwrap,
"can.serialize": defineHelpers.reflectSerialize,
// observable
"can.keyHasDependencies": function(key) {
return !!(this._computed && this._computed[key] && this._computed[key].compute);
},
"can.getKeyDependencies": function(key) {
var ret;
if(this._computed && this._computed[key] && this._computed[key].compute) {
ret = {};
ret.valueDependencies = new Set();
ret.valueDependencies.add(this._computed[key].compute);
}
return ret;
}
};
//!steal-remove-start
if(process.env.NODE_ENV !== 'production') {
defineMapProto["can.getName"] = function() {
return canReflect.getName(this.constructor) + "{}";
};
}
//!steal-remove-end
canReflect.assignSymbols(DefineMap.prototype, defineMapProto);
canReflect.setKeyValue(DefineMap.prototype, canSymbol.iterator, function() {
return new define.Iterator(this);
});
// Add necessary event methods to this object.
for(var prop in define.eventsProto) {
DefineMap[prop] = define.eventsProto[prop];
Object.defineProperty(DefineMap.prototype, prop, {
enumerable:false,
value: define.eventsProto[prop],
writable: true
});
}
// @@can.onKeyValue and @@can.offKeyValue are also on define.eventsProto
// but symbols are not enumerated in for...in loops
var eventsProtoSymbols = ("getOwnPropertySymbols" in Object) ?
Object.getOwnPropertySymbols(define.eventsProto) :
[canSymbol.for("can.onKeyValue"), canSymbol.for("can.offKeyValue")];
eventsProtoSymbols.forEach(function(sym) {
Object.defineProperty(DefineMap.prototype, sym, {
configurable: true,
enumerable:false,
value: define.eventsProto[sym],
writable: true
});
});
//!steal-remove-start
if(process.env.NODE_ENV !== 'production') {
// call `map.log()` to log all event changes
// pass `key` to only log the matching property, e.g: `map.log("foo")`
define.defineConfigurableAndNotEnumerable(DefineMap.prototype, "log", defineHelpers.log);
}
//!steal-remove-end
// tells `can-define` to use this
define.DefineMap = DefineMap;
Object.defineProperty(DefineMap.prototype, "toObject", {
enumerable: false,
writable: true,
value: function(){
canLog.warn("Use DefineMap::get instead of DefineMap::toObject");
return this.get();
}
});
module.exports = ns.DefineMap = DefineMap;