-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-compute-like.js
62 lines (57 loc) · 1.97 KB
/
make-compute-like.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
"use strict";
var singleReference = require("can-single-reference");
var canReflect = require('can-reflect');
var Compute = function(newVal){
if(arguments.length) {
return canReflect.setValue(this, newVal);
} else {
return canReflect.getValue(this);
}
};
module.exports = function(observable) {
var compute = Compute.bind(observable);
//!steal-remove-start
if (process.env.NODE_ENV !== 'production') {
Object.defineProperty(compute, "name", {
value: "Compute<"+canReflect.getName(observable) + ">",
});
}
//!steal-remove-end
compute.on = compute.bind = compute.addEventListener = function(event, handler) {
var translationHandler = function(newVal, oldVal) {
handler.call(compute, {type:'change'}, newVal, oldVal);
};
singleReference.set(handler, this, translationHandler);
observable.on(translationHandler);
};
compute.off = compute.unbind = compute.removeEventListener = function(event, handler) {
observable.off( singleReference.getAndDelete(handler, this) );
};
canReflect.assignSymbols(compute, {
"can.getValue": function(){
return canReflect.getValue(observable);
},
"can.setValue": function(newVal){
return canReflect.setValue(observable, newVal);
},
"can.onValue": function(handler, queue){
return canReflect.onValue(observable, handler, queue);
},
"can.offValue": function(handler, queue){
return canReflect.offValue(observable, handler, queue);
},
"can.valueHasDependencies": function(){
return canReflect.valueHasDependencies(observable);
},
"can.getPriority": function(){
return canReflect.getPriority( observable );
},
"can.setPriority": function(newPriority){
canReflect.setPriority( observable, newPriority );
},
"can.isValueLike": true,
"can.isFunctionLike": false
});
compute.isComputed = true;
return compute;
};