-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
44 lines (38 loc) · 1.16 KB
/
background.js
File metadata and controls
44 lines (38 loc) · 1.16 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
import {self} from 'react-native-threads';
import 'node-libs-react-native/globals';
import 'crypto';
import './src/globals.js';
import './src/shim.js';
import {store} from './src/store/createBackgroundStore.js';
import actions from './src/store/actions.js';
import {persistStore} from 'redux-persist';
export const postMessage = data => {
self.postMessage(JSON.stringify({...data}));
};
self.onmessage = async _msg => {
const {id, type, data} = JSON.parse(_msg);
switch (type) {
case 'ACTION_REQUEST':
const {action, payload} = data;
console.log('BG ACTION', action);
console.log(payload);
try {
const result =
(await store.dispatch(actions[action]({...payload}))) || {};
postMessage({id, type: 'ACTION_RESPONSE', payload: {result}});
} catch (error) {
postMessage({
id,
type: 'ACTION_RESPONSE',
payload: {result: {error: error.message}},
});
}
break;
case 'REHYDRATE_STATE':
persistStore(store, {}, () => {
const state = store.getState();
postMessage({id, type: 'REHYDRATE_STATE', payload: state});
});
break;
}
};