-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathbrowserSync.js
79 lines (66 loc) · 2.24 KB
/
browserSync.js
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
77
78
import { setModalData, throwError } from 'store/modal/actions';
/*
* This service uses the browser.storage.sync API to
* store requested data in Chrome or Firefox sync.
*
* With this service you should always be able to
* assume that BrowserSync is enabled and supported,
* as all checks agains this will be done here.
*/
function handleErrorsAndCallCallback(dispatch, callback, ...args) {
if (chrome.runtime.lastError) {
dispatch(throwError(
'An error occured when reading/writing the browser sync storage\n',
chrome.runtime.lastError,
));
return;
}
if (args.length === 0) return;
callback.apply(this, args);
}
/**
* Assert sync is supported and not disabled
*/
function assertInvariants() {
// TODO! Connect with redux for options && $rootScope.options.sync === true;
return chrome.storage.sync;
}
//
// TODO Refactor these to use promises
// TODO Integreate neatly with redux as a higher order component instead
// Needs to be connected and dispatch, so this is a good use for a HoC
//
/** Pass null as name to get all data */
export function get(dispatch, name, callback) {
// Abort if sync is not supported or disabled
if (!assertInvariants()) return;
if (!name) {
chrome.storage.sync.get(handleErrorsAndCallCallback.bind(this, dispatch, callback));
} else {
chrome.storage.sync.get(name, handleErrorsAndCallCallback.bind(this, dispatch, callback));
}
}
export function sizeOf() {}
export function set(dispatch, name, data, callback) {
// Abort if sync is not supported or disabled
if (!assertInvariants()) return;
if (!name) return;
const keyValue = {};
keyValue[name] = data;
chrome.storage.sync.set(keyValue, handleErrorsAndCallCallback.bind(this, dispatch, callback));
}
export function clear(dispatch, callback) {
// Abort if sync is not supported or disabled
if (!assertInvariants()) return;
dispatch(setModalData({
title: 'Clear synced storage?',
body: 'Are you sure you would like to clear the browser\'s synced ' +
'data? This will clear data on all devices with sync enabled.',
actions: [{
text: 'Clear sync',
click: function action() {
chrome.storage.sync.clear(handleErrorsAndCallCallback.bind(this, dispatch, callback));
},
}],
}));
}