Skip to content
Merged
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
8 changes: 4 additions & 4 deletions src/embed/hostEventClient/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HostEvent } from '../../types';
import { ContextType, HostEvent } from '../../types';

export enum UIPassthroughEvent {
PinAnswerToLiveboard = 'addVizToPinboard',
Expand Down Expand Up @@ -103,13 +103,13 @@ export type HostEventRequest<HostEventT extends HostEvent> =
? UIPassthroughRequest<EmbedApiHostEventMapping[HostEventT]>
: any;

export type HostEventResponse<HostEventT extends HostEvent> =
export type HostEventResponse<HostEventT extends HostEvent, ContextT extends ContextType> =
HostEventT extends keyof EmbedApiHostEventMapping
? UIPassthroughResponse<EmbedApiHostEventMapping[HostEventT]>
: any;

// trigger response and request
export type TriggerPayload<PayloadT, HostEventT extends HostEvent> =
PayloadT | HostEventRequest<HostEventT>;
export type TriggerResponse<PayloadT, HostEventT extends HostEvent> =
PayloadT extends HostEventRequest<HostEventT> ? HostEventResponse<HostEventT> : any;
export type TriggerResponse<PayloadT, HostEventT extends HostEvent, ContextT extends ContextType> =
PayloadT extends HostEventRequest<HostEventT> ? HostEventResponse<HostEventT, ContextT> : any;
8 changes: 7 additions & 1 deletion src/embed/hostEventClient/host-event-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('HostEventClient', () => {
type: apiName,
parameters,
},
undefined,
);
expect(result).toEqual(await triggerResponse);
});
Expand Down Expand Up @@ -148,6 +149,7 @@ describe('HostEventClient', () => {
HostEvent.UIPassthrough,
'http://localhost',
{ parameters: payload, type: UIPassthroughEvent.PinAnswerToLiveboard },
undefined,
);
expect(result).toEqual(mockResponse.value);
});
Expand Down Expand Up @@ -185,6 +187,7 @@ describe('HostEventClient', () => {
parameters: payload,
type: 'saveAnswer',
},
undefined,
);
expect(result).toEqual({ answerId: 'newAnswer', ...mockResponse[0].value });
});
Expand All @@ -198,7 +201,7 @@ describe('HostEventClient', () => {

const result = await client.triggerHostEvent(hostEvent, payload);

expect(client.hostEventFallback).toHaveBeenCalledWith(hostEvent, payload);
expect(client.hostEventFallback).toHaveBeenCalledWith(hostEvent, payload, undefined);
expect(result).toEqual(mockResponse);
});

Expand All @@ -223,6 +226,7 @@ describe('HostEventClient', () => {
HostEvent.Pin,
mockThoughtSpotHost,
{},
undefined,
);
expect(result).toEqual([mockResponse]);
});
Expand All @@ -248,6 +252,7 @@ describe('HostEventClient', () => {
HostEvent.Save,
mockThoughtSpotHost,
{},
undefined,
);
expect(result).toEqual([mockResponse]);
});
Expand Down Expand Up @@ -303,6 +308,7 @@ describe('HostEventClient', () => {
parameters: { ...payload, pinboardId: 'test', newPinboardName: 'testLiveboard' },
type: 'addVizToPinboard',
},
undefined,
);
expect(result).toEqual({
pinboardId: 'testLiveboard',
Expand Down
33 changes: 22 additions & 11 deletions src/embed/hostEventClient/host-event-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HostEvent } from '../../types';
import { ContextType, HostEvent } from '../../types';
import { processTrigger as processTriggerService } from '../../utils/processTrigger';
import { getEmbedConfig } from '../embedConfig';
import {
Expand All @@ -23,7 +23,7 @@ export class HostEventClient {
* @param {any} data Data to send with the host event
* @returns {Promise<any>} - the response from the process trigger
*/
protected async processTrigger(message: HostEvent, data: any): Promise<any> {
protected async processTrigger(message: HostEvent, data: any, context?: ContextType): Promise<any> {
if (!this.iFrame) {
throw new Error('Iframe element is not set');
}
Expand All @@ -34,14 +34,16 @@ export class HostEventClient {
message,
thoughtspotHost,
data,
context,
);
}

public async handleHostEventWithParam<UIPassthroughEventT extends UIPassthroughEvent>(
apiName: UIPassthroughEventT,
parameters: UIPassthroughRequest<UIPassthroughEventT>,
context?: ContextType,
): Promise<UIPassthroughResponse<UIPassthroughEventT>> {
const response = (await this.triggerUIPassthroughApi(apiName, parameters))
const response = (await this.triggerUIPassthroughApi(apiName, parameters, context))
?.filter?.((r) => r.error || r.value)[0];

if (!response) {
Expand All @@ -65,8 +67,9 @@ export class HostEventClient {
public async hostEventFallback(
hostEvent: HostEvent,
data: any,
context?: ContextType,
): Promise<any> {
return this.processTrigger(hostEvent, data);
return this.processTrigger(hostEvent, data, context);
}

/**
Expand All @@ -80,20 +83,22 @@ export class HostEventClient {
public async triggerUIPassthroughApi<UIPassthroughEventT extends UIPassthroughEvent>(
apiName: UIPassthroughEventT,
parameters: UIPassthroughRequest<UIPassthroughEventT>,
context?: ContextType,
): Promise<UIPassthroughArrayResponse<UIPassthroughEventT>> {
const res = await this.processTrigger(HostEvent.UIPassthrough, {
type: apiName,
parameters,
});
}, context);

return res;
}

protected async handlePinEvent(
payload: HostEventRequest<HostEvent.Pin>,
): Promise<HostEventResponse<HostEvent.Pin>> {
context?: ContextType,
): Promise<HostEventResponse<HostEvent.Pin, ContextType>> {
if (!payload || !('newVizName' in payload)) {
return this.hostEventFallback(HostEvent.Pin, payload);
return this.hostEventFallback(HostEvent.Pin, payload, context);
}

const formattedPayload = {
Expand All @@ -104,6 +109,7 @@ export class HostEventClient {

const data = await this.handleHostEventWithParam(
UIPassthroughEvent.PinAnswerToLiveboard, formattedPayload,
context as ContextType,
);

return {
Expand All @@ -114,14 +120,16 @@ export class HostEventClient {

protected async handleSaveAnswerEvent(
payload: HostEventRequest<HostEvent.SaveAnswer>,
context?: ContextType,
): Promise<any> {
if (!payload || !('name' in payload) || !('description' in payload)) {
// Save is the fallback for SaveAnswer
return this.hostEventFallback(HostEvent.Save, payload);
return this.hostEventFallback(HostEvent.Save, payload, context);
}

const data = await this.handleHostEventWithParam(
UIPassthroughEvent.SaveAnswer, payload,
context as ContextType,
);
return {
...data,
Expand All @@ -132,19 +140,22 @@ export class HostEventClient {
public async triggerHostEvent<
HostEventT extends HostEvent,
PayloadT,
ContextT extends ContextType,
>(
hostEvent: HostEventT,
payload?: TriggerPayload<PayloadT, HostEventT>,
): Promise<TriggerResponse<PayloadT, HostEventT>> {
context?: ContextT,
): Promise<TriggerResponse<PayloadT, HostEventT, ContextType>> {
switch (hostEvent) {
case HostEvent.Pin:
return this.handlePinEvent(payload as HostEventRequest<HostEvent.Pin>) as any;
return this.handlePinEvent(payload as HostEventRequest<HostEvent.Pin>, context as ContextType) as any;
case HostEvent.SaveAnswer:
return this.handleSaveAnswerEvent(
payload as HostEventRequest<HostEvent.SaveAnswer>,
context as ContextType,
) as any;
default:
return this.hostEventFallback(hostEvent, payload);
return this.hostEventFallback(hostEvent, payload, context);
}
}
}
2 changes: 1 addition & 1 deletion src/embed/liveboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,7 @@ describe('Liveboard/viz embed tests', () => {
await liveboardEmbed.trigger(HostEvent.Save);
expect(mockProcessTrigger).toHaveBeenCalledWith(HostEvent.Save, {
vizId: 'testViz',
});
}, undefined);
});
});
});
Expand Down
8 changes: 5 additions & 3 deletions src/embed/liveboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
LiveboardAppEmbedViewConfig,
ErrorDetailsTypes,
EmbedErrorCodes,
ContextType,
} from '../types';
import { calculateVisibleElementData, getQueryParamString, isUndefined, isValidCssMargin } from '../utils';
import { getAuthPromise } from './base';
Expand Down Expand Up @@ -842,10 +843,11 @@ export class LiveboardEmbed extends V1Embed {
* @param {any} data The payload to send with the message
* @returns A promise that resolves with the response from the embedded app
*/
public trigger<HostEventT extends HostEvent, PayloadT>(
public trigger<HostEventT extends HostEvent, PayloadT, ContextT extends ContextType>(
messageType: HostEventT,
data: TriggerPayload<PayloadT, HostEventT> = ({} as any),
): Promise<TriggerResponse<PayloadT, HostEventT>> {
context?: ContextT,
): Promise<TriggerResponse<PayloadT, HostEventT, ContextT>> {
const dataWithVizId: any = data;
if (messageType === HostEvent.SetActiveTab) {
this.setActiveTab(data as { tabId: string });
Expand All @@ -854,7 +856,7 @@ export class LiveboardEmbed extends V1Embed {
if (typeof dataWithVizId === 'object' && this.viewConfig.vizId) {
dataWithVizId.vizId = this.viewConfig.vizId;
}
return super.trigger(messageType, dataWithVizId);
return super.trigger(messageType, dataWithVizId, context);
}
/**
* Destroys the ThoughtSpot embed, and remove any nodes from the DOM.
Expand Down
Loading
Loading