-
Notifications
You must be signed in to change notification settings - Fork 7
/
can-define.js
1324 lines (1199 loc) · 39.8 KB
/
can-define.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
"format cjs";
var ns = require("can-namespace");
var canSymbol = require("can-symbol");
var canReflect = require("can-reflect");
var Observation = require("can-observation");
var ObservationRecorder = require("can-observation-recorder");
var AsyncObservable = require("can-simple-observable/async/async");
var SettableObservable = require("can-simple-observable/settable/settable");
var ResolverObservable = require("can-simple-observable/resolver/resolver");
var eventQueue = require("can-event-queue/map/map");
var addTypeEvents = require("can-event-queue/type/type");
var queues = require("can-queues");
var assign = require("can-assign");
var canLogDev = require("can-log/dev/dev");
var stringToAny = require("can-string-to-any");
var defineLazyValue = require("can-define-lazy-value");
var MaybeBoolean = require("can-data-types/maybe-boolean/maybe-boolean"),
MaybeDate = require("can-data-types/maybe-date/maybe-date"),
MaybeNumber = require("can-data-types/maybe-number/maybe-number"),
MaybeString = require("can-data-types/maybe-string/maybe-string");
var newSymbol = canSymbol.for("can.new"),
serializeSymbol = canSymbol.for("can.serialize"),
inSetupSymbol = canSymbol.for("can.initializing");
var eventsProto, define,
make, makeDefinition, getDefinitionsAndMethods, getDefinitionOrMethod;
// UTILITIES
function isDefineType(func){
return func && (func.canDefineType === true || func[newSymbol] );
}
var peek = ObservationRecorder.ignore(canReflect.getValue.bind(canReflect));
var Object_defineNamedPrototypeProperty = Object.defineProperty;
//!steal-remove-start
if(process.env.NODE_ENV !== 'production') {
Object_defineNamedPrototypeProperty = function(obj, prop, definition) {
if (definition.get) {
Object.defineProperty(definition.get, "name", {
value: "get "+canReflect.getName(obj) + "."+prop,
writable: true,
configurable: true
});
}
if (definition.set) {
Object.defineProperty(definition.set, "name", {
value: "set "+canReflect.getName(obj) + "."+prop,
configurable: true
});
}
return Object.defineProperty(obj, prop, definition);
};
}
//!steal-remove-end
function defineConfigurableAndNotEnumerable(obj, prop, value) {
Object.defineProperty(obj, prop, {
configurable: true,
enumerable: false,
writable: true,
value: value
});
}
function eachPropertyDescriptor(map, cb){
for(var prop in map) {
if(map.hasOwnProperty(prop)) {
cb.call(map, prop, Object.getOwnPropertyDescriptor(map,prop));
}
}
}
function getEveryPropertyAndSymbol(obj) {
var props = Object.getOwnPropertyNames(obj);
var symbols = ("getOwnPropertySymbols" in Object) ?
Object.getOwnPropertySymbols(obj) : [];
return props.concat(symbols);
}
function cleanUpDefinition(prop, definition, shouldWarn, typePrototype){
// cleanup `value` -> `default`
if(definition.value !== undefined && ( typeof definition.value !== "function" || definition.value.length === 0) ){
//!steal-remove-start
if(process.env.NODE_ENV !== 'production') {
if(shouldWarn) {
canLogDev.warn(
"can-define: Change the 'value' definition for " + canReflect.getName(typePrototype)+"."+prop + " to 'default'."
);
}
}
//!steal-remove-end
definition.default = definition.value;
delete definition.value;
}
// cleanup `Value` -> `DEFAULT`
if(definition.Value !== undefined ){
//!steal-remove-start
if(process.env.NODE_ENV !== 'production') {
if(shouldWarn) {
canLogDev.warn(
"can-define: Change the 'Value' definition for " + canReflect.getName(typePrototype)+"."+prop + " to 'Default'."
);
}
}
//!steal-remove-end
definition.Default = definition.Value;
delete definition.Value;
}
}
function isValueResolver(definition) {
// there's a function and it has one argument
return typeof definition.value === "function" && definition.value.length;
}
module.exports = define = ns.define = function(typePrototype, defines, baseDefine) {
// default property definitions on _data
var prop,
dataInitializers = Object.create(baseDefine ? baseDefine.dataInitializers : null),
// computed property definitions on _computed
computedInitializers = Object.create(baseDefine ? baseDefine.computedInitializers : null);
var result = getDefinitionsAndMethods(defines, baseDefine, typePrototype);
result.dataInitializers = dataInitializers;
result.computedInitializers = computedInitializers;
// Goes through each property definition and creates
// a `getter` and `setter` function for `Object.defineProperty`.
canReflect.eachKey(result.definitions, function(definition, property){
define.property(typePrototype, property, definition, dataInitializers, computedInitializers, result.defaultDefinition);
});
// Places a `_data` on the prototype that when first called replaces itself
// with a `_data` object local to the instance. It also defines getters
// for any value that has a default value.
if(typePrototype.hasOwnProperty("_data")) {
for (prop in dataInitializers) {
defineLazyValue(typePrototype._data, prop, dataInitializers[prop].bind(typePrototype), true);
}
} else {
defineLazyValue(typePrototype, "_data", function() {
var map = this;
var data = {};
for (var prop in dataInitializers) {
defineLazyValue(data, prop, dataInitializers[prop].bind(map), true);
}
return data;
});
}
// Places a `_computed` on the prototype that when first called replaces itself
// with a `_computed` object local to the instance. It also defines getters
// that will create the property's compute when read.
if(typePrototype.hasOwnProperty("_computed")) {
for (prop in computedInitializers) {
defineLazyValue(typePrototype._computed, prop, computedInitializers[prop].bind(typePrototype));
}
} else {
defineLazyValue(typePrototype, "_computed", function() {
var map = this;
var data = Object.create(null);
for (var prop in computedInitializers) {
defineLazyValue(data, prop, computedInitializers[prop].bind(map));
}
return data;
});
}
// Add necessary event methods to this object.
getEveryPropertyAndSymbol(eventsProto).forEach(function(prop){
Object.defineProperty(typePrototype, prop, {
enumerable: false,
value: eventsProto[prop],
configurable: true,
writable: true
});
});
// also add any symbols
// add so instance defs can be dynamically added
Object.defineProperty(typePrototype,"_define",{
enumerable: false,
value: result,
configurable: true,
writable: true
});
// Places Symbol.iterator or @@iterator on the prototype
// so that this can be iterated with for/of and canReflect.eachIndex
var iteratorSymbol = canSymbol.iterator || canSymbol.for("iterator");
if(!typePrototype[iteratorSymbol]) {
defineConfigurableAndNotEnumerable(typePrototype, iteratorSymbol, function(){
return new define.Iterator(this);
});
}
return result;
};
var onlyType = function(obj){
for(var prop in obj) {
if(prop !== "type") {
return false;
}
}
return true;
};
define.extensions = function () {};
// typePrototype - the prototype of the type we are defining `prop` on.
// `definition` - the user provided definition
define.property = function(typePrototype, prop, definition, dataInitializers, computedInitializers, defaultDefinition) {
var propertyDefinition = define.extensions.apply(this, arguments);
if (propertyDefinition) {
definition = makeDefinition(prop, propertyDefinition, defaultDefinition || {}, typePrototype);
}
var type = definition.type;
//!steal-remove-start
if(process.env.NODE_ENV !== 'production') {
var hasZeroArgGetter = definition.get && definition.get.length === 0;
var noSetter = !definition.set;
var defaultInDefinition = ( "default" in definition || "Default" in definition );
var typeInDefinition = (definition.type && defaultDefinition && definition.type !== defaultDefinition.type) ||
(definition.Type && defaultDefinition && definition.Type !== defaultDefinition.Type);
if(hasZeroArgGetter && noSetter && defaultInDefinition) {
var defaultOrDefault = "default" in definition ? "default" : "Default";
canLogDev.warn("can-define: " + defaultOrDefault + " value for property " +
canReflect.getName(typePrototype)+"."+ prop +
" ignored, as its definition has a zero-argument getter");
}
if(hasZeroArgGetter && noSetter && typeInDefinition) {
var typeOrType = definition.type ? "type" : "Type";
canLogDev.warn("can-define: " + typeOrType + " value for property " +
canReflect.getName(typePrototype)+"."+ prop +
" ignored, as its definition has a zero-argument getter");
}
if (type && canReflect.isConstructorLike(type) && !isDefineType(type)) {
canLogDev.warn(
"can-define: the definition for " + canReflect.getName(typePrototype) + "."+
prop +
" uses a constructor for \"type\". Did you mean \"Type\"?"
);
}
}
//!steal-remove-end
// Special case definitions that have only `type: "*"`.
if (type && onlyType(definition) && type === define.types["*"]) {
Object_defineNamedPrototypeProperty(typePrototype, prop, {
get: make.get.data(prop),
set: make.set.events(prop, make.get.data(prop), make.set.data(prop), make.eventType.data(prop)),
enumerable: true,
configurable: true
});
return;
}
definition.type = type;
// Where the value is stored. If there is a `get` the source of the value
// will be a compute in `this._computed[prop]`. If not, the source of the
// value will be in `this._data[prop]`.
var dataProperty = definition.get || isValueResolver(definition) ? "computed" : "data",
// simple functions that all read/get/set to the right place.
// - reader - reads the value but does not observe.
// - getter - reads the value and notifies observers.
// - setter - sets the value.
reader = make.read[dataProperty](prop),
getter = make.get[dataProperty](prop),
setter = make.set[dataProperty](prop),
getInitialValue;
//!steal-remove-start
if(process.env.NODE_ENV !== 'production') {
if (definition.get) {
Object.defineProperty(definition.get, "name", {
value: canReflect.getName(typePrototype) + "'s " + prop + " getter",
configurable: true
});
}
if (definition.set) {
Object.defineProperty(definition.set, "name", {
value: canReflect.getName(typePrototype) + "'s " + prop + " setter",
configurable: true
});
}
if(isValueResolver(definition)) {
Object.defineProperty(definition.value, "name", {
value: canReflect.getName(typePrototype) + "'s " + prop + " value",
configurable: true
});
}
}
//!steal-remove-end
// Determine the type converter
var typeConvert = function(val) {
return val;
};
if (definition.Type) {
typeConvert = make.set.Type(prop, definition.Type, typeConvert);
}
if (type) {
typeConvert = make.set.type(prop, type, typeConvert);
}
// make a setter that's going to fire of events
var eventsSetter = make.set.events(prop, reader, setter, make.eventType[dataProperty](prop));
if(isValueResolver(definition)) {
computedInitializers[prop] = make.valueResolver(prop, definition, typeConvert);
}
// Determine a function that will provide the initial property value.
else if ((definition.default !== undefined || definition.Default !== undefined)) {
//!steal-remove-start
if (process.env.NODE_ENV !== 'production') {
// If value is an object or array, give a warning
if (definition.default !== null && typeof definition.default === 'object') {
canLogDev.warn("can-define: The default value for " + canReflect.getName(typePrototype)+"."+prop + " is set to an object. This will be shared by all instances of the DefineMap. Use a function that returns the object instead.");
}
// If value is a constructor, give a warning
if (definition.default && canReflect.isConstructorLike(definition.default)) {
canLogDev.warn("can-define: The \"default\" for " + canReflect.getName(typePrototype)+"."+prop + " is set to a constructor. Did you mean \"Default\" instead?");
}
}
//!steal-remove-end
getInitialValue = ObservationRecorder.ignore(make.get.defaultValue(prop, definition, typeConvert, eventsSetter));
}
// If property has a getter, create the compute that stores its data.
if (definition.get) {
computedInitializers[prop] = make.compute(prop, definition.get, getInitialValue);
}
// If the property isn't a getter, but has an initial value, setup a
// default value on `this._data[prop]`.
else if (getInitialValue) {
dataInitializers[prop] = getInitialValue;
}
// Define setter behavior.
// If there's a `get` and `set`, make the setter get the `lastSetValue` on the
// `get`'s compute.
if (definition.get && definition.set) {
// the compute will set off events, so we can use the basic setter
setter = make.set.setter(prop, definition.set, make.read.lastSet(prop), setter, true);
// If there's zero-arg `get`, warn on all sets in dev mode
if (definition.get.length === 0 ) {
//!steal-remove-start
if(process.env.NODE_ENV !== 'production') {
canLogDev.warn("can-define: Set value for property " +
canReflect.getName(typePrototype)+"."+ prop +
" ignored, as its definition has a zero-argument getter");
}
//!steal-remove-end
}
}
// If there's a `set` and no `get`,
else if (definition.set) {
// Add `set` functionality to the eventSetter.
setter = make.set.setter(prop, definition.set, reader, eventsSetter, false);
}
// If there's neither `set` or `get` or `value` (resolver)
else if (dataProperty === "data") {
// make a set that produces events.
setter = eventsSetter;
}
// If there's zero-arg `get` but not `set`, warn on all sets in dev mode
else if (definition.get && definition.get.length < 1) {
setter = function() {
//!steal-remove-start
if(process.env.NODE_ENV !== 'production') {
canLogDev.warn("can-define: Set value for property " +
canReflect.getName(typePrototype)+"."+ prop +
" ignored, as its definition has a zero-argument getter and no setter");
}
//!steal-remove-end
};
}
// Add type behavior to the setter.
if (type) {
setter = make.set.type(prop, type, setter);
}
if (definition.Type) {
setter = make.set.Type(prop, definition.Type, setter);
}
// Define the property.
Object_defineNamedPrototypeProperty(typePrototype, prop, {
get: getter,
set: setter,
enumerable: "serialize" in definition ? !!definition.serialize : !definition.get,
configurable: true
});
};
define.makeDefineInstanceKey = function(constructor) {
constructor[canSymbol.for("can.defineInstanceKey")] = function(property, value) {
var defineResult = this.prototype._define;
if(typeof value === "object") {
// change `value` to default.
cleanUpDefinition(property, value, false, this);
}
var definition = getDefinitionOrMethod(property, value, defineResult.defaultDefinition, this);
if(definition && typeof definition === "object") {
define.property(constructor.prototype, property, definition, defineResult.dataInitializers, defineResult.computedInitializers, defineResult.defaultDefinition);
defineResult.definitions[property] = definition;
} else {
defineResult.methods[property] = definition;
}
this.prototype.dispatch({
action: "can.keys",
type: "can.keys", // TODO: Remove in 6.0
target: this.prototype
});
};
};
// Makes a simple constructor function.
define.Constructor = function(defines, sealed) {
var constructor = function DefineConstructor(props) {
Object.defineProperty(this, inSetupSymbol, {
configurable: true,
enumerable: false,
value: true,
writable: true
});
define.setup.call(this, props, sealed);
this[inSetupSymbol] = false;
};
var result = define(constructor.prototype, defines);
addTypeEvents(constructor);
define.makeDefineInstanceKey(constructor, result);
return constructor;
};
// A bunch of helper functions that are used to create various behaviors.
make = {
computeObj: function(map, prop, observable) {
var computeObj = {
oldValue: undefined,
compute: observable,
count: 0,
handler: function(newVal) {
var oldValue = computeObj.oldValue;
computeObj.oldValue = newVal;
map.dispatch({
action: "set",
key: "prop",
target: map,
value: newVal,
oldValue: oldValue,
type: prop, // TODO: Remove in 6.0
}, [newVal, oldValue]);
}
};
return computeObj;
},
valueResolver: function(prop, definition, typeConvert) {
var getDefault = make.get.defaultValue(prop, definition, typeConvert);
return function(){
var map = this;
var defaultValue = getDefault.call(this);
var computeObj = make.computeObj(map, prop, new ResolverObservable(definition.value, map, defaultValue));
//!steal-remove-start
if(process.env.NODE_ENV !== 'production') {
Object.defineProperty(computeObj.handler, "name", {
value: canReflect.getName(definition.value).replace('value', 'event emitter')
});
}
//!steal-remove-end
return computeObj;
};
},
// Returns a function that creates the `_computed` prop.
compute: function(prop, get, defaultValueFn) {
return function() {
var map = this,
defaultValue = defaultValueFn && defaultValueFn.call(this),
observable, computeObj;
if(get.length === 0) {
observable = new Observation(get, map);
} else if(get.length === 1) {
observable = new SettableObservable(get, map, defaultValue);
} else {
observable = new AsyncObservable(get, map, defaultValue);
}
computeObj = make.computeObj(map, prop, observable);
//!steal-remove-start
if(process.env.NODE_ENV !== 'production') {
Object.defineProperty(computeObj.handler, "name", {
value: canReflect.getName(get).replace('getter', 'event emitter')
});
}
//!steal-remove-end
return computeObj;
};
},
// Set related helpers.
set: {
data: function(prop) {
return function(newVal) {
this._data[prop] = newVal;
};
},
computed: function(prop) {
return function(val) {
canReflect.setValue( this._computed[prop].compute, val );
};
},
events: function(prop, getCurrent, setData, eventType) {
return function(newVal) {
if (this[inSetupSymbol]) {
setData.call(this, newVal);
}
else {
var current = getCurrent.call(this);
if (newVal === current) {
return;
}
var dispatched;
setData.call(this, newVal);
dispatched = {
patches: [{type: "set", key: prop, value: newVal}],
target: this,
action: "set",
value: newVal,
oldValue: current,
key: prop,
type: prop // TODO: Remove in 6.0
};
//!steal-remove-start
if(process.env.NODE_ENV !== 'production') {
var lastItem, lastFn;
dispatched.reasonLog = [ canReflect.getName(this) + "'s", prop, "changed to", newVal, "from", current ];
// If there are observations currently recording, this isn't a good time to
// mutate values: it's likely a cycle, and even if it doesn't cycle infinitely,
// it will likely cause unnecessary recomputation of derived values. Warn the user.
if(ObservationRecorder.isRecording() && queues.stack().length && !this[inSetupSymbol]) {
lastItem = queues.stack()[queues.stack().length - 1];
lastFn = lastItem.context instanceof Observation ? lastItem.context.func : lastItem.fn;
var mutationWarning = "can-define: The " + prop + " property on " +
canReflect.getName(this) +
" is being set in " +
(canReflect.getName(lastFn) || canReflect.getName(lastItem.fn)) +
". This can cause infinite loops and performance issues. " +
"Use the value() behavior for " +
prop +
" instead, and listen to other properties and observables with listenTo(). https://canjs.com/doc/can-define.types.value.html";
canLogDev.warn(mutationWarning);
queues.logStack();
}
}
//!steal-remove-end
this.dispatch(dispatched, [newVal, current]);
}
};
},
setter: function(prop, setter, getCurrent, setEvents, hasGetter) {
return function(value) {
//!steal-remove-start
var asyncTimer;
//!steal-remove-end
var self = this;
// call the setter, if returned value is undefined,
// this means the setter is async so we
// do not call update property and return right away
queues.batch.start();
var setterCalled = false,
current = getCurrent.call(this),
setValue = setter.call(this, value, function(value) {
setEvents.call(self, value);
setterCalled = true;
//!steal-remove-start
if(process.env.NODE_ENV !== 'production') {
clearTimeout(asyncTimer);
}
//!steal-remove-end
}, current);
if (setterCalled) {
queues.batch.stop();
} else {
if (hasGetter) {
// we got a return value
if (setValue !== undefined) {
// if the current `set` value is returned, don't set
// because current might be the `lastSetVal` of the internal compute.
if (current !== setValue) {
setEvents.call(this, setValue);
}
queues.batch.stop();
}
// this is a side effect, it didn't take a value
// so use the original set value
else if (setter.length === 0) {
setEvents.call(this, value);
queues.batch.stop();
return;
}
// it took a value
else if (setter.length === 1) {
// if we have a getter, and undefined was returned,
// we should assume this is setting the getters properties
// and we shouldn't do anything.
queues.batch.stop();
}
// we are expecting something
else {
//!steal-remove-start
if(process.env.NODE_ENV !== 'production') {
asyncTimer = setTimeout(function() {
canLogDev.warn('can-define: Setter "' + canReflect.getName(self)+"."+prop + '" did not return a value or call the setter callback.');
}, canLogDev.warnTimeout);
}
//!steal-remove-end
queues.batch.stop();
return;
}
} else {
// we got a return value
if (setValue !== undefined) {
// if the current `set` value is returned, don't set
// because current might be the `lastSetVal` of the internal compute.
setEvents.call(this, setValue);
queues.batch.stop();
}
// this is a side effect, it didn't take a value
// so use the original set value
else if (setter.length === 0) {
setEvents.call(this, value);
queues.batch.stop();
return;
}
// it took a value
else if (setter.length === 1) {
// if we don't have a getter, we should probably be setting the
// value to undefined
setEvents.call(this, undefined);
queues.batch.stop();
}
// we are expecting something
else {
//!steal-remove-start
if(process.env.NODE_ENV !== 'production') {
asyncTimer = setTimeout(function() {
canLogDev.warn('can/map/setter.js: Setter "' + canReflect.getName(self)+"."+prop + '" did not return a value or call the setter callback.');
}, canLogDev.warnTimeout);
}
//!steal-remove-end
queues.batch.stop();
return;
}
}
}
};
},
type: function(prop, type, set) {
function setter(newValue) {
return set.call(this, type.call(this, newValue, prop));
}
if(isDefineType(type)) {
// TODO: remove this `canDefineType` check in a future release.
if(type.canDefineType) {
return setter;
} else {
return function setter(newValue){
return set.call(this, canReflect.convert(newValue, type));
};
}
}
// If type is a nested object: `type: {foo: "string", bar: "number"}`
if (typeof type === "object") {
return make.set.Type(prop, type, set);
} else {
return setter;
}
},
Type: function(prop, Type, set) {
// `type`: {foo: "string"}
if(Array.isArray(Type) && define.DefineList) {
Type = define.DefineList.extend({
"#": Type[0]
});
} else if (typeof Type === "object") {
if(define.DefineMap) {
Type = define.DefineMap.extend(Type);
} else {
Type = define.Constructor(Type);
}
}
return function(newValue) {
if (newValue instanceof Type || newValue == null) {
return set.call(this, newValue);
} else {
return set.call(this, new Type(newValue));
}
};
}
},
// Helpes that indicate what the event type should be. These probably aren't needed.
eventType: {
data: function(prop) {
return function(newVal, oldVal) {
return oldVal !== undefined || this._data.hasOwnProperty(prop) ? "set" : "add";
};
},
computed: function() {
return function() {
return "set";
};
}
},
// Helpers that read the data in a non-observable way.
read: {
data: function(prop) {
return function() {
return this._data[prop];
};
},
computed: function(prop) {
// might want to protect this
return function() {
return canReflect.getValue( this._computed[prop].compute );
};
},
lastSet: function(prop) {
return function() {
var observable = this._computed[prop].compute;
if(observable.lastSetValue) {
return canReflect.getValue(observable.lastSetValue);
}
};
}
},
// Helpers that read the data in an observable way.
get: {
// uses the default value
defaultValue: function(prop, definition, typeConvert, callSetter) {
return function() {
var value = definition.default;
if (value !== undefined) {
if (typeof value === "function") {
value = value.call(this);
}
value = typeConvert.call(this, value);
}
else {
var Default = definition.Default;
if (Default) {
value = typeConvert.call(this,new Default());
}
}
if(definition.set) {
// TODO: there's almost certainly a faster way of making this happen
// But this is maintainable.
var VALUE;
var sync = true;
var setter = make.set.setter(prop, definition.set, function(){}, function(value){
if(sync) {
VALUE = value;
} else {
callSetter.call(this, value);
}
}, definition.get);
setter.call(this,value);
sync= false;
// VALUE will be undefined if the callback is never called.
return VALUE;
}
return value;
};
},
data: function(prop) {
return function() {
if (!this[inSetupSymbol]) {
ObservationRecorder.add(this, prop);
}
return this._data[prop];
};
},
computed: function(prop) {
return function(val) {
var compute = this._computed[prop].compute;
if (ObservationRecorder.isRecording()) {
ObservationRecorder.add(this, prop);
if (!canReflect.isBound(compute)) {
Observation.temporarilyBind(compute);
}
}
return peek(compute);
};
}
}
};
define.behaviors = ["get", "set", "value", "Value", "type", "Type", "serialize"];
// This cleans up a particular behavior and adds it to the definition
var addBehaviorToDefinition = function(definition, behavior, value) {
if(behavior === "enumerable") {
// treat enumerable like serialize
definition.serialize = !!value;
}
else if(behavior === "type") {
var behaviorDef = value;
if(typeof behaviorDef === "string") {
behaviorDef = define.types[behaviorDef];
if(typeof behaviorDef === "object" && !isDefineType(behaviorDef)) {
assign(definition, behaviorDef);
behaviorDef = behaviorDef[behavior];
}
}
if (typeof behaviorDef !== 'undefined') {
definition[behavior] = behaviorDef;
}
}
else {
definition[behavior] = value;
}
};
// This is called by `define.property` AND `getDefinitionOrMethod` (which is called by `define`)
// Currently, this is adding default behavior
// copying `type` over, and even cleaning up the final definition object
makeDefinition = function(prop, def, defaultDefinition, typePrototype) {
var definition = {};
canReflect.eachKey(def, function(value, behavior) {
addBehaviorToDefinition(definition, behavior, value);
});
// only add default if it doesn't exist
canReflect.eachKey(defaultDefinition, function(value, prop){
if(definition[prop] === undefined) {
if(prop !== "type" && prop !== "Type") {
definition[prop] = value;
}
}
});
// normalize Type that implements can.new
if(def.Type) {
var value = def.Type;
var serialize = value[serializeSymbol];
if(serialize) {
definition.serialize = function(val){
return serialize.call(val);
};
}
if(value[newSymbol]) {
definition.type = value;
delete definition.Type;
}
}
// We only want to add a defaultDefinition if def.type is not a string
// if def.type is a string it is handled in addDefinition
if(typeof def.type !== 'string') {
// if there's no type definition, take it from the defaultDefinition
if(!definition.type && !definition.Type) {
var defaultsCopy = canReflect.assignMap({},defaultDefinition);
definition = canReflect.assignMap(defaultsCopy, definition);
}
if( canReflect.size(definition) === 0 ) {
definition.type = define.types["*"];
}
}
cleanUpDefinition(prop, definition, true, typePrototype);
return definition;
};
// called by `can.defineInstanceKey` and `getDefinitionsAndMethods`
// returns the value or the definition object.
// calls makeDefinition
// This is dealing with a string value
getDefinitionOrMethod = function(prop, value, defaultDefinition, typePrototype){
// Clean up the value to make it a definition-like object
var definition;
if(typeof value === "string") {
definition = {type: value};
}
// copies a `Type`'s methods over
else if(value && (value[serializeSymbol] || value[newSymbol]) ) {
definition = { Type: value };
}
else if(typeof value === "function") {
if(canReflect.isConstructorLike(value)) {
definition = {Type: value};
}
// or leaves as a function
} else if( Array.isArray(value) ) {
definition = {Type: value};
} else if( canReflect.isPlainObject(value) ){
definition = value;
}
if(definition) {
return makeDefinition(prop, definition, defaultDefinition, typePrototype);
}
else {
return value;
}
};
// called by can.define
getDefinitionsAndMethods = function(defines, baseDefines, typePrototype) {
// make it so the definitions include base definitions on the proto
var definitions = Object.create(baseDefines ? baseDefines.definitions : null);
var methods = {};
// first lets get a default if it exists
var defaults = defines["*"],
defaultDefinition;
if(defaults) {
delete defines["*"];
defaultDefinition = getDefinitionOrMethod("*", defaults, {});
} else {
defaultDefinition = Object.create(null);
}
eachPropertyDescriptor(defines, function( prop, propertyDescriptor ) {
var value;
if(propertyDescriptor.get || propertyDescriptor.set) {
value = {get: propertyDescriptor.get, set: propertyDescriptor.set};
} else {
value = propertyDescriptor.value;
}
if(prop === "constructor") {
methods[prop] = value;
return;
} else {
var result = getDefinitionOrMethod(prop, value, defaultDefinition, typePrototype);
if(result && typeof result === "object" && canReflect.size(result) > 0) {
definitions[prop] = result;
}
else {
// Removed adding raw values that are not functions
if (typeof result === 'function') {
methods[prop] = result;
}
//!steal-remove-start
else if (typeof result !== 'undefined') {
if(process.env.NODE_ENV !== 'production') {
// Ex: {prop: 0}
canLogDev.error(canReflect.getName(typePrototype)+"."+prop + " does not match a supported propDefinition. See: https://canjs.com/doc/can-define.types.propDefinition.html");
}
}
//!steal-remove-end