| 7 | import { ScopeObserver, ScopeObserverDelegate } from "./scope_observer" |
| 8 | |
| 9 | export class Router implements ScopeObserverDelegate { |
| 10 | readonly application: Application |
| 11 | private scopeObserver: ScopeObserver |
| 12 | private scopesByIdentifier: Multimap<string, Scope> |
| 13 | private modulesByIdentifier: Map<string, Module> |
| 14 | |
| 15 | constructor(application: Application) { |
| 16 | this.application = application |
| 17 | this.scopeObserver = new ScopeObserver(this.element, this.schema, this) |
| 18 | this.scopesByIdentifier = new Multimap() |
| 19 | this.modulesByIdentifier = new Map() |
| 20 | } |
| 21 | |
| 22 | get element() { |
| 23 | return this.application.element |
| 24 | } |
| 25 | |
| 26 | get schema() { |
| 27 | return this.application.schema |
| 28 | } |
| 29 | |
| 30 | get logger() { |
| 31 | return this.application.logger |
| 32 | } |
| 33 | |
| 34 | get controllerAttribute(): string { |
| 35 | return this.schema.controllerAttribute |
| 36 | } |
| 37 | |
| 38 | get modules() { |
| 39 | return Array.from(this.modulesByIdentifier.values()) |
| 40 | } |
| 41 | |
| 42 | get contexts() { |
| 43 | return this.modules.reduce((contexts, module) => contexts.concat(module.contexts), [] as Context[]) |
| 44 | } |
| 45 | |
| 46 | start() { |
| 47 | this.scopeObserver.start() |
| 48 | } |
| 49 | |
| 50 | stop() { |
| 51 | this.scopeObserver.stop() |
| 52 | } |
| 53 | |
| 54 | loadDefinition(definition: Definition) { |
| 55 | this.unloadIdentifier(definition.identifier) |
| 56 | const module = new Module(this.application, definition) |
| 57 | this.connectModule(module) |
| 58 | const afterLoad = (definition.controllerConstructor as any).afterLoad |
| 59 | if (afterLoad) { |
| 60 | afterLoad.call(definition.controllerConstructor, definition.identifier, this.application) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | unloadIdentifier(identifier: string) { |
| 65 | const module = this.modulesByIdentifier.get(identifier) |
| 66 | if (module) { |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…