* Adds a subscriber for the specified property. If the property has not * yet been tracked, the tracking is started. If the used value is * already available, the handler is called immediately. The handler is * only called if a valid used value is available and only if this value * has c
(prop, handler)
| 185 | * @template T |
| 186 | */ |
| 187 | subscribe(prop, handler) { |
| 188 | const used = this.startUsed_(prop); |
| 189 | |
| 190 | if (!pushIfNotExist(used.subscribers, handler)) { |
| 191 | // Already a subscriber. |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | // The handler is notified right away if the value is available. |
| 196 | const existingValue = used.value; |
| 197 | if (isDefined(existingValue) && this.isConnected_()) { |
| 198 | handler(existingValue); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Unsubscribes a previously added handler. If there are no other subscribers |
no test coverage detected