Skip to content

Commit

Permalink
adds configuration to components
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniusostermann committed Jan 15, 2017
1 parent f791538 commit 9eea44d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@ js
node_modules
dts
lib
.vscode
16 changes: 13 additions & 3 deletions src/component/component.ts
@@ -1,12 +1,18 @@
import { Component as ComponentInterface } from "../interfaces/interfaces";
import { Component as ComponentInterface, ExtensionPointDescriptor, Configuration } from "../interfaces/interfaces";

export class Component implements ComponentInterface {
readonly name: string;
readonly extensionPoints: { [name: string]: symbol } = {};
readonly extensionPoints: ExtensionPointDescriptor;
private _configuration: Configuration;

constructor(name: string, extensionPoints: { [name: string]: symbol }) {
get configuration() {
return this._configuration;
}

constructor(name: string, extensionPoints: ExtensionPointDescriptor = {}, configuration: Configuration = {}) {
this.name = name;
this.extensionPoints = extensionPoints;
this._configuration = configuration;
}

getExtensionPoint(name: string) {
Expand All @@ -16,4 +22,8 @@ export class Component implements ComponentInterface {

return this.extensionPoints[name];
}

addConfiguration(c: Configuration) {
this._configuration = Object.assign(this._configuration, c);
}
}
6 changes: 4 additions & 2 deletions src/component/registry.ts
Expand Up @@ -34,14 +34,16 @@ export class ComponentRegistry implements ComponentRegistryInterface {

addFromDescriptor(descriptor: ComponentDescriptor) {
let extensionPoints = descriptor.hasOwnProperty("extensionPoints") ? descriptor.extensionPoints : {};
this.add(new ComponentImpl(descriptor.name, extensionPoints));
let defaultConfig = descriptor.hasOwnProperty("defaultConfiguration") ? descriptor.defaultConfiguration : {};
this.add(new ComponentImpl(descriptor.name, extensionPoints, defaultConfig));

debug("Registering bindings for " + descriptor.name + "..");
this.registeredBindings[descriptor.name] = descriptor.bindings;
}

executeBinding(componentName: string, container: Container) {
debug("Executing bindings for " + componentName + "..");
debug("Executing self-bind and registered bindings for " + componentName + "..");
container.bind<Component>("meta:component//" + componentName).toConstantValue(this.registeredComponents[componentName]);
this.registeredBindings[componentName](this.getBinder(componentName, container), this);
}

Expand Down
15 changes: 13 additions & 2 deletions src/interfaces/interfaces.ts
Expand Up @@ -5,14 +5,24 @@ export interface ExecutableExtension {
execute(): any;
}

export interface ExtensionPointDescriptor {
[name: string]: symbol;
}

export interface Configuration {
[name: string]: any;
}

// If you register your component, you will get an instance of this.
// Every component must have a name, so we need it in the constructor.
// After setting, the name is not changable anymore. You set the name via the
// ComponentDescriptor.
export interface Component {
readonly name: string;
readonly extensionPoints: { [name: string]: symbol };
readonly extensionPoints: ExtensionPointDescriptor;
getExtensionPoint(name: string): symbol;
configuration: Configuration;
addConfiguration(configuration: Configuration): void;
}

// General interface to bind sth via dependency injection
Expand All @@ -38,7 +48,8 @@ export interface ComponentBinder {
// own consumptions / bindings.
export interface ComponentDescriptor {
name: string;
extensionPoints?: { [name: string]: symbol };
extensionPoints?: ExtensionPointDescriptor;
defaultConfiguration?: Configuration;
bindings: BindingDescriptor;
}

Expand Down

0 comments on commit 9eea44d

Please sign in to comment.