* Add a binding to the context. If a locked binding already exists with the * same key, an error will be thrown. * @param binding - The configured binding to be added
(binding: Binding<unknown>)
| 229 | * @param binding - The configured binding to be added |
| 230 | */ |
| 231 | add(binding: Binding<unknown>): this { |
| 232 | const key = binding.key; |
| 233 | this.debug('[%s] Adding binding: %s', key); |
| 234 | let existingBinding: Binding | undefined; |
| 235 | const keyExists = this.registry.has(key); |
| 236 | if (keyExists) { |
| 237 | existingBinding = this.registry.get(key); |
| 238 | const bindingIsLocked = existingBinding?.isLocked; |
| 239 | if (bindingIsLocked) |
| 240 | throw new Error(`Cannot rebind key "${key}" to a locked binding`); |
| 241 | } |
| 242 | this.registry.set(key, binding); |
| 243 | if (existingBinding !== binding) { |
| 244 | if (existingBinding != null) { |
| 245 | this.emitEvent('unbind', { |
| 246 | binding: existingBinding, |
| 247 | context: this, |
| 248 | type: 'unbind', |
| 249 | }); |
| 250 | } |
| 251 | this.emitEvent('bind', {binding, context: this, type: 'bind'}); |
| 252 | } |
| 253 | return this; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Create a corresponding binding for configuration of the target bound by |