Skip to content

Commit

Permalink
KOGITO-3042: Replace MessageBusClient with proxified version of ApiTo…
Browse files Browse the repository at this point in the history
…Consume (#250)

* Proxyfied clientApi

* Replace MessageBusClient by MessageBusClientApi

* Remove unnecessary test

* Formatting

* Cleanup

* Fixing a doc

* Fix tests

* Remove KogitoEditorEnvelopeServer

* Fix tests
  • Loading branch information
tiagobento committed Aug 17, 2020
1 parent c71b84c commit 0d81983
Show file tree
Hide file tree
Showing 35 changed files with 284 additions and 260 deletions.
5 changes: 3 additions & 2 deletions packages/editor/src/api/EditorFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ import { KogitoEditorEnvelopeContextType } from "./KogitoEditorEnvelopeContext";
export interface EditorFactory {
/**
* Returns true if the factory supports a language.
* @param fileExtension The custom LanguageData
* @param fileExtension The extension of a file (e.g. "txt").
*/
supports(fileExtension: string): boolean;

/**
* Returns an Editor instance.
* Receives a messageBus to be used by the Editor to communicate with the outside of the envelope.
* @param envelopeContext The context to be used by Editor implementation.
* @param initArgs Initial arguments required for the Editor to initialize itself properly.
*/
createEditor(envelopeContext: KogitoEditorEnvelopeContextType, initArgs: EditorInitArgs): Promise<Editor>;
}
4 changes: 2 additions & 2 deletions packages/editor/src/api/KogitoEditorEnvelopeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

import * as React from "react";
import { useContext } from "react";
import { MessageBusClient } from "@kogito-tooling/envelope-bus/dist/api";
import { MessageBusClientApi } from "@kogito-tooling/envelope-bus/dist/api";
import { KogitoEditorChannelApi } from "./KogitoEditorChannelApi";
import { EditorContext } from "./EditorContext";
import { DefaultKeyboardShortcutsService } from "@kogito-tooling/keyboard-shortcuts/dist/envelope";
import { I18nService } from "./I18nService";

export interface KogitoEditorEnvelopeContextType {
channelApi: MessageBusClient<KogitoEditorChannelApi>;
channelApi: MessageBusClientApi<KogitoEditorChannelApi>;
context: EditorContext;
services: {
keyboardShortcuts: DefaultKeyboardShortcutsService;
Expand Down
52 changes: 0 additions & 52 deletions packages/editor/src/channel/KogitoEditorEnvelopeServer.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { EmbeddedEditor, EmbeddedEditorRef } from "../../embedded";
import { incomingMessage } from "./EmbeddedEditorTestUtils";
import { render } from "@testing-library/react";
import { EnvelopeBusMessagePurpose } from "@kogito-tooling/envelope-bus/dist/api";
import { KogitoEditorEnvelopeServer } from "../../../channel";

describe("EmbeddedEditor::ONLINE", () => {
const file: File = {
Expand All @@ -40,10 +39,8 @@ describe("EmbeddedEditor::ONLINE", () => {

const channelType = ChannelType.ONLINE;
const editorRef = React.createRef<EmbeddedEditorRef>();
const envelopeServerId = "test-bus-id";

beforeEach(() => {
jest.spyOn(EnvelopeServer.prototype, "generateRandomId").mockReturnValue(envelopeServerId);
jest.clearAllMocks();
});

Expand All @@ -66,7 +63,6 @@ describe("EmbeddedEditor::ONLINE", () => {
});

test("EmbeddedEditor::setContent", () => {
const spyRespond_contentRequest = jest.spyOn(KogitoEditorEnvelopeServer.prototype, "notify_contentChanged");
render(
<EmbeddedEditor
ref={editorRef}
Expand All @@ -76,16 +72,18 @@ describe("EmbeddedEditor::ONLINE", () => {
locale={"en"}
/>
);

const spyOnContentChangedNotification = jest.spyOn(
editorRef.current!.getEnvelopeServer().envelopeApi.notifications,
"receive_contentChanged"
);

editorRef.current?.setContent("content", "");

expect(spyRespond_contentRequest).toBeCalledWith({ content: "content" });
expect(spyOnContentChangedNotification).toBeCalledWith({ content: "content" });
});

test("EmbeddedEditor::requestContent", () => {
const spyRequest_contentResponse = jest.spyOn(
KogitoEditorEnvelopeServer.prototype,
"request_contentResponse"
);
render(
<EmbeddedEditor
ref={editorRef}
Expand All @@ -95,16 +93,17 @@ describe("EmbeddedEditor::ONLINE", () => {
locale={"en"}
/>
);

const spyRequest_contentResponse = jest.spyOn(
editorRef.current!.getEnvelopeServer().envelopeApi.requests,
"receive_contentRequest"
);
editorRef.current?.getContent();

expect(spyRequest_contentResponse).toBeCalled();
});

test("EmbeddedEditor::requestPreview", () => {
const spyRequest_previewResponse = jest.spyOn(
KogitoEditorEnvelopeServer.prototype,
"request_previewResponse"
);
render(
<EmbeddedEditor
ref={editorRef}
Expand All @@ -114,6 +113,11 @@ describe("EmbeddedEditor::ONLINE", () => {
locale={"en"}
/>
);

const spyRequest_previewResponse = jest.spyOn(
editorRef.current!.getEnvelopeServer().envelopeApi.requests,
"receive_previewRequest"
);
editorRef.current?.getPreview();

expect(spyRequest_previewResponse).toBeCalled();
Expand All @@ -134,7 +138,7 @@ describe("EmbeddedEditor::ONLINE", () => {
);

await incomingMessage({
envelopeServerId: envelopeServerId,
envelopeServerId: editorRef.current!.getEnvelopeServer().id,
purpose: EnvelopeBusMessagePurpose.NOTIFICATION,
type: "receive_setContentError",
data: []
Expand All @@ -159,7 +163,7 @@ describe("EmbeddedEditor::ONLINE", () => {
);

await incomingMessage({
envelopeServerId: envelopeServerId,
envelopeServerId: editorRef.current!.getEnvelopeServer().id,
purpose: EnvelopeBusMessagePurpose.NOTIFICATION,
type: "receive_ready",
data: []
Expand All @@ -184,7 +188,7 @@ describe("EmbeddedEditor::ONLINE", () => {
);

await incomingMessage({
envelopeServerId: envelopeServerId,
envelopeServerId: editorRef.current!.getEnvelopeServer().id,
requestId: "1",
purpose: EnvelopeBusMessagePurpose.REQUEST,
type: "receive_resourceContentRequest",
Expand All @@ -210,7 +214,7 @@ describe("EmbeddedEditor::ONLINE", () => {
);

await incomingMessage({
envelopeServerId: envelopeServerId,
envelopeServerId: editorRef.current!.getEnvelopeServer().id,
requestId: "1",
purpose: EnvelopeBusMessagePurpose.REQUEST,
type: "receive_resourceListRequest",
Expand All @@ -236,7 +240,7 @@ describe("EmbeddedEditor::ONLINE", () => {
);

await incomingMessage({
envelopeServerId: envelopeServerId,
envelopeServerId: editorRef.current!.getEnvelopeServer().id,
purpose: EnvelopeBusMessagePurpose.NOTIFICATION,
type: "receive_newEdit",
data: [new KogitoEdit("1")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as React from "react";
import { renderHook } from "@testing-library/react-hooks";
import { EmbeddedEditorRef } from "../../embedded";
import { useDirtyState, StateControl } from "../../stateControl";
import { StateControl, useDirtyState } from "../../stateControl";
import { act } from "react-test-renderer";

describe("useDirtyState", () => {
Expand All @@ -29,12 +29,13 @@ describe("useDirtyState", () => {
stateControl = new StateControl();
embeddedEditorRef = {
getStateControl: () => stateControl,
getEnvelopeServer: () => ({} as any),
undo: jest.fn(),
redo: jest.fn(),
getContent: jest.fn(),
getPreview: jest.fn(),
setContent: jest.fn(),
getElementPosition: jest.fn(),
getElementPosition: jest.fn()
};
editorRef = {
current: embeddedEditorRef
Expand Down
39 changes: 25 additions & 14 deletions packages/editor/src/embedded/embedded/EmbeddedEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { EditorApi, EditorEnvelopeLocator, KogitoEditorChannelApi } from "../../api";
import { EditorApi, EditorEnvelopeLocator, KogitoEditorChannelApi, KogitoEditorEnvelopeApi } from "../../api";
import { ChannelType } from "@kogito-tooling/channel-common-api";
import { useSyncedKeyboardEvents } from "@kogito-tooling/keyboard-shortcuts/dist/channel";
import { useGuidedTourPositionProvider } from "@kogito-tooling/guided-tour/dist/channel";
Expand All @@ -25,7 +25,7 @@ import { File, useEffectAfterFirstRender } from "../common";
import { StateControl } from "../stateControl";
import { KogitoEditorChannelApiImpl } from "./KogitoEditorChannelApiImpl";
import { useConnectedEnvelopeServer } from "@kogito-tooling/envelope-bus/dist/hooks";
import { KogitoEditorEnvelopeServer } from "../../channel";
import { EnvelopeServer } from "@kogito-tooling/envelope-bus/dist/channel";

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

Expand All @@ -48,7 +48,12 @@ export type Props = EmbeddedEditorChannelApiOverrides & {
/**
* Forward reference for the `EmbeddedEditor` to support consumers to call upon embedded operations.
*/
export type EmbeddedEditorRef = (EditorApi & { getStateControl(): StateControl }) | null;
export type EmbeddedEditorRef =
| (EditorApi & {
getStateControl(): StateControl;
getEnvelopeServer(): EnvelopeServer<KogitoEditorChannelApi, KogitoEditorEnvelopeApi>;
})
| null;

const containerStyles: CSS.Properties = {
display: "flex",
Expand Down Expand Up @@ -80,24 +85,28 @@ const RefForwardingEmbeddedEditor: React.RefForwardingComponent<EmbeddedEditorRe
}, [stateControl, props.file, props]);

const envelopeServer = useMemo(() => {
return new KogitoEditorEnvelopeServer(
return new EnvelopeServer<KogitoEditorChannelApi, KogitoEditorEnvelopeApi>(
{ postMessage: message => iframeRef.current?.contentWindow?.postMessage(message, "*") },
props.editorEnvelopeLocator.targetOrigin,
{ fileExtension: props.file.fileExtension, resourcesPathPrefix: envelopeMapping?.resourcesPathPrefix ?? "" }
self =>
self.envelopeApi.requests.receive_initRequest(
{ origin: self.origin, envelopeServerId: self.id },
{ fileExtension: props.file.fileExtension, resourcesPathPrefix: envelopeMapping?.resourcesPathPrefix ?? "" }
)
);
}, []);

useConnectedEnvelopeServer(envelopeServer, kogitoEditorChannelApiImpl);

useEffectAfterFirstRender(() => {
envelopeServer.client.notify("receive_localeChange", props.locale);
envelopeServer.envelopeApi.notifications.receive_localeChange(props.locale);
}, [props.locale]);

// Register position provider for Guided Tour
useGuidedTourPositionProvider(envelopeServer.client, iframeRef);
useGuidedTourPositionProvider(envelopeServer.envelopeApi, iframeRef);

// Forward keyboard events to the EditorEnvelope
useSyncedKeyboardEvents(envelopeServer.client);
useSyncedKeyboardEvents(envelopeServer.envelopeApi);

//Forward reference methods
useImperativeHandle(
Expand All @@ -109,13 +118,15 @@ const RefForwardingEmbeddedEditor: React.RefForwardingComponent<EmbeddedEditorRe

return {
getStateControl: () => stateControl,
getEnvelopeServer: () => envelopeServer,
getElementPosition: selector =>
envelopeServer.client.request("receive_guidedTourElementPositionRequest", selector),
redo: () => Promise.resolve(envelopeServer.notify_editorRedo()),
undo: () => Promise.resolve(envelopeServer.notify_editorUndo()),
getContent: () => envelopeServer.request_contentResponse().then(c => c.content),
getPreview: () => envelopeServer.request_previewResponse(),
setContent: async content => envelopeServer.notify_contentChanged({ content: content })
envelopeServer.envelopeApi.requests.receive_guidedTourElementPositionRequest(selector),
undo: () => Promise.resolve(envelopeServer.envelopeApi.notifications.receive_editorUndo()),
redo: () => Promise.resolve(envelopeServer.envelopeApi.notifications.receive_editorRedo()),
getContent: () => envelopeServer.envelopeApi.requests.receive_contentRequest().then(c => c.content),
getPreview: () => envelopeServer.envelopeApi.requests.receive_previewRequest(),
setContent: async content =>
envelopeServer.envelopeApi.notifications.receive_contentChanged({ content: content })
};
},
[envelopeServer]
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/envelope/KogitoEditorEnvelope.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class KogitoEditorEnvelope {
KogitoEditorEnvelopeContextType
> = new Envelope(args.bus),
private readonly context: KogitoEditorEnvelopeContextType = {
channelApi: envelope.busClient,
channelApi: envelope.channelApi,
context: args.editorContext,
services: {
keyboardShortcuts: keyboardShortcutsService,
Expand Down
8 changes: 4 additions & 4 deletions packages/editor/src/envelope/KogitoEditorEnvelopeApiImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ export class KogitoEditorEnvelopeApiImpl implements KogitoEditorEnvelopeApi {

this.args.view.setLoading();

const content = await this.args.envelopeContext.channelApi.request("receive_contentRequest");
const content = await this.args.envelopeContext.channelApi.requests.receive_contentRequest();

await this.editor
.setContent(content.path ?? "", content.content)
.finally(() => this.args.view.setLoadingFinished());

this.args.envelopeContext.channelApi.notify("receive_ready");
this.args.envelopeContext.channelApi.notifications.receive_ready();
};

public receive_contentChanged = (editorContent: EditorContent) => {
Expand Down Expand Up @@ -147,15 +147,15 @@ export class KogitoEditorEnvelopeApiImpl implements KogitoEditorEnvelopeApi {
"Edit | Redo last edit",
async () => {
this.editor.redo();
this.args.envelopeContext.channelApi.notify("receive_stateControlCommandUpdate", StateControlCommand.REDO);
this.args.envelopeContext.channelApi.notifications.receive_stateControlCommandUpdate(StateControlCommand.REDO);
}
);
this.args.envelopeContext.services.keyboardShortcuts.registerKeyPress(
"ctrl+z",
"Edit | Undo last edit",
async () => {
this.editor.undo();
this.args.envelopeContext.channelApi.notify("receive_stateControlCommandUpdate", StateControlCommand.UNDO);
this.args.envelopeContext.channelApi.notifications.receive_stateControlCommandUpdate(StateControlCommand.UNDO);
}
);
}
Expand Down
Loading

0 comments on commit 0d81983

Please sign in to comment.