-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ContextService #40554
Closed
Closed
Add ContextService #40554
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
.../development/core/public/kibana-plugin-public.contextcontainer.createcontext.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-public](./kibana-plugin-public.md) > [ContextContainer](./kibana-plugin-public.contextcontainer.md) > [createContext](./kibana-plugin-public.contextcontainer.createcontext.md) | ||
|
||
## ContextContainer.createContext() method | ||
|
||
Create a new context. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
createContext(plugin: string, baseContext: Partial<TContext>, ...contextArgs: TProviderParameters): Promise<TContext>; | ||
``` | ||
|
||
## Parameters | ||
|
||
| Parameter | Type | Description | | ||
| --- | --- | --- | | ||
| plugin | <code>string</code> | The plugin the context will be exposed to. | | ||
| baseContext | <code>Partial<TContext></code> | The initial context for the given handler. | | ||
| contextArgs | <code>TProviderParameters</code> | Additional parameters to call providers with. | | ||
|
||
<b>Returns:</b> | ||
|
||
`Promise<TContext>` | ||
|
||
A Promise for the new context object. | ||
|
27 changes: 27 additions & 0 deletions
27
docs/development/core/public/kibana-plugin-public.contextcontainer.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-public](./kibana-plugin-public.md) > [ContextContainer](./kibana-plugin-public.contextcontainer.md) | ||
|
||
## ContextContainer interface | ||
|
||
An object that handles registration of context providers and building of new context objects. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export interface ContextContainer<TContext extends {}, TProviderParameters extends any[] = []> | ||
``` | ||
|
||
## Methods | ||
|
||
| Method | Description | | ||
| --- | --- | | ||
| [createContext(plugin, baseContext, contextArgs)](./kibana-plugin-public.contextcontainer.createcontext.md) | Create a new context. | | ||
| [register(contextName, provider, plugin)](./kibana-plugin-public.contextcontainer.register.md) | Register a new context provider. Throws an exception if more than one provider is registered for the same context key. | | ||
|
||
## Remarks | ||
|
||
A `ContextContainer` can be used by any Core service or plugin (known as the "service owner") which wishes to expose APIs in a handler function. The container object will manage registering context providers and building a context object for a handler with all of the contexts that should be exposed to the handler's plugin. This is dependent on the dependencies that the handler's plugin declares. | ||
|
||
Contexts providers are executed in the order they were registered. Each provider gets access to context values provided by any plugins that it depends on. | ||
|
28 changes: 28 additions & 0 deletions
28
docs/development/core/public/kibana-plugin-public.contextcontainer.register.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-public](./kibana-plugin-public.md) > [ContextContainer](./kibana-plugin-public.contextcontainer.md) > [register](./kibana-plugin-public.contextcontainer.register.md) | ||
|
||
## ContextContainer.register() method | ||
|
||
Register a new context provider. Throws an exception if more than one provider is registered for the same context key. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
register<TContextName extends keyof TContext>(contextName: TContextName, provider: ContextProvider<TContext, TContextName, TProviderParameters>, plugin?: string): this; | ||
``` | ||
|
||
## Parameters | ||
|
||
| Parameter | Type | Description | | ||
| --- | --- | --- | | ||
| contextName | <code>TContextName</code> | The key of the <code>TContext</code> object this provider supplies the value for. | | ||
| provider | <code>ContextProvider<TContext, TContextName, TProviderParameters></code> | A [ContextProvider](./kibana-plugin-public.contextprovider.md) to be called each time a new context is created. | | ||
| plugin | <code>string</code> | The plugin this provider is associated with. If <code>undefined</code>, provider gets access to all provided context keys. Only the service owner should be able to call with <code>undefined</code>. | | ||
|
||
<b>Returns:</b> | ||
|
||
`this` | ||
|
||
The `ContextContainer` for method chaining. | ||
|
18 changes: 18 additions & 0 deletions
18
docs/development/core/public/kibana-plugin-public.contextprovider.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-public](./kibana-plugin-public.md) > [ContextProvider](./kibana-plugin-public.contextprovider.md) | ||
|
||
## ContextProvider type | ||
|
||
A function that returns a context value for a specific key of given context type. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export declare type ContextProvider<TContext extends {}, TContextName extends keyof TContext, TProviderParameters extends any[] = []> = (context: Partial<TContext>, ...rest: TProviderParameters) => Promise<TContext[TContextName]> | TContext[TContextName]; | ||
``` | ||
|
||
## Remarks | ||
|
||
This function will be called each time a new context is built for a handler invocation. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { PluginName } from '../../server'; | ||
import { ContextContainerImplementation } from './context'; | ||
|
||
const plugins: ReadonlyMap<PluginName, PluginName[]> = new Map([ | ||
['pluginA', []], | ||
['pluginB', ['pluginA']], | ||
['pluginC', ['pluginA', 'pluginB']], | ||
['pluginD', []], | ||
]); | ||
|
||
interface MyContext { | ||
core1: string; | ||
core2: number; | ||
ctxFromA: string; | ||
ctxFromB: number; | ||
ctxFromC: boolean; | ||
ctxFromD: object; | ||
baseCtx: number; | ||
} | ||
|
||
describe('ContextContainer', () => { | ||
it('does not allow the same context to be registered twice', () => { | ||
const contextContainer = new ContextContainerImplementation<MyContext>(plugins); | ||
contextContainer.register('ctxFromA', () => 'aString', 'pluginA'); | ||
|
||
expect(() => | ||
contextContainer.register('ctxFromA', () => 'aString', 'pluginA') | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"Context provider for ctxFromA has already been registered."` | ||
); | ||
}); | ||
|
||
it('resolves dependencies', async () => { | ||
// expect.assertions(3); | ||
const contextContainer = new ContextContainerImplementation<MyContext>(plugins); | ||
|
||
contextContainer | ||
.register('core1', context => { | ||
expect(context).toEqual({}); | ||
return 'core'; | ||
}) | ||
.register( | ||
'ctxFromA', | ||
context => { | ||
expect(context).toEqual({ core1: 'core' }); | ||
return 'aString'; | ||
}, | ||
'pluginA' | ||
) | ||
.register( | ||
'ctxFromB', | ||
context => { | ||
expect(context).toEqual({ core1: 'core', ctxFromA: 'aString' }); | ||
return 299; | ||
}, | ||
'pluginB' | ||
) | ||
.register( | ||
'ctxFromC', | ||
context => { | ||
expect(context).toEqual({ core1: 'core', ctxFromA: 'aString', ctxFromB: 299 }); | ||
return false; | ||
}, | ||
'pluginC' | ||
) | ||
.register( | ||
'ctxFromD', | ||
context => { | ||
expect(context).toEqual({ core1: 'core' }); | ||
return {}; | ||
}, | ||
'pluginD' | ||
); | ||
|
||
// Should have context from pluginC, its deps, and core | ||
expect(await contextContainer.createContext('pluginC')).toEqual({ | ||
core1: 'core', | ||
ctxFromA: 'aString', | ||
ctxFromB: 299, | ||
ctxFromC: false, | ||
}); | ||
|
||
// Should have context from pluginD, and core | ||
expect(await contextContainer.createContext('pluginD')).toEqual({ | ||
core1: 'core', | ||
ctxFromD: {}, | ||
}); | ||
}); | ||
|
||
it('exposes all previously registered context to Core providers', async () => { | ||
expect.assertions(3); | ||
const contextContainer = new ContextContainerImplementation<MyContext>(plugins); | ||
|
||
contextContainer | ||
.register('core1', context => { | ||
expect(context).toEqual({}); | ||
return 'core'; | ||
}) | ||
.register('core2', context => { | ||
expect(context).toEqual({ core1: 'core' }); | ||
return 101; | ||
}); | ||
|
||
// If no context is registered for pluginA, only core contexts should be exposed | ||
expect(await contextContainer.createContext('pluginA')).toEqual({ | ||
core1: 'core', | ||
core2: 101, | ||
}); | ||
}); | ||
|
||
it('passes additional arguments to providers', async () => { | ||
expect.assertions(5); | ||
const contextContainer = new ContextContainerImplementation<MyContext, [string, number]>( | ||
plugins | ||
); | ||
|
||
contextContainer | ||
.register('core1', (context, str, num) => { | ||
expect(str).toEqual('passed string'); | ||
expect(num).toEqual(77); | ||
return `core ${str}`; | ||
}) | ||
.register('ctxFromD', (context, str, num) => { | ||
expect(str).toEqual('passed string'); | ||
expect(num).toEqual(77); | ||
return { | ||
num: 77, | ||
}; | ||
}); | ||
|
||
expect(await contextContainer.createContext('pluginD', {}, 'passed string', 77)).toEqual({ | ||
core1: 'core passed string', | ||
ctxFromD: { | ||
num: 77, | ||
}, | ||
}); | ||
}); | ||
|
||
it('passes baseContext to context providers', async () => { | ||
expect.assertions(3); | ||
const contextContainer = new ContextContainerImplementation<MyContext>(plugins); | ||
|
||
contextContainer | ||
.register('core1', context => { | ||
expect(context.baseCtx).toEqual(1234); | ||
return 'core'; | ||
}) | ||
.register('ctxFromD', context => { | ||
expect(context.baseCtx).toEqual(1234); | ||
return {}; | ||
}); | ||
|
||
expect(await contextContainer.createContext('pluginD', { baseCtx: 1234 })).toEqual({ | ||
core1: 'core', | ||
baseCtx: 1234, | ||
ctxFromD: {}, | ||
}); | ||
}); | ||
|
||
it('throws error for unknown plugin', async () => { | ||
const contextContainer = new ContextContainerImplementation<MyContext>(plugins); | ||
await expect( | ||
contextContainer.createContext('otherPlugin') | ||
).rejects.toThrowErrorMatchingInlineSnapshot( | ||
`"Cannot create context for unknown plugin: otherPlugin"` | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we add a test to check an exception, raised during
register
phase, bubbles to the caller?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you mean? Do you mean that we move / duplicate this test to ContextService?