guy 3 years ago
parent
commit
2ba0e964fb
  1. 66
      dist/fix/worker.compact.js
  2. 4
      examples/demo.worker.js
  3. 2
      src/core/worker.js

66
dist/fix/worker.compact.js vendored

@ -1,10 +1,13 @@
;(function () { ;(function () {
var contexts = {}; var contexts = {};
var init = false;
var WORKER; var WORKER;
BI.useWorker = function (wk) {
WORKER = wk;
var enableWorker = function () {
if (init) {
return init;
}
var _init = BI.Widget.prototype._init; var _init = BI.Widget.prototype._init;
BI.Widget.prototype._init = function () { BI.Widget.prototype._init = function () {
this.$destroyWorker = createWorker.call(this); this.$destroyWorker = createWorker.call(this);
@ -29,7 +32,7 @@
} }
}; };
BI.Widget.prototype._initRender = function () { BI.Widget.prototype._initRender = function () {
if (WORKER && this._worker) { if (this._worker) {
this.__asking = true; this.__asking = true;
this.__async = true; this.__async = true;
} else { } else {
@ -46,7 +49,12 @@
console.error(e); console.error(e);
} }
}; };
init = postMessage;
return postMessage;
};
BI.useWorker = function (wk) {
var postMessage = enableWorker();
WORKER = wk;
if (WORKER) { if (WORKER) {
WORKER.addEventListener("message", function (e) { WORKER.addEventListener("message", function (e) {
var data = e.data; var data = e.data;
@ -101,57 +109,7 @@
eventType: "destroy" eventType: "destroy"
}); });
}; };
} else {
this.store = BI.Models.getModel(modelType, options);
this.store && (this.store._widget = this);
if (this.store instanceof Fix.Model) {
this.model = this.store.model;
} else {
this.model = this.store;
}
initWatch(this, this.watch);
return function () {
this.store && BI.isFunction(this.store.destroy) && this.store.destroy();
BI.each(this._watchers, function (i, unwatches) {
unwatches = BI.isArray(unwatches) ? unwatches : [unwatches];
BI.each(unwatches, function (j, unwatch) {
unwatch();
});
});
this._watchers && (this._watchers = []);
if (this.store) {
this.store._parent && (this.store._parent = null);
this.store._widget && (this.store._widget = null);
this.store = null;
}
};
} }
}
}
function initWatch (vm, watch) {
vm._watchers || (vm._watchers = []);
for (var key in watch) {
var handler = watch[key];
if (BI.isArray(handler)) {
for (var i = 0; i < handler.length; i++) {
vm._watchers.push(createWatcher(vm, key, handler[i]));
}
} else {
vm._watchers.push(createWatcher(vm, key, handler));
}
}
}
function createWatcher (vm, keyOrFn, cb, options) {
if (BI.isPlainObject(cb)) {
options = cb;
cb = cb.handler;
} }
options = options || {};
return Fix.watch(vm.model, keyOrFn, _.bind(cb, vm), BI.extend(options, {
store: vm.store
}));
} }
}()); }());

4
examples/demo.worker.js

@ -1,6 +1,6 @@
if (this.importScripts) { if (this.importScripts) {
importScripts("../dist/fineui_without_jquery_polyfill.js"); importScripts("https://fanruan.design/fineui/fineui_without_jquery_polyfill.js");
BI.initWorker(); BI.useInWorker();
} }
var Model = BI.inherit(Fix.Model, { var Model = BI.inherit(Fix.Model, {
state: function () { state: function () {

2
src/core/worker.js

@ -1,5 +1,5 @@
!(function () { !(function () {
BI.initWorker = function () { BI.useInWorker = function () {
function createWatcher (model, keyOrFn, cb, options) { function createWatcher (model, keyOrFn, cb, options) {
options = options || {}; options = options || {};
return Fix.watch(model, keyOrFn, cb, BI.extend(options, { return Fix.watch(model, keyOrFn, cb, BI.extend(options, {

Loading…
Cancel
Save