Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release: 4.0.0 #15

Merged
merged 2 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ Brandi has no dependencies, but requires the following globals in order to work:

## Production

By default, Brandi will be in development mode. The development mode includes warnings about common mistakes.
By default, Brandi will be in development mode. The development mode includes warnings about common mistakes
and `capture()/resotre()` `Container` methods.

Don't forget to set `process.env.NODE_ENV` to `production` when deploying your application.

Expand Down
3 changes: 2 additions & 1 deletion docs/getting-started/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ Brandi has no dependencies, but requires the following globals in order to work:

## Production

By default, Brandi will be in development mode. The development mode includes warnings about common mistakes.
By default, Brandi will be in development mode. The development mode includes warnings about common mistakes
and `capture()/resotre()` `Container` methods.

Don't forget to set `process.env.NODE_ENV` to `production` when deploying your application.

Expand Down
16 changes: 16 additions & 0 deletions docs/reference/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,26 @@ expect(containerClone).not.toBe(originalContainer);

Captures (snapshots) the current container state.

:::note

The `capture()` method works only in development mode (`process.env.NODE_ENV !== 'production'`).

`Container.capture` is `undefined` in production mode.

:::

### `restore()`

Restores the [captured](#capture) container state.

:::note

The `restore()` method works only in development mode (`process.env.NODE_ENV !== 'production'`).

`Container.restore` is `undefined` in production mode.

:::

#### Example

```typescript
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/brandi-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "brandi-react",
"version": "3.1.0",
"version": "4.0.0",
"description": "React bindings for Brandi — the dependency injection container.",
"main": "./lib/brandi-react.js",
"module": "./lib/brandi-react.mjs",
Expand Down Expand Up @@ -47,7 +47,7 @@
"container"
],
"peerDependencies": {
"brandi": "^3.0.0",
"brandi": "^3 || ^4",
"react": "^16.8.0 || ^17"
}
}
3 changes: 2 additions & 1 deletion packages/brandi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ Brandi has no dependencies, but requires the following globals in order to work:

### Production

By default, Brandi will be in development mode. The development mode includes warnings about common mistakes.
By default, Brandi will be in development mode. The development mode includes warnings about common mistakes
and `capture()/resotre()` `Container` methods.

Don't forget to set `process.env.NODE_ENV` to `production` when deploying your application.

Expand Down
2 changes: 1 addition & 1 deletion packages/brandi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "brandi",
"version": "3.1.0",
"version": "4.0.0",
"description": "The dependency injection container.",
"main": "./lib/brandi.js",
"module": "./lib/brandi.mjs",
Expand Down
86 changes: 72 additions & 14 deletions packages/brandi/spec/container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ describe('container', () => {
const container = new Container();
container.bind(TOKENS.value).toConstant(someValue);

container.capture();
container.capture!();

container.bind(TOKENS.value).toConstant(anotherValue);
container.bind(TOKENS.additional).toConstant(additionalValue);

expect(container.get(TOKENS.value)).toBe(anotherValue);
expect(container.get(TOKENS.additional)).toBe(additionalValue);

container.restore();
container.restore!();

expect(container.get(TOKENS.value)).toBe(someValue);
expect(() =>
Expand All @@ -156,7 +156,7 @@ describe('container', () => {
expect(container.get(TOKENS.value)).toBe(anotherValue);
expect(container.get(TOKENS.additional)).toBe(additionalValue);

container.restore();
container.restore!();

expect(container.get(TOKENS.value)).toBe(someValue);
expect(() =>
Expand All @@ -180,30 +180,90 @@ describe('container', () => {

const container = new Container().extend(firstContainer);

container.capture();
container.capture!();

container.extend(secondContainer);

expect(container.get(TOKENS.value)).toBe(secondValue);

container.restore();
container.restore!();

expect(container.get(TOKENS.value)).toBe(firstValue);

container.extend(secondContainer);

expect(container.get(TOKENS.value)).toBe(secondValue);

container.restore();
container.restore!();

expect(container.get(TOKENS.value)).toBe(firstValue);
});

it('captures a singleton scoped binding state to a snapshot', () => {
class Singleton {}

const TOKENS = {
some: token<Singleton>('some'),
another: token<Singleton>('another'),
};

const container = new Container();
container.bind(TOKENS.some).toInstance(Singleton).inSingletonScope();
container.bind(TOKENS.another).toInstance(Singleton).inSingletonScope();

const firstSomeInstance = container.get(TOKENS.some);

container.capture!();

const secondSomeInstance = container.get(TOKENS.some);
const firstAnotherInstance = container.get(TOKENS.another);

container.restore!();
container.capture!();

const thirdSomeInstance = container.get(TOKENS.some);
const secondAnotherInstance = container.get(TOKENS.another);

expect(firstSomeInstance).toBe(secondSomeInstance);
expect(secondSomeInstance).toBe(thirdSomeInstance);
expect(firstAnotherInstance).not.toBe(secondAnotherInstance);
});

it('captures a container scoped binding state to a snapshot', () => {
class Some {}

const TOKENS = {
some: token<Some>('some'),
another: token<Some>('another'),
};

const container = new Container();
container.bind(TOKENS.some).toInstance(Some).inContainerScope();
container.bind(TOKENS.another).toInstance(Some).inContainerScope();

const firstSomeInstance = container.get(TOKENS.some);

container.capture!();

const secondSomeInstance = container.get(TOKENS.some);
const firstAnotherInstance = container.get(TOKENS.another);

container.restore!();
container.capture!();

const thirdSomeInstance = container.get(TOKENS.some);
const secondAnotherInstance = container.get(TOKENS.another);

expect(firstSomeInstance).toBe(secondSomeInstance);
expect(secondSomeInstance).not.toBe(thirdSomeInstance);
expect(firstAnotherInstance).not.toBe(secondAnotherInstance);
});

it('logs an error when trying to restore a non-captured container state', () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => null);

const container = new Container();
container.restore();
container.restore!();

expect(spy).toHaveBeenCalledTimes(1);
expect(spy.mock.calls[0]?.[0]).toMatchSnapshot();
Expand All @@ -215,25 +275,23 @@ describe('container', () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => null);

const container = new Container();
container.capture();
container.restore();
container.capture!();
container.restore!();

expect(spy).toHaveBeenCalledTimes(0);

spy.mockRestore();
});

it("skips the logging in 'production' env", () => {
it("does not include capturing in 'production' env", () => {
const restoreEnv = setEnv('production');
const spy = jest.spyOn(console, 'error').mockImplementation(() => null);

const container = new Container();
container.restore();

expect(spy).toHaveBeenCalledTimes(0);
expect(container.capture).toBeUndefined();
expect(container.restore).toBeUndefined();

restoreEnv();
spy.mockRestore();
});

it("creates a container by 'createContainer'", () => {
Expand Down
43 changes: 33 additions & 10 deletions packages/brandi/src/container/BindingsVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,33 @@ import { Token, TokenValue, tag as createTag } from '../pointers';
import { Binding } from './bindings';
import { ResolutionCache } from './ResolutionCache';

type BindingsMap = Map<ResolutionCondition, Binding | BindingsVault>;

export class BindingsVault {
private static notag = createTag('NO_TAG');

public parent: BindingsVault | null = null;

private readonly map = new Map<
symbol,
Map<ResolutionCondition, Binding | BindingsVault>
>();
private readonly map = new Map<symbol, BindingsMap>();

public copy?(): BindingsVault;

constructor() {
if (process.env.NODE_ENV !== 'production') {
this.copy = (): BindingsVault =>
this.from((prev) => {
const next = new Map<ResolutionCondition, Binding | BindingsVault>();
prev.forEach((binding, key) => {
if (binding instanceof BindingsVault) {
next.set(key, binding.copy!());
} else {
next.set(key, binding.clone?.() ?? binding);
}
});
return next;
});
}
}

public set(
binding: Binding | BindingsVault,
Expand Down Expand Up @@ -122,17 +140,22 @@ export class BindingsVault {
return null;
}

public clone(): BindingsVault {
private from(
callback: (bindings: BindingsMap) => BindingsMap,
): BindingsVault {
const vault = new BindingsVault();
vault.parent = this.parent;

this.map.forEach((value, key) => {
vault.map.set(
key,
new Map<ResolutionCondition, Binding | BindingsVault>(value),
);
this.map.forEach((bindings, key) => {
vault.map.set(key, callback(bindings));
});

return vault;
}

public clone(): BindingsVault {
return this.from(
(prev) => new Map<ResolutionCondition, Binding | BindingsVault>(prev),
);
}
}
Loading