File tree Expand file tree Collapse file tree 2 files changed +20
-5
lines changed Expand file tree Collapse file tree 2 files changed +20
-5
lines changed Original file line number Diff line number Diff line change @@ -42,25 +42,33 @@ export class Context {
42
42
* Create a binding with the given key in the context. If a locked binding
43
43
* already exists with the same key, an error will be thrown.
44
44
*
45
- * @param key Binding key
45
+ * @param keyOrBinding Binding key or a binding
46
46
*/
47
47
bind < ValueType = BoundValue > (
48
- key : BindingAddress < ValueType > ,
48
+ keyOrBinding : BindingAddress < ValueType > | Binding ,
49
49
) : Binding < ValueType > {
50
+ let key : string ;
51
+ let binding : Binding < ValueType > ;
52
+ if ( keyOrBinding instanceof Binding ) {
53
+ key = keyOrBinding . key ;
54
+ binding = keyOrBinding ;
55
+ } else {
56
+ key = keyOrBinding . toString ( ) ;
57
+ binding = new Binding < ValueType > ( key ) ;
58
+ }
59
+
50
60
/* istanbul ignore if */
51
61
if ( debug . enabled ) {
52
62
debug ( 'Adding binding: %s' , key ) ;
53
63
}
54
- key = BindingKey . validate ( key ) ;
64
+
55
65
const keyExists = this . registry . has ( key ) ;
56
66
if ( keyExists ) {
57
67
const existingBinding = this . registry . get ( key ) ;
58
68
const bindingIsLocked = existingBinding && existingBinding . isLocked ;
59
69
if ( bindingIsLocked )
60
70
throw new Error ( `Cannot rebind key "${ key } " to a locked binding` ) ;
61
71
}
62
-
63
- const binding = new Binding < ValueType > ( key ) ;
64
72
this . registry . set ( key , binding ) ;
65
73
return binding ;
66
74
}
Original file line number Diff line number Diff line change @@ -71,6 +71,13 @@ describe('Context', () => {
71
71
expect ( result ) . to . be . true ( ) ;
72
72
} ) ;
73
73
74
+ it ( 'accepts a binding' , ( ) => {
75
+ const binding = new Binding ( 'foo' ) . to ( 'bar' ) ;
76
+ expect ( ctx . bind ( binding ) ) . to . be . exactly ( binding ) ;
77
+ const result = ctx . contains ( 'foo' ) ;
78
+ expect ( result ) . to . be . true ( ) ;
79
+ } ) ;
80
+
74
81
it ( 'returns a binding' , ( ) => {
75
82
const binding = ctx . bind ( 'foo' ) ;
76
83
expect ( binding ) . to . be . instanceOf ( Binding ) ;
You can’t perform that action at this time.
0 commit comments