Skip to content

Commit

Permalink
wip(background): switch to alarm
Browse files Browse the repository at this point in the history
  • Loading branch information
lutangar committed Jun 14, 2024
1 parent ef00b13 commit 0c0dd93
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 72 deletions.
110 changes: 47 additions & 63 deletions manifest/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/*'],
});
3 changes: 2 additions & 1 deletion src/app/background/store/sagas/lib/sendToTab.saga.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
3 changes: 2 additions & 1 deletion src/app/background/store/sagas/refreshContributors.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/app/background/store/sagas/refreshMatchingContexts.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
3 changes: 2 additions & 1 deletion src/app/content/store/sagas/ui/fakeLoading.saga.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/app/content/store/sagas/ui/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down
3 changes: 2 additions & 1 deletion src/app/profiles/store/sagas/portConnection.saga.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
2 changes: 1 addition & 1 deletion src/app/profiles/store/sagas/windowConnection.saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { channel } from 'redux-saga';
import {
actionChannel,
call,
delay,
fork,
put,
select,
Expand All @@ -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';

Expand Down
3 changes: 2 additions & 1 deletion src/libs/store/sagas/effects/callAndRetry.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
48 changes: 48 additions & 0 deletions src/libs/store/sagas/effects/delay.ts
Original file line number Diff line number Diff line change
@@ -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) => <T>(
resolve: (value?: T | PromiseLike<T>) => 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<T = true>(ms = 1000, val: T): Promise<T> {
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<T>(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)<T>(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;

0 comments on commit 0c0dd93

Please sign in to comment.