unamespace
Version:
Namespace with autoloading
1 lines • 8.2 kB
JavaScript
!function(e,s){"use strict";let i="1.3.3",t="DOMContentLoaded",a=!1,c=0,h=1e5,n=null,u={},l={},g="text/javascript",y={},m=[],v={},f={prefix:"[namespace.js]",status:!1,disable(){this.status=!1},enable(){this.status=!0},log(){this.status&&([].unshift.call(arguments,this.prefix),console.debug.apply(null,arguments))}};Object.assign=void 0!==Object.assign?Object.assign:function(){let t=arguments[0]||null;if(!t)throw"Invalid assign target";for(let e=1;e<arguments.length;e++)for(var n in arguments[e])t[n]=arguments[e][n];return t};const w=function(n){n=n||{};for(let t=1;t<arguments.length;t++){let e=arguments[t];if(e)for(var r in e)e.hasOwnProperty(r)&&(d._object(e[r])?n[r]=w(n[r],e[r]):n[r]=e[r])}return n};let d={_function(e){return"function"==typeof e},_async(e){return d._function(e)&&e instanceof(async()=>{}).constructor},_string(e){return"string"==typeof e||e instanceof String},_array(e){return e&&"object"==typeof e&&e.constructor===Array},_object(e){return e&&"object"==typeof e},_null(e){return null===e},_undefined(e){return void 0===e},set(e){return!d._undefined(e)&&!d._null(e)},http(){return"http"==e}};function b(e){let t=this,n=[e];Object.assign(t,{use(){return[].map.call(arguments,e=>{n.push("use:"+e)}),t},is(){return[].map.call(arguments,e=>{n.push("is:"+e)}),t},define(e){if(!d._object(e)&&!d._function(e))throw"Invalid factory type (function|object expected) got "+typeof e;n.push(e),o.apply(u,n)}})}const r=()=>{d.http()&&document.removeEventListener(t,r,!1),a=!0,f.log(t),m.sort((e,t)=>e[0]===t[0]?0:e[0]>t[0]?1:-1);let e;for(;e=m.shift();)e[1].call(u)},_=e=>{let t=e.split("."),n=[t.pop()+".js"],r,i,o;do{if(o=t.join("."),i&&n.unshift(i),d.set(l[o])){r=l[o]+"/"+n.join("/");break}}while(i=t.pop());return r||null};let p={defined:{},ready:{},readyStack:[],define(e){p.defined[e]=!0},isdefined(e){return!0===p.defined[e]},isready(e){return!0===p.ready[e]},bindReady(e){p.readyStack.push(e),f.log("bind ready:",p.readyStack.length)},unbindReady(t){p.readyStack=p.readyStack.filter(e=>e!==t),f.log("unbind ready:",p.readyStack.length)},readyCall(t){f.log("ready call `"+t+"`"),p.readyStack.map(e=>{e(t)&&(f.log("ready `"+t+"`"),p.unbindReady(e))})},async put(e,t,n){if(!d._function(t)&&!d._object(t))throw"Invalid factory for: "+e;var r=(e=e.split(":"))[1]||null;let i=e[0].split(".");var o=i.pop();let l,a=u;for(e=e[0];l=i.shift();)d.set(a[l])||(a[l]={}),a=a[l];if(d.set(a[o]))throw"Item `"+e+"` already defined!";if(r&&d.set(s[r]))throw"Global item `"+r+"` already exists!";if(t=d._async(t)?await t():t,!d._function(t)&&!d._object(t))throw"Invalid factory for `"+e+"`";a[o]=t,r&&0!==r.length&&(s[r]=a[o]),r=e,o=a[o]=t,t=void 0===n||n,!d.http()&&t&&(v[r].exports=o),f.log("put in storage `"+e+"`"),p.ready[e]=!0,p.readyCall(e)},get(e,t){var n=(e=e.split(".")).pop();let r,i=t||u;for(;r=e.shift();){if(!d.set(i[r]))return null;i=i[r]}if(!d.set(i[n]))throw"Item `"+e+"` not found!";return i[n]},load(e){if(p.isready(e))return p.readyCall(e);if(!p.isdefined(e)){var n,r=e;if(!y[r]){y[r]=!0,f.log("autoload start `"+r+"`");let t=_(r);if(!t&&d.http())return void f.log("autoload failed `"+r+"` - empty path");if(d.http()){let e=document.createElement("script");e.type=g,e.src=t+"?"+Math.random(),e.addEventListener("load",()=>{f.log("autoload complete `"+r+"` from `"+t+"`")}),e.addEventListener("error",()=>{f.log("autoload failed `"+r+"` from `"+t+"`")}),document.getElementsByTagName("head")[0].appendChild(e)}else t?require(t):(e=r.split("."),n=require(e[0]),e=1<e.length?n[e[1]]:n,p.put(r,e,!1))}}}};function j(e){if(arguments.length<1)throw"To few arguments in `use` method";var t,n,r=arguments[arguments.length-1];let i=[];if(!d._function(r)){for(let e=1;e<arguments.length;e++){if(!d._string(arguments[e]))throw"Invalid argument type in use method (string expected)";i.push("use:"+arguments[e])}return i}for(let e=1;e<arguments.length-1;e++){if(!d._string(arguments[e]))throw"Invalid argument type in use method (string expected)";var o=-1!==arguments[e].indexOf(":")?"":"use:";i.push(o+arguments[e])}p.bindReady(x(i,I(i,r,e))),t=i,n=r;let l=()=>{if(0===t.length)return n.call(u);f.log("start factory for",t),t.map(e=>{e=(e=e.split(":")[1]).split("#")[0],p.load(e)})};if(!a)return r=e?c+i.length:h+i.length,void m.push([r,l]);l()}const o=function(){if(arguments.length<1)throw"To few arguments in `namespace` method";let t=arguments[0];if(!d._string(t))throw"Invalid argument type for `name` (string expected)";var e;if(i=t.split(":")[0],e=(()=>{var t,e=Error.prepareStackTrace;let n;try{let e=new Error;for(Error.prepareStackTrace=(e,t)=>t,t=e.stack.shift().getFileName();e.stack.length&&t===(n=e.stack.shift().getFileName()););}catch(e){}return Error.prepareStackTrace=e,n})(),d.http()||(v[i]=require.cache[e]),1===arguments.length)return new b(t);var n,r,i=arguments[arguments.length-1];if(!d._function(i)&&!d._object(i))throw"Invalid factory type (function|object expected) got "+typeof i;let o=[];for(let e=1;e<arguments.length-1;e++)if(d._string(arguments[e])){if(arguments[e]===t)throw"Object cannot use itself";o.push(arguments[e])}else{if(!d._array(arguments[e]))throw"Invalid usage type (string or array expected)";o=o.concat(arguments[e])}p.define(t.split(":")[0]),f.log("define new item `"+t+"` with uses:",o),o.unshift(!0),o.push((n=t,r=i,function(){f.log("run factory `"+n.split(":")[0]+"`");let t={is:[],use:[]};for(let e=0;e<arguments.length;e++)t[arguments[e][0]].push(arguments[e][1]);let e=null;if(t.use.length){if(!d._function(r))throw"Invalid factory type (loaded not allowed for object factory)";e=r.apply(u,t.use)}else e=r;t.is.length&&(d._function(e)?(t.is.unshift(e.prototype),e.prototype=w.apply(u,t.is)):(t.is.unshift({}),t.is.push(e),e=w.apply(u,t.is))),p.put(n,e)})),j.apply(u,o)};const x=(e,n)=>t=>0===e.length||0===(e=e.filter(e=>t!==e.split(":")[1].split("#")[0])).length&&(n.call(u),!0),I=(e,t,o)=>()=>{let i=[];e.map(e=>{var t=e.split(":")[0];let n=e.split(":")[1];e=n.split("#")[1]||null;n=n.split("#")[0];let r=p.get(n);if(!r)throw"Object `"+n+"` not found. Namespace missing or invalid?";if(e&&d._function(r))throw"Unable to use property for function factory";if(e&&!(r=p.get(e,r)))throw"Property `"+e+"` not found in `"+n+"`";i.push(o?[t,r]:r)}),t.apply(u,i)};Object.assign(o,{version(){return console.log("namespace.js version is `1.3.3`"),i},debug(e,t=null){return e=e||!1,f.prefix=t||f.prefix,e?f.enable():f.disable(),f.log("debug:",e),o},listen(e){if(!d._string(e)||e.length<1)throw"Invalid global event name";return document.removeEventListener(t,r,!1),t=e,document.addEventListener(t,r,!1),f.log("change start event to `"+e+"`"),o},autoload(){if(arguments.length<1)return l;if(d._string(arguments[0]))return l[arguments[0]]||null;if(!d._object(arguments[0]))throw"Invalid `autoload` argument type (expected object), got "+typeof arguments[0];return Object.assign(l,arguments[0]),2===arguments.length&&(g=arguments[1]),f.log("register new autoload:",arguments[0]),o},resolve(e){return _(e)},resolveForRequire(){if(d.http())throw"Resolving for require is not available in HTTP mode";let e=require("module"),i=e.prototype.require;return e.prototype.require=function(){if(arguments.length<1)throw"Invalid arguments length for require";var n=d._array(arguments[0])?arguments[0]:arguments;let r=[];for(let t=0;t<n.length;t++){if(!d._string(n[t]))throw"Invalid argument type in use method (string expected)";let e=_(n[t]);e=e||n[t],r.push(i.call(this,e))}return 1<r.length?r:r.shift()},o},app(e){if(arguments.length<1)return n;if(!d._string(e))throw"Invalid `name` argument type (string expected), got "+typeof e;if(e.length<1)throw"Invalid `name` length";return n&&delete s[n],s[n=e]=u,f.log("define global application name `"+e+"`"),o}}),Object.assign(s,{namespace:o,is:function(){if(arguments.length<1)throw"To few arguments in extends method";let t=[];for(let e=0;e<arguments.length;e++){if(!d._string(arguments[e]))throw"Invalid argument type in extends method";t.push("is:"+arguments[e])}return t},use(){return[].unshift.call(arguments,!1),j.apply(u,arguments)},autoload:o.autoload,listen:o.listen}),d.http()?document.addEventListener(t,r,!1):r()}("undefined"!=typeof window?"http":"node","undefined"!=typeof window?window:global);