@@ -116,7 +116,11 @@ export interface GlobalContexts {
116
116
* Adds conditional or primitive global contexts
117
117
* @param contexts - An Array of either Conditional Contexts or Primitive Contexts
118
118
*/
119
- addGlobalContexts ( contexts : Array < ConditionalContextProvider | ContextPrimitive > ) : void ;
119
+ addGlobalContexts (
120
+ contexts :
121
+ | Array < ConditionalContextProvider | ContextPrimitive >
122
+ | Record < string , ConditionalContextProvider | ContextPrimitive >
123
+ ) : void ;
120
124
121
125
/**
122
126
* Removes all global contexts
@@ -127,7 +131,7 @@ export interface GlobalContexts {
127
131
* Removes previously added global context, performs a deep comparison of the contexts or conditional contexts
128
132
* @param contexts - An Array of either Condition Contexts or Primitive Contexts
129
133
*/
130
- removeGlobalContexts ( contexts : Array < ConditionalContextProvider | ContextPrimitive > ) : void ;
134
+ removeGlobalContexts ( contexts : Array < ConditionalContextProvider | ContextPrimitive | string > ) : void ;
131
135
132
136
/**
133
137
* Returns all applicable global contexts for a specified event
@@ -142,6 +146,8 @@ export interface GlobalContexts {
142
146
export function globalContexts ( ) : GlobalContexts {
143
147
let globalPrimitives : Array < ContextPrimitive > = [ ] ;
144
148
let conditionalProviders : Array < ConditionalContextProvider > = [ ] ;
149
+ let namedPrimitives : Record < string , ContextPrimitive > = { } ;
150
+ let namedConditionalProviders : Record < string , ConditionalContextProvider > = { } ;
145
151
146
152
/**
147
153
* Returns all applicable global contexts for a specified event
@@ -152,46 +158,75 @@ export function globalContexts(): GlobalContexts {
152
158
const eventSchema = getUsefulSchema ( event ) ;
153
159
const eventType = getEventType ( event ) ;
154
160
const contexts : Array < SelfDescribingJson > = [ ] ;
155
- const generatedPrimitives = generatePrimitives ( globalPrimitives , event , eventType , eventSchema ) ;
161
+ const generatedPrimitives = generatePrimitives (
162
+ globalPrimitives . concat ( Object . values ( namedPrimitives ) ) ,
163
+ event ,
164
+ eventType ,
165
+ eventSchema
166
+ ) ;
156
167
contexts . push ( ...generatedPrimitives ) ;
157
168
158
- const generatedConditionals = generateConditionals ( conditionalProviders , event , eventType , eventSchema ) ;
169
+ const generatedConditionals = generateConditionals (
170
+ conditionalProviders . concat ( Object . values ( namedConditionalProviders ) ) ,
171
+ event ,
172
+ eventType ,
173
+ eventSchema
174
+ ) ;
159
175
contexts . push ( ...generatedConditionals ) ;
160
176
161
177
return contexts ;
162
178
} ;
163
179
164
180
return {
165
181
getGlobalPrimitives ( ) : Array < ContextPrimitive > {
166
- return globalPrimitives ;
182
+ return globalPrimitives . concat ( Object . values ( namedPrimitives ) ) ;
167
183
} ,
168
184
169
185
getConditionalProviders ( ) : Array < ConditionalContextProvider > {
170
- return conditionalProviders ;
186
+ return conditionalProviders . concat ( Object . values ( namedConditionalProviders ) ) ;
171
187
} ,
172
188
173
- addGlobalContexts ( contexts : Array < ConditionalContextProvider | ContextPrimitive > ) : void {
174
- const acceptedConditionalContexts : ConditionalContextProvider [ ] = [ ] ;
175
- const acceptedContextPrimitives : ContextPrimitive [ ] = [ ] ;
176
- for ( const context of contexts ) {
177
- if ( isConditionalContextProvider ( context ) ) {
178
- acceptedConditionalContexts . push ( context ) ;
179
- } else if ( isContextPrimitive ( context ) ) {
180
- acceptedContextPrimitives . push ( context ) ;
189
+ addGlobalContexts (
190
+ contexts :
191
+ | Array < ConditionalContextProvider | ContextPrimitive >
192
+ | Record < string , ConditionalContextProvider | ContextPrimitive >
193
+ ) : void {
194
+ if ( Array . isArray ( contexts ) ) {
195
+ const acceptedConditionalContexts : ConditionalContextProvider [ ] = [ ] ;
196
+ const acceptedContextPrimitives : ContextPrimitive [ ] = [ ] ;
197
+ for ( const context of contexts ) {
198
+ if ( isConditionalContextProvider ( context ) ) {
199
+ acceptedConditionalContexts . push ( context ) ;
200
+ } else if ( isContextPrimitive ( context ) ) {
201
+ acceptedContextPrimitives . push ( context ) ;
202
+ }
203
+ }
204
+ globalPrimitives = globalPrimitives . concat ( acceptedContextPrimitives ) ;
205
+ conditionalProviders = conditionalProviders . concat ( acceptedConditionalContexts ) ;
206
+ } else {
207
+ for ( const [ name , context ] of Object . entries ( contexts ) ) {
208
+ if ( isConditionalContextProvider ( context ) ) {
209
+ namedConditionalProviders [ name ] = context ;
210
+ } else if ( isContextPrimitive ( context ) ) {
211
+ namedPrimitives [ name ] = context ;
212
+ }
181
213
}
182
214
}
183
- globalPrimitives = globalPrimitives . concat ( acceptedContextPrimitives ) ;
184
- conditionalProviders = conditionalProviders . concat ( acceptedConditionalContexts ) ;
185
215
} ,
186
216
187
217
clearGlobalContexts ( ) : void {
188
218
conditionalProviders = [ ] ;
189
219
globalPrimitives = [ ] ;
220
+ namedConditionalProviders = { } ;
221
+ namedPrimitives = { } ;
190
222
} ,
191
223
192
- removeGlobalContexts ( contexts : Array < ConditionalContextProvider | ContextPrimitive > ) : void {
224
+ removeGlobalContexts ( contexts : Array < ConditionalContextProvider | ContextPrimitive | string > ) : void {
193
225
for ( const context of contexts ) {
194
- if ( isConditionalContextProvider ( context ) ) {
226
+ if ( typeof context === 'string' ) {
227
+ delete namedConditionalProviders [ context ] ;
228
+ delete namedPrimitives [ context ] ;
229
+ } else if ( isConditionalContextProvider ( context ) ) {
195
230
conditionalProviders = conditionalProviders . filter (
196
231
( item ) => JSON . stringify ( item ) !== JSON . stringify ( context )
197
232
) ;
0 commit comments