const wrt = require('../browser/wrt');
const vm = require('vm');
+var sandbox = [];
-wrt.on('start-service', (event, appID) => {
- console.log('start service app : ' + appID);
- let sandbox = { console: console };
- let options = { filename: appID };
- vm.runInNewContext(wrt.readService(appID), sandbox, options);
+wrt.on('start-service', (event, app_id) => {
+ console.log('start service app : ' + app_id);
+ if (sandbox[app_id] === undefined) {
+ sandbox[app_id] = {
+ console: console,
+ module: module,
+ require: require,
+ };
+ let options = { filename: app_id };
+ vm.runInNewContext(wrt.readService(app_id), sandbox[app_id], options);
+ }
+ if (sandbox[app_id]['started'] === undefined) {
+ sandbox[app_id]['started'] = true;
+ const start_callback_string = 'if (module.exports.onStart !== undefined) { module.exports.onStart(); }';
+ vm.runInContext(start_callback_string, sandbox[app_id]);
+ }
+ const request_callback_string = 'if (module.exports.onRequest !== undefined) { module.exports.onRequest(); }';
+ vm.runInContext(request_callback_string, sandbox[app_id]);
if (!wrt.terminateClient(app_id)) {
console.log('Failed to terminate client process');
}