From c1b4ebf3bf3b3dd370c624eda91348f7819f6aa5 Mon Sep 17 00:00:00 2001 From: guy Date: Wed, 11 Nov 2020 02:03:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E4=B8=80=E4=B8=AAworker=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/worker.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/core/worker.js diff --git a/src/core/worker.js b/src/core/worker.js new file mode 100644 index 000000000..1e431b005 --- /dev/null +++ b/src/core/worker.js @@ -0,0 +1,35 @@ +!(function () { + BI.initWorker = function () { + function createWatcher (model, keyOrFn, cb, options) { + options = options || {}; + return Fix.watch(model, keyOrFn, cb, BI.extend(options, { + store: model + })); + } + + var models = {}; + addEventListener("message", function (e) { + var data = e.data; + switch (data.eventType) { + case "action": + models[data.name][data.action].apply(models[data.name], data.args); + break; + default: + var store = models[data.name] = BI.Models.getModel(data.type, data.options); + for (var i = 0, len = data.watches.length; i < len; i++) { + var key = data.watches[i]; + createWatcher(store.model, key, function () { + postMessage(BI.extend({}, data, { + eventType: "watch", + currentWatchType: key + }, {args: [].slice.call(arguments, 0, 2)})); + }); + } + postMessage(BI.extend({ + eventType: "create" + }, data, {msg: store.model})); + break; + } + }, false); + }; +}());