123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- /**
- * 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.
- */
- /// <reference path="../Modules/Memory.ts" />
- class StoredMemory<T> {
- private id : string;
- private value : T;
- private listeners : Array<Function> = [];
- 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);
- }
- }
- }
|