Core class
Represents an isolated Context in which Modules can be registered/unregistered and started/stopped.
An Dart application could use several Core instances.
class Core { final Map<Symbol, ClassMirror> _registeredModules = new Map<Symbol, ClassMirror>(); final Map<Symbol, dynamic> _runningModules = new Map<Symbol, dynamic>(); final Mediator mediator = new Mediator(); /** * Register a new module in the [Core] by passing an instance * of its class as first parameter. * * By default the name of the [runtimeType] is used as the module name. * You can provide a custom name for the module by passing it as second argument. * * __no subclassing required__ * The module class doesn't have to subclass any specific framework type, * but it has to have is a constructor which receives a [Sandbox] * object as single parameter when the module is started by the [Core]. * * Also two methods are required: * * 1. a _stop_ method without any parameters * 2. a _start_ method which will receive the options passed to the [Core]s [start], * [startMany] or [startAll] method. * * __example__ * * class MyModuleClass { * Sandbox _sandbox; * * MyModuleClass(Sandbox this._sandbox); * * start(options) { * //do something * } * * stop() { * //remove HtmlElements, release ressources... * } * } */ register(module, [String moduleName]) { final uniqueModuleName = moduleName != null ? moduleName : module.runtimeType.toString(); final Symbol uniqueModuleIdentifier = new Symbol(uniqueModuleName); if (_registeredModules.containsKey(uniqueModuleName)) { throw new StateError("Module ${moduleName} already registered!"); } final ClassMirror mirror = reflect(module).type; _registeredModules[uniqueModuleIdentifier] = mirror; } /** * Remove the registered module from the [Core]. */ unregister(String moduleName) { final Symbol uniqueModuleIdentifier = new Symbol(moduleName); if (!_registeredModules.containsKey(uniqueModuleIdentifier)) { throw new StateError("Module ${moduleName} not registered!"); } _registeredModules.remove(uniqueModuleIdentifier); } /** * Start a module. By default the modules name is used as its Id. * You can provide a custom Id by passing as string as optional second argument. */ start(String moduleName, [String id, options]) { if (moduleName is String) { this._start(new Symbol(moduleName), id, options); } else if (moduleName == null) { for (Symbol name in _registeredModules.keys) { this._start(name, id, options); } } else { throw new ArgumentError("Invalid Type given for argument moduleName"); } } /** * Start all modules whose name are in the List of module names passed as first argument. */ startMany(List moduleNames, [options]) { for (String name in moduleNames) { this._start(new Symbol(name), null, options); } } /** * Start all modules registered in this [Core]. */ startAll([options]) { for (Symbol name in _registeredModules.keys) { this._start(name, null, options); } } _start(Symbol moduleName, String id, options) { if (!_registeredModules.containsKey(moduleName)) { throw new StateError("Module ${moduleName} not registered!"); } final ClassMirror mirror = _registeredModules[moduleName]; final Symbol moduleId = id != null ? new Symbol(id) : moduleName; final Sandbox sandbox = new Sandbox(this.mediator); if (_runningModules.containsKey(moduleId)) { throw new StateError("Module with id #${moduleId} already running!"); } final InstanceMirror moduleInstance = mirror.newInstance(new Symbol(''), [sandbox], null); moduleInstance.invoke(new Symbol("start"), [options]); _runningModules[moduleId] = moduleInstance; } /** * Stop a single, multiple or all modules which are running. * * Can be used in 3 different ways: * * __single module__ * core.stop("myModuleId"); * * __multiple modules__ * core.stop(["myFirstModuleId", "mySecondModuleId"]); * * __all modules__ * core.stop(); */ stop([moduleId]) { if (moduleId is String) { this._stop(new Symbol(moduleId)); } else if (moduleId is List) { for (String id in moduleId) { this._stop(new Symbol(id)); } } else if (moduleId == null) { List<Symbol> ids = []; ///_stop manipulates the _runningModules HashMap, ///thus it'd result in an error calling _stop in an iteration for (Symbol id in _runningModules.keys) { ids.add(id); } ids.forEach((Symbol id) => this._stop(id)); } else { throw new ArgumentError("Invalid Type given for argument moduleId"); } } _stop(Symbol moduleId) { if (!_runningModules.containsKey(moduleId)) { throw new StateError("Module with id #${moduleId} not running!"); } _runningModules.remove(moduleId).invoke(new Symbol("stop"), []); } /** * Check if a module is registered. * * If no argument is provided, a [List] of strings with the names of all * registered modules is returned. */ registered([String moduleName]) { if (moduleName != null) { return _registeredModules.containsKey(new Symbol(moduleName)); } else { return _registeredModules.keys; } } /** * Check if a module is running. * * If no argument is provided, a [List] of strings with the names of all * running modules is returned. */ running([String moduleId = null]) { if (moduleId != null) { return _runningModules.containsKey(new Symbol(moduleId)); } else { return _runningModules.keys; } } }
Methods
dynamic register(module, [String moduleName]) #
Register a new module in the Core by passing an instance of its class as first parameter.
By default the name of the runtimeType is used as the module name. You can provide a custom name for the module by passing it as second argument.
no subclassing required The module class doesn't have to subclass any specific framework type, but it has to have is a constructor which receives a Sandbox object as single parameter when the module is started by the Core.
Also two methods are required:
- a stop method without any parameters
-
a start method which will receive the options passed to the Cores start, startMany or startAll method.
example
class MyModuleClass {
Sandbox _sandbox;
MyModuleClass(Sandbox this._sandbox);
start(options) {
//do something
}
stop() {
//remove HtmlElements, release ressources...
}
}
register(module, [String moduleName]) { final uniqueModuleName = moduleName != null ? moduleName : module.runtimeType.toString(); final Symbol uniqueModuleIdentifier = new Symbol(uniqueModuleName); if (_registeredModules.containsKey(uniqueModuleName)) { throw new StateError("Module ${moduleName} already registered!"); } final ClassMirror mirror = reflect(module).type; _registeredModules[uniqueModuleIdentifier] = mirror; }
dynamic registered([String moduleName]) #
Check if a module is registered.
If no argument is provided, a List of strings with the names of all registered modules is returned.
registered([String moduleName]) { if (moduleName != null) { return _registeredModules.containsKey(new Symbol(moduleName)); } else { return _registeredModules.keys; } }
dynamic running([String moduleId = null]) #
Check if a module is running.
If no argument is provided, a List of strings with the names of all running modules is returned.
running([String moduleId = null]) { if (moduleId != null) { return _runningModules.containsKey(new Symbol(moduleId)); } else { return _runningModules.keys; } }
dynamic start(String moduleName, [String id, options]) #
Start a module. By default the modules name is used as its Id. You can provide a custom Id by passing as string as optional second argument.
start(String moduleName, [String id, options]) { if (moduleName is String) { this._start(new Symbol(moduleName), id, options); } else if (moduleName == null) { for (Symbol name in _registeredModules.keys) { this._start(name, id, options); } } else { throw new ArgumentError("Invalid Type given for argument moduleName"); } }
dynamic startAll([options]) #
Start all modules registered in this Core.
startAll([options]) { for (Symbol name in _registeredModules.keys) { this._start(name, null, options); } }
dynamic startMany(List moduleNames, [options]) #
Start all modules whose name are in the List of module names passed as first argument.
startMany(List moduleNames, [options]) { for (String name in moduleNames) { this._start(new Symbol(name), null, options); } }
dynamic stop([moduleId]) #
Stop a single, multiple or all modules which are running.
Can be used in 3 different ways:
single module
core.stop("myModuleId");
multiple modules
core.stop(["myFirstModuleId", "mySecondModuleId"]);
all modules
core.stop();
stop([moduleId]) { if (moduleId is String) { this._stop(new Symbol(moduleId)); } else if (moduleId is List) { for (String id in moduleId) { this._stop(new Symbol(id)); } } else if (moduleId == null) { List<Symbol> ids = []; ///_stop manipulates the _runningModules HashMap, ///thus it'd result in an error calling _stop in an iteration for (Symbol id in _runningModules.keys) { ids.add(id); } ids.forEach((Symbol id) => this._stop(id)); } else { throw new ArgumentError("Invalid Type given for argument moduleId"); } }
dynamic unregister(String moduleName) #
Remove the registered module from the Core.
unregister(String moduleName) { final Symbol uniqueModuleIdentifier = new Symbol(moduleName); if (!_registeredModules.containsKey(uniqueModuleIdentifier)) { throw new StateError("Module ${moduleName} not registered!"); } _registeredModules.remove(uniqueModuleIdentifier); }