Skip to content

Commit

Permalink
make handler more flexible, extend builtin Pledge
Browse files Browse the repository at this point in the history
  • Loading branch information
dlueth committed Feb 26, 2020
1 parent 1702ec3 commit 050db20
Show file tree
Hide file tree
Showing 32 changed files with 792 additions and 743 deletions.
33 changes: 23 additions & 10 deletions addon/handler/css.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
(function(document) {
'use strict';

function definition(abstractHandler, functionResolveSourcemaps) {
var suffix = '.css',
target = document.getElementsByTagName('head')[0],
function definition(path, abstractHandler, functionResolveSourcemaps, isObject, merge) {
var target = document.getElementsByTagName('head')[0],
resolver = document.createElement('a'),
regexMatchUrl = /url\s*\(\s*["']?(.+?)["']?\s*\)/gi,
regexMatchImport = /@import\s+["'](.+?)["']/gi,
regexIsAbsolutePath = /^\//i,
regexIsAbsoluteUri = /^data:|http(s?):|\/\//i,
regexMatchType = /^text\/css/;
regexMatchType = /^text\/css/,
settings = { suffix: '.css' };

demand
.on('postConfigure:' + path, function(options) {
if(isObject(options)) {
merge(settings, options);
}
});

function resolveUrl(url) {
resolver.href = url;
Expand All @@ -31,10 +38,16 @@
validate: function(type) {
return regexMatchType.test(type);
},
onPreRequest: function(dependency) {
var pathname = dependency.url.pathname;

dependency.url.pathname = pathname.slice(-suffix.length) !== suffix ? pathname + suffix : pathname;
onPreRequest: function(dependency, suffix) {
var pathname;

suffix = (typeof suffix !== 'undefined') ? suffix : settings.suffix;

if(suffix) {
pathname = dependency.url.pathname;

dependency.url.pathname = pathname.slice(-suffix.length) !== suffix ? pathname + suffix : pathname;
}
},
onPostRequest: function(dependency) {
var url = resolveUrl(dependency.url + '/..'),
Expand Down Expand Up @@ -79,5 +92,5 @@
return new (HandlerCss.extends(abstractHandler));
}

provide([ '/demand/abstract/handler', '/demand/function/resolveSourcemaps' ], definition);
}(document));
provide([ 'path', '/demand/abstract/handler', '/demand/function/resolveSourcemaps', '/demand/validator/isObject', '/demand/function/merge' ], definition);
}(document));
33 changes: 23 additions & 10 deletions addon/handler/html.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
(function() {
'use strict';

function definition(abstractHandler) {
var suffix = '.html',
regexMatchType = /^text\/html/,
container = document.createElement('body');
function definition(path, abstractHandler, isObject, merge) {
var regexMatchType = /^text\/html/,
container = document.createElement('body'),
settings = { suffix: '.html' };

demand
.on('postConfigure:' + path, function(options) {
if(isObject(options)) {
merge(settings, options);
}
});

function parseHtml(source) {
var fragment = document.createDocumentFragment(),
Expand All @@ -25,10 +32,16 @@
validate: function(type) {
return regexMatchType.test(type);
},
onPreRequest: function(dependency) {
var pathname = dependency.url.pathname;

dependency.url.pathname = pathname.slice(-suffix.length) !== suffix ? pathname + suffix : pathname;
onPreRequest: function(dependency, suffix) {
var pathname;

suffix = (typeof suffix !== 'undefined') ? suffix : settings.suffix;

if(suffix) {
pathname = dependency.url.pathname;

dependency.url.pathname = pathname.slice(-suffix.length) !== suffix ? pathname + suffix : pathname;
}
},
process: function(dependency) {
provide(function() { return parseHtml(dependency.source); });
Expand All @@ -38,5 +51,5 @@
return new (HandlerHtml.extends(abstractHandler));
}

provide([ '/demand/abstract/handler' ], definition);
}());
provide([ 'path', '/demand/abstract/handler', '/demand/validator/isObject', '/demand/function/merge' ], definition);
}());
31 changes: 22 additions & 9 deletions addon/handler/json.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
(function() {
'use strict';

function definition(abstractHandler) {
var suffix = '.json',
regexMatchType = /^application\/json/;
function definition(path, abstractHandler, isObject, merge) {
var regexMatchType = /^application\/json/,
settings = { suffix: '.json' };

demand
.on('postConfigure:' + path, function(options) {
if(isObject(options)) {
merge(settings, options);
}
});

function HandlerJson() {}

HandlerJson.prototype = {
validate: function(type) {
return regexMatchType.test(type);
},
onPreRequest: function(dependency) {
var pathname = dependency.url.pathname;

dependency.url.pathname = pathname.slice(-suffix.length) !== suffix ? pathname + suffix : pathname;
onPreRequest: function(dependency, suffix) {
var pathname;

suffix = (typeof suffix !== 'undefined') ? suffix : settings.suffix;

if(suffix) {
pathname = dependency.url.pathname;

dependency.url.pathname = pathname.slice(-suffix.length) !== suffix ? pathname + suffix : pathname;
}
},
process: function(dependency) {
var data = JSON.parse(dependency.source);
Expand All @@ -26,5 +39,5 @@
return new (HandlerJson.extends(abstractHandler));
}

provide([ '/demand/abstract/handler' ], definition);
}());
provide([ 'path', '/demand/abstract/handler', '/demand/validator/isObject', '/demand/function/merge' ], definition);
}());
34 changes: 24 additions & 10 deletions addon/handler/legacy.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
(function() {
'use strict';

function definition(path, Failure, handlerModule, isObject) {
var settings = {};
function definition(path, Failure, handlerModule, isObject, merge) {
var settings = { suffix: '.js' };

demand
.on('postConfigure:' + path, function(options) {
settings = isObject(options) ? options : {};
if(isObject(options)) {
merge(settings, options);
}
});

function resolve() {
Expand All @@ -17,20 +19,32 @@

handlerModule.process(self);

if(probe && (result = probe())) {
provide(function() { return result; });
} else {
function resolve(module) {
provide(function() { return module; });
}

function reject() {
dfd.reject(new Failure('error probing', self.path));
}

if(probe) {
if((result = probe(resolve, reject))) {
resolve(result);
}
} else {
reject();
}
}

function HandlerLegacy() {}

HandlerLegacy.prototype = {
onPreRequest: function(dependency) {
onPreRequest: function(dependency, suffix) {
var dependencies = settings[dependency.path] && settings[dependency.path].dependencies;

handlerModule.onPreRequest(dependency);
suffix = (typeof suffix !== 'undefined') ? suffix : settings.suffix;

handlerModule.onPreRequest(dependency, suffix || false);

if(dependencies) {
dependency.enqueue = demand.apply(null, dependencies);
Expand Down Expand Up @@ -63,5 +77,5 @@
return new (HandlerLegacy.extends(handlerModule));
}

provide([ 'path', '/demand/failure', '/demand/handler/module', '/demand/validator/isObject' ], definition);
}());
provide([ 'path', '/demand/failure', '/demand/handler/module', '/demand/validator/isObject', '/demand/function/merge' ], definition);
}());
27 changes: 23 additions & 4 deletions addon/handler/text.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
(function() {
'use strict';

function definition(abstractHandler) {
var regexMatchType = /^text\/.+/;
function definition(path, abstractHandler, isObject, merge) {
var regexMatchType = /^text\/.+/,
settings = {};

demand
.on('postConfigure:' + path, function(options) {
if(isObject(options)) {
merge(settings, options);
}
});

function HandlerText() {}

HandlerText.prototype = {
validate: function(type) {
return regexMatchType.test(type);
},
onPreRequest: function(dependency, suffix) {
var pathname;

suffix = (typeof suffix !== 'undefined') ? suffix : settings.suffix;

if(suffix) {
pathname = dependency.url.pathname;

dependency.url.pathname = pathname.slice(-suffix.length) !== suffix ? pathname + suffix : pathname;
}
},
process: function(dependency) {
provide(function() { return dependency.source; });
}
Expand All @@ -18,5 +37,5 @@
return new (HandlerText.extends(abstractHandler));
}

provide([ '/demand/abstract/handler' ], definition);
}());
provide([ 'path', '/demand/abstract/handler', '/demand/validator/isObject', '/demand/function/merge' ], definition);
}());
4 changes: 2 additions & 2 deletions dist/cache/dispose.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/cache/dispose.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/cache/states.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/cache/states.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/demand.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/demand.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 050db20

Please sign in to comment.