Skip to content

Commit

Permalink
Support for unbind within container modules (#464)
Browse files Browse the repository at this point in the history
* Updated some dependencies

* fixed property decorator issue

* Explicit gulp test

* Implements 457

* Rollback harmonize

* Implements support unbind within container modules

* Updated docs

* Temp fix for harmonize issues

* Updated harmonize

* Added support for isBound within a containter module
  • Loading branch information
remojansen authored Dec 29, 2016
1 parent 627a551 commit ccd6d93
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//******************************************************************************

// Enable ES6
require("harmonize")();
require("harmonize")(["harmony", "harmony-proxies", "harmony_proxies"]);

var gulp = require("gulp"),
browserify = require("browserify"),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"gulp-tslint": "^7.0.0",
"gulp-typescript": "^3.0.0",
"gulp-uglify": "^2.0.0",
"harmonize": "^1.4.4",
"harmonize": "^2.0.0",
"harmony-proxy": "^1.0.0",
"istanbul": "^0.4.2",
"karma": "^1.1.2",
Expand Down
18 changes: 17 additions & 1 deletion src/container/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,25 @@ class Container implements interfaces.Container {
};
};

let getUnbindFunction = (moduleId: string) => {
return (serviceIdentifier: interfaces.ServiceIdentifier<any>) => {
let _unbind = this.unbind.bind(this);
_unbind(serviceIdentifier);
};
};

let getIsboundFunction = (moduleId: string) => {
return (serviceIdentifier: interfaces.ServiceIdentifier<any>) => {
let _isBound = this.isBound.bind(this);
return _isBound(serviceIdentifier);
};
};

modules.forEach((module) => {
let bindFunction = getBindFunction(module.guid);
module.registry(bindFunction);
let unbindFunction = getUnbindFunction(module.guid);
let isboundFunction = getIsboundFunction(module.guid);
module.registry(bindFunction, unbindFunction, isboundFunction);
});

}
Expand Down
4 changes: 2 additions & 2 deletions src/container/container_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { guid } from "../utils/guid";
class ContainerModule implements interfaces.ContainerModule {

public guid: string;
public registry: (bind: interfaces.Bind) => void;
public registry: (bind: interfaces.Bind, unbind: interfaces.Unbind, isBound: interfaces.IsBound) => void;

public constructor(registry: (bind: interfaces.Bind) => void) {
public constructor(registry: (bind: interfaces.Bind, unbind: interfaces.Unbind, isBound: interfaces.IsBound) => void) {
this.guid = guid();
this.registry = registry;
}
Expand Down
10 changes: 9 additions & 1 deletion src/interfaces/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,17 @@ namespace interfaces {
<T>(serviceIdentifier: ServiceIdentifier<T>): BindingToSyntax<T>;
}

export interface Unbind extends Function {
<T>(serviceIdentifier: ServiceIdentifier<T>): void;
}

export interface IsBound extends Function {
<T>(serviceIdentifier: ServiceIdentifier<T>): boolean;
}

export interface ContainerModule {
guid: string;
registry: (bind: Bind) => void;
registry: (bind: Bind, unbind: Unbind, isBound: IsBound) => void;
}

export interface ContainerSnapshot {
Expand Down
43 changes: 40 additions & 3 deletions test/container/container_module.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@
import { interfaces } from "../../src/interfaces/interfaces";
import { expect } from "chai";
import { ContainerModule } from "../../src/container/container_module";
import { Container } from "../../src/container/container";

describe("ContainerModule", () => {

it("Should throw when invoking get, remove or hasKey with a null key", () => {

it("Should be able to set the registry of a container module", () => {
let registry = (bind: interfaces.Bind) => { /* do nothing */ };

let warriors = new ContainerModule(registry);
expect(warriors.guid.length).eql(36);
expect(warriors.registry).eql(registry);
});

it("Should be able to remove some bindings from within a container module", () => {

let container = new Container();
container.bind<string>("A").toConstantValue("1");
expect(container.get<string>("A")).to.eql("1");

let warriors = new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Unbind) => {
expect(container.get<string>("A")).to.eql("1");
unbind("A");
expect(() => { container.get<string>("A"); }).to.throw();
bind<string>("A").toConstantValue("2");
expect(container.get<string>("A")).to.eql("2");
bind<string>("B").toConstantValue("3");
expect(container.get<string>("B")).to.eql("3");
});

container.load(warriors);
expect(container.get<string>("A")).to.eql("2");
expect(container.get<string>("B")).to.eql("3");

});

it("Should be able to check for existance of bindings within a container module", () => {

let container = new Container();
container.bind<string>("A").toConstantValue("1");
expect(container.get<string>("A")).to.eql("1");

let warriors = new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Unbind, isBound: interfaces.IsBound) => {
expect(container.get<string>("A")).to.eql("1");
expect(isBound("A")).to.eql(true);
unbind("A");
expect(isBound("A")).to.eql(false);
});

container.load(warriors);

});

Expand Down
4 changes: 2 additions & 2 deletions wiki/container_modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
Container modules can help you to manage the complexity of your bindings in very large applications.

```ts
let warriors = new ContainerModule((bind: Bind) => {
let warriors = new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Unbind) => {
bind<Ninja>("Ninja").to(Ninja);
});

let weapons = new ContainerModule((bind: Bind) => {
let weapons = new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Unbind) => {
bind<Katana>("Katana").to(Katana);
bind<Shuriken>("Shuriken").to(Shuriken);
});
Expand Down

0 comments on commit ccd6d93

Please sign in to comment.