feat(di): a read-only events token, and framework names nobody can take
Three things that turned out to be one decision.
## `@needs('events')`
`app.bus` is public and `Bus` is an exported type, but the bus was not a graph token, so
`@needs('bus')` failed at boot. There is a good reason for the difference — putting the
`Bus` in the graph hands `emit` to every node and turns a one-way observation channel into
something anything can forge events on — and it was written down nowhere, inferable only
by reading `provideBuiltins` and noticing what is absent.
The read-only half is a token now: `events`, bound to `{ on }`, the same narrowing plugins
already get. `@needs('events')` works the way `@needs('logger')` does, which is the
symmetry a reader expects, and `emit` still belongs to nobody.
A plugin remains the right home for observation and the docs say so, because it gets
`on` and `onShutdown` *together*. The token gives the subscribe half alone: a `@Provider`
that subscribes should release in `dispose()` — `on()` returns its own unsubscribe — and a
`@Step` should not subscribe at all, since it runs per request and would add a listener
each time.
## The boot error teaches it
Documenting it prevents the mistake for a reader; it does nothing for the person typing
`@needs('bus')`, which is who the issue was about. That need now fails with what it should
have used and why, rather than the generic "nothing provides it" plus a nearest-match that
has nothing near enough to suggest.
## Framework names are reserved
**Behaviour change.** Builtins were registered with `if (!runners.has(name))`, so a
provider called `logger` silently replaced the framework's own and every `@needs('logger')`
in the app got something that was not the logger core writes to — a divergence you
discover from a log line that never appeared. `logger`, `rooms`, `events` and `bus` are
reserved now, enforced in `setRunner`, which is the single point modules, plugins and mesh
exports all register through. Taking one is a boot error naming it.
`bus` is reserved without being provided, so `@needs('bus')` cannot resolve to whatever a
user happened to call `bus` and be wrong in silence.
This replaces a test that asserted the old shadowing as a feature.
Closes #67.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
7 files changed
+307
-18
No files matched your search
+12
-1
@@ -14,7 +14,18 @@ export interface ScopeNode {
|
||||
export interface ScopeApi {
|
||||
add(node: ScopeNode): void;
|
||||
}
|
||||
/** The capabilities handed to a plugin: event subscription, node registration, and teardown. */
|
||||
/**
|
||||
* The capabilities handed to a plugin: event subscription, node registration, and teardown.
|
||||
*
|
||||
* This is the home for observation, and the reason is the pairing: `bus.on` arrives next to
|
||||
* `onShutdown`, so whatever a plugin subscribes to it can also release. A graph node can reach the
|
||||
* same read-only slice through `@needs('events')`, but it gets the subscribe half without the
|
||||
* unsubscribe half — and a `@Step` reaching it would register a listener per request.
|
||||
*
|
||||
* The `Bus` itself is deliberately not here and is not a graph token either: handing over `emit`
|
||||
* would turn a one-way observation channel into something any node can forge events on.
|
||||
* `@needs('bus')` fails at boot and says so.
|
||||
*/
|
||||
export interface PluginApi {
|
||||
bus: { on: Bus['on'] };
|
||||
scope: ScopeApi;
|
||||
|
||||
Reference in new issue
Block a user