Introduced state class for simple reactive state propagation
Change-Id: I3c98751ff124b289499d179bf3fff4bee9350e7e
diff --git a/dev/js/src/state.js b/dev/js/src/state.js
new file mode 100644
index 0000000..013ae10
--- /dev/null
+++ b/dev/js/src/state.js
@@ -0,0 +1,64 @@
+/**
+ * Create a state object, that can have a single value
+ * (mostly boolean) and multiple objects associated to it.
+ * Whenever the state changes, all objects are informed
+ * by their setState() method of the value change.
+ *
+ * @author Nils Diewald
+ */
+define(function () {
+
+ "use strict";
+
+ return {
+
+ /**
+ * Constructor
+ */
+ create : function (value) {
+ return Object.create(this)._init(value);
+ },
+
+ // Initialize
+ _init : function (value) {
+ this._assoc = [];
+ this.value = value;
+ return this;
+ },
+
+
+ /**
+ * Associate the state with some objects.
+ */
+ associate : function (obj) {
+
+ // Check if the object has a setState() method
+ if (obj.hasOwnProperty("setState")) {
+ this._assoc.push(obj);
+ obj.setState(this.value);
+ } else {
+ console.log("Object " + obj + " has no setState() method");
+ }
+ },
+
+ /**
+ * Set the state to a certain value.
+ * This will set the state to all associated objects as well.
+ */
+ set : function (value) {
+ if (value != this.value) {
+ this.value = value;
+ for (let i in this._assoc) {
+ this._assoc[i].setState(value);
+ }
+ };
+ },
+
+ /**
+ * Get the state value
+ */
+ get : function () {
+ return this.value;
+ }
+ }
+});