aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview
diff options
context:
space:
mode:
authorLibravatar Vijay A <avijayr@protonmail.com>2021-08-31 16:29:31 +0530
committerLibravatar Vijay A <avijayr@protonmail.com>2021-08-31 16:49:36 +0530
commit91ebfb1c7dc2ef63be02f614585fdf9fd9ef6773 (patch)
tree510505dbf4f5997f46dc6e0d9cf6ddd61553b548 /src/webview
parent5.6.1-nightly.44 [skip ci] (diff)
downloadferdium-app-91ebfb1c7dc2ef63be02f614585fdf9fd9ef6773.tar.gz
ferdium-app-91ebfb1c7dc2ef63be02f614585fdf9fd9ef6773.tar.zst
ferdium-app-91ebfb1c7dc2ef63be02f614585fdf9fd9ef6773.zip
refactor: expose safeParseInt so that all recipes can use the same for consistent error handling
Diffstat (limited to 'src/webview')
-rw-r--r--src/webview/badge.ts13
-rw-r--r--src/webview/lib/RecipeWebview.js9
-rw-r--r--src/webview/recipe.js2
3 files changed, 20 insertions, 4 deletions
diff --git a/src/webview/badge.ts b/src/webview/badge.ts
index b1da36271..753e90fef 100644
--- a/src/webview/badge.ts
+++ b/src/webview/badge.ts
@@ -12,11 +12,16 @@ export class BadgeHandler {
12 }; 12 };
13 } 13 }
14 14
15 _normalizeNumber(count: string | number) { 15 // TODO: Need to extract this into a utility class and reuse outside of the recipes
16 safeParseInt(text: string | number | undefined | null) {
17 if (text === undefined || text === null) {
18 return 0;
19 }
20
16 // Parse number to integer 21 // Parse number to integer
17 // This will correct errors that recipes may introduce, e.g. 22 // This will correct errors that recipes may introduce, e.g.
18 // by sending a String instead of an integer 23 // by sending a String instead of an integer
19 const parsedNumber = parseInt(count.toString(), 10); 24 const parsedNumber = parseInt(text.toString(), 10);
20 const adjustedNumber = Number.isNaN(parsedNumber) ? 0 : parsedNumber; 25 const adjustedNumber = Number.isNaN(parsedNumber) ? 0 : parsedNumber;
21 return Math.max(adjustedNumber, 0); 26 return Math.max(adjustedNumber, 0);
22 } 27 }
@@ -28,8 +33,8 @@ export class BadgeHandler {
28 } 33 }
29 34
30 const count = { 35 const count = {
31 direct: this._normalizeNumber(direct), 36 direct: this.safeParseInt(direct),
32 indirect: this._normalizeNumber(indirect), 37 indirect: this.safeParseInt(indirect),
33 }; 38 };
34 39
35 debug('Sending badge count to host', count); 40 debug('Sending badge count to host', count);
diff --git a/src/webview/lib/RecipeWebview.js b/src/webview/lib/RecipeWebview.js
index 96caa125e..2bd6bad8d 100644
--- a/src/webview/lib/RecipeWebview.js
+++ b/src/webview/lib/RecipeWebview.js
@@ -47,6 +47,15 @@ class RecipeWebview {
47 } 47 }
48 48
49 /** 49 /**
50 * Safely parse the given text into an integer
51 *
52 * @param {string | number | undefined | null} text to be parsed
53 */
54 safeParseInt(text) {
55 return this.badgeHandler.safeParseInt(text);
56 }
57
58 /**
50 * Injects the contents of a CSS file into the current webview 59 * Injects the contents of a CSS file into the current webview
51 * 60 *
52 * @param {Array} files CSS files that should be injected. This must 61 * @param {Array} files CSS files that should be injected. This must
diff --git a/src/webview/recipe.js b/src/webview/recipe.js
index f88b94cfe..1981c8731 100644
--- a/src/webview/recipe.js
+++ b/src/webview/recipe.js
@@ -102,6 +102,8 @@ contextBridge.exposeInMainWorld('ferdi', {
102 open: window.open, 102 open: window.open,
103 setBadge: (direct, indirect) => 103 setBadge: (direct, indirect) =>
104 badgeHandler.setBadge(direct, indirect), 104 badgeHandler.setBadge(direct, indirect),
105 safeParseInt: (text) =>
106 badgeHandler.safeParseInt(text),
105 displayNotification: (title, options) => 107 displayNotification: (title, options) =>
106 notificationsHandler.displayNotification(title, options), 108 notificationsHandler.displayNotification(title, options),
107 getDisplayMediaSelector, 109 getDisplayMediaSelector,