/*
**  Microkernel -- Microkernel for Server Applications
**  Copyright (c) 2015 Ralf S. Engelschall <rse@engelschall.com>
**
**  Permission is hereby granted, free of charge, to any person obtaining
**  a copy of this software and associated documentation files (the
**  "Software"), to deal in the Software without restriction, including
**  without limitation the rights to use, copy, modify, merge, publish,
**  distribute, sublicense, and/or sell copies of the Software, and to
**  permit persons to whom the Software is furnished to do so, subject to
**  the following conditions:
**
**  The above copyright notice and this permission notice shall be included
**  in all copies or substantial portions of the Software.
**
**  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
**  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
**  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
**  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
**  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
**  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
**  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import glob from "glob"

/*  the mixin class  */
export default class MicrokernelLoader {
    /*  load source files as a procedure
        (and hence execute them with the microkernel as context)  */
    exec (...files) {
        files.forEach((file) => {
            let filesExpanded = file.match(/[*?]/) !== null ? glob.sync(file) : [ file ]
            if (filesExpanded.length === 0)
                throw new Error("no files found")
            filesExpanded.forEach((file) => {
                let mod = require(file)
                mod(this)
            })
        })
        return this
    }

    /*  load source files as module definition
        (and hence instanciate and add them to microkernel)  */
    load (...files) {
        files.forEach((file) => {
            let filesExpanded = file.match(/[*?]/) !== null ? glob.sync(file) : [ file ]
            filesExpanded.forEach((file) => {
                let mod = require(file)
                this.add(new mod())
            })
        })
        return this
    }
}


@@ -1428,32 +1428,130 @@
 ec (...files) %7B%0A
+        return new Promise((resolve, reject) =%3E %7B%0A            let seq = new Promise.resolve()%0A    
         files.fo
@@ -1560,32 +1560,36 @@
 ach((file) =%3E %7B%0A
+    
             let 
@@ -1673,16 +1673,20 @@
         
+    
 if (file
@@ -1725,16 +1725,20 @@
         
+   
+ 
 throw ne
@@ -1763,16 +1763,20 @@
 found%22)%0A
+    
         
@@ -1821,32 +1821,36 @@
                 
+ 
+   
 let mod = requir
@@ -1873,16 +1873,41 @@
         
+   
+ seq = seq.then(() =%3E 
 mod(this
@@ -1907,17 +1907,19 @@
 od(this)
-%0A
+)%0A 
         
@@ -1914,35 +1914,42 @@
 ))%0A             
-%7D)%0A
+   %7D)%0A    
         %7D)%0A     
@@ -1947,35 +1947,64 @@
  %7D)%0A        
-return this
+    seq.then(() =%3E resolve())%0A        %7D)
 %0A    %7D%0A%0A    

use Promises for exec()