|
| 1 | +# ControllerProvider — Design Notes |
| 2 | + |
| 3 | +## Purpose |
| 4 | + |
| 5 | +`ControllerProvider` implements AngularTS’s `$controller` mechanism. It is responsible for: |
| 6 | + |
| 7 | +- registering controller definitions during configuration |
| 8 | +- resolving controller expressions at runtime |
| 9 | +- instantiating controllers with DI support |
| 10 | +- supporting `ctrl as` syntax and explicit identifiers |
| 11 | +- optionally deferring instantiation (`later` mode) |
| 12 | + |
| 13 | +The design closely mirrors AngularJS semantics while tightening types and removing legacy behaviors (e.g. global/window lookup). |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## Core Concepts |
| 18 | + |
| 19 | +### Controller Definition |
| 20 | + |
| 21 | +A **controller definition** is normalized at registration time into an: |
| 22 | + |
| 23 | +``` |
| 24 | +Injectable<ControllerConstructor> |
| 25 | +``` |
| 26 | + |
| 27 | +Supported forms: |
| 28 | +- constructor function |
| 29 | +- ES class |
| 30 | +- DI-annotated array (`['dep1', ..., ControllerFn]`) |
| 31 | + |
| 32 | +Unsupported: |
| 33 | +- plain objects |
| 34 | +- non-callables |
| 35 | + |
| 36 | +This invariant is enforced by `normalizeControllerDef()` and relied on everywhere else. |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +## Internal State |
| 41 | + |
| 42 | +### `controllers: Map<string, InjectableController>` |
| 43 | + |
| 44 | +- Keys are **literal controller names** |
| 45 | +- Values are **normalized injectables** |
| 46 | +- No fallback to `window` or dotted-path resolution |
| 47 | +- Dotted names (`"a.b.Ctrl"`) are treated as plain keys |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## Registration Phase |
| 52 | + |
| 53 | +### `register(name, constructor)` |
| 54 | + |
| 55 | +Supports: |
| 56 | +- single controller registration |
| 57 | +- bulk registration |
| 58 | + |
| 59 | +Behavior: |
| 60 | +- controller names are validated |
| 61 | +- definitions are normalized eagerly |
| 62 | +- invalid definitions throw `ctrlreg` |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +## Runtime: `$controller` Service |
| 67 | + |
| 68 | +### Signature (conceptual) |
| 69 | + |
| 70 | +``` |
| 71 | +$controller(expression, locals?, later?, ident?) |
| 72 | + → instance | () => instance |
| 73 | +``` |
| 74 | + |
| 75 | +### Expression Resolution |
| 76 | + |
| 77 | +If `expression` is a **string**: |
| 78 | +- must match `CNTRL_REG` |
| 79 | +- extracts controller name and optional `as` identifier |
| 80 | +- resolves only from the internal registry |
| 81 | +- throws `ctrlfmt` or `ctrlreg` on failure |
| 82 | + |
| 83 | +If `expression` is **not a string**: |
| 84 | +- treated as a controller injectable directly |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +## Controller Unwrapping |
| 89 | + |
| 90 | +### `unwrapController(injectable)` |
| 91 | + |
| 92 | +Central helper that: |
| 93 | +- extracts the underlying constructor function |
| 94 | +- asserts callability |
| 95 | +- exposes: |
| 96 | + - `func` |
| 97 | + - `prototype` |
| 98 | + - `name` |
| 99 | + |
| 100 | +This removes duplicated logic and provides a single point of truth. |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +## Instantiation Modes |
| 105 | + |
| 106 | +### Normal Mode |
| 107 | + |
| 108 | +- controller instantiated immediately via `$injector.instantiate` |
| 109 | +- object/function return values replace the instance |
| 110 | +- primitive return values are ignored |
| 111 | +- identifier export happens after instantiation |
| 112 | + |
| 113 | +### Deferred Mode (`later === true`) |
| 114 | + |
| 115 | +- instance shell created via `Object.create(prototype)` |
| 116 | +- identifier export happens before invocation |
| 117 | +- returned function invokes the controller and may replace the instance |
| 118 | +- identifier is rebound if replacement occurs |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +## Identifier (`ctrl as`) Handling |
| 123 | + |
| 124 | +Sources of identifier (priority order): |
| 125 | +1. explicit `ident` argument |
| 126 | +2. `as` syntax in controller string |
| 127 | + |
| 128 | +Rules: |
| 129 | +- identifier requires `locals.$scope` |
| 130 | +- exports instance to scope and sets `$controllerIdentifier` |
| 131 | +- missing `$scope` throws `noscp` |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +## Locals Handling |
| 136 | + |
| 137 | +- `locals` is a DI locals map |
| 138 | +- `$scope` is optional unless exporting |
| 139 | +- locals override injectable dependencies |
| 140 | +- no implicit scope creation |
| 141 | + |
| 142 | +--- |
| 143 | + |
| 144 | +## `$scopename` Propagation |
| 145 | + |
| 146 | +If a controller defines `$scopename`, it is propagated to `locals.$scope` |
| 147 | +in both normal and deferred modes when a scope is present. |
| 148 | + |
| 149 | +--- |
| 150 | + |
| 151 | +## Error Semantics |
| 152 | + |
| 153 | +| Code | Condition | |
| 154 | +|----------|-----------| |
| 155 | +| ctrlfmt | malformed controller string | |
| 156 | +| ctrlreg | unknown controller or invalid definition | |
| 157 | +| noscp | identifier export without `$scope` | |
| 158 | +| badname | illegal controller registration name | |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +## Design Invariants |
| 163 | + |
| 164 | +- controllers are always normalized |
| 165 | +- no global/window lookup |
| 166 | +- deferred mode preserves prototype semantics |
| 167 | +- identifier export is explicit and scoped |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## Non-Goals |
| 172 | + |
| 173 | +- no object-literal controllers |
| 174 | +- no dotted-path resolution |
| 175 | +- no implicit scope creation |
| 176 | +- no silent fallbacks |
| 177 | + |
| 178 | +--- |
| 179 | + |
| 180 | +## Summary |
| 181 | + |
| 182 | +`ControllerProvider` favors explicitness and predictability: |
| 183 | +- strict registration |
| 184 | +- centralized normalization |
| 185 | +- well-defined error cases |
| 186 | +- compatibility where it matters, simplicity everywhere else |
0 commit comments