fineui是帆软报表和BI产品线所使用的前端框架。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.3 KiB

4 years ago
;(function () {
var contexts = {};
var WORKER;
4 years ago
BI.useWorker = function (wk) {
WORKER = wk;
4 years ago
var _init = BI.Widget.prototype._init;
BI.Widget.prototype._init = function () {
4 years ago
this.$destroyWorker = createWorker.call(this);
4 years ago
try {
_init.apply(this, arguments);
} catch (e) {
console.error(e);
}
};
var _initRender = BI.Widget.prototype._initRender;
4 years ago
var postMessage = function (data) {
4 years ago
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 (WORKER && this._worker) {
4 years ago
this.__asking = true;
this.__async = true;
} else {
_initRender.apply(this, arguments);
}
};
var unMount = BI.Widget.prototype.__d;
BI.Widget.prototype.__d = function () {
4 years ago
this.$destroyWorker && this.$destroyWorker();
4 years ago
try {
unMount.apply(this, arguments);
} catch (e) {
console.error(e);
}
};
if (WORKER) {
WORKER.addEventListener("message", function (e) {
var data = e.data;
postMessage.apply(contexts[data.name], [data]);
}, false);
}
4 years ago
};
function createWorker () {
var self = this;
if (this._worker) {
4 years ago
var name = this.getName();
var modelType = this._worker();
4 years ago
var options;
if (BI.isArray(modelType)) {
options = modelType[1];
modelType = modelType[0];
}
if (WORKER) {
contexts[name] = this;
WORKER.postMessage({
4 years ago
type: modelType,
name: name,
eventType: "create",
4 years ago
options: options,
watches: BI.map(this.watch, function (key) {
return key;
})
4 years ago
});
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);
}
});
return function () {
4 years ago
delete contexts[name];
WORKER.postMessage({
type: modelType,
name: name,
eventType: "destroy"
});
};
} else {
4 years ago
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;
4 years ago
}
options = options || {};
return Fix.watch(vm.model, keyOrFn, _.bind(cb, vm), BI.extend(options, {
store: vm.store
}));
4 years ago
}
}());