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.
 
 
 

54 lines
1.8 KiB

import { isPlainObject, extend, each, isArray } from "./2.base";
import { Models } from "./5.inject";
export function useInWorker () {
function createWatcher (model, keyOrFn, cb, options) {
if (isPlainObject(cb)) {
options = cb;
cb = cb.handler;
}
options = options || {};
return Fix.watch(model, keyOrFn, cb, extend(options, {
store: model,
}));
}
const models = {}, watches = {};
addEventListener("message", e => {
const data = e.data;
let store;
switch (data.eventType) {
case "action":
models[data.name][data.action](...data.args);
break;
case "destroy":
each(watches[data.name], (i, unwatches) => {
unwatches = isArray(unwatches) ? unwatches : [unwatches];
each(unwatches, (j, unwatch) => {
unwatch();
});
});
delete models[data.name];
delete watches[data.name];
break;
case "create":
store = models[data.name] = Models.getModel(data.type, data.options);
watches[data.name] = [];
each(data.watches, (i, key) => {
watches[data.name].push(createWatcher(store.model, key, (newValue, oldValue) => {
postMessage(extend({}, data, {
eventType: "watch",
currentWatchType: key,
}, { args: [newValue, oldValue] }));
}));
});
postMessage(extend({}, data, {
eventType: "create",
}, { msg: store.model }));
break;
default:
break;
}
}, false);
}