From 0c0dd9394469e6ed2fb7e29a0e8d3697ae86d795 Mon Sep 17 00:00:00 2001 From: lutangar Date: Tue, 24 Aug 2021 17:36:34 +0200 Subject: [PATCH] wip(background): switch to alarm --- manifest/base.js | 110 ++++++++---------- .../store/sagas/lib/sendToTab.saga.ts | 3 +- .../store/sagas/refreshContributors.ts | 3 +- .../store/sagas/refreshMatchingContexts.ts | 3 +- .../store/sagas/ui/fakeLoading.saga.ts | 3 +- src/app/content/store/sagas/ui/index.tsx | 4 +- .../store/sagas/portConnection.saga.ts | 3 +- .../store/sagas/windowConnection.saga.ts | 2 +- src/libs/store/sagas/effects/callAndRetry.ts | 3 +- src/libs/store/sagas/effects/delay.ts | 48 ++++++++ 10 files changed, 110 insertions(+), 72 deletions(-) create mode 100644 src/libs/store/sagas/effects/delay.ts diff --git a/manifest/base.js b/manifest/base.js index 6820e3852..f2c705733 100644 --- a/manifest/base.js +++ b/manifest/base.js @@ -8,67 +8,51 @@ const version = require('../package.json').version; const icons = require('./icons'); -module.exports = facet => { - const facetName = facet === 'lmel' ? 'Le Même en Local' : 'Dismoi'; - - return Object.freeze({ - name: facetName, - description: - facet === 'lmel' - ? 'La façon la plus simple de trouver des alternatives locales, en un seul clic. Gratuit et sans publicité.' - : 'Amis, media, experts vous informent directement sur les pages web visitées. ', - version, - manifest_version: 3, - icons: icons[facet], - background: { - scripts: ['/js/browser-polyfill.js', 'js/background.bundle.js'] - }, - content_scripts: [ - { - exclude_globs: [ - '*.pdf*', - '*.Pdf*', - '*.PDF*', - '*.jpeg*', - '*.jpg*', - '*.png*', - '*.gif*' - ], - matches: ['*://*/*'], - exclude_matches: [ - '*://*.googleusercontent.com/viewer/secure/pdf/*', - '*://*.cdn.mozilla.net/*', - '*://*.accounts.firefox.com/*', - '*://*.addons.mozilla.org/*', - '*://*.input.mozilla.org/*', - '*://*.install.mozilla.org/*', - '*://*.support.mozilla.org/*', - '*://*.services.mozilla.org/*', - '*://*.testpilot.firefox.com/*' - ], - js: ['js/browser-polyfill.js', 'js/content.bundle.js'], - run_at: 'document_end' - } - ], - browser_action: { - default_icon: { - '16': 'img/logo/' + facet + '/16x16.png', - '48': 'img/logo/' + facet + '/48x48.png', - '128': 'img/logo/' + facet + '/128x128.png' - }, - default_title: facetName +module.exports = Object.freeze({ + name: 'Dismoi', + description: + 'Amis, media, experts vous informent directement sur les pages web visitées. ', + version, + manifest_version: 2, + icons, + background: { + scripts: ['/js/browser-polyfill.js', 'js/background.bundle.js'] + }, + content_scripts: [ + { + exclude_globs: [ + '*.pdf*', + '*.Pdf*', + '*.PDF*', + '*.jpeg*', + '*.jpg*', + '*.png*', + '*.gif*' + ], + matches: ['*://*/*'], + exclude_matches: [ + '*://*.googleusercontent.com/viewer/secure/pdf/*', + '*://*.cdn.mozilla.net/*', + '*://*.accounts.firefox.com/*', + '*://*.addons.mozilla.org/*', + '*://*.input.mozilla.org/*', + '*://*.install.mozilla.org/*', + '*://*.support.mozilla.org/*', + '*://*.services.mozilla.org/*', + '*://*.testpilot.firefox.com/*' + ], + js: ['js/browser-polyfill.js', 'js/content.bundle.js'], + run_at: 'document_end' + } + ], + browser_action: { + default_icon: { + '16': 'img/logo/16x16.png', + '48': 'img/logo/48x48.png', + '128': 'img/logo/128x128.png' }, - permissions: ['activeTab', 'storage', 'contextMenus'], - web_accessible_resources: [ - { - "resources": [ - 'img/*', - 'fonts/*' - ], - "matches": [ - "*://*/*" - ] - } - ] - }); -}; + default_title: 'Dismoi' + }, + permissions: ['activeTab', 'storage', 'contextMenus', 'alarms'], + web_accessible_resources: ['img/*', 'fonts/*'], +}); diff --git a/src/app/background/store/sagas/lib/sendToTab.saga.ts b/src/app/background/store/sagas/lib/sendToTab.saga.ts index 717b37c49..3095237ab 100644 --- a/src/app/background/store/sagas/lib/sendToTab.saga.ts +++ b/src/app/background/store/sagas/lib/sendToTab.saga.ts @@ -1,6 +1,7 @@ import { SagaIterator } from 'redux-saga'; -import { delay, call, put } from 'redux-saga/effects'; +import { call, put } from 'redux-saga/effects'; import { AppAction, tabDied } from 'libs/store/actions'; +import delay from 'libs/store/sagas/effects/delay'; import Tab from 'libs/domain/tab'; import Logger from 'libs/utils/Logger'; import sendToTab from 'libs/webext/sendActionToTab'; diff --git a/src/app/background/store/sagas/refreshContributors.ts b/src/app/background/store/sagas/refreshContributors.ts index 3bdd17364..19421f795 100644 --- a/src/app/background/store/sagas/refreshContributors.ts +++ b/src/app/background/store/sagas/refreshContributors.ts @@ -1,7 +1,8 @@ -import { fork, delay, put, takeLatest } from 'redux-saga/effects'; +import { fork, put, takeLatest } from 'redux-saga/effects'; import { REFRESH_CONTRIBUTORS, refreshContributors } from 'libs/store/actions'; import minutesToMilliseconds from 'libs/utils/minutesToMilliseconds'; import refreshContributorsSaga from 'libs/store/sagas/refreshContributors.saga'; +import delay from 'libs/store/sagas/effects/delay'; const refreshInterval = minutesToMilliseconds( Number(process.env.REFRESH_CONTRIBUTORS_INTERVAL) diff --git a/src/app/background/store/sagas/refreshMatchingContexts.ts b/src/app/background/store/sagas/refreshMatchingContexts.ts index 5b2623c8c..5456b366d 100644 --- a/src/app/background/store/sagas/refreshMatchingContexts.ts +++ b/src/app/background/store/sagas/refreshMatchingContexts.ts @@ -1,10 +1,11 @@ -import { delay, put, select, takeEvery, fork } from 'redux-saga/effects'; +import { put, select, takeEvery, fork } from 'redux-saga/effects'; import { receivedMatchingContexts, refreshMatchingContextsFailed, SUBSCRIBE, SubscribeAction } from 'libs/store/actions'; +import delay from 'libs/store/sagas/effects/delay'; import { createCallAndRetry } from 'libs/store/sagas/effects/callAndRetry'; import { getSubscriptions } from 'app/background/store/selectors/subscriptions.selectors'; import minutesToMilliseconds from 'libs/utils/minutesToMilliseconds'; diff --git a/src/app/content/store/sagas/ui/fakeLoading.saga.ts b/src/app/content/store/sagas/ui/fakeLoading.saga.ts index 947973c04..912f909dd 100644 --- a/src/app/content/store/sagas/ui/fakeLoading.saga.ts +++ b/src/app/content/store/sagas/ui/fakeLoading.saga.ts @@ -1,5 +1,6 @@ -import { delay, put } from '@redux-saga/core/effects'; +import { put } from '@redux-saga/core/effects'; import { OpenedAction, OpenFrom } from 'libs/store/actions'; +import delay from 'libs/store/sagas/effects/delay'; import { loaded } from 'app/content/store/actions/ui/open.actions'; const DELAY_BEFORE_SHOWING = process.env.NODE_ENV === 'production' ? 1100 : 110; diff --git a/src/app/content/store/sagas/ui/index.tsx b/src/app/content/store/sagas/ui/index.tsx index ad8f9e865..6efe3600b 100644 --- a/src/app/content/store/sagas/ui/index.tsx +++ b/src/app/content/store/sagas/ui/index.tsx @@ -5,8 +5,7 @@ import { put, select, takeEvery, - takeLatest, - delay + takeLatest } from 'redux-saga/effects'; import { render } from 'react-dom'; import { go, replace } from 'connected-react-router'; @@ -30,6 +29,7 @@ import { ToggleUIAction, CloseAction } from 'libs/store/actions'; +import delay from 'libs/store/sagas/effects/delay'; import { CloseCause } from 'libs/domain/ui'; import { append, create, hide, show } from 'app/content/extensionIframe'; import { getTheme } from 'libs/facets/getTheme'; diff --git a/src/app/profiles/store/sagas/portConnection.saga.ts b/src/app/profiles/store/sagas/portConnection.saga.ts index b8d13aa3b..e8d3776c4 100644 --- a/src/app/profiles/store/sagas/portConnection.saga.ts +++ b/src/app/profiles/store/sagas/portConnection.saga.ts @@ -1,8 +1,9 @@ -import { call, delay, fork, put, select, takeLatest } from 'redux-saga/effects'; +import { call, fork, put, select, takeLatest } from 'redux-saga/effects'; import { AppActionWithMeta } from 'libs/store/actions'; import { connect, CONNECT, disconnected } from 'libs/store/actions/connection'; import watchPortSaga from 'libs/store/sagas/watchPort.saga'; import Logger from 'libs/utils/Logger'; +import delay from 'libs/store/sagas/effects/delay'; import extensionId from 'app/profiles/extensionId'; import { isConnected, isConnecting } from '../selectors/connection'; diff --git a/src/app/profiles/store/sagas/windowConnection.saga.ts b/src/app/profiles/store/sagas/windowConnection.saga.ts index 80bfa04f8..18ef360d8 100644 --- a/src/app/profiles/store/sagas/windowConnection.saga.ts +++ b/src/app/profiles/store/sagas/windowConnection.saga.ts @@ -2,7 +2,6 @@ import { channel } from 'redux-saga'; import { actionChannel, call, - delay, fork, put, select, @@ -12,6 +11,7 @@ import { AppAction, AppActionWithMeta } from 'libs/store/actions'; import { connect } from 'libs/store/actions/connection'; import stripReceiverMeta from 'libs/store/stripReceiverMeta'; import watchWindow from 'libs/store/sagas/window/watch.saga'; +import delay from 'libs/store/sagas/effects/delay'; import extensionId, { extensionMessageSender } from 'app/profiles/extensionId'; import { isConnected } from 'app/profiles/store/selectors/connection'; diff --git a/src/libs/store/sagas/effects/callAndRetry.ts b/src/libs/store/sagas/effects/callAndRetry.ts index 7cfe67d7d..d1e8e35f2 100644 --- a/src/libs/store/sagas/effects/callAndRetry.ts +++ b/src/libs/store/sagas/effects/callAndRetry.ts @@ -1,8 +1,9 @@ import { SagaIterator } from 'redux-saga'; -import { call, delay } from 'redux-saga/effects'; +import { call } from 'redux-saga/effects'; import * as R from 'ramda'; import secondsToMilliseconds from 'libs/utils/secondsToMilliseconds'; import minutesToMilliseconds from 'libs/utils/minutesToMilliseconds'; +import delay from './delay'; // eslint-disable-next-line @typescript-eslint/no-explicit-any type AnyFunction = (...args: any[]) => any; diff --git a/src/libs/store/sagas/effects/delay.ts b/src/libs/store/sagas/effects/delay.ts new file mode 100644 index 000000000..3e6eaf82a --- /dev/null +++ b/src/libs/store/sagas/effects/delay.ts @@ -0,0 +1,48 @@ +import { CANCEL } from '@redux-saga/symbols'; +import { call } from 'redux-saga/effects'; +import Logger from 'libs/utils/Logger'; +import uniqId from "../../../utils/uniqId"; + +const createAlarmResolver = (alarmName: string) => ( + resolve: (value?: T | PromiseLike) => void, + val?: T +) => (alarm: browser.alarms.Alarm) => { + Logger.debug(`Alarm ${alarmName} has been triggered.`); + + if (alarm.name === alarmName) { + resolve(val); + Logger.debug(`Alarm ${alarmName} has been resolved.`); + } +}; + +function delayP(ms = 1000, val: T): Promise { + if (ms < 1000) { + // see https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/alarms/create#parameters + throw new Error("Period can't be inferior to 1 minute."); + } + + const alarmName = `delayP_${uniqId()}`; + const periodInMinutes = ms / 1000 / 60; + const promise = new Promise(resolve => { + Logger.debug(`Creating an alarm named ${alarmName} with a period of ${periodInMinutes} minute(s).`) + browser.alarms.create(alarmName, { + periodInMinutes + }); + + browser.alarms.onAlarm.addListener( + createAlarmResolver(alarmName)(resolve, val) + ); + }); + + // see https://github.com/redux-saga/redux-saga/blob/v1.1.3/packages/delay-p/src/index.js + // @ts-ignore + promise[CANCEL] = () => { + browser.alarms.clear(alarmName); + }; + + return promise; +} + +export const delay = call.bind(null, delayP); + +export default delay;