diff --git a/dist/fix/worker.compact.js b/dist/fix/worker.compact.js index 43264e284..00c5c0ad4 100644 --- a/dist/fix/worker.compact.js +++ b/dist/fix/worker.compact.js @@ -7,7 +7,7 @@ var _init = BI.Widget.prototype._init; BI.Widget.prototype._init = function () { - createWorker.call(this); + this.$destroyWorker = createWorker.call(this); try { _init.apply(this, arguments); } catch (e) { @@ -40,6 +40,7 @@ var unMount = BI.Widget.prototype.__d; BI.Widget.prototype.__d = function () { delete contexts[this.getName()]; + this.$destroyWorker && this.$destroyWorker(); try { unMount.apply(this, arguments); } catch (e) { @@ -84,6 +85,13 @@ return Reflect.set(target, key, value); } }); + return function () { + worker.postMessage({ + type: modelType, + name: name, + eventType: "destroy" + }); + }; } } }()); diff --git a/src/core/worker.js b/src/core/worker.js index 1e431b005..e23d73c5d 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -7,27 +7,39 @@ })); } - var models = {}; + var models = {}, watches = {}; 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: + case "destroy": + BI.each(watches[data.name], function (i, unwatches) { + unwatches = BI.isArray(unwatches) ? unwatches : [unwatches]; + BI.each(unwatches, function (j, unwatch) { + unwatch(); + }); + }); + delete models[data.name]; + delete watches[data.name]; + break; + case "create": 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 () { + watches[data.name] = []; + BI.each(data.watches, function (i, key) { + watches[data.name].push(createWatcher(store.model, key, function (newValue, oldValue) { postMessage(BI.extend({}, data, { eventType: "watch", currentWatchType: key - }, {args: [].slice.call(arguments, 0, 2)})); - }); - } - postMessage(BI.extend({ + }, {args: [newValue, oldValue]})); + })); + }); + postMessage(BI.extend({}, data, { eventType: "create" - }, data, {msg: store.model})); + }, {msg: store.model})); + break; + default: break; } }, false);