forked from SpringRoll/SpringRoll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProperty.js
More file actions
68 lines (62 loc) · 1.81 KB
/
Property.js
File metadata and controls
68 lines (62 loc) · 1.81 KB
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
/**
* A class for representing changeable/subscribable properties.
* @class Property
* @property {*} _value the value of the property
* @property {[]} listeners all the objects listening to this property
* @property {boolean} alwaysNotify Determines if the property will notify a value change regardless if it's a new value or not.
*/
export class Property {
/**
* Creates a new property with an initial value.
* @param {*} initialValue The initial value of this property.
* @param {boolean} alwaysNotify Determines if the property will notify a value change regardless if it's a new value or not.
*/
constructor(initialValue, alwaysNotify = false) {
this._value = initialValue;
this.listeners = [];
this.alwaysNotify = alwaysNotify;
}
/**
* The current value of the property
* @type {*}
*/
get value() {
return this._value;
}
/**
* Setting this value notifies all listeners of the change.
* @type {*}
*/
set value(value) {
if (this.value === value && !this.alwaysNotify) {
return;
}
const prevValue = this._value;
this._value = value;
for (let i = 0; i < this.listeners.length; i++) {
this.listeners[i](this._value, prevValue);
}
}
/**
* Adds a subscriber to this property.
* @param {function} callback The callback to call whenever the property changes.
*/
subscribe(callback) {
this.listeners.push(callback);
}
/**
* Unsubscribes a listener from this property.
* @param {function} callback The callback to unsubscribe.
*/
unsubscribe(callback) {
this.listeners = this.listeners.filter(listener => listener !== callback);
}
/**
* Whether or not this property has any subscribed listeners
* @readonly
* @type {Boolean}
*/
get hasListeners() {
return this.listeners.length > 0;
}
}