Skip to content
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

[3] Create services architecture #23

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/plugins/shared_ux/.storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import { addDecorator } from '@storybook/react';
import { ServicesProvider } from '../public/services';
import { servicesFactory } from '../public/services/storybook';

addDecorator((storyFn) => (
<ServicesProvider {...servicesFactory({})}>{storyFn()}</ServicesProvider>
));
22 changes: 0 additions & 22 deletions src/plugins/shared_ux/public/plugin.ts

This file was deleted.

40 changes: 40 additions & 0 deletions src/plugins/shared_ux/public/plugin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import { CoreSetup, CoreStart, Plugin } from '../../../core/public';
import {
SharedUXPluginSetup,
SharedUXPluginStart,
SharedUXPluginStartDeps,
SharedUXPluginSetupDeps,
} from './types';

import { ServicesProvider } from './services';
import { servicesFactory } from './services/kibana';

export class SharedUXPlugin implements Plugin<SharedUXPluginSetup, SharedUXPluginStart> {
public setup(
_coreSetup: CoreSetup<SharedUXPluginStartDeps, SharedUXPluginStart>,
_setupPlugins: SharedUXPluginSetupDeps
): SharedUXPluginSetup {
return {};
}

public start(coreStart: CoreStart, startPlugins: SharedUXPluginStartDeps): SharedUXPluginStart {
const services = servicesFactory({ coreStart, startPlugins });

return {
ServicesContext: ({ children }) => (
<ServicesProvider {...services}>{children}</ServicesProvider>
),
};
}

public stop() {}
}
27 changes: 27 additions & 0 deletions src/plugins/shared_ux/public/services/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React, { FC, createContext, useContext } from 'react';
import { SharedUXPlatformService } from './platform';
import { servicesFactory } from './stub';

export interface SharedUXServices {
platform: SharedUXPlatformService;
}

const ServicesContext = createContext<SharedUXServices>(servicesFactory());

export const ServicesProvider: FC<SharedUXServices> = ({ children, ...services }) => (
<ServicesContext.Provider value={services}>{children}</ServicesContext.Provider>
);

export function useServices() {
return useContext(ServicesContext);
}

export const usePlatformService = () => useServices().platform;
16 changes: 16 additions & 0 deletions src/plugins/shared_ux/public/services/jest/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { SharedUXServices } from '../.';
import { PluginServiceFactory } from '../types';

export const servicesFactory: PluginServiceFactory<SharedUXServices> = () => ({
platform: {
setIsFullscreen: jest.fn(),
},
});
19 changes: 19 additions & 0 deletions src/plugins/shared_ux/public/services/kibana/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { SharedUXServices } from '..';
import type { SharedUXPluginStartDeps } from '../../types';
import type { KibanaPluginServiceFactory } from '../types';
import { platformServiceFactory } from './platform';

export const servicesFactory: KibanaPluginServiceFactory<
SharedUXServices,
SharedUXPluginStartDeps
> = (params) => ({
platform: platformServiceFactory(params),
});
20 changes: 20 additions & 0 deletions src/plugins/shared_ux/public/services/kibana/platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { SharedUXPluginStartDeps } from '../../types';
import { KibanaPluginServiceFactory } from '../types';
import { SharedUXPlatformService } from '../platform';

export type PlatformServiceFactory = KibanaPluginServiceFactory<
SharedUXPlatformService,
SharedUXPluginStartDeps
>;

export const platformServiceFactory: PlatformServiceFactory = ({ coreStart }) => ({
setIsFullscreen: (isVisible: boolean) => coreStart.chrome.setIsVisible(isVisible),
});
11 changes: 11 additions & 0 deletions src/plugins/shared_ux/public/services/platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export interface SharedUXPlatformService {
setIsFullscreen: (isFullscreen: boolean) => void;
}
15 changes: 15 additions & 0 deletions src/plugins/shared_ux/public/services/storybook/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { SharedUXServices } from '../.';
import { PluginServiceFactory } from '../types';
import { platformServiceFactory } from './platform';

export const servicesFactory: PluginServiceFactory<SharedUXServices, {}> = (params) => ({
platform: platformServiceFactory(params),
});
18 changes: 18 additions & 0 deletions src/plugins/shared_ux/public/services/storybook/platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { action } from '@storybook/addon-actions';

import { PluginServiceFactory } from '../types';
import { SharedUXPlatformService } from '../platform';

export type PlatformServiceFactory = PluginServiceFactory<SharedUXPlatformService, {}>;

export const platformServiceFactory: PlatformServiceFactory = () => ({
setIsFullscreen: action('setIsChromeVisible'),
});
15 changes: 15 additions & 0 deletions src/plugins/shared_ux/public/services/stub/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { SharedUXServices } from '../.';
import { PluginServiceFactory } from '../types';
import { platformServiceFactory } from './platform';

export const servicesFactory: PluginServiceFactory<SharedUXServices> = () => ({
platform: platformServiceFactory(),
});
16 changes: 16 additions & 0 deletions src/plugins/shared_ux/public/services/stub/platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { PluginServiceFactory } from '../types';
import { SharedUXPlatformService } from '../platform';

export type PlatformServiceFactory = PluginServiceFactory<SharedUXPlatformService>;

export const platformServiceFactory: PlatformServiceFactory = () => ({
setIsFullscreen: (_isFullscreen) => {},
});
42 changes: 42 additions & 0 deletions src/plugins/shared_ux/public/services/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { BehaviorSubject } from 'rxjs';
import { CoreStart, AppUpdater, PluginInitializerContext } from 'src/core/public';

/**
* A factory function for creating one or more services.
*
* The `S` generic determines the shape of the API being produced.
* The `Parameters` generic determines what parameters are expected to
* create the service.
*/
export type PluginServiceFactory<S, Parameters = void> = (params: Parameters) => S;

/**
* Parameters necessary to create a Kibana-based service, (e.g. during Plugin
* startup or setup).
*
* The `Start` generic refers to the specific Plugin `TPluginsStart`.
*/
export interface KibanaPluginServiceParams<Start extends {}> {
coreStart: CoreStart;
startPlugins: Start;
appUpdater?: BehaviorSubject<AppUpdater>;
initContext?: PluginInitializerContext;
}

/**
* A factory function for creating a Kibana-based service.
*
* The `Service` generic determines the shape of the Service being produced.
* The `Start` generic refers to the specific Plugin `TPluginsStart`.
*/
export type KibanaPluginServiceFactory<Service, Start extends {}> = (
params: KibanaPluginServiceParams<Start>
) => Service;
7 changes: 5 additions & 2 deletions src/plugins/shared_ux/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
* Side Public License, v 1.
*/

import { FC } from 'react';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface SharedUXPluginSetup {}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface SharedUXPluginStart {}
export interface SharedUXPluginStart {
ServicesContext: FC;
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface SharedUXPluginSetupDeps {}
Expand Down