jaredhanson / electrolyte

Elegant dependency injection for Node.js.
MIT License
564 stars 61 forks source link

feat(async): add support for async dependencies #46

Closed patrickhulce closed 7 years ago

patrickhulce commented 8 years ago

Usage

Define an Async Module

exports = module.exports = function createDbConnetion(conf, mysql) {
  return mysql.connect(conf.sqlConnectionString).then(...);
};

exports['@async'] = true; // async because the module it returns is a within a promise
exports['@require'] = ['conf', 'mysql'];

Use an Async Module

IoC.createAsync('database').then(function (db) {
  db.execute('SELECT * FROM my_table');
}):

Depend on an Async Module

exports = module.exports = function usersModel(db) {
  return {
    create: function (name, email) {
      return db.execute('INSERT INTO users ...');
    }
  };
};

exports['@async'] = true; // async because it depends on an async module
exports['@require'] = ['database'];
coveralls commented 8 years ago

Coverage Status

Coverage increased (+0.03%) to 99.576% when pulling 61fc49f85251f3e73928e0c4d725299c30457273 on patrickhulce:add_async_support into a42170d28ef28dec935e7b16b8cbdfcf95c2ce96 on jaredhanson:master.

shaharke commented 8 years ago

Nice! 👍

coveralls commented 8 years ago

Coverage Status

Coverage increased (+0.03%) to 99.576% when pulling cc01e9e2a0f06cb9f29efaf644854e7f62ad793d on patrickhulce:add_async_support into a42170d28ef28dec935e7b16b8cbdfcf95c2ce96 on jaredhanson:master.

coveralls commented 8 years ago

Coverage Status

Coverage increased (+0.03%) to 99.578% when pulling 45e07ef5bcf338a1a0b2a72933ad71e42b4fa0f3 on patrickhulce:add_async_support into 9cf4679a197b0a8fbc6d148f07449c6acd88da80 on jaredhanson:master.

coveralls commented 8 years ago

Coverage Status

Coverage increased (+0.03%) to 99.578% when pulling 45e07ef5bcf338a1a0b2a72933ad71e42b4fa0f3 on patrickhulce:add_async_support into 9cf4679a197b0a8fbc6d148f07449c6acd88da80 on jaredhanson:master.

coveralls commented 8 years ago

Coverage Status

Coverage increased (+0.03%) to 99.578% when pulling 45e07ef5bcf338a1a0b2a72933ad71e42b4fa0f3 on patrickhulce:add_async_support into 9cf4679a197b0a8fbc6d148f07449c6acd88da80 on jaredhanson:master.

coveralls commented 8 years ago

Coverage Status

Coverage increased (+0.03%) to 99.578% when pulling 45e07ef5bcf338a1a0b2a72933ad71e42b4fa0f3 on patrickhulce:add_async_support into 9cf4679a197b0a8fbc6d148f07449c6acd88da80 on jaredhanson:master.

coveralls commented 8 years ago

Coverage Status

Coverage decreased (-0.4%) to 99.163% when pulling f7326c57407f6ce44412c4141d7f35747a963865 on patrickhulce:add_async_support into 9cf4679a197b0a8fbc6d148f07449c6acd88da80 on jaredhanson:master.

coveralls commented 8 years ago

Coverage Status

Coverage decreased (-0.4%) to 99.163% when pulling 1fd8e21d1c46ac97b72ded1e596db2d91a1a7a72 on patrickhulce:add_async_support into 9cf4679a197b0a8fbc6d148f07449c6acd88da80 on jaredhanson:master.

coveralls commented 8 years ago

Coverage Status

Coverage increased (+0.03%) to 99.582% when pulling 532ee91b66a912e0a8c20f47028b155f032076d8 on patrickhulce:add_async_support into 9cf4679a197b0a8fbc6d148f07449c6acd88da80 on jaredhanson:master.

coveralls commented 8 years ago

Coverage Status

Coverage increased (+0.03%) to 99.582% when pulling b3ae363562041e7c94568fd29d3ecf456aebe088 on patrickhulce:add_async_support into 9cf4679a197b0a8fbc6d148f07449c6acd88da80 on jaredhanson:master.

xduseko commented 8 years ago

@jaredhanson, is this going to be merged? released?

patrickhulce commented 8 years ago

@jaredhanson any feedback and/or thoughts on this, positive or negative?

jaredhanson commented 8 years ago

@patrickhulce I pulled your changes into a local branch, promises here: https://github.com/jaredhanson/electrolyte/tree/promises

The only thing I wasn't to fond of, was that the async and sync paths were different, and it wasn't clear at all as a developer using the IoC container, if a dependency was going to be async or not. For example, lets say you had a component that was sync today, and then tomorrow took an async dependency. When updating, you could no longer IoC.create that dependency, and had to switch to IoC.createAsync.

I do like the ability to create components asynchronously. It seems like the best approach is to simply make that the default. That's what I've done in the branch above. It is stable, I just need to review test coverage.

If you want to pull my branch, look it over, and then update any tests and documentation, I would really appreciate it. This will be released as electrolyte@0.4.0.

Thanks for your PR!

coveralls commented 8 years ago

Coverage Status

Coverage increased (+0.04%) to 99.573% when pulling d34c6af9f146014131eed73d80cbe73adc97e89d on patrickhulce:add_async_support into 74e04a36ea35b4154646c4df10157cbe4d6497cd on jaredhanson:master.

coveralls commented 8 years ago

Coverage Status

Coverage increased (+0.04%) to 99.573% when pulling 4c942dec7940a6e730b202548f468bf091d09e1b on patrickhulce:add_async_support into 74e04a36ea35b4154646c4df10157cbe4d6497cd on jaredhanson:master.

fwoelffel commented 7 years ago

Is this still relevant?

patrickhulce commented 7 years ago

@FWoelffel no the core functionality has since been merged to master 🎉 I'll close.