Skip to content

Instantly share code, notes, and snippets.

@resistdesign
Last active December 9, 2016 03:57
Show Gist options
  • Save resistdesign/e300d5356a1daa03f6dc908297c7c9a2 to your computer and use it in GitHub Desktop.
Save resistdesign/e300d5356a1daa03f6dc908297c7c9a2 to your computer and use it in GitHub Desktop.
JS DI Concept
// 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);
@resistdesign
Copy link
Author

resistdesign commented Dec 9, 2016

Next phase, async all methods.

Async solution created.

@resistdesign
Copy link
Author

Next, integrate async solution with getInstance and it's internal method calls.

@resistdesign
Copy link
Author

resistdesign commented Dec 9, 2016

Also, add lifecycle with pub-sub.

Allow the update of resolved instances via lifecycle methods like:

{
  args: ...
  factory: ...
  lifecycle: {
    thing1: (instance, value) => {instance.prop = value;},
    thing2: (instance, value) => {instance.method(value);}
  }
}

Also try to find a way to trigger re-resolution (updates) of instances from within other instances. (Maybe with some sort of special handler function that is created based on some special arg type or something maybe.)

@resistdesign
Copy link
Author

resistdesign commented Dec 9, 2016

Add the ability to cache resolved instances. (Via some supplied Object or Map???)

@resistdesign
Copy link
Author

resistdesign commented Dec 9, 2016

Possibly add nested asynchronous dependency maps. (via async functions as map values, instead of objects map entries?)

Maybe add path parsing of dot notation keys like: thing1.otherThing.children.0

And maybe even allow path parsing on values resolved from non-nested dependencies. (in the args array)

@resistdesign
Copy link
Author

Create a way to add DI map keys/entries from arbitrary location/files while ensuring (key/entry name) uniqueness??? (Crazy talk :P)

@resistdesign
Copy link
Author

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