Last active
December 9, 2016 03:57
-
-
Save resistdesign/e300d5356a1daa03f6dc908297c7c9a2 to your computer and use it in GitHub Desktop.
JS DI Concept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JSDI | |
class K{ | |
handler; | |
constructor(handler){ | |
this.handler = handler; | |
} | |
exec() { | |
this.handler(); | |
} | |
} | |
const m = { | |
thing1: { | |
args: [ | |
(context) => context.xyz | |
], | |
factory: (a) => { | |
return { | |
success: a | |
}; | |
} | |
}, | |
thing2: { | |
args: [ | |
'thing1' | |
], | |
factory: (a) => { | |
return () => { | |
console.log('Data:', a); | |
}; | |
} | |
}, | |
thing3: { | |
args: [ | |
'thing2' | |
], | |
factory: (a) => { | |
return new K(a); | |
} | |
} | |
}; | |
function getInstance(name, map, context){ | |
let instance; | |
if(typeof name === 'string' && map instanceof Object){ | |
const def = map[name]; | |
const args = def.args; | |
const factory = def.factory; | |
if(args instanceof Array && factory instanceof Function){ | |
const resolvedArgs = []; | |
for(let i = 0; i < args.length; i++){ | |
const argItem = args[i]; | |
if(typeof argItem === 'string'){ | |
resolvedArgs.push(getInstance(argItem, map, context)); | |
}else if(argItem instanceof Function){ | |
resolvedArgs.push(argItem(context)); | |
} | |
} | |
instance = factory.apply(null, resolvedArgs); | |
} | |
} | |
return instance; | |
} | |
const inst = getInstance('thing3', m, {xyz: 'yes'}); | |
inst.exec(); | |
/*** Async Solution Demo ***/ | |
const items = [ | |
async () => { | |
return await new Promise((res, rej) => { | |
setTimeout(() => res(1), 10); | |
}); | |
}, | |
() => 2, | |
async () => { | |
return await new Promise((res, rej) => { | |
setTimeout(() => res(3), 800); | |
}); | |
}, | |
async () => { | |
return await new Promise((res, rej) => { | |
setTimeout(() => res(4), 5000); | |
}); | |
}, | |
async () => { | |
return await new Promise((res, rej) => { | |
setTimeout(() => res(5), 200); | |
}); | |
}, | |
async () => { | |
return await new Promise((res, rej) => { | |
setTimeout(() => res(6), 1500); | |
}); | |
}, | |
() => 7, | |
async () => { | |
return await new Promise((res, rej) => { | |
setTimeout(() => res(8), 7000); | |
}); | |
}, | |
]; | |
async function x(items = []){ | |
const list = []; | |
const promises = []; | |
for(let i = 0; i < items.length; i++){ | |
const f = items[i]; | |
const val = f(); | |
if(val instanceof Promise){ | |
val.then(data => list[i] = data); | |
promises.push(val); | |
}else{ | |
list[i] = val; | |
} | |
} | |
await Promise.all(promises); | |
console.log(list); | |
} | |
x(items); | |
Main Objective:
Create an easy to implement DI system with lifecycle capabilities that could potentially serve as the basis for the core architecture of both client and server applications.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create a way to add DI map keys/entries from arbitrary location/files while ensuring (key/entry name) uniqueness??? (Crazy talk :P)