Add method to watch for workflows storage changes

This method will be reused throughout the code to watch for changes in
the workflows storage area, in order to present the most recent list of
workflows available.

Bug: twpowertools:74
Change-Id: I60eff85d8436e40c6912fdbb75f02f6457e4e295
diff --git a/src/workflows/manager/index.js b/src/workflows/manager/index.js
index 01710b4..113177c 100644
--- a/src/workflows/manager/index.js
+++ b/src/workflows/manager/index.js
@@ -38,11 +38,10 @@
   constructor() {
     super();
     this._workflows = undefined;
-    this._updateWorkflows();
-    chrome.storage.onChanged.addListener((changes, areaName) => {
-      if (areaName == 'local' && changes[kWorkflowsDataKey])
-        this._updateWorkflows();
-    });
+    WorkflowsStorage.watch(workflows => {
+      this._workflows = workflows;
+      this.requestUpdate();
+    }, /* asProtobuf = */ true);
   }
 
   render() {
@@ -59,13 +58,6 @@
     `;
   }
 
-  _updateWorkflows() {
-    WorkflowsStorage.getAll(/* asProtobuf = */ true).then(workflows => {
-      this._workflows = workflows;
-      this.requestUpdate();
-    });
-  }
-
   _showAddDialog() {
     this.addDialog.value.open = true;
   }
diff --git a/src/workflows/workflowsStorage.js b/src/workflows/workflowsStorage.js
index 95b3583..6e10a07 100644
--- a/src/workflows/workflowsStorage.js
+++ b/src/workflows/workflowsStorage.js
@@ -4,6 +4,20 @@
 export const kWorkflowsDataKey = 'workflowsData';
 
 export default class WorkflowsStorage {
+  static watch(callback, asProtobuf = false) {
+    // Function which will be called when the watcher is initialized and every
+    // time the workflows storage changes.
+    const callOnChanged = () => {
+      this.getAll(asProtobuf).then(workflows => callback(workflows));
+    };
+
+    chrome.storage.onChanged.addListener((changes, areaName) => {
+      if (areaName == 'local' && changes[kWorkflowsDataKey]) callOnChanged();
+    });
+
+    callOnChanged();
+  }
+
   static getAll(asProtobuf = false) {
     return new Promise(res => {
       chrome.storage.local.get(kWorkflowsDataKey, items => {