From 023763e88480e2d0d0ef3bde823fa771405dd961 Mon Sep 17 00:00:00 2001 From: guy Date: Sun, 12 Dec 2021 17:15:58 +0800 Subject: [PATCH] =?UTF-8?q?worker=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/fix/worker.compact.js | 10 +++++++++- src/core/4.widget.js | 4 +++- src/core/system.js | 9 +++++++++ src/core/worker.js | 4 ++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/dist/fix/worker.compact.js b/dist/fix/worker.compact.js index aaeb7016b..f178f37bf 100644 --- a/dist/fix/worker.compact.js +++ b/dist/fix/worker.compact.js @@ -8,6 +8,10 @@ if (init) { return init; } + // 开启Worker模式 + BI.config("bi.provider.system", function (provider) { + provider.setWorkerMode(true); + }); var _init = BI.Widget.prototype._init; BI.Widget.prototype._init = function () { this.$destroyWorker = createWorker.call(this); @@ -32,7 +36,7 @@ } }; BI.Widget.prototype._initRender = function () { - if (this._worker) { + if (WORKER && this._worker) { this.__asking = true; this.__async = true; } else { @@ -52,7 +56,11 @@ init = postMessage; return postMessage; }; + BI.useWorker = function (wk) { + if (!_global.Worker || !_global.Proxy) { + return; + } var postMessage = enableWorker(); WORKER = wk; if (WORKER) { diff --git a/src/core/4.widget.js b/src/core/4.widget.js index 5de5d7c85..5d420df20 100644 --- a/src/core/4.widget.js +++ b/src/core/4.widget.js @@ -326,7 +326,9 @@ _initElement: function () { var self = this; this.__isMounting = true; - var render = BI.isFunction(this.options.render) ? this.options.render : this.render; + // 当开启worker模式时,可以通过$render来实现另一种效果 + var workerMode = BI.Providers.getProvider("bi.provider.system").getWorkerMode(); + var render = BI.isFunction(this.options.render) ? this.options.render : (workerMode ? (this.$render || this.render) : this.render); var els = render && render.call(this); els = BI.Plugin.getRender(this.options.type, els); if (BI.isPlainObject(els)) { diff --git a/src/core/system.js b/src/core/system.js index 08a872860..59281243c 100644 --- a/src/core/system.js +++ b/src/core/system.js @@ -8,6 +8,7 @@ var system = { dependencies: {}, responsiveMode: false, + workerMode: false, size: { // 尺寸 TOOL_BAR_HEIGHT: 24, LIST_ITEM_HEIGHT: 24, @@ -28,6 +29,10 @@ system.responsiveMode = !!mode; }; + this.setWorkerMode = function (mode) { + system.workerMode = !!mode; + }; + this.addDependency = function (moduleId, minVersion, maxVersion) { system.dependencies[moduleId] = { min: minVersion, @@ -50,6 +55,10 @@ return system.responsiveMode; }, + getWorkerMode: function () { + return system.workerMode; + }, + getDependencies: function () { return system.dependencies; } diff --git a/src/core/worker.js b/src/core/worker.js index e616ed839..d3ba08ee4 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -1,6 +1,10 @@ !(function () { BI.useInWorker = function () { function createWatcher (model, keyOrFn, cb, options) { + if (BI.isPlainObject(cb)) { + options = cb; + cb = cb.handler; + } options = options || {}; return Fix.watch(model, keyOrFn, cb, BI.extend(options, { store: model