self.importScripts("https://fanruan.design/fineui/fineui_without_jquery_polyfill.js"); var CookieAction = BI.inherit(BI.Workers.WorkerBaseAction, { addActionHandler: function() {}, getCookie: function() { return this.controller.requestPromise("Cookie"); } }); function fibonacci(n) { if (n === 1 || n === 2) { return 1; } return fibonacci(n - 2) + fibonacci(n - 1); } var FibonacciAction = BI.inherit(BI.Workers.WorkerBaseAction, { addActionHandler: function() { this.controller.addActionHandler("Fibonacci", this.getResult.bind(this)); }, getResult: function(payload) { return fibonacci(payload.times); } }); const HeartBeatCheckAction = BI.inherit(BI.Workers.WorkerBaseAction, { addActionHandler: function() { this.controller.addActionHandler("HeartBeatCheckStart", this.startHeatBeatCheck.bind(this)); this.controller.addActionHandler("HeartBeatCheckStop", this.stopHeatBeatCheck.bind(this)); }, startHeatBeatCheck: function() { var self = this; if (!this.timer) { console.log("heart beat check started"); this.timer = setInterval(function() { // 模拟请求 setTimeout(function() { self.controller.request("HeartBeatChecked", { time: new Date() }); }, 50); }, 5 * 1000); } else { console.log("heart beat has already started!"); } }, stopHeatBeatCheck: function() { console.log("heart beat check stopped"); clearInterval(this.timer); } }); var MainThreadWorker = BI.inherit(BI.Workers.WorkerThreadWorker, { initActions: function() { this.cookieAction = this.createAction(CookieAction); this.fibonacciAction = this.createAction(FibonacciAction); this.heartBeatCheckAction = this.createAction(HeartBeatCheckAction); }, fetchCookie: function() { return this.cookieAction.getCookie() .then(function (v) { console.log(v); }); } }); var workerThreadWorker = BI.Workers.createWorker(MainThreadWorker); workerThreadWorker.fetchCookie();