This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
obs.amd.js
218 lines (198 loc) · 6.04 KB
/
obs.amd.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
/*! obs 0.10.1 Original author Alan Plum <me@pluma.io>. Released into the Public Domain under the UNLICENSE. @preserve */
define(function(require, exports, module) {
var PubSub = require('sublish').PubSub,
slice = Array.prototype.slice,
isArray = Array.isArray ? Array.isArray : function(arr) {
return Object.prototype.toString.call(arr) === '[object Array]';
},
contains = Array.prototype.indexOf ? function(arr, el) {
return !!~arr.indexOf(el);
} : function(arr, el) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] === el) {
return true;
}
}
return false;
};
function ext(dest) {
var srcs = slice.call(arguments, 1);
for (var i = 0; i < srcs.length; i++) {
var src = srcs[i];
for (var key in src) {
if (src.hasOwnProperty(key)) {
dest[key] = src[key];
}
}
}
return dest;
}
function parseComputedConfig(args) {
var config = args[0];
if (typeof config === 'function') {
config = {compute: args[0]};
if (args.length > 1) {
if (typeof args[1] === 'function') {
config.write = args[1];
if (args.length > 2) {
config.watch = args[2];
}
} else {
config.watch = args[1];
}
}
}
return config;
}
function obs(config) {
function observable(value) {
if (arguments.length) {
if (typeof observable.write === 'function') {
observable.write.call(observable.context, value);
} else {
throw new Error('This observable cannot be written to!');
}
} else {
if (typeof observable.read === 'function') {
return observable.read.call(observable.context);
} else {
throw new Error('This observable cannot be read from!');
}
}
}
ext(observable, PubSub.prototype, {
context: config.context || observable,
read: config.read,
write: config.write,
onNotify: (function(onNotify) {
return function() {
onNotify.call(observable);
};
}(config.onNotify || function() {})),
_initialValue: config.value,
_currentValue: config.value,
_previousValue: undefined,
_subscriptions: []
}, obs.fn);
PubSub.apply(observable);
if (config.watch) {
observable.watch.apply(observable, (
isArray(config.watch) ? config.watch : [config.watch]
));
}
return observable;
}
obs.fn = {
__is_obs__: true,
notify: function() {
this.dirty = (this._currentValue === this._initialValue);
this.publish(this._currentValue, this._previousValue);
},
peek: function() {
return this._currentValue;
},
commit: function() {
this._initialValue = this._currentValue;
this.dirty = false;
},
reset: function() {
this._previousValue = this._currentValue;
this._currentValue = this._initialValue;
this.notify();
},
watch: function() {
var args = slice.call(arguments, 0), sub, i;
for (i = 0; i < args.length; i++) {
sub = args[i];
if (contains(this._subscriptions, sub)) {
continue;
}
if (sub && typeof sub.subscribe === 'function') {
sub.subscribe(this.onNotify);
this._subscriptions.push(sub);
}
}
return this;
},
unwatch: function() {
var subs = slice.call(arguments, 0),
allSubs = this._subscriptions,
i, sub;
for (i = 0; i < subs.length; i++) {
sub = subs[i];
if (sub && typeof sub.unsubscribe === 'function') {
sub.unsubscribe(this.onNotify);
}
}
this._subscriptions = [];
for (i = 0; i < allSubs.length; i++) {
sub = allSubs[i];
if (!contains(subs, sub)) {
this._subscriptions.push(sub);
}
}
return this;
},
dismiss: function() {
this.unwatch.apply(this, this._subscriptions);
}
};
obs.prop = function(initialValue) {
return obs({
read: function() {
return this._currentValue;
},
write: function(value) {
this._previousValue = this._currentValue;
this._currentValue = value;
this.notify();
},
value: initialValue
});
};
obs.computed = function(config) {
config = parseComputedConfig(arguments);
var observable = obs(ext({}, config, {
read: function() {
return observable._currentValue;
},
onNotify: function() {
this._previousValue = this._currentValue;
this._currentValue = config.compute.call(this.context);
this.notify();
}
}));
if (config.compute) {
observable._initialValue = config.compute.call(observable.context);
observable._currentValue = observable._initialValue;
}
if (config.watch) {
observable.watch.apply(observable, (
isArray(config.watch) ? config.watch : [config.watch]
));
}
return observable;
};
obs.computed.lazy = function(config) {
config = parseComputedConfig(arguments);
var changed = true;
var observable = obs(ext({}, config, {
context: config.context,
read: function() {
if (changed) {
changed = false;
observable._previousValue = observable._currentValue;
observable._currentValue = config.compute.call(this);
observable.notify();
}
return observable._currentValue;
},
write: config.write,
onNotify: function() {
changed = true;
},
watch: config.watch
}));
return observable;
};
module.exports = obs;return module.exports;});