用户登录并存储在索引数据库中后,尝试调用api来获取一些数据。但是在登录之前,即一旦应用程序启动,工作代码便会被调用,从而完成工作。
app-worker.js
import Api from "./api/index";
import { openDb, addToStore, closeDb } from "./indexedDB";
self.onmessage = (oEvent) => {
Api().get("person").then((results) => {
openDb();
addToStore({
storeName: "persons",
data: results,
});
closeDb();
});
};
workerfile.js
import Worker from "./app-worker.js";
let workerObj = null;
export function startWorker() {
workerObj = new Worker();
workerObj.postMessage();
}
export function stopWorker() {
workerObj.terminate();
}
actionfile.js
import { startWorker } from "../workerfile";
export const login = (user, history) => async (dispatch) => {
dispatch({ type: LOGIN_USER });
Api().post("login", user)
.then((result) => {
dispatch({
type: LOGIN_USER_SUCCESS,
payload: result.user
});
history.push("/home");
startWorker();
})
.catch((err) => {
dispatch({
type: LOGIN_USER_FAILURE,
});
});
};
要求是用户登录->让工作程序运行,直到从api获取数据并将其存储在数据库中为止->停止工作程序