Jean-Yves Didier

started to modify module system; is not runnable yet

......@@ -22,19 +22,11 @@ var ARCS = ARCS || {};
* Helper functions to determine environment
* ***************************************************************************/
/**
* @return {boolean} true if ARCS is run in a node.js environment
*/
ARCS.isInNode = function () {
return (typeof require === 'function' && require.resolve);
return (typeof require === 'function' && require.resolve);
};
/**
* @return {boolean} true if ARCS is run with require.js
*/
ARCS.isInRequire = function () {
return (typeof define === 'function' && define.amd);
};
......
......@@ -4,6 +4,10 @@
* @file
*/
// basically, here we start by importing the module ARCS
import ARCS from './arcs.js';
console.log("Bootstrapping ARCS...");
var baseUrl, appDescription, requireMarkup, xhr;
......@@ -14,34 +18,15 @@ if (requireMarkup !== undefined) {
appDescription = requireMarkup.dataset.arcsapp || "arcsapp.json";
}
// do not move these lines: xhr.open must be performed before require
// changes paths.
xhr = new XMLHttpRequest();
xhr.open('GET',appDescription,true);
xhr.overrideMimeType("application/json");
xhr.onerror = function (e) {
console.error("[ARCS] Failed to get app description (",appDescription,"):",e.target.status,e.message);
};
require(['arcs'], function (ARCS) {
//console.log(ARCS);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
try {
console.log("ARCS application description loaded");
var applicationObject = JSON.parse(xhr.responseText);
if (baseUrl) {
require.config( { baseUrl: baseUrl });
}
var aap = new ARCS.Application();
aap.import(applicationObject);
console.log("Starting application...");
aap.start();
} catch (e) {
console.error("[ARCS] Error while parsing JSON:",e);
}
}
};
xhr.send();
});
const appDescription = await fetch(appDescription);
const applicationObject = await appDescription.json();
console.log("ARCS application description loaded");
if (baseUrl) {
require.config( { baseUrl: baseUrl });
}
var aap = new ARCS.Application();
aap.import(applicationObject);
console.log("Starting application...");
aap.start();
......
......@@ -8,6 +8,13 @@
* @param deps {mixed[]} dependencies
*/
// TODO arcs_module seems to be not needed anymore!!!!
// more cunning, this time, we export an arcs_module function
// that we call later!
// still we need to do something about it in order to register
// components.
// in fact factories should be registered in load library.
// reimplementation using native promises
arcs_module = function(moduleDefinition, deps) {
var storeComponents, i;
......
......@@ -39,7 +39,7 @@ ARCS.Context = function( ctx ) {
}
//! TODO use fetch API?
loadDataFile = function(fileName) {
var dataPromise ;
......@@ -75,36 +75,17 @@ ARCS.Context = function( ctx ) {
}
};
//! TODO not needed anymore?
this.addLibraryPromise = function(p) {
depLibPromises.push(p);
};
promiseLibrary = function(libName) {
return new Promise(function(resolve, reject) {
if (libName.substr(-3) === '.js') {
reject(libName);
}
if (ARCS.isInNode()) {
if (require("./" + libraries[i] + ".js") === undefined) {
reject(libName);
} else {
resolve();
}
} else {
require([libName],
function() {
resolve();
},
function(err) {
reject(libName,err);
}
);
}
});
return import(libName);
};
//! TODO modify loadLibraries and loadLibrary to directly register
// factories so that arcs_module is not needed anymore ?
loadLibraries = function () {
var i;
// we will use different instances of require either the one of node
......@@ -113,7 +94,8 @@ ARCS.Context = function( ctx ) {
var res=[];
for(i=0; i < libraries.length; i++) {
res.push(promiseLibrary(libraries[i]));
//! TODO
res.push(import(libraries[i]));
}
return Promise.all(res);
};
......@@ -301,6 +283,7 @@ ARCS.Context = function( ctx ) {
// this should return a promise !
this.instanciate = function () {
//! TODO
return loadLibraries().then(function() { return Promise.all(depLibPromises); })
.then(instanciateComponents)
.catch(function(msg) { console.log("[ARCS] Trouble instanciating context", msg); });
......
// no longer needed with the use of imports
// ARCS is then defined as a node.js module
if (!ARCS.isInNode()) {
var exports = {};
}
exports.Component = ARCS.Component;
exports.Connection = ARCS.Connection;
exports.Invocation = ARCS.Invocation;
exports.Statemachine = ARCS.Statemachine;
exports.Sheet = ARCS.Sheet;
exports.Application = ARCS.Application;
exports.EventLogicParser = ARCS.EventLogicParser;
exports.TransitionSystem = ARCS.TransitionSystem;
exports.isInNode = ARCS.isInNode;
exports.isInRequire = ARCS.isInRequire;
// hack for require.js
// ARCS is then defined as a require.js module.
if (ARCS.isInRequire()) {
//console.log("module ARCS in require.js");
define(exports);
}
export default ARCS;
\ No newline at end of file
......