File tree Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,17 @@ describe('ContextView', () => {
45
45
expect ( await taggedAsFoo . asGetter ( ) ( ) ) . to . eql ( [ 'BAR' , 'FOO' ] ) ;
46
46
} ) ;
47
47
48
+ it ( 'reports error on singleValue() if multiple values exist' , async ( ) => {
49
+ return expect ( taggedAsFoo . singleValue ( ) ) . to . be . rejectedWith (
50
+ / T h e C o n t e x t V i e w h a s m o r e t h a n o n e v a l u e \. U s e v a l u e s \( \) t o a c c e s s t h e m \. / ,
51
+ ) ;
52
+ } ) ;
53
+
54
+ it ( 'supports singleValue() if only one value exist' , async ( ) => {
55
+ server . unbind ( 'bar' ) ;
56
+ expect ( await taggedAsFoo . singleValue ( ) ) . to . eql ( 'FOO' ) ;
57
+ } ) ;
58
+
48
59
it ( 'reloads bindings after refresh' , async ( ) => {
49
60
taggedAsFoo . refresh ( ) ;
50
61
const abcBinding = server
Original file line number Diff line number Diff line change @@ -157,6 +157,18 @@ export class ContextView<T = unknown> extends EventEmitter
157
157
asGetter ( session ?: ResolutionSession ) : Getter < T [ ] > {
158
158
return ( ) => this . values ( session ) ;
159
159
}
160
+
161
+ /**
162
+ * Get the single value
163
+ */
164
+ async singleValue ( session ?: ResolutionSession ) : Promise < T | undefined > {
165
+ const values = await this . values ( session ) ;
166
+ if ( values . length === 0 ) return undefined ;
167
+ if ( values . length === 1 ) return values [ 0 ] ;
168
+ throw new Error (
169
+ 'The ContextView has more than one value. Use values() to access them.' ,
170
+ ) ;
171
+ }
160
172
}
161
173
162
174
/**
You can’t perform that action at this time.
0 commit comments