Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
smonn committed May 25, 2022
1 parent dd7ba83 commit 8d503f9
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,44 @@ Basic example usage. See more examples in the test file.
import { Container, createToken } from '@smonn/container';

// Interfaces are optional, but can help to ensure you depend on abstractions only.
interface Greeter {
interface IGreeter {
sayHello(): string;
}
interface Shouter {
interface IShouter {
shoutHello(): string;
}

class Other {
ping() {
return 'pong';
}
}

// It's recommended to assemble all tokens in one place for easier management.
// Use the createToken utility to assist with typings.
const Tokens = {
greeter: createToken<Greeter>('greeter'),
shouter: createToken<Shouter>('shouter'),
greeter: createToken<IGreeter>('greeter'),
shouter: createToken<IShouter>('shouter'),
name: createToken<string>('name'),

// You can also rely on the class's name when creating a token. But be careful
// when using this method if you minify your code. Also note that this means
// you will depend on the class directly instead of an interface.
other: createToken(Other),

// Tokens can also be strings if you prefer.
simple: 'simple',
} as const;

class Greeter implements Greeter {
class Greeter implements IGreeter {
constructor(private readonly name: string) {}

sayHello(): string {
return `Hello, ${this.name}!`;
}
}
class Shouter implements Shouter {
constructor(private readonly greeter: Greeter) {}
class Shouter implements IShouter {
constructor(private readonly greeter: IGreeter) {}

shoutHello(): string {
return this.greeter.sayHello().toUpperCase();
Expand All @@ -55,9 +69,11 @@ class Shouter implements Shouter {
// the token spec, explicitly declaring the generic type is not required.
function provideModule(container: Container) {
// Literal/basic values are allowed
container.set(Tokens.name, () => 'Joy');
container.set(Tokens.name, 'Joy');
container.set(Tokens.greeter, (c) => new Greeter(c.get(Tokens.name)));
container.set(Tokens.shouter, (c) => new Shouter(c.get(Tokens.greeter)));
container.set(Tokens.other, () => new Other());
container.set(Tokens.simple, 123);
}

const container = new Container();
Expand All @@ -80,4 +96,10 @@ console.log(
container.create(Tokens.shouter)
) === false
);

console.log('ping', container.get(Tokens.other).ping());

// Since the `simple` token is just a string, the default inferred type is
// `any`. Therefore, you need to set the type explicitly, if desired.
console.log('one-two-three', container.get<number>(Tokens.simple));
```

0 comments on commit 8d503f9

Please sign in to comment.