From e1823e42294ccf318989dbe7b040917038b48906 Mon Sep 17 00:00:00 2001 From: guy Date: Wed, 11 Nov 2020 10:27:46 +0800 Subject: [PATCH] webworker --- demo.worker.js | 27 ++++++++++++ dist/fix/worker.compact.js | 89 ++++++++++++++++++++++++++++++++++++++ worker.html | 56 ++++++++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 demo.worker.js create mode 100644 dist/fix/worker.compact.js create mode 100644 worker.html diff --git a/demo.worker.js b/demo.worker.js new file mode 100644 index 000000000..5f2491f3e --- /dev/null +++ b/demo.worker.js @@ -0,0 +1,27 @@ +importScripts("./dist/fineui_without_jquery_polyfill.js"); +BI.initWorker(); +var Model = BI.inherit(Fix.Model, { + state: function () { + return { + count: 0 + }; + }, + + computed: { + name: function () { + return this.getText(this.model.count); + } + }, + + actions: { + addCount: function () { + this.model.count += 1; + } + }, + + getText (count) { + return "被点击了" + count + "次"; + } +}); + +BI.model("demo.model", Model); diff --git a/dist/fix/worker.compact.js b/dist/fix/worker.compact.js new file mode 100644 index 000000000..ad72d227e --- /dev/null +++ b/dist/fix/worker.compact.js @@ -0,0 +1,89 @@ +;(function () { + var contexts = {}; + + var worker; + BI.useWorker = function (wk) { + worker = wk; + + var _init = BI.Widget.prototype._init; + BI.Widget.prototype._init = function () { + createWorker.call(this); + try { + _init.apply(this, arguments); + } catch (e) { + console.error(e); + } + }; + + var _initRender = BI.Widget.prototype._initRender; + BI.Widget.prototype.postMessage = function (data) { + switch (data.eventType) { + case "create": + this.model = data.msg; + _initRender.call(this); + break; + case "watch": + var watchArgs = data.args; + this.watch[data.currentWatchType].apply(this, watchArgs); + break; + } + }; + BI.Widget.prototype._initRender = function () { + if (_global.Worker && this._worker) { + this.__asking = true; + this.__async = true; + } else { + _initRender.apply(this, arguments); + } + }; + + var unMount = BI.Widget.prototype.__d; + BI.Widget.prototype.__d = function () { + delete contexts[this.getName()]; + try { + unMount.apply(this, arguments); + } catch (e) { + console.error(e); + } + }; + + worker.addEventListener("message", function (e) { + var data = e.data; + contexts[data.name].postMessage(data); + }, false); + }; + + function createWorker () { + var self = this; + if (_global.Worker && this._worker) { + var name = this.getName(); + contexts[name] = this; + var modelType = this._worker(); + worker.postMessage({ + type: modelType, + name: name, + eventType: "create", + watches: BI.map(this.watch, function (key) { + return key; + }) + }); + var store = {}; + this.store = new Proxy(store, { + get (target, key) { + return function () { + worker.postMessage({ + type: modelType, + name: name, + eventType: "action", + action: key, + args: [].slice.call(arguments) + }); + }; + }, + set (target, key, value) { + return Reflect.set(target, key, value); + } + }); + } + } +}()); diff --git a/worker.html b/worker.html new file mode 100644 index 000000000..11f95b90c --- /dev/null +++ b/worker.html @@ -0,0 +1,56 @@ + + + + + + + + + + + +
+ + +