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

[PoC] persisted state service #63451

Closed
wants to merge 7 commits into from
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
7 changes: 7 additions & 0 deletions src/plugins/share/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ export {
ShareContextMenuPanelItem,
} from './types';

export {
PersistableState,
PersistableStates,
PersistableStateDefinition,
SerializableState,
} from './persistable_state';

export {
UrlGeneratorId,
UrlGeneratorState,
Expand Down
67 changes: 67 additions & 0 deletions src/plugins/share/public/mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 { identity } from 'lodash';
import { SharePluginSetup, SharePluginStart } from './';
import { SharePlugin } from './plugin';
import { coreMock } from '../../../core/public/mocks';

const createSetupContract = (): SharePluginSetup => ({
register: jest.fn(),
persistableState: {
register: jest.fn().mockResolvedValue(undefined),
},
urlGenerators: {
registerUrlGenerator: jest.fn(),
},
});

const createStartContract = (): SharePluginStart => ({
toggleShareContextMenu: jest.fn(),
persistableState: {
get: jest.fn().mockResolvedValue({
id: 'fooState',
extractReferences: (s: unknown) => [s, []],
injectReferences: identity,
migrate: identity,
}),
},
urlGenerators: {
getUrlGenerator: jest.fn(),
},
});

const createInstance = async () => {
const plugin = new SharePlugin();

const setup = plugin.setup(coreMock.createSetup());
const doStart = () => plugin.start(coreMock.createStart());

return {
plugin,
setup,
doStart,
};
};

export const sharePluginMock = {
createSetupContract,
createStartContract,
createInstance,
};
21 changes: 21 additions & 0 deletions src/plugins/share/public/persistable_state/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.
*/

export * from './persistable_state_service';
export * from './types';
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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 { CoreSetup, CoreStart, Plugin } from 'src/core/public';
import { identity } from 'lodash';
import { PersistableStateDefinition, PersistableStateContract } from './types';

export interface PersistableStateSetup {
register: <Id extends string>(
id: Id,
definition: PersistableStateDefinition<Id>
) => Promise<void>;
}

export interface PersistableStateStart {
get: <Id extends string>(definitionId: Id) => Promise<PersistableStateContract<Id>>;
}

export class PersistableStateService
implements Plugin<PersistableStateSetup, PersistableStateStart> {
private definitions = new Map();

public setup(core: CoreSetup): PersistableStateSetup {
return {
register: async (id, definition) => {
const { extractReferences, injectReferences, migrate } = definition;

this.definitions.set(id, {
id,
extractReferences: extractReferences || ((s: unknown) => [s, []]),
injectReferences: injectReferences || identity,
migrate: migrate || identity,
});
},
};
}

public start(core: CoreStart): PersistableStateStart {
return {
get: async (id: string) => {
const d = this.definitions.get(id);
if (!d) {
return {
id,
migrate: identity,
injectReferences: identity,
extractReferences: (s: unknown) => [s, []],
};
}
return d;
},
};
}

public stop() {}
}
94 changes: 94 additions & 0 deletions src/plugins/share/public/persistable_state/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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 { SavedObjectReference } from 'src/core/public';

type State = string | number | boolean | null | undefined | SerializableState;

export interface SerializableState {
[key: string]: State | State[];
}

type ExtractReferences<Id extends string> = (
state: PersistableStates[Id]['state']
) => [PersistableStates[Id]['state'], SavedObjectReference[]];

type InjectReferences<Id extends string> = (
state: PersistableStates[Id]['state'],
references: SavedObjectReference[]
) => PersistableStates[Id]['state'];

type MigrateState<Id extends string> = <T extends SerializableState>(
oldState: T
) => PersistableStates[Id]['state'];

/** @internal */
export interface PersistableStateContract<Id extends string> {
id: Id;
extractReferences: ExtractReferences<Id>;
injectReferences: InjectReferences<Id>;
migrate: MigrateState<Id>;
}

/**
* Use this generic to wrap your persistable state interface before
* adding it to the PersistableStates interface via `declare module`.
*
* We are providing this as a generic to make things more flexible
* should we choose to add properties to the interface in the future.
*
* @public
* TODO: should be <S extends SerializableState>
*/
export interface PersistableState<S extends any> {
state: S;
}

/**
* Anybody registering persistable state should add a property to this
* interface so that any module importing PersistableStates can
* have the interfaces of all persistable states available to them.
*
* declare module '../../plugins/share/public' {
* interface PersistableStates {
* myId: PersistableState<MySerializableStateInterface>;
* }
* }
*
* @public
*/
export interface PersistableStates {
// Fallback state for if someone forgets to use `declare module` to
// add their state to this interface.
// TODO: should be PersistableState<SerializableState>
[key: string]: PersistableState<any>;
}

/**
* These are options that can be added when registering persistable state.
* If they are not provided, default noop functions will be provided by
* the service.
*
* @public
*/
export interface PersistableStateDefinition<Id extends string> {
extractReferences?: ExtractReferences<Id>;
injectReferences?: InjectReferences<Id>;
migrate?: MigrateState<Id>;
}
10 changes: 10 additions & 0 deletions src/plugins/share/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,44 @@ import {
UrlGeneratorsSetup,
UrlGeneratorsStart,
} from './url_generators/url_generator_service';
import {
PersistableStateService,
PersistableStateSetup,
PersistableStateStart,
} from './persistable_state';

export class SharePlugin implements Plugin<SharePluginSetup, SharePluginStart> {
private readonly shareMenuRegistry = new ShareMenuRegistry();
private readonly shareContextMenu = new ShareMenuManager();
private readonly persistableStateService = new PersistableStateService();
private readonly urlGeneratorsService = new UrlGeneratorsService();

public setup(core: CoreSetup): SharePluginSetup {
core.application.register(createShortUrlRedirectApp(core, window.location));
return {
...this.shareMenuRegistry.setup(),
persistableState: this.persistableStateService.setup(core),
urlGenerators: this.urlGeneratorsService.setup(core),
};
}

public start(core: CoreStart): SharePluginStart {
return {
...this.shareContextMenu.start(core, this.shareMenuRegistry.start()),
persistableState: this.persistableStateService.start(core),
urlGenerators: this.urlGeneratorsService.start(core),
};
}
}

/** @public */
export type SharePluginSetup = ShareMenuRegistrySetup & {
persistableState: PersistableStateSetup;
urlGenerators: UrlGeneratorsSetup;
};

/** @public */
export type SharePluginStart = ShareMenuManagerStart & {
persistableState: PersistableStateStart;
urlGenerators: UrlGeneratorsStart;
};
2 changes: 1 addition & 1 deletion src/plugins/visualizations/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"version": "kibana",
"server": true,
"ui": true,
"requiredPlugins": ["data", "expressions", "uiActions", "embeddable", "usageCollection", "inspector"]
"requiredPlugins": ["data", "expressions", "uiActions", "embeddable", "usageCollection", "inspector", "share"]
}
2 changes: 2 additions & 0 deletions src/plugins/visualizations/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { coreMock } from '../../../core/public/mocks';
import { embeddablePluginMock } from '../../../plugins/embeddable/public/mocks';
import { expressionsPluginMock } from '../../../plugins/expressions/public/mocks';
import { dataPluginMock } from '../../../plugins/data/public/mocks';
import { sharePluginMock } from '../../../plugins/share/public/mocks';
import { usageCollectionPluginMock } from '../../../plugins/usage_collection/public/mocks';
import { uiActionsPluginMock } from '../../../plugins/ui_actions/public/mocks';
import { inspectorPluginMock } from '../../../plugins/inspector/public/mocks';
Expand Down Expand Up @@ -57,6 +58,7 @@ const createInstance = async () => {
embeddable: embeddablePluginMock.createSetupContract(),
expressions: expressionsPluginMock.createSetupContract(),
inspector: inspectorPluginMock.createSetupContract(),
share: sharePluginMock.createSetupContract(),
usageCollection: usageCollectionPluginMock.createSetupContract(),
});
const doStart = () =>
Expand Down
17 changes: 15 additions & 2 deletions src/plugins/visualizations/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ import {
convertFromSerializedVis,
convertToSerializedVis,
} from './saved_visualizations/_saved_vis';

import { SharePluginSetup } from '../../share/public';
import {
extractReferences,
injectReferences,
} from './saved_visualizations/saved_visualization_references';
// @ts-ignore
import { updateOldState } from './legacy/vis_update_state';
/**
* Interface for this plugin's returned setup/start contracts.
*
Expand All @@ -88,6 +94,7 @@ export interface VisualizationsSetupDeps {
expressions: ExpressionsSetup;
inspector: InspectorSetup;
usageCollection: UsageCollectionSetup;
share: SharePluginSetup;
}

export interface VisualizationsStartDeps {
Expand Down Expand Up @@ -120,7 +127,7 @@ export class VisualizationsPlugin

public setup(
core: CoreSetup<VisualizationsStartDeps, VisualizationsStart>,
{ expressions, embeddable, usageCollection, data }: VisualizationsSetupDeps
{ expressions, embeddable, usageCollection, data, share }: VisualizationsSetupDeps
): VisualizationsSetup {
const start = (this.getStartServicesOrDie = createStartServicesGetter(core.getStartServices));

Expand All @@ -132,6 +139,12 @@ export class VisualizationsPlugin
expressions.registerFunction(rangeExpressionFunction);
expressions.registerFunction(visDimensionExpressionFunction);

share.persistableState.register('visualization', {
injectReferences,
extractReferences,
migrate: updateOldState,
});

const embeddableFactory = new VisualizeEmbeddableFactory({ start });
embeddable.registerEmbeddableFactory(VISUALIZE_EMBEDDABLE_TYPE, embeddableFactory);

Expand Down
Loading