Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This isn't really an answer, since you don't state what's going wrong, but some general advice. Mostly, I'd avoid <code>new Symbol()</code> and mirrors if you can easily avoid them, and in this case you can.</p> <p>First, you should figure out if you want to register module instances or produce instances on demand, you probably don't want both like you are here. If you register an instance, then can't you just reuse that instance? Does <code>start()</code> need to produce new instances as part of its spec? You turn around and try to make sure that an instance isn't already running anyway.</p> <p>If you really do need to produce instances, a simple factory function will eliminate the need for mirrors. So instead of:</p> <pre class="lang-dart prettyprint-override"><code>core.register(new ToDos()); </code></pre> <p>You write:</p> <pre class="lang-dart prettyprint-override"><code>core.register('ToDos', () =&gt; new ToDos()); </code></pre> <p>If you still want to use mirrors, you can clean up the use of <code>new Symbol()</code>. Here's some recommendations:</p> <ul> <li>Don't use Symbols as keys unless you're really getting them from reflective APIs like mirrors and noSuchMethod in the first place. Just use the String name or maybe the runtimeType. In your case you're mixing Symbols and Strings as keys in your <code>_registeredModules</code> map, which is probably causing some bugs, like modules will never appear to be registered. (are you testing in checked mode?)</li> <li>Don't use <code>new Symbol('name')</code>, use <code>const Symbol('name')</code></li> <li><p>Don't use InstanceMirror.invoke, getField, or setField when you can just call the method directly. In your code you can replace </p> <pre><code>moduleInstance.invoke(new Symbol("start"), [options]); </code></pre> <p>with</p> <pre><code>moduleInstance.reflectee.start(options); </code></pre></li> <li>Factories aren't evil. It'd be nice to invoke a constructor from a type instance, but until then registering a factory is pretty lightweight in Dart.</li> </ul> <p>Here's your code with those suggestions:</p> <pre class="lang-dart prettyprint-override"><code>typedef Object Factory(Sandbox sandbox); final Map&lt;Symbol, Factory&gt; _registeredModules = new Map&lt;Type, Factory&gt;(); register(Type type, Factory factory) { if (_registeredModules.containsKey(type)) { throw new StateError("Module $type already registered!"); } _registeredModules[type] = factory; } start(Type type, options) { if (!_registeredModules.containsKey(type)) { throw new StateError("Module $type not registered!"); } if (_runningModules.containsKey(type)) { throw new StateError("Module $type already running!"); } Sandbox sandbox = new Sandbox(this.mediator); var module = _runningModules[type](sandbox)..start(options); _runningModules[type] = module; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload