Skip to content

Commit

Permalink
Rewrite with async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
neolao committed Dec 6, 2017
1 parent 8cc3f38 commit 7107ac3
Show file tree
Hide file tree
Showing 15 changed files with 5,785 additions and 156 deletions.
30 changes: 30 additions & 0 deletions example/lib/Bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
var Bundle = class Bundle {
constructor() {}

getPath() {
return __dirname;
}

initialize(app) {
app.on("start", this.onStart);
console.log("Bundle initialized");
}

async onStart(app) {
var parameters = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];

console.log("started");

var container = app.getParameter("serviceContainer");
console.log("Service container:", container);

var selfService = await container.get("container");
console.log("Service container from service container:", selfService);
}
};
exports.default = Bundle;
5 changes: 5 additions & 0 deletions example/lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ var _Bundle = require("../../lib/Bundle");

var _Bundle2 = _interopRequireDefault(_Bundle);

var _Bundle3 = require("./Bundle");

var _Bundle4 = _interopRequireDefault(_Bundle3);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var application = new _solfegejs2.default.Application();
application.addBundle(new _Bundle2.default());
application.addBundle(new _Bundle4.default());

var parameters = process.argv.slice(2);
application.start(parameters);
43 changes: 43 additions & 0 deletions example/src/Bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* @flow */
import type Application from "solfegejs/src/Application"
import type {BundleInterface, InitializableBundleInterface} from "solfegejs/src/BundleInterface"

/**
* Example bundle
*/
export default class Bundle implements BundleInterface, InitializableBundleInterface
{
/**
* Constructor
*/
constructor():void
{
}

/**
* Get bundle path
*
* @return {String} The bundle path
*/
getPath():string
{
return __dirname;
}

initialize(app:Application)
{
app.on("start", this.onStart);
console.log("Bundle initialized");
}

async onStart(app:Application, parameters:Array<String> = [])
{
console.log("started");

let container = app.getParameter("serviceContainer");
console.log("Service container:", container);

let selfService = await container.get("container");
console.log("Service container from service container:", selfService);
}
}
3 changes: 2 additions & 1 deletion example/src/console.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import solfege from "solfegejs"
import DIBundle from "../../lib/Bundle"
//import MyBundle from "./Bundle";
import MyBundle from "./Bundle";

// Create application instance
let application = new solfege.Application;
application.addBundle(new DIBundle);
application.addBundle(new MyBundle);

// Load configuration file
//application.loadConfigurationFile(`${__dirname}/config/production.yml`, "yaml");
Expand Down
10 changes: 5 additions & 5 deletions interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,31 +207,31 @@ export interface ContainerInterface
* @param {String} id Service id
* @return {String} Service class path
*/
getServiceClassPath(id:string):Generator<*,string,*>;
getServiceClassPath(id:string):Promise<string>;

/**
* Get definition class path
*
* @param {Definition} definition Service definition
* @return {String} Service class path
*/
getDefinitionClassPath(definition:DefinitionInterface):Generator<*,string,*>;
getDefinitionClassPath(definition:DefinitionInterface):Promise<string>;

/**
* Build definition instance
*
* @param {Definition} definition Service definition
* @return {*} Service instance
*/
buildInstance(definition:DefinitionInterface):Generator<*,*,*>;
buildInstance(definition:DefinitionInterface):*;

/**
* Resolve a parameter
*
* @param {*} parameter The parameter
* @return {*} The resolved parameter
*/
resolveParameter(parameter:any):Generator<*,*,*>;
resolveParameter(parameter:any):*;
}

/**
Expand All @@ -244,7 +244,7 @@ export interface CompilerPassInterface
*
* @param {ContainerInterface} container Container
*/
process(container:ContainerInterface):Generator<*,*,*>;
process(container:ContainerInterface):*;
}


39 changes: 17 additions & 22 deletions lib/Bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@ var _path = require("path");

var _path2 = _interopRequireDefault(_path);

var _coFs = require("co-fs");
var _fs = require("fs");

var _coFs2 = _interopRequireDefault(_coFs);
var _fs2 = _interopRequireDefault(_fs);

var _configYaml = require("config-yaml");

var _configYaml2 = _interopRequireDefault(_configYaml);

var _bindGenerator = require("bind-generator");

var _bindGenerator2 = _interopRequireDefault(_bindGenerator);

var _isGenerator = require("is-generator");

var _Container = require("./ServiceContainer/Container");

var _Container2 = _interopRequireDefault(_Container);
Expand All @@ -46,22 +40,24 @@ var Bundle = class Bundle {
return __dirname;
}

*initialize(application) {
initialize(application) {
this.application = application;

this.application.on("configuration_loaded", (0, _bindGenerator2.default)(this, this.onConfigurationLoaded));
this.application.setParameter("serviceContainer", this.container);

this.application.on("configuration_loaded", this.onConfigurationLoaded.bind(this));

this.application.on("bundles_initialized", (0, _bindGenerator2.default)(this, this.onBundlesInitialized));
this.application.on("bundles_initialized", this.onBundlesInitialized.bind(this));

var definition = this.container.register("container", this.container);
definition.setClassPath("" + __dirname + _path2.default.sep + "ServiceContainer" + _path2.default.sep + "Container");
}

*onConfigurationLoaded(application, configuration) {
onConfigurationLoaded(application, configuration) {
this.container.setConfiguration(configuration);
}

*onBundlesInitialized() {
async onBundlesInitialized() {
var bundles = this.application.getBundles();

var _iteratorNormalCompletion = true;
Expand All @@ -72,17 +68,17 @@ var Bundle = class Bundle {
for (var _iterator = bundles[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var bundle = _step.value;

if ((0, _isGenerator.fn)(bundle.configureContainer)) {
yield bundle.configureContainer(this.container);
if (typeof bundle.configureContainer === "function") {
await bundle.configureContainer(this.container);
}

var bundlePath = this.application.getBundleDirectoryPath(bundle);
if (!bundlePath) {
throw new Error("Unable to find bundle directory path");
}
var configurationFile = "" + bundlePath + _path2.default.sep + "services.yml";
if (yield _coFs2.default.exists(configurationFile)) {
yield this.loadConfigurationFile(configurationFile);
if (await _fs2.default.exists(configurationFile)) {
this.loadConfigurationFile(configurationFile);
}
}
} catch (err) {
Expand All @@ -101,11 +97,11 @@ var Bundle = class Bundle {
}
}

*boot() {
yield this.container.compile();
async boot() {
await this.container.compile();
}

*loadConfigurationFile(filePath) {
loadConfigurationFile(filePath) {
var configuration = (0, _configYaml2.default)(filePath, { encoding: "utf8" });

if (_typeof(configuration.services) !== "object") {
Expand Down Expand Up @@ -135,5 +131,4 @@ var Bundle = class Bundle {
}
}
};
exports.default = Bundle;
module.exports = exports["default"];
exports.default = Bundle;
3 changes: 1 addition & 2 deletions lib/Command/DebugCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,4 @@ var DebugCommand = class DebugCommand {
}
}
};
exports.default = DebugCommand;
module.exports = exports["default"];
exports.default = DebugCommand;
Loading

0 comments on commit 7107ac3

Please sign in to comment.