32 lines
980 B
TypeScript
32 lines
980 B
TypeScript
import { logger } from "../common/logger";
|
|
import { Actions } from "../common";
|
|
import { messageSubscribers } from "./messaging";
|
|
|
|
export class Caches {
|
|
private _state: string = "";
|
|
constructor() {
|
|
messageSubscribers.addListener(Actions.UPLOAD_STATE, (request, sender, sendResponse) => {
|
|
sendResponse('recieved!');
|
|
this.setState(request.fileName, request.state)
|
|
});
|
|
}
|
|
get state(): string {
|
|
let s = this._state;
|
|
this._state = "";
|
|
return s;
|
|
}
|
|
setState(name: string, content: string) {
|
|
this._state = content;
|
|
logger.info(`State (${name}) recieved. To load it: some_var = new Extractor().load()`);
|
|
// clear cache in 30 seconds
|
|
setTimeout(() => {
|
|
if (this._state) {
|
|
logger.info(`Uploaded state is cleaned after 30 second.`);
|
|
this._state = "";
|
|
}
|
|
}, 30000);
|
|
}
|
|
}
|
|
|
|
export const caches = new Caches();
|