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.

124 lines
3.8 KiB

4 years ago
;(function () {
var contexts = {};
3 years ago
var init = false;
4 years ago
var WORKER;
4 years ago
3 years ago
var enableWorker = function () {
if (init) {
return init;
}
3 years ago
// 开启Worker模式
BI.config("bi.provider.system", function (provider) {
provider.setWorkerMode(true);
});
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 () {
3 years ago
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);
}
};
3 years ago
init = postMessage;
return postMessage;
};
3 years ago
3 years ago
BI.useWorker = function (wk) {
3 years ago
if (!_global.Worker || !_global.Proxy) {
return;
}
3 years ago
var postMessage = enableWorker();
WORKER = wk;
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,
3 years ago
watches: BI.map(this.$watch || this.watch, function (key) {
return key;
})
4 years ago
});
var store = {};
this.store = new Proxy(store, {
4 years ago
get: function (target, key) {
return function () {
WORKER.postMessage({
type: modelType,
name: name,
eventType: "action",
action: key,
args: [].slice.call(arguments)
});
};
},
4 years ago
set: function (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"
});
};
}
}
}
4 years ago
}());