Skip to content

Commit

Permalink
WIP (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBuchholz committed Oct 6, 2017
1 parent 876b325 commit 1610977
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions src/require.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,28 @@
*/
/*#ifndef(UMD)*/
"use strict";
/*global _gpfEmptyFunc*/
/*global _gpfIgnore*/
/*global _gpfArrayForEach*/
/*global _gpfPathJoin*/
/*global _gpfErrorDeclare*/ // Declare new gpf.Error names
/*#endif*/

_gpfErrorDeclare("stream", {

/**
* ### Summary
*
* Invalid {@link gpf.require.configure} option
*
* ### Description
*
* This error is triggered whenever an option passed to {@link gpf.require.configure} is not recognized.
* Please check the {@link gpf.typedef.requireOptions} documentation.
*/
invalidRequireConfigureOption:
"Invalid configuration option"
});

/**
* @typedef gpf.typedef.requireOptions
* @property {String} [base] Base path used to resolve names
Expand Down Expand Up @@ -66,14 +83,14 @@ function _gpfRequireConfigure (options) {
// Some keys must be processed first
return _gpfRequireOptionPriority(key1) - _gpfRequireOptionPriority(key2);
}), function (key) {
(_gpfRequireOptionHandler[key] || _gpfEmptyFunc).call(me, options[key]);
(_gpfRequireOptionHandler[key] || gpf.error.invalidRequireConfigureOption).call(me, options[key]);
}, me);
}

/**
* Resolves the resource name according to current require context
*
* @param {String} name
* @param {String} name Relative resource name
* @return {String} Resolved name
*/
function _gpfRequireResolve (name) {
Expand All @@ -82,6 +99,10 @@ function _gpfRequireResolve (name) {
return _gpfPathJoin(me.base, name);
}

function _gpfRequireLoad (name) {

}

/**
* Load all dependencies and pass them to the factory function as a single object
*
Expand All @@ -95,7 +116,31 @@ function _gpfRequireResolve (name) {
* @return {Promise<*>} Resolved with the factory result
*/
function _gpfRequire (dependencies, factory) {
return new Promise();
/*jshint validthis:true*/
var me = this, //eslint-disable-line no-invalid-this
promises = [],
keys = Object.keys(dependencies);
_gpfArrayForEach(keys, function (key) {
var name = _gpfRequireResolve.call(me, dependencies[key]);
promises.push(_gpfRequireLoad(name));
}, me);
return Promise.all(promises)
.then(function (resources) {
var result,
require;
if ("function" === typeof factory) {
_gpfArrayForEach(keys, function (key, index) {
require[key] = resources[index];
});
result = factory(require);
} else {
result = factory;
}
if (me.name) {
me.cache[me.name] = result;
}
return result;
});
}

function _gpfRequireAllocate (parentContext) {
Expand Down

0 comments on commit 1610977

Please sign in to comment.