/**
* This is used to store and retrieve values to/from Data Storage.
* The value used in the constructor is merely the Default value. It is safe to assume that the correct value will be loaded immediately.
* WARNING: IF YOU CHANGE THE TYPE OF VALUE A MEMORY STORES, YOU ALSO NEED A NEW ID. OR CHECKS. NEW ID IS EASIER.
* Optionally: use listeners if you have to change value on the fly.
*/
///
class StoredMemory {
private id : string;
private value : T;
private listeners : Array = [];
public constructor (id : string, value : T) {
this.id = id;
this.value = value;
Controls.Memory.registerMemory (this);
}
/**
* For use exclusively by Controls.Memory. Do not invoke. Use storeValue instead.
* @param {T} value
*/
public setValueFromLocalStorageDoNotInvoke (value : T) {
this.value = value;
}
public storeValue (value : T) {
if (JSON.stringify(value) !== JSON.stringify(this.value)) {
this.value = value;
this.triggerListeners();
}
}
public getValue () {
return this.value;
}
public getId () {
return this.id;
}
public addListener (listener : Function) {
this.listeners.push(listener);
}
public triggerListeners () {
for (var i = 0; i < this.listeners.length; i++) {
this.listeners[i](this);
}
}
}