| @@ -56,7 +56,7 typescript { | |||||
| 56 |
|
56 | |||
| 57 | if(symbols != 'none') { |
|
57 | if(symbols != 'none') { | |
| 58 | sourceMap = true |
|
58 | sourceMap = true | |
| 59 | sourceRoot = packageName |
|
59 | sourceRoot = "pack:${packageName}" | |
| 60 | } |
|
60 | } | |
| 61 |
|
61 | |||
| 62 | if (flavour == 'node') { |
|
62 | if (flavour == 'node') { | |
| @@ -14,6 +14,10 declare const Buffer: any; | |||||
| 14 |
|
14 | |||
| 15 | const _window: any = "undefined" !== typeof window ? window : null; |
|
15 | const _window: any = "undefined" !== typeof window ? window : null; | |
| 16 |
|
16 | |||
|
|
17 | function noop(): never { | |||
|
|
18 | throw new Error(); | |||
|
|
19 | } | |||
|
|
20 | ||||
| 17 | interface WritableArrayLike<T> { |
|
21 | interface WritableArrayLike<T> { | |
| 18 | length: number; |
|
22 | length: number; | |
| 19 | [n: number]: T; |
|
23 | [n: number]: T; | |
| @@ -24,13 +28,13 interface WritableArrayLike<T> { | |||||
| 24 | // detect to determine the best RNG source, normalizing to a function |
|
28 | // detect to determine the best RNG source, normalizing to a function | |
| 25 | // that |
|
29 | // that | |
| 26 | // returns 128-bits of randomness, since that's what's usually required |
|
30 | // returns 128-bits of randomness, since that's what's usually required | |
| 27 |
let _rng: () => WritableArrayLike<number> = |
|
31 | let _rng: () => WritableArrayLike<number> = noop; | |
| 28 |
|
32 | |||
| 29 | function setupBrowser() { |
|
33 | function setupBrowser() { | |
| 30 | // Allow for MSIE11 msCrypto |
|
34 | // Allow for MSIE11 msCrypto | |
| 31 | const _crypto = _window.crypto || _window.msCrypto; |
|
35 | const _crypto = _window.crypto || _window.msCrypto; | |
| 32 |
|
36 | |||
| 33 |
if ( |
|
37 | if (_rng === noop && _crypto && _crypto.getRandomValues) { | |
| 34 | // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto |
|
38 | // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto | |
| 35 | // |
|
39 | // | |
| 36 | // Moderately fast, high quality |
|
40 | // Moderately fast, high quality | |
| @@ -44,7 +48,7 function setupBrowser() { | |||||
| 44 | } catch (e) { /**/ } |
|
48 | } catch (e) { /**/ } | |
| 45 | } |
|
49 | } | |
| 46 |
|
50 | |||
| 47 |
if ( |
|
51 | if (_rng === noop) { | |
| 48 | // Math.random()-based (RNG) |
|
52 | // Math.random()-based (RNG) | |
| 49 | // |
|
53 | // | |
| 50 | // If all else fails, use Math.random(). It's fast, but is of |
|
54 | // If all else fails, use Math.random(). It's fast, but is of | |
| @@ -5,7 +5,7 import { ServiceMap, Descriptor, Partial | |||||
| 5 | import { TraceSource } from "../log/TraceSource"; |
|
5 | import { TraceSource } from "../log/TraceSource"; | |
| 6 | import { Configuration, RegistrationMap } from "./Configuration"; |
|
6 | import { Configuration, RegistrationMap } from "./Configuration"; | |
| 7 | import { Cancellation } from "../Cancellation"; |
|
7 | import { Cancellation } from "../Cancellation"; | |
| 8 |
import { |
|
8 | import { ICancellation } from "../interfaces"; | |
| 9 | import { isDescriptor } from "./traits"; |
|
9 | import { isDescriptor } from "./traits"; | |
| 10 | import { LifetimeManager } from "./LifetimeManager"; |
|
10 | import { LifetimeManager } from "./LifetimeManager"; | |
| 11 | import { each, isString } from "../safe"; |
|
11 | import { each, isString } from "../safe"; | |
| @@ -14,7 +14,7 import { FluentConfiguration } from "./f | |||||
| 14 |
|
14 | |||
| 15 | const trace = TraceSource.get("@implab/core/di/ActivationContext"); |
|
15 | const trace = TraceSource.get("@implab/core/di/ActivationContext"); | |
| 16 |
|
16 | |||
| 17 |
export class Container<S extends object = any> implements ServiceContainer<S> |
|
17 | export class Container<S extends object = any> implements ServiceContainer<S> { | |
| 18 | readonly _services: ContainerServiceMap<S>; |
|
18 | readonly _services: ContainerServiceMap<S>; | |
| 19 |
|
19 | |||
| 20 | readonly _lifetimeManager: LifetimeManager; |
|
20 | readonly _lifetimeManager: LifetimeManager; | |
| @@ -29,10 +29,11 export class Container<S extends object | |||||
| 29 |
|
29 | |||
| 30 | constructor(parent?: Container<S>) { |
|
30 | constructor(parent?: Container<S>) { | |
| 31 | this._parent = parent; |
|
31 | this._parent = parent; | |
| 32 |
this._services = |
|
32 | this._services = Object.create(parent ? parent._services : null); | |
| 33 | this._cleanup = []; |
|
33 | this._cleanup = []; | |
| 34 | this._root = parent ? parent.getRootContainer() : this; |
|
34 | this._root = parent ? parent.getRootContainer() : this; | |
| 35 | this._services.container = new ValueDescriptor(this) as any; |
|
35 | this._services.container = new ValueDescriptor(this) as any; | |
|
|
36 | this._services.childContainer = { activate: () => this.createChildContainer() }; | |||
| 36 | this._disposed = false; |
|
37 | this._disposed = false; | |
| 37 | this._lifetimeManager = new LifetimeManager(); |
|
38 | this._lifetimeManager = new LifetimeManager(); | |
| 38 | } |
|
39 | } | |
| @@ -1,3 +1,4 | |||||
|
|
1 | import { IDestroyable } from "../interfaces"; | |||
| 1 | import { ActivationContext } from "./ActivationContext"; |
|
2 | import { ActivationContext } from "./ActivationContext"; | |
| 2 | import { LifetimeManager } from "./LifetimeManager"; |
|
3 | import { LifetimeManager } from "./LifetimeManager"; | |
| 3 |
|
4 | |||
| @@ -27,7 +28,7 export interface ServiceLocator<S extend | |||||
| 27 | resolve<K extends ContainerKeys<S>>(name: K, def?: TypeOfService<S, K>): TypeOfService<S, K>; |
|
28 | resolve<K extends ContainerKeys<S>>(name: K, def?: TypeOfService<S, K>): TypeOfService<S, K>; | |
| 28 | } |
|
29 | } | |
| 29 |
|
30 | |||
| 30 | export interface ServiceContainer<S extends object> extends ServiceLocator<S> { |
|
31 | export interface ServiceContainer<S extends object> extends ServiceLocator<S>, IDestroyable { | |
| 31 | getLifetimeManager(): LifetimeManager; |
|
32 | getLifetimeManager(): LifetimeManager; | |
| 32 | register<K extends keyof S>(name: K, service: Descriptor<S, S[K]>): this; |
|
33 | register<K extends keyof S>(name: K, service: Descriptor<S, S[K]>): this; | |
| 33 | register(services: PartialServiceMap<S>): this; |
|
34 | register(services: PartialServiceMap<S>): this; | |
| @@ -37,6 +38,8 export interface ServiceContainer<S exte | |||||
| 37 |
|
38 | |||
| 38 | export interface ContainerProvided<S extends object> { |
|
39 | export interface ContainerProvided<S extends object> { | |
| 39 | container: ServiceLocator<S>; |
|
40 | container: ServiceLocator<S>; | |
|
|
41 | ||||
|
|
42 | childContainer: ServiceContainer<S>; | |||
| 40 | } |
|
43 | } | |
| 41 |
|
44 | |||
| 42 | export type ContainerRegistered<S extends object> = /*{ |
|
45 | export type ContainerRegistered<S extends object> = /*{ | |
| @@ -2,6 +2,7 import { StringBuilder } from "../text/S | |||||
| 2 | import { test } from "./TestTraits"; |
|
2 | import { test } from "./TestTraits"; | |
| 3 | import { MockConsole } from "../mock/MockConsole"; |
|
3 | import { MockConsole } from "../mock/MockConsole"; | |
| 4 | import { ConsoleWriter } from "../log/ConsoleWriter"; |
|
4 | import { ConsoleWriter } from "../log/ConsoleWriter"; | |
|
|
5 | import { Uuid } from "../Uuid"; | |||
| 5 |
|
6 | |||
| 6 | test("String builder", async t => { |
|
7 | test("String builder", async t => { | |
| 7 | const sb = new StringBuilder(); |
|
8 | const sb = new StringBuilder(); | |
| @@ -84,3 +85,11 test("ConsoleWriter", t => { | |||||
| 84 | t.deepEqual(mockConsole.getBuffer()[0].data, ["25 or 6 to 4! Let's have some ", { product: "tee" }], "Should handle many text chunks and object at the end"); |
|
85 | t.deepEqual(mockConsole.getBuffer()[0].data, ["25 or 6 to 4! Let's have some ", { product: "tee" }], "Should handle many text chunks and object at the end"); | |
| 85 |
|
86 | |||
| 86 | }); |
|
87 | }); | |
|
|
88 | ||||
|
|
89 | test("Uuid test", (t, log) => { | |||
|
|
90 | const id = Uuid(); | |||
|
|
91 | log.log("uuid = {0}", id); | |||
|
|
92 | t.assert(id, "Should generate uuid"); | |||
|
|
93 | t.notEqual(id, Uuid(), "uuid should never match"); | |||
|
|
94 | ||||
|
|
95 | }); | |||
General Comments 0
You need to be logged in to leave comments.
Login now
