Skip to content

Commit

Permalink
Merge pull request #912 from concord-consortium/181169438-add-options…
Browse files Browse the repository at this point in the history
…-to-get-interactive-state

Added unloading option to get interactive state [#181169438]
  • Loading branch information
dougmartin authored Mar 8, 2022
2 parents 74a29d7 + 1207c95 commit f305b99
Show file tree
Hide file tree
Showing 11 changed files with 5,448 additions and 5,140 deletions.
15 changes: 15 additions & 0 deletions docs/interactive-api-client/classes/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* [removeDecorateContentListener](client.md#removedecoratecontentlistener)
* [removeGetReportItemAnswerListener](client.md#removegetreportitemanswerlistener)
* [removeListener](client.md#removelistener)
* [setOnUnload](client.md#setonunload)
* [setSupportedFeatures](client.md#setsupportedfeatures)

## Constructors
Expand Down Expand Up @@ -175,6 +176,20 @@ Name | Type |

___

### setOnUnload

**setOnUnload**(`onUnload?`: [OnUnloadFunction](../globals.md#onunloadfunction)): *void*

**Parameters:**

Name | Type |
------ | ------ |
`onUnload?` | [OnUnloadFunction](../globals.md#onunloadfunction) |

**Returns:** *void*

___

### setSupportedFeatures

**setSupportedFeatures**(`request`: [ISupportedFeaturesRequest](../interfaces/isupportedfeaturesrequest.md)): *void*
Expand Down
33 changes: 33 additions & 0 deletions docs/interactive-api-client/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* [IGetInteractiveSnapshotOptions](interfaces/igetinteractivesnapshotoptions.md)
* [IGetInteractiveSnapshotRequest](interfaces/igetinteractivesnapshotrequest.md)
* [IGetInteractiveSnapshotResponse](interfaces/igetinteractivesnapshotresponse.md)
* [IGetInteractiveState](interfaces/igetinteractivestate.md)
* [IGetLibraryInteractiveListOptions](interfaces/igetlibraryinteractivelistoptions.md)
* [IGetLibraryInteractiveListRequest](interfaces/igetlibraryinteractivelistrequest.md)
* [IGetLibraryInteractiveListResponse](interfaces/igetlibraryinteractivelistresponse.md)
Expand Down Expand Up @@ -113,6 +114,7 @@
* [InteractiveItemId](globals.md#interactiveitemid)
* [LoggerClientMessage](globals.md#loggerclientmessage)
* [ModalType](globals.md#modaltype)
* [OnUnloadFunction](globals.md#onunloadfunction)
* [ReadAttachmentParams](globals.md#readattachmentparams)
* [ServerMessage](globals.md#servermessage)
* [WriteAttachmentParams](globals.md#writeattachmentparams)
Expand Down Expand Up @@ -163,6 +165,7 @@
* [setInteractiveState](globals.md#const-setinteractivestate)
* [setLinkedInteractives](globals.md#const-setlinkedinteractives)
* [setNavigation](globals.md#const-setnavigation)
* [setOnUnload](globals.md#const-setonunload)
* [setSupportedFeatures](globals.md#const-setsupportedfeatures)
* [showModal](globals.md#const-showmodal)
* [useAuthoredState](globals.md#const-useauthoredstate)
Expand Down Expand Up @@ -368,6 +371,22 @@ ___

___

### OnUnloadFunction

Ƭ **OnUnloadFunction**: *function*

#### Type declaration:

▸ (`options`: [IGetInteractiveState](interfaces/igetinteractivestate.md)): *Promise‹InteractiveState›*

**Parameters:**

Name | Type |
------ | ------ |
`options` | [IGetInteractiveState](interfaces/igetinteractivestate.md) |

___

### ReadAttachmentParams

Ƭ **ReadAttachmentParams**: *Omit‹[IAttachmentUrlRequest](interfaces/iattachmenturlrequest.md), "requestId" | "operation" | "contentType" | "expiresIn"›*
Expand Down Expand Up @@ -1055,6 +1074,20 @@ Name | Type |

___

### `Const` setOnUnload

**setOnUnload**(`onUnload?`: [OnUnloadFunction](globals.md#onunloadfunction)): *void*

**Parameters:**

Name | Type |
------ | ------ |
`onUnload?` | [OnUnloadFunction](globals.md#onunloadfunction) |

**Returns:** *void*

___

### `Const` setSupportedFeatures

**setSupportedFeatures**(`features`: [ISupportedFeatures](interfaces/isupportedfeatures.md)): *void*
Expand Down
19 changes: 19 additions & 0 deletions docs/interactive-api-client/interfaces/igetinteractivestate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[@concord-consortium/lara-interactive-api](../README.md)[Globals](../globals.md)[IGetInteractiveState](igetinteractivestate.md)

# Interface: IGetInteractiveState

## Hierarchy

* **IGetInteractiveState**

## Index

### Properties

* [unloading](igetinteractivestate.md#optional-unloading)

## Properties

### `Optional` unloading

**unloading**? : *undefined | false | true*
18 changes: 18 additions & 0 deletions docs/interactive-api-host/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* [IGetInteractiveSnapshotOptions](interfaces/igetinteractivesnapshotoptions.md)
* [IGetInteractiveSnapshotRequest](interfaces/igetinteractivesnapshotrequest.md)
* [IGetInteractiveSnapshotResponse](interfaces/igetinteractivesnapshotresponse.md)
* [IGetInteractiveState](interfaces/igetinteractivestate.md)
* [IGetLibraryInteractiveListOptions](interfaces/igetlibraryinteractivelistoptions.md)
* [IGetLibraryInteractiveListRequest](interfaces/igetlibraryinteractivelistrequest.md)
* [IGetLibraryInteractiveListResponse](interfaces/igetlibraryinteractivelistresponse.md)
Expand Down Expand Up @@ -103,6 +104,7 @@
* [InteractiveItemId](globals.md#interactiveitemid)
* [LoggerClientMessage](globals.md#loggerclientmessage)
* [ModalType](globals.md#modaltype)
* [OnUnloadFunction](globals.md#onunloadfunction)
* [ReadAttachmentParams](globals.md#readattachmentparams)
* [S3Operation](globals.md#s3operation)
* [ServerMessage](globals.md#servermessage)
Expand Down Expand Up @@ -296,6 +298,22 @@ ___

___

### OnUnloadFunction

Ƭ **OnUnloadFunction**: *function*

#### Type declaration:

▸ (`options`: [IGetInteractiveState](interfaces/igetinteractivestate.md)): *Promise‹InteractiveState›*

**Parameters:**

Name | Type |
------ | ------ |
`options` | [IGetInteractiveState](interfaces/igetinteractivestate.md) |

___

### ReadAttachmentParams

Ƭ **ReadAttachmentParams**: *Omit‹[IAttachmentUrlRequest](interfaces/iattachmenturlrequest.md), "requestId" | "operation" | "contentType" | "expiresIn"›*
Expand Down
19 changes: 19 additions & 0 deletions docs/interactive-api-host/interfaces/igetinteractivestate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[@concord-consortium/interactive-api-host](../README.md)[Globals](../globals.md)[IGetInteractiveState](igetinteractivestate.md)

# Interface: IGetInteractiveState

## Hierarchy

* **IGetInteractiveState**

## Index

### Properties

* [unloading](igetinteractivestate.md#optional-unloading)

## Properties

### `Optional` unloading

**unloading**? : *undefined | false | true*
7 changes: 6 additions & 1 deletion lara-typescript/src/interactive-api-client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import {
ReadAttachmentParams,
GetAttachmentUrlParams,
IGetReportItemAnswerHandler,
IReportItemAnswer
IReportItemAnswer,
OnUnloadFunction
} from "./types";
import { getClient } from "./client";
import { v4 as uuidv4 } from "uuid";
Expand Down Expand Up @@ -483,3 +484,7 @@ export const getAttachmentUrl = (params: GetAttachmentUrlParams) => {
export const sendReportItemAnswer = (request: Omit<IReportItemAnswer, "requestId">) => {
getClient().post("reportItemAnswer", request);
};

export const setOnUnload = (onUnload?: OnUnloadFunction) => {
getClient().setOnUnload(onUnload);
};
20 changes: 17 additions & 3 deletions lara-typescript/src/interactive-api-client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// to the same message and auto-removing listeners when a requestId is given.
import * as iframePhone from "iframe-phone";
import { ClientMessage, ICustomMessageHandler, ICustomMessagesHandledMap, IInitInteractive, ISupportedFeaturesRequest,
ServerMessage, ITextDecorationHandler, ITextDecorationInfo, IGetReportItemAnswerHandler } from "./types";
ServerMessage, ITextDecorationHandler, ITextDecorationInfo, IGetReportItemAnswerHandler,
IGetInteractiveState, OnUnloadFunction } from "./types";
import { postDecoratedContentEvent } from "../interactive-api-client";
import { IEventListener } from "../plugin-api";
import { inIframe } from "./in-frame";
import { ManagedState } from "./managed-state";

Expand Down Expand Up @@ -47,6 +47,8 @@ export class Client {
private listeners: IListenerMap = {};
private requestId = 1;

private onUnload: OnUnloadFunction | undefined = undefined;

constructor() {
if (!inIframe()) {
// tslint:disable-next-line:no-console
Expand Down Expand Up @@ -188,6 +190,10 @@ export class Client {
this.post("supportedFeatures", newRequest);
}

public setOnUnload = (onUnload?: OnUnloadFunction) => {
this.onUnload = onUnload;
}

private connect() {
this.phone = iframePhone.getIFrameEndpoint();

Expand All @@ -212,7 +218,15 @@ export class Client {
}
});

this.addListener("getInteractiveState", () => {
this.addListener("getInteractiveState", async (options?: IGetInteractiveState) => {
if (options?.unloading && this.onUnload) {
// call the interactive's registered onUnload function
// and if it returns a value use that as the final interactive state
const finalState = await this.onUnload(options);
if (finalState) {
this.managedState.interactiveState = finalState;
}
}
this.post("interactiveState", this.managedState.interactiveState);
this.managedState.interactiveStateDirty = false;
});
Expand Down
5 changes: 5 additions & 0 deletions lara-typescript/src/interactive-api-shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,8 @@ export interface IDataset {
export interface IInteractiveStateWithDataset {
dataset?: IDataset | null;
}

export interface IGetInteractiveState {
unloading?: boolean; // set to true to tell the interactive it is getting the final state
}
export type OnUnloadFunction<InteractiveState = {}> = (options: IGetInteractiveState) => Promise<InteractiveState>;
Loading

0 comments on commit f305b99

Please sign in to comment.