Browse Source

refactor(layouts/toolbar): abstract help, status and error text

Josh Habdas 1 year ago
parent
commit
69441a87fd
Signed by: Josh Habdas <jhabdas@protonmail.com> GPG Key ID: B148B31154C75A74
1 changed files with 107 additions and 78 deletions
  1. 107
    78
      layouts/partials/modules/toxic-swamp/toolbar.html

+ 107
- 78
layouts/partials/modules/toxic-swamp/toolbar.html View File

@@ -102,13 +102,85 @@
102 102
       const interstitial = form.querySelector('#js-interstitial');
103 103
       const ticker = form.querySelector('#js-ticker');
104 104
 
105
-      class Miner {
105
+      const displaySetting = state.shouldDisclose ? 'full' : 'compact';
106
+      const helpText = {
107
+        minerActivated: 'Miner activated…',
108
+        minerDeactivated: 'Miner deactivated…',
109
+        deviceOnline: 'Device online…',
110
+        deviceOffline: 'Device offline…',
111
+        devicePowered: 'Device powered…',
112
+        powerUnplugged: 'Power unplugged…',
113
+        miningPaused: 'Mining paused…',
114
+        miningResumed: 'Mining resumed…',
115
+        networkDisconnected: 'Network disconnected…',
116
+        networkRestored: 'Network restored…',
117
+        clickToActivate: 'Click to activate…',
118
+        clickToOverride: 'Click to override…',
119
+        savingBattery: 'Saving battery…',
120
+        phraseHashPower: 'hash power…',
121
+        wordHashes: 'hashes',
122
+        unitsPerSecond: 'H/s'
123
+      };
124
+      const statusText = {
125
+        waitingForNetwork: 'waiting for network',
126
+        waitingForServer: 'waiting for server',
127
+        newJob: 'new job',
128
+        solvedJob: 'solved job',
129
+        poolAcceptedHash: 'pool accepted hash!',
130
+        error: 'error'
131
+      }
132
+      const errorText = {
133
+        displayModeInvalid: `Display mode invalid! Expected one of 'full', 'compact', 'minimal' or 'hidden'.`
134
+      }
135
+
136
+      let statusIntervalId;
137
+      const startMining = () => {
138
+        clearInterval(statusIntervalId);
139
+        WebMiner.start();
140
+        const { receiveStack, sendStack } = WebMiner;
141
+        statusIntervalId = setInterval(function () {
142
+          while (sendStack.length) updateStatus(sendStack.pop());
143
+          while (receiveStack.length) updateStatus(receiveStack.pop());
144
+        }, 2000);
145
+      };
146
+      const stopMining = () => {
147
+        WebMiner.stop();
148
+        clearInterval(statusIntervalId);
149
+      }
150
+      const updateStatus = data => {
151
+        status.textContent = `[${new Date().toLocaleString()}] `;
152
+        if (data.identifier === 'job') {
153
+          form.toggle.classList.add('-mining');
154
+          status.textContent += `${statusText.newJob}: ${data.job_id}`;
155
+        } else if (data.identifier === 'solved') {
156
+          status.textContent += `${statusText.solvedJob}: ${data.job_id}`;
157
+        } else if (data.identifier === 'hashsolved') {
158
+          status.textContent += statusText.poolAcceptedHash;
159
+        } else if (data.identifier === 'error') {
160
+          form.toggle.classList.remove('-mining');
161
+          status.textContent += `${statusText.error}: ${data.param}`;
162
+        } else status.textContent += data;
163
+        debug(status.textContent);
164
+      };
165
+      const showInterstitial = (message, delay = 0) => {
166
+        setTimeout(function () {
167
+          interstitial.textContent = message;
168
+          ticker.hidden = true;
169
+          interstitial.hidden = false;
170
+          setTimeout(function () {
171
+            ticker.hidden = false;
172
+            interstitial.hidden = true;
173
+          }, 2000);
174
+        }, delay);
175
+      };
176
+
177
+      class Actuator {
106 178
         static activate (shouldPersist = false) {
107 179
           state.isMining = true;
108 180
           startMining();
109 181
           shouldPersist && (() => {
110 182
             SessionManager.shouldMine = true;
111
-            showInterstitial('Miner activated…');
183
+            showInterstitial(helpText.minerActivated);
112 184
           })();
113 185
         }
114 186
         static deactivate (shouldPersist = false) {
@@ -116,7 +188,7 @@
116 188
           stopMining();
117 189
           shouldPersist && (() => {
118 190
             SessionManager.shouldMine = false;
119
-            showInterstitial('Miner deactivated…');
191
+            showInterstitial(helpText.minerDeactivated);
120 192
           })();
121 193
         }
122 194
         static get status () {
@@ -140,26 +212,26 @@
140 212
           requestAnimationFrame(updateTicker);
141 213
         }
142 214
         static togglePower (wasUserInitiated = false) {
143
-          const isMinerActive = Miner.status === 'active';
215
+          const isMinerActive = Actuator.status === 'active';
144 216
           const isDeviceOnline = navigator.onLine;
145 217
           form.toggle.classList.toggle('-active');
146 218
           isMinerActive
147
-            ? Miner.deactivate(wasUserInitiated) && Toolbar.toggleStatusbar()
148
-            : Miner.activate(wasUserInitiated);
219
+            ? Actuator.deactivate(wasUserInitiated) && Toolbar.toggleStatusbar()
220
+            : Actuator.activate(wasUserInitiated);
149 221
           isDeviceOnline
150
-            ? updateStatus('waiting for server')
151
-            : updateStatus('waiting for network');
222
+            ? updateStatus(statusText.waitingForServer)
223
+            : updateStatus(statusText.waitingForNetwork);
152 224
         }
153 225
         static setThrottle (throttle) {
154 226
           WebMiner.throttle = 100 - throttle;
155 227
           SessionManager.throttle = 100 - throttle;
156
-          showInterstitial(`${throttle}% hash power…`);
228
+          showInterstitial(`${throttle}% ${helpText.phraseHashPower}`);
157 229
         }
158 230
         static setDisplayMode (displayMode = 'full') {
159 231
           const isValidMode = [
160 232
             'full', 'compact', 'hidden', 'minimal'
161 233
           ].includes(displayMode);
162
-          if (!isValidMode) throw new Error(`Toolbar expected display mode of 'full', 'compact', 'minimal' or 'hidden': '${displayMode}' received`);
234
+          if (!isValidMode) throw new Error(errorText.displayModeInvalid);
163 235
           Toolbar.displayMode = displayMode;
164 236
           Toolbar.updateDisplayMode();
165 237
         }
@@ -169,7 +241,7 @@
169 241
         static updateTickerTotal () {
170 242
           const total = state.totalHashes + WebMiner.hashTotal;
171 243
           const hashrate = state.hashrate || 0;
172
-          ticker.textContent = `${total} hashes (${hashrate} H/s)`;
244
+          ticker.textContent = `${total} ${helpText.wordHashes} (${hashrate} ${helpText.unitsPerSecond})`;
173 245
         }
174 246
         static updateDisplayMode () {
175 247
           const displayMode = Toolbar.displayMode;
@@ -203,7 +275,7 @@
203 275
           });
204 276
         }
205 277
         static toggleStatusbar () {
206
-          const isMinerActive = Miner.status === 'active';
278
+          const isMinerActive = Actuator.status === 'active';
207 279
           isMinerActive
208 280
             ? form.classList.toggle('-disclosed')
209 281
             : form.classList.remove('-disclosed');
@@ -235,105 +307,62 @@
235 307
           );
236 308
         }
237 309
       }
238
-      const displaySetting = state.shouldDisclose ? 'full' : 'compact';
239 310
 
240 311
       Toolbar.initialize();
241 312
       Toolbar.setDisplayMode(displaySetting);
242 313
       Toolbar.registerListeners();
243 314
 
244
-      const updateStatus = data => {
245
-        status.textContent = `[${new Date().toLocaleString()}] `;
246
-        if (data.identifier === 'job') {
247
-          form.toggle.classList.add('-mining');
248
-          status.textContent += `new job: ${data.job_id}`;
249
-        } else if (data.identifier === 'solved') {
250
-          status.textContent += `solved job: ${data.job_id}`;
251
-        } else if (data.identifier === 'hashsolved') {
252
-          status.textContent += 'pool accepted hash!';
253
-        } else if (data.identifier === 'error') {
254
-          form.toggle.classList.remove('-mining');
255
-          status.textContent += `error: ${data.param}`;
256
-        } else status.textContent += data;
257
-        debug(status.textContent);
258
-      };
259
-      const showInterstitial = (message, delay = 0) => {
260
-        setTimeout(function () {
261
-          interstitial.textContent = message;
262
-          ticker.hidden = true;
263
-          interstitial.hidden = false;
264
-          setTimeout(function () {
265
-            ticker.hidden = false;
266
-            interstitial.hidden = true;
267
-          }, 2000);
268
-        }, delay);
269
-      };
270
-
271
-      let statusIntervalId;
272
-      const startMining = () => {
273
-        clearInterval(statusIntervalId);
274
-        WebMiner.start();
275
-        const { receiveStack, sendStack } = WebMiner;
276
-        statusIntervalId = setInterval(function () {
277
-          while (sendStack.length) updateStatus(sendStack.pop());
278
-          while (receiveStack.length) updateStatus(receiveStack.pop());
279
-        }, 2000);
280
-      };
281
-      const stopMining = () => {
282
-        WebMiner.stop();
283
-        clearInterval(statusIntervalId);
284
-      }
285
-
286 315
       const handleChargingChange = evt => {
287 316
         const shouldMine = SessionManager.shouldMine;
288 317
         if (!shouldMine) return;
289
-        const isMinerActive = Miner.status === 'active';
318
+        const isMinerActive = Actuator.status === 'active';
290 319
         const startedCharging = evt.target.charging;
291 320
         const isDeviceOnline = navigator.onLine;
292 321
         if (startedCharging) {
293 322
           const isDeviceOnline = navigator.onLine;
294
-          showInterstitial('Device powered…');
323
+          showInterstitial(helpText.devicePowered);
295 324
           !isMinerActive && Toolbar.togglePower()
296 325
           if (isDeviceOnline) {
297
-            showInterstitial('Mining resumed…', 3000);
326
+            showInterstitial(helpText.miningResumed, 3000);
298 327
           } else {
299 328
             updateStatus('waiting for network');
300
-            showInterstitial('Network disconnected…', 3000);
329
+            showInterstitial(helpText.networkDisconnected, 3000);
301 330
           }
302 331
         } else {
303
-          showInterstitial('Power unplugged…');
332
+          showInterstitial(helpText.powerUnplugged);
304 333
           isMinerActive && Toolbar.togglePower();
305 334
           isDeviceOnline
306
-            ? showInterstitial('Saving battery…', 3000)
307
-            : showInterstitial('Network disconnected…', 3000);
335
+            ? showInterstitial(helpText.savingBattery, 3000)
336
+            : showInterstitial(helpText.networkDisconnected, 3000);
308 337
         }
309 338
       };
310 339
       const handleOnlineChange = evt => {
311 340
         const shouldMine = SessionManager.shouldMine;
312 341
         if (!shouldMine) return;
313
-        const isMinerActive = Miner.status === 'active';
342
+        const isMinerActive = Actuator.status === 'active';
314 343
         const wentOnline = evt.type === 'online';
315 344
         if (wentOnline) {
316
-          showInterstitial('Device online…');
345
+          showInterstitial(helpText.networkRestored);
317 346
           if (isMinerActive) {
318
-            updateStatus('waiting for server');
319
-            showInterstitial('Mining resumed…', 3000);
347
+            updateStatus(statusText.waitingForServer);
348
+            showInterstitial(helpText.miningResumed, 3000);
320 349
           } else {
321
-            showInterstitial('Click to activate…', 3000);
350
+            showInterstitial(helpText.clickToActivate, 3000);
322 351
           }
323 352
         } else {
324
-          showInterstitial('Device went offline…');
353
+          showInterstitial(helpText.networkDisconnected);
325 354
           if (isMinerActive) {
326
-            updateStatus('waiting for network');
327
-            showInterstitial('Miner on standby…', 3000);
355
+            updateStatus(statusText.waitingForNetwork);
356
+            showInterstitial(helpText.miningPaused, 3000);
328 357
           } else {
329
-            showInterstitial('Mining paused…', 3000);
358
+            showInterstitial(helpText.miningPaused, 3000);
330 359
           }
331 360
         }
332 361
       };
333 362
       const handleVisibilityChange = evt => {
334 363
         const shouldMine = SessionManager.shouldMine;
335 364
         if (!shouldMine) return;
336
-        const isMinerActive = Miner.status === 'active';
365
+        const isMinerActive = Actuator.status === 'active';
337 366
         const wasDocumentHidden = document['hidden'];
338 367
         if (wasDocumentHidden) {
339 368
           isMinerActive && Toolbar.togglePower();
@@ -343,8 +372,8 @@
343 372
             if (isDeviceCharging) {
344 373
               !isMinerActive && Toolbar.togglePower();
345 374
             } else {
346
-              showInterstitial('Saving battery…');
347
-              showInterstitial('Click to override…', 3000);
375
+              showInterstitial(helpText.savingBattery);
376
+              showInterstitial(helpText.clickToOverride, 3000);
348 377
             }
349 378
           });
350 379
         }
@@ -369,11 +398,11 @@
369 398
         const isDeviceCharging = battery.charging;
370 399
         if (isDeviceCharging) {
371 400
           if (isDeviceOnline) return Toolbar.togglePower();
372
-          showInterstitial('Network disconnected…');
373
-          showInterstitial('Mining suspended…', 3000);
401
+          showInterstitial(helpText.networkDisconnected);
402
+          showInterstitial(helpText.miningPaused, 3000);
374 403
         } else {
375
-          showInterstitial('Power unplugged…');
376
-          showInterstitial('Miner on standby…', 3000);
404
+          showInterstitial(helpText.powerUnplugged);
405
+          showInterstitial(helpText.miningPaused, 3000);
377 406
         }
378 407
       }); // zip it up and zip it out
379 408
     });

Loading…
Cancel
Save