-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
618 additions
and
1 deletion.
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: PluginName, baseContext: Partial<TContext>, ...contextArgs: TProviderParameters): Promise<TContext>; | ||
``` | ||
|
||
## Parameters | ||
|
||
| Parameter | Type | Description | | ||
| --- | --- | --- | | ||
| plugin | <code>PluginName</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. | ||
|
25 changes: 25 additions & 0 deletions
25
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,25 @@ | ||
<!-- 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 excpetion if more than one provider is registered for the same context key. | | ||
|
||
## Remarks | ||
|
||
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 excpetion 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?: PluginName): this; | ||
``` | ||
## Parameters | ||
| Parameter | Type | Description | | ||
| --- | --- | --- | | ||
| contextName | <code>TContextName</code> | The key of the 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>PluginName</code> | The plugin this provider is associated with. If <code>undefined</code>, provider gets access to all provided context keys. | | ||
<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,178 @@ | ||
/* | ||
* 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(8); | ||
const contextContainer = new ContextContainerImplementation<MyContext>(plugins); | ||
|
||
contextContainer | ||
.register('core1', context => { | ||
expect(context).toEqual({}); | ||
return 'core'; | ||
}) | ||
.register( | ||
'ctxFromA', | ||
context => { | ||
expect(context).toEqual({}); | ||
return 'aString'; | ||
}, | ||
'pluginA' | ||
) | ||
.register( | ||
'ctxFromB', | ||
context => { | ||
expect(context).toEqual({ ctxFromA: 'aString' }); | ||
return 299; | ||
}, | ||
'pluginB' | ||
) | ||
.register( | ||
'ctxFromC', | ||
context => { | ||
expect(context).toEqual({ ctxFromA: 'aString', ctxFromB: 299 }); | ||
return false; | ||
}, | ||
'pluginC' | ||
) | ||
.register( | ||
'ctxFromD', | ||
context => { | ||
expect(context).toEqual({}); | ||
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 registerd 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: {}, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.