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.
48 lines
1.9 KiB
48 lines
1.9 KiB
4 years ago
|
!(function () {
|
||
|
BI.initWorker = function () {
|
||
|
function createWatcher (model, keyOrFn, cb, options) {
|
||
|
options = options || {};
|
||
|
return Fix.watch(model, keyOrFn, cb, BI.extend(options, {
|
||
|
store: model
|
||
|
}));
|
||
|
}
|
||
|
|
||
4 years ago
|
var models = {}, watches = {};
|
||
4 years ago
|
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;
|
||
4 years ago
|
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":
|
||
4 years ago
|
var store = models[data.name] = BI.Models.getModel(data.type, data.options);
|
||
4 years ago
|
watches[data.name] = [];
|
||
|
BI.each(data.watches, function (i, key) {
|
||
|
watches[data.name].push(createWatcher(store.model, key, function (newValue, oldValue) {
|
||
4 years ago
|
postMessage(BI.extend({}, data, {
|
||
|
eventType: "watch",
|
||
|
currentWatchType: key
|
||
4 years ago
|
}, {args: [newValue, oldValue]}));
|
||
|
}));
|
||
|
});
|
||
|
postMessage(BI.extend({}, data, {
|
||
4 years ago
|
eventType: "create"
|
||
4 years ago
|
}, {msg: store.model}));
|
||
|
break;
|
||
|
default:
|
||
4 years ago
|
break;
|
||
|
}
|
||
|
}, false);
|
||
|
};
|
||
|
}());
|