forked from mozilla/price-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.js
More file actions
76 lines (66 loc) · 2.04 KB
/
Copy pathsync.js
File metadata and controls
76 lines (66 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Redux duck that handles syncing the state tree to extension storage so that
* it persists between JS contexts and restarts.
* @module
*/
// Actions
export const LOAD_FROM_STORAGE = 'commerce/sync/LOAD_FROM_STORAGE';
// Reducer
export default function reducer(state, action = {}) {
switch (action.type) {
case LOAD_FROM_STORAGE:
return {
...action.state,
needsSync: false,
};
default:
return {
...state,
needsSync: true,
};
}
}
// Action Creators
/**
* Load persisted state from extension storage, and replace the entire state
* with it.
*/
export function loadStateFromStorage() {
return async (dispatch) => {
const results = await browser.storage.local.get({commerce: null});
const serializedData = results.commerce;
if (serializedData) {
dispatch({
type: LOAD_FROM_STORAGE,
state: JSON.parse(serializedData),
});
}
};
}
// Helpers
/**
* Save the given state to extension storage, and notify the rest of the add-on
* that the newly saved state needs to be loaded.
* @param {ReduxState} state
*/
export async function saveStateToStorage(state) {
const serializedState = JSON.stringify(state);
await browser.storage.local.set({commerce: serializedState});
// needsSync is always true, unless the last action to be processed was
// LOAD_FROM_STORAGE. This avoids a loop between two contexts that keep
// sending update messages in response to loading new state from eachother.
if (state.needsSync) {
try {
await browser.runtime.sendMessage({type: 'store-update'});
} catch (err) {
// Ignore the case where there are no other listeners, such as when the
// background script is the only active context.
if (!err.message.includes('Receiving end does not exist.')) {
throw err;
}
}
}
}