Skip to content

Commit

Permalink
feat(subscriptions): update subscriptions on browser startup
Browse files Browse the repository at this point in the history
  • Loading branch information
lutangar committed Feb 6, 2020
1 parent ebce2c1 commit f68f705
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
27 changes: 26 additions & 1 deletion src/app/actions/webext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ActionMeta, AppAction, BaseAction, ErrorAction } from './index';
import {
ActionMeta,
AppAction,
BaseAction,
ErrorAction,
StandardAction
} from './index';
import { From } from 'webext/From';

export const LISTENING_ACTIONS_READY = 'LISTENING_ACTIONS_READY';
Expand Down Expand Up @@ -29,3 +35,22 @@ export const listenActionFailed = (e: Error): ListenActionFailedAction => ({
payload: e,
error: true
});

export interface LifecycleAction extends StandardAction {
meta: {
at: Date;
};
}

export const STARTUP = 'STARTUP';
export interface StartupAction extends LifecycleAction {
type: typeof STARTUP;
meta: {
at: Date;
};
}

export const startup = (): StartupAction => ({
type: STARTUP,
meta: { at: new Date() }
});
6 changes: 5 additions & 1 deletion src/app/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import prepareDraftPreview from '../lmem/draft-preview/main';
import { BACKEND_ORIGIN } from 'app/constants/origins';
import onInstalled from 'webext/onInstalled';
import onStartup from 'webext/onStartup';
import {
updateDraftNotices,
installed,
updateRestrictedContexts
updateRestrictedContexts,
startup
} from 'app/actions';
import { configureSentryScope, initSentry } from 'app/utils/sentry';
import { store } from './store';
Expand Down Expand Up @@ -43,3 +45,5 @@ onInstalled.then(installedDetails => {

store.dispatch(installed(installedDetails));
});

onStartup.then(() => store.dispatch(startup()));
13 changes: 9 additions & 4 deletions src/app/background/sagas/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { SagaIterator } from 'redux-saga';
import { call, select, put, takeLatest } from 'redux-saga/effects';
import { createErrorAction, SUBSCRIBE, UNSUBSCRIBE } from 'app/actions';
import { getUserId } from 'app/background/selectors/user';
import {
createErrorAction,
STARTUP,
SUBSCRIBE,
UNSUBSCRIBE
} from 'app/actions';
import { getSubscriptions } from 'app/background/selectors/subscriptions.selectors';
import postSubscriptions from 'api/postSubscriptions';
import { loginSaga } from './user.saga';

function* postSubscriptionsSaga(): SagaIterator {
try {
const extensionId = yield select(getUserId);
const extensionId = yield call(loginSaga);
const subscriptions = yield select(getSubscriptions);

yield call(postSubscriptions, {
Expand All @@ -20,5 +25,5 @@ function* postSubscriptionsSaga(): SagaIterator {
}

export default function*() {
yield takeLatest([SUBSCRIBE, UNSUBSCRIBE], postSubscriptionsSaga);
yield takeLatest([SUBSCRIBE, UNSUBSCRIBE, STARTUP], postSubscriptionsSaga);
}
3 changes: 3 additions & 0 deletions src/webext/onStartup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default new Promise<{}>(resolve =>
chrome.runtime.onStartup.addListener(() => resolve({}))
);

0 comments on commit f68f705

Please sign in to comment.