Skip to content

Commit

Permalink
man-1701 adds new messaging contract for connect/disconnect and sdkme…
Browse files Browse the repository at this point in the history
…tadata

translates LegacyImageStudioEvents into the new format for translation and processing. Allows backwards compatability with an older version of image-studio
  • Loading branch information
bennealon committed Nov 14, 2024
1 parent 2fc1100 commit 49d5976
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 41 deletions.
88 changes: 61 additions & 27 deletions src/AmplienceImageStudio.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { ApplicationBlockedError } from './errors';
import {
ImageStudioResponse,
SDKEvent,
ImageStudioReason,
ImageStudioEvent,
SDKImage,
SDKMetadata,
ImageStudioEventType,
LegacyImageStudioEvent,
SDKEvent,
SDKEventType,
} from './types';
import { ImageSaveEventData } from './types/ImageStudioEventData';

Expand Down Expand Up @@ -141,30 +143,57 @@ class AmplienceImageStudioInstance<T> {
* This code can be removed once v1.5.0 of image-studio has been released.
* @param event
*/
private translateLegacyImageStudioEvent(event: { data: ImageStudioEvent }) {
console.warn('[LEGACY] An old version of Image Studio is being used');

if (event.data?.connect) {
event.data.type = ImageStudioEventType.Connect;
private translateLegacyImageStudioEvent(
data: LegacyImageStudioEvent,
): ImageStudioEvent {
if (data?.connect) {
/**
* To maintain backwards compatability, newer versions of the studio will send both NEW and Legacy message formats for `connect` only.
* This means that we will always receive duplicate connection messages, and therefore should NOT translate the legacy message and just wait for the new format.
* This version of the SDK is not designed to be backwards compatible with older versions of the studio, which should not be a problem due to them being hosted by us anyway.
* If we translate this message, we will double-up on the request, therefore we will purposefully ignore this message
*/
return {
type: ImageStudioEventType.Unknown,
data: {},
};
}

if (event.data?.disconnect) {
event.data.type = ImageStudioEventType.Disconnect;
// log line intentionally after the 'connect' message
console.warn('[LEGACY] An old version of Image Studio is being used');
if (data?.disconnect) {
return {
type: ImageStudioEventType.Disconnect,
data: {},
};
}

if (event.data?.exportImageInfo) {
event.data.type = ImageStudioEventType.ImageSave;
event.data.data = { image: event.data.exportImageInfo };
if (data?.exportImageInfo) {
return {
type: ImageStudioEventType.ImageSave,
data: { image: data.exportImageInfo },
};
}

return {
type: ImageStudioEventType.Unknown,
data: {},
};
}

protected handleEvent(event: { data: ImageStudioEvent }) {
// for any events that don't contain the `type` var, these conform to legacy structure.
let eventData: ImageStudioEvent;
if (event.data && !('type' in event.data)) {
// for any events that don't contain the `type` var, these conform to legacy structure.
this.translateLegacyImageStudioEvent(event);
eventData = this.translateLegacyImageStudioEvent(event.data);
if (eventData.type == ImageStudioEventType.Unknown) {
return; // we should ignore any unknown legacy messages.
}
} else {
eventData = event.data;
}

switch (event.data?.type) {
switch (eventData.type) {
case ImageStudioEventType.Connect:
if (!this.isActive) {
this.handleActivate();
Expand All @@ -176,15 +205,11 @@ class AmplienceImageStudioInstance<T> {
}
break;
case ImageStudioEventType.ImageSave:
if ((event.data?.data as ImageSaveEventData)?.image) {
this.handleExportedImage(
(event.data?.data as ImageSaveEventData).image,
);
}
this.handleExportedImage((eventData.data as ImageSaveEventData).image);
break;
default:
console.log(
`Event received with unspported ImageStudioEventType: ${event.data?.type}`,
`Event received with unspported ImageStudioEventType: ${eventData.type}`,
);
break;
}
Expand All @@ -193,16 +218,25 @@ class AmplienceImageStudioInstance<T> {
private handleActivate() {
this.isActive = true;

// on connection/activation, submit the metadata and any input images.
const message: SDKEvent = {};
message.focus = true;
message.sdkMetadata = this.launchProps.sdkMetadata;
this.sendSDKEvent({
type: SDKEventType.Focus,
data: {},
});
// message.focus = true;

this.sendSDKEvent({
type: SDKEventType.SDKMetadata,
data: this.launchProps.sdkMetadata,
});
// message.sdkMetadata = this.launchProps.sdkMetadata;

if (this.launchProps.inputImages?.length > 0) {
message.inputImages = this.launchProps.inputImages;
this.sendSDKEvent({
type: SDKEventType.ImageInput,
data: { images: this.launchProps.inputImages },
});
// message.inputImages = this.launchProps.inputImages;
}

this.sendSDKEvent(message);
}

private handleExportedImage(image: SDKImage) {
Expand Down
11 changes: 7 additions & 4 deletions src/types/ImageStudioEvent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ImageStudioEventData } from './ImageStudioEventData';
import { ImageStudioEventType } from './ImageStudioEventType';
import { SDKImage } from './SdkImage';

Expand All @@ -7,18 +8,20 @@ import { SDKImage } from './SdkImage';
export interface ImageStudioEvent {
/**
* Event Type
* undefined signifies legacy vars will exist
*/
type: ImageStudioEventType;

/**
* Event Data
*/
data?: unknown;
data: ImageStudioEventData;
}

// BEGIN Legacy vars
/**
* Legacy Interface for Events sent to the SDK from ImageStudio
*/
export interface LegacyImageStudioEvent {
connect?: boolean;
disconnect?: boolean;
exportImageInfo?: SDKImage;
// END Legacy vars
}
3 changes: 3 additions & 0 deletions src/types/ImageStudioEventData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ import { SDKImage } from './SdkImage';
export interface ImageSaveEventData {
image: SDKImage;
}

// intended to be a union type of the above interfaces, otherwise an empty Record (object)
export type ImageStudioEventData = ImageSaveEventData | Record<string, never>;
5 changes: 3 additions & 2 deletions src/types/ImageStudioEventType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*/

export enum ImageStudioEventType {
Connect = 'CONNECT',
Disconnect = 'DISCONNECT',
Unknown = 'UNKNOWN', //data: {}
Connect = 'CONNECT', //data: {}
Disconnect = 'DISCONNECT', //data: {}
ImageSave = 'IMAGE_SAVE', //data: ImageSaveEventData
}
17 changes: 17 additions & 0 deletions src/types/SdkEvent.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import { SDKEventData } from './SdkEventData';
import { SDKEventType } from './SdkEventType';
import { SDKImage } from './SdkImage';
import { SDKMetadata } from './SdkMetadata';

/**
* Interface for Events sent to ImageStudio from the SDK
*/
export interface SDKEvent {
/**
* SDK Event Type
*/
type: SDKEventType;

/**
* SDK Event Data
*/
data: SDKEventData;
}

/**
* Legacy Interface for Events sent to ImageStudio from the SDK
*/
export interface LegacySDKEvent {
sdkMetadata?: SDKMetadata;
inputImages?: SDKImage[];
focus?: boolean;
Expand Down
12 changes: 12 additions & 0 deletions src/types/SdkEventData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { SDKImage } from './SdkImage';
import { SDKMetadata } from './SdkMetadata';

export interface SDKImageInputEventData {
images: SDKImage[];
}

// intended to be a union type of the above interfaces, otherwise an empty Record (object)
export type SDKEventData =
| SDKImageInputEventData
| SDKMetadata
| Record<string, never>;
15 changes: 8 additions & 7 deletions src/types/SdkEventType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
* You are encouraged to migrate to a new ENUM to enforrce backwards comatability.
*/
export enum SDKEventType {
None = 'NONE',
NoCallback = 'NO_CALLBACK',
Success = 'SUCCESS',
Fail = 'FAIL',
Focus = 'FOCUS',
Metadata = 'METADATA',
ImageInput = 'IMAGE_INPUT',
Unknown = 'UNKNOWN', //data: {}
Focus = 'FOCUS', //data: {}
SDKMetadata = 'SDKMETADATA', //data: SDKMetadata
ImageInput = 'IMAGE_INPUT', //data: SDKImageInputEventData

// NoCallback = 'NO_CALLBACK',
// Success = 'SUCCESS',
// Fail = 'FAIL',
}
3 changes: 2 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export * from './ImageStudioEventType';
export * from './ImageStudioEventData';
export * from './SdkEvent';
export * from './SdkEventType';
export * from './SdkImage';
export * from './SdkMetadata';
export * from './SdkEventData';
export * from './SdkImage';

0 comments on commit 49d5976

Please sign in to comment.