2015-02-26 16:16:20 +01:00
|
|
|
var H5P = H5P || {};
|
2015-03-04 11:13:32 +01:00
|
|
|
importScripts('h5p-version.js', 'h5p-content-upgrade-process.js');
|
2015-02-26 16:16:20 +01:00
|
|
|
|
|
|
|
var libraryLoadedCallback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register message handlers
|
|
|
|
*/
|
|
|
|
var messageHandlers = {
|
2015-08-26 15:58:49 +02:00
|
|
|
newJob: function (job) {
|
2015-02-26 16:16:20 +01:00
|
|
|
// Start new job
|
2018-08-30 12:33:24 +02:00
|
|
|
new H5P.ContentUpgradeProcess(job.name, new H5P.Version(job.oldVersion), new H5P.Version(job.newVersion), job.params, job.id, function loadLibrary(name, version, next) {
|
2015-02-26 16:16:20 +01:00
|
|
|
// TODO: Cache?
|
|
|
|
postMessage({
|
|
|
|
action: 'loadLibrary',
|
|
|
|
name: name,
|
|
|
|
version: version.toString()
|
|
|
|
});
|
|
|
|
libraryLoadedCallback = next;
|
2018-08-30 12:33:24 +02:00
|
|
|
}, function done(err, result) {
|
2015-02-26 16:16:20 +01:00
|
|
|
if (err) {
|
|
|
|
// Return error
|
|
|
|
postMessage({
|
|
|
|
action: 'error',
|
|
|
|
id: job.id,
|
2015-03-04 11:30:20 +01:00
|
|
|
err: err.message ? err.message : err
|
2015-02-26 16:16:20 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return upgraded content
|
|
|
|
postMessage({
|
|
|
|
action: 'done',
|
|
|
|
id: job.id,
|
2018-08-30 12:33:24 +02:00
|
|
|
params: result
|
2015-02-26 16:16:20 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
libraryLoaded: function (data) {
|
|
|
|
var library = data.library;
|
|
|
|
if (library.upgradesScript) {
|
|
|
|
try {
|
|
|
|
importScripts(library.upgradesScript);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
libraryLoadedCallback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
libraryLoadedCallback(null, data.library);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle messages from our master
|
|
|
|
*/
|
|
|
|
onmessage = function (event) {
|
|
|
|
if (event.data.action !== undefined && messageHandlers[event.data.action]) {
|
|
|
|
messageHandlers[event.data.action].call(this, event.data);
|
|
|
|
}
|
|
|
|
};
|