Skip to content

Commit

Permalink
async load js-sha256
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover committed Jul 20, 2021
1 parent fc1a52b commit 8ccd241
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
13 changes: 11 additions & 2 deletions x-pack/plugins/cloud/public/fullstory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { sha256 } from 'js-sha256'; // loaded here to reduce page load bundle size when FullStory is disabled
import type { IBasePath, PackageInfo } from '../../../../src/core/public';

export interface FullStoryDeps {
Expand All @@ -18,11 +19,16 @@ export interface FullStoryApi {
event(eventName: string, eventProperties: Record<string, any>): void;
}

export interface FullStoryService {
fullStory: FullStoryApi;
sha256: typeof sha256;
}

export const initializeFullStory = ({
basePath,
orgId,
packageInfo,
}: FullStoryDeps): FullStoryApi => {
}: FullStoryDeps): FullStoryService => {
// @ts-expect-error
window._fs_debug = false;
// @ts-expect-error
Expand Down Expand Up @@ -72,5 +78,8 @@ export const initializeFullStory = ({
// @ts-expect-error
const fullStory: FullStoryApi = window.FSKibana;

return fullStory;
return {
fullStory,
sha256,
};
};
10 changes: 6 additions & 4 deletions x-pack/plugins/cloud/public/plugin.test.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
* 2.0.
*/

import type { FullStoryDeps, FullStoryApi } from './fullstory';
import { sha256 } from 'js-sha256';
import type { FullStoryDeps, FullStoryApi, FullStoryService } from './fullstory';

export const fullStoryApiMock: jest.Mocked<FullStoryApi> = {
event: jest.fn(),
identify: jest.fn(),
};
export const initializeFullStoryMock = jest.fn<FullStoryApi, [FullStoryDeps]>(
() => fullStoryApiMock
);
export const initializeFullStoryMock = jest.fn<FullStoryService, [FullStoryDeps]>(() => ({
fullStory: fullStoryApiMock,
sha256,
}));
jest.doMock('./fullstory', () => {
return { initializeFullStory: initializeFullStoryMock };
});
3 changes: 1 addition & 2 deletions x-pack/plugins/cloud/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
IBasePath,
} from 'src/core/public';
import { i18n } from '@kbn/i18n';
import { sha256 } from 'js-sha256';
import type {
AuthenticatedUser,
SecurityPluginSetup,
Expand Down Expand Up @@ -178,7 +177,7 @@ export class CloudPlugin implements Plugin<CloudSetup> {
userIdPromise,
]);

const fullStory = initializeFullStory({
const { fullStory, sha256 } = initializeFullStory({
basePath,
orgId,
packageInfo: this.initializerContext.env.packageInfo,
Expand Down

0 comments on commit 8ccd241

Please sign in to comment.