-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobservable.js
176 lines (167 loc) · 5.26 KB
/
observable.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
"use strict";
module.exports = (function () {
/**
* Used for tracking Computeds dependencies
*/
function called( observable ) {
var tracking = Computed.tracking;
if (tracking.dependencies.indexOf( observable ) === -1) {
tracking.dependencies.push( observable );
observable.subscribe( tracking.run.bind( tracking ) );
}
}
/**
* var s = new Subscribable();
*
* s.subscribe(function ( value ) { console.log( value ) });
* A subscriber fn will be called any time s.notifyChange() is called
*/
function Subscribable() {
if (this === undefined) {
throw new Error('Subscribable is a constructor and should be called with "new"');
}
this.init();
}
Subscribable.prototype.init = function () {
this.subcriptions = [];
this._namedSubscribers = [];
this.value;
};
/**
* subscribe( function () {} );
* subscribe( 'namespace', function () {} );
*/
Subscribable.prototype.subscribe = function subscribe() {
var ns, fn;
if (arguments.length === 1) {
fn = arguments[0];
} else {
ns = arguments[0];
fn = arguments[1];
// Only one function per namespace
this.unsubscribe( ns );
// This holds the index that the fn will be stored at
this._namedSubscribers[ this.subcriptions.length ] = ns;
}
if (typeof fn !== 'function') {
throw new Error( 'Subscribe only accepts functions' );
}
this.subcriptions.push( fn );
};
/**
* unsubscribe a function by pointer or by namespace
*/
Subscribable.prototype.unsubscribe = function unsubscribe( namespace ) {
var index;
if (typeof namespace === 'string') {
index = this._namedSubscribers.indexOf( namespace );
this._namedSubscribers.splice( index, 1 );
} else {
index = this.subcriptions.indexOf( namespace );
if (this._namedSubscribers[ index ] != undefined) {
this._namedSubscribers.splice( index, 1 );
}
}
this.subcriptions.splice( index, 1 );
};
/**
* Call all subscribers with the latest value
*/
Subscribable.prototype.notifyChange = function notifyChange() {
for (var i = 0; i < this.subcriptions.length; i++) {
this.subcriptions[ i ]( this.value );
};
};
Subscribable.prototype.dispose = function dispose() {
this.subcriptions = null;
this._namedSubscribers = null;
this.value = null;
};
/**
* var o = new Observable( initValue );
* returns an observable object that inherits from Subscribable
*/
function Observable( initValue ) {
if (this === undefined) {
throw new Error('Observable is a constructor and should be called with "new"');
}
this.init( initValue );
}
// Inherit from Subscribable
Observable.prototype = Object.create(Subscribable.prototype);
Observable.prototype.init = function ( initValue ) {
Subscribable.prototype.init.call( this );
this.value = initValue;
};
Observable.prototype.equals = equals;
Observable.prototype.set = function ( v ) {
if (this.equals( v, this.value )) return;
this.value = v;
this.notifyChange();
return this;
};
Observable.prototype.get = function () {
if (Computed.tracking) {
called( this );
}
return this.value;
};
/**
* var c = new Computed( function () {} );
* returns a computed object that inherits from Subscribable
*/
function Computed( fn ) {
if (this === undefined) {
throw new Error('Computed is a constructor and should be called with "new"');
}
if (typeof fn !== 'function') {
throw new Error('A computed requires a function as the first argument');
}
this.init( fn );
}
Computed.tracking = null;
// Inherit from Subscribable
Computed.prototype = Object.create(Subscribable.prototype);
Computed.prototype.init = function ( fn ) {
Subscribable.prototype.init.call( this );
this.fn = fn;
this.dependencies = [];
Computed.tracking = this;
this.value = fn();
Computed.tracking = null;
};
Computed.prototype.equals = equals;
Computed.prototype.run = function () {
var v = this.fn();
if (this.equals( v, this.value )) return;
this.value = v;
this.notifyChange();
return this;
};
Computed.prototype.get = function () {
if (Computed.tracking) {
called( this );
}
return this.fn();
};
Computed.prototype.dispose = function () {
Subscribable.prototype.dispose.call( this );
while (this.dependencies.length) {
var dep = this.dependencies.pop();
dep.unsubscribe( this.run );
}
this.dependencies = null;
this.fn = null;
};
return {
'Subscribable': Subscribable,
'Observable': Observable,
'Computed': Computed
}
}());
/**
* The default equality comparer
*/
function equals( v, nv ) {
return v === nv;
}