Skip to content

Commit

Permalink
fix: 🪲💥Fixed #24
Browse files Browse the repository at this point in the history
  • Loading branch information
ohager committed Apr 6, 2022
1 parent ce08f3c commit 48ef44a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"watch:src": "cross-env NODE_ENV=development MANIFEST_VERSION=3 webpack --watch --progress",
"watch:dist": "mv3-hot-reload",
"build": "cross-env NODE_ENV=production webpack",
"prebuild": "rimraf ./dist && yarn clear:webpack-cache",
"build-all": "rimraf ./dist && run-s build:*",
"build:chrome": "cross-env TARGET_BROWSER=chrome MANIFEST_VERSION=3 run-s build",
"build:firefox": "cross-env TARGET_BROWSER=firefox MANIFEST_VERSION=2 run-s build",
Expand All @@ -19,6 +20,7 @@
"lint": "eslint src",
"lint:fix": "prettier \"./**/*\" --write --ignore-unknown",
"clear:lint": "rimraf node_modules/.cache/eslint-loader",
"clear:webpack-cache": "rimraf node_modules/.cache/webpack",
"prepare": "husky install"
},
"dependencies": {
Expand Down
7 changes: 7 additions & 0 deletions public/sw.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/* eslint-disable */

try {
const window = globalThis;
// This is the file produced by webpack
importScripts('background.js');

// wake up signal
chrome.runtime.onMessage.addListener(() => {
console.debug('⏰ Wake up, Sleepy Worker! ☕')
})

} catch (e) {
// This will allow you to see error logs during registration/execution
console.error(e);
Expand Down
43 changes: 39 additions & 4 deletions src/contentScript.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,43 @@
import browser from 'webextension-polyfill';

import { IntercomClient } from 'lib/intercom/client';
import { serializeError } from 'lib/intercom/helpers';
import { XTMessageType, TempleResponse } from 'lib/messaging';

async function testIntercomConnection() {
try {
await getIntercom().request({
type: XTMessageType.PageRequest,
payload: 'PING'
});
} catch (err: any) {
console.debug('Intercom connection corrupted', err);
unsubscribe();
intercom?.destroy();
intercom = null;
throw err;
}
}

function keepSWAlive() {
setTimeout(async function () {
try {
await browser.runtime.sendMessage('wakeup');
// what to do here?!
await testIntercomConnection();
console.debug('✅ Service Worker still listening');
} catch (e) {
console.debug('😣 Wakeup failed - Service Worker is deaf!');
}
keepSWAlive();
}, 10_000);
}

const manifest = browser.runtime.getManifest();
if (manifest.manifest_version === 3) {
keepSWAlive();
}

interface SignumPageMessage {
type: SignumPageMessageType;
payload: any;
Expand All @@ -28,7 +64,6 @@ window.addEventListener(

function walletRequest(evt: MessageEvent) {
const { payload, reqId } = evt.data as SignumPageMessage;

getIntercom()
.request({
type: XTMessageType.PageRequest,
Expand Down Expand Up @@ -77,12 +112,12 @@ function handleWalletNotification(msg: any) {
}
}

let intercom: IntercomClient;

let intercom: IntercomClient | null;
let unsubscribe: () => void;
function getIntercom() {
if (!intercom) {
intercom = new IntercomClient();
intercom.subscribe(handleWalletNotification);
unsubscribe = intercom.subscribe(handleWalletNotification);
}
return intercom;
}
1 change: 1 addition & 0 deletions src/lib/intercom/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class IntercomServer {
});

this.handleMessage = this.handleMessage.bind(this);
console.debug('Intercom Server is up...');
}

isConnected(port: Runtime.Port) {
Expand Down

0 comments on commit 48ef44a

Please sign in to comment.