Dart DocumentationdartscaleCore

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 named argument.
  */
 start(String moduleName, {String id: null, options:null}) {
   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.
  * 
  * This will use the module names as their Ids by default.
  */
 startMany(List moduleNames, [options]) {
   for (String name in moduleNames) {
     this._start(new Symbol(name), null, options);
   }
 }
 
 /**
  * Start all modules registered in this [Core].
  * 
  * This will use the module names as their Ids by default.
  */
 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 the currently running module which Id is passed as argument.
  */
 stop(moduleId) {
   this._stop(new Symbol(moduleId));
 }
 
 /**
  * Stop the modules which Id are in the [List] of Ids passed as argument.
  */
 stopMany(List moduleIds) {
   for (String id in moduleIds) {
     this._stop(new Symbol(id));
   }
 }
 
 /**
  * Stop all currently running modules.
  */
 stopAll() {
   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));
 }
 
 _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;
   }
 }
}

Properties

final Mediator mediator #

final Mediator mediator = new Mediator()

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:

  1. a stop method without any parameters
  2. 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: null, options: null}) #

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 named argument.

start(String moduleName, {String id: null, options:null}) {
 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.

This will use the module names as their Ids by default.

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.

This will use the module names as their Ids by default.

startMany(List moduleNames, [options]) {
 for (String name in moduleNames) {
   this._start(new Symbol(name), null, options);
 }
}

dynamic stop(moduleId) #

Stop the currently running module which Id is passed as argument.

stop(moduleId) {
 this._stop(new Symbol(moduleId));
}

dynamic stopAll() #

Stop all currently running modules.

stopAll() {
 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));
}

dynamic stopMany(List moduleIds) #

Stop the modules which Id are in the List of Ids passed as argument.

stopMany(List moduleIds) {
 for (String id in moduleIds) {
   this._stop(new Symbol(id));
 }
}

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);
}