Skip to content

Commit

Permalink
use sync getter for config on client side instead of observable
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Nov 19, 2019
1 parent bd1e3bc commit 1fe445f
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 28 deletions.
14 changes: 6 additions & 8 deletions src/core/MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -1261,23 +1261,21 @@ export const config: PluginConfigDescriptor<ConfigType> = {
exposeToBrowser: ['uiProp'],
schema: configSchema,
};
export type ClientConfigType = Pick<ConfigType, 'uiProp'>
```

Configuration containing only the exposed properties will be then available on the client-side plugin using the same API as the server-side:
Configuration containing only the exposed properties will be then available on the client-side using the plugin's `initializerContext`:
```typescript
// my_plugin/public/index.ts
import { ClientConfigType } from '../server';
interface ClientConfigType {
uiProp: string;
}
export class Plugin implements Plugin<PluginSetup, PluginStart> {
constructor(private readonly initializerContext: PluginInitializerContext) {}
public async setup(core: CoreSetup, deps: {}) {
const config = await this.initializerContext.config
.create<ClientConfigType>()
.pipe(take(1))
.toPromise();
const config = this.initializerContext.config.get<ClientConfigType>();
// ...
}
```
Expand Down
3 changes: 1 addition & 2 deletions src/core/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* specific language governing permissions and limitations
* under the License.
*/
import { of } from 'rxjs';
import { applicationServiceMock } from './application/application_service.mock';
import { chromeServiceMock } from './chrome/chrome_service.mock';
import { CoreContext, CoreSetup, CoreStart, PluginInitializerContext, NotificationsSetup } from '.';
Expand Down Expand Up @@ -94,7 +93,7 @@ function pluginInitializerContextMock() {
},
},
config: {
create: <T>() => of({} as T),
get: <T>() => ({} as T),
},
};

Expand Down
7 changes: 3 additions & 4 deletions src/core/public/plugins/plugin_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

import { omit } from 'lodash';
import { Observable, of } from 'rxjs';
import { DiscoveredPlugin } from '../../server';
import { PluginOpaqueId, PackageInfo, EnvironmentMode } from '../../server/types';
import { CoreContext } from '../core_system';
Expand All @@ -41,7 +40,7 @@ export interface PluginInitializerContext<ConfigSchema = unknown> {
packageInfo: Readonly<PackageInfo>;
};
readonly config: {
create: <T = ConfigSchema>() => Observable<T>;
get: <T = ConfigSchema>() => T;
};
}

Expand All @@ -67,8 +66,8 @@ export function createPluginInitializerContext(
opaqueId,
env: coreContext.env,
config: {
create<T>() {
return of<T>((pluginConfig as unknown) as T);
get<T>() {
return (pluginConfig as unknown) as T;
},
},
};
Expand Down
6 changes: 1 addition & 5 deletions src/core/public/plugins/plugins_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import { CoreSetup, CoreStart, PluginInitializerContext } from '..';
import { docLinksServiceMock } from '../doc_links/doc_links_service.mock';
import { savedObjectsMock } from '../saved_objects/saved_objects_service.mock';
import { contextServiceMock } from '../context/context_service.mock';
import { take } from 'rxjs/operators';

export let mockPluginInitializers: Map<PluginName, MockedPluginInitializer>;

Expand Down Expand Up @@ -229,10 +228,7 @@ describe('PluginsService', () => {

const initializerContext = mockPluginInitializers.get('pluginA')!.mock
.calls[0][0] as PluginInitializerContext;
const config = await initializerContext.config
.create()
.pipe(take(1))
.toPromise();
const config = initializerContext.config.get();
expect(config).toMatchObject(pluginConfig);
});

Expand Down
6 changes: 1 addition & 5 deletions src/plugins/testbed/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/

import { take } from 'rxjs/operators';
import { Plugin, CoreSetup, PluginInitializerContext } from 'kibana/public';

interface ConfigType {
Expand All @@ -28,10 +27,7 @@ export class TestbedPlugin implements Plugin<TestbedPluginSetup, TestbedPluginSt
constructor(private readonly initializerContext: PluginInitializerContext) {}

public async setup(core: CoreSetup, deps: {}) {
const config = await this.initializerContext.config
.create<ConfigType>()
.pipe(take(1))
.toPromise();
const config = this.initializerContext.config.get<ConfigType>();

// eslint-disable-next-line no-console
console.log(`Testbed plugin set up. uiProp: '${config.uiProp}'`);
Expand Down
3 changes: 1 addition & 2 deletions x-pack/legacy/plugins/siem/public/apps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { of } from 'rxjs';
import chrome from 'ui/chrome';
import { npStart } from 'ui/new_platform';
import { Plugin } from './plugin';

new Plugin(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{ opaqueId: Symbol('siem'), env: {} as any, config: { create: () => of({} as any) } },
{ opaqueId: Symbol('siem'), env: {} as any, config: { get: () => ({} as any) } },
chrome
).start(npStart.core, npStart.plugins);
3 changes: 1 addition & 2 deletions x-pack/legacy/plugins/uptime/public/apps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { of } from 'rxjs';
import chrome from 'ui/chrome';
import { npStart } from 'ui/new_platform';
import { Plugin } from './plugin';

new Plugin(
{ opaqueId: Symbol('uptime'), env: {} as any, config: { create: () => of({} as any) } },
{ opaqueId: Symbol('uptime'), env: {} as any, config: { get: () => ({} as any) } },
chrome
).start(npStart);

0 comments on commit 1fe445f

Please sign in to comment.