Skip to content

Commit

Permalink
Merge pull request #4 from Amitkanswal/feat/MKT-7848-register-events
Browse files Browse the repository at this point in the history
Feat/mkt 7848 register events
  • Loading branch information
Amitkanswal authored Sep 25, 2024
2 parents 41c5f25 + 0fbadbf commit 6ebe9d4
Show file tree
Hide file tree
Showing 9 changed files with 1,510 additions and 1,342 deletions.
12 changes: 8 additions & 4 deletions __test__/entry.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import postRobot from "post-robot";
import Entry from "../src/entry";
import { GenericObjectType } from "../src/types/common.types";
import RegisterEvents from '../src/registerEvents';
import testData from "./data/testData.json";
import { jest } from "@jest/globals";

Expand All @@ -8,6 +9,7 @@ describe("Entry", () => {
let emitter: any;
let entry: Entry;
let sendToParent: any;
const eventRegistry = new RegisterEvents({connection: postRobot,installationUID:"", appUID:"", locationUID:""})

beforeEach(() => {
sendToParent = () => {};
Expand All @@ -28,12 +30,14 @@ describe("Entry", () => {
jest.spyOn(emitter, "on");

const changedData = JSON.parse(JSON.stringify(testData));

changedData.entry.title = "changed title";

entry = new Entry(
{ ...testData, changedData } as any,
connection as any,
emitter
emitter,
eventRegistry
);
});

Expand Down Expand Up @@ -134,7 +138,7 @@ describe("Entry", () => {
});
it("should use custom Field instance if internal flag is set", () => {
const fieldInstance: any = jest.fn();
entry = new Entry(testData as any, connection as any, emitter, {
entry = new Entry(testData as any, connection as any, emitter,eventRegistry ,{
_internalFlags: {
FieldInstance: fieldInstance,
},
Expand Down Expand Up @@ -172,7 +176,7 @@ describe("Entry", () => {
it("getField within Create page", function () {
const dataWithoutEntry = JSON.parse(JSON.stringify(testData));
dataWithoutEntry.entry = {};
entry = new Entry(dataWithoutEntry, connection as any, emitter);
entry = new Entry(dataWithoutEntry, connection as any, emitter, eventRegistry);
expect(() => entry.getField("invaliduid")).toThrowError(
"The data is unsaved. Save the data before requesting the field."
);
Expand Down
8 changes: 6 additions & 2 deletions __test__/fieldModifierLocation/entry.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import postRobot from 'post-robot';
import FieldModifierLocationEntry from "../../src/fieldModifierLocation/entry";
import testData from "../data/testData.json";
import { IFieldModifierLocationInitData, LocationType } from "../../src/types";
import generateErrorMessages, {
ERROR_MESSAGES,
} from "../../src/utils/errorMessages";
import RegisterEvents from '../../src/registerEvents';

describe("FieldModifierLocationEntry", () => {
let entryInstance: FieldModifierLocationEntry;
let sendToParent: any;
let connection: { sendToParent: (...props: any[]) => any };

const eventRegistry = new RegisterEvents({connection: postRobot,installationUID:"", appUID:"", locationUID:""});

let emitter: any;
function getEntryInitialData(): IFieldModifierLocationInitData {
return {
Expand Down Expand Up @@ -93,7 +96,8 @@ describe("FieldModifierLocationEntry", () => {
entryInstance = new FieldModifierLocationEntry(
entryIntialData,
connection as any,
emitter
emitter,
eventRegistry
);
});

Expand Down
1,788 changes: 880 additions & 908 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/app-sdk",
"version": "2.0.3",
"version": "2.0.4",
"types": "dist/src/index.d.ts",
"description": "The Contentstack App SDK allows you to customize your Contentstack applications.",
"main": "dist/index.js",
Expand All @@ -12,6 +12,7 @@
"test:watch": "jest --watchAll",
"test:coverage": "jest --coverage --coverageDirectory=\"coverage\"",
"dev": "webpack --watch --mode development",
"dev:build": "npm run build && cp -r dist ../../marketplace-app-boilerplate/node_modules/@contentstack/app-sdk/dist",
"lint": "eslint . --ext .ts --max-warnings=0",
"docs": "typedoc",
"prettier": "npx prettier src --check",
Expand Down
8 changes: 8 additions & 0 deletions src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from "./types/entry.types";
import { ContentType, PublishDetails, Schema } from "./types/stack.types";
import { GenericObjectType } from "./types/common.types";
import RegisterEvents from './registerEvents';

/** Class representing an entry from Contentstack UI. Not available for Dashboard UI Location. */

Expand All @@ -31,6 +32,7 @@ class Entry {
_emitter: EventEmitter;
_changedData?: GenericObjectType;
_options: IEntryOptions;
_eventRegistry: RegisterEvents;

constructor(
initializationData:
Expand All @@ -40,6 +42,7 @@ class Entry {
| IFieldModifierLocationInitData,
connection: typeof postRobot,
emitter: EventEmitter,
eventRegistry: RegisterEvents,
options?: IEntryOptions
) {
/**
Expand All @@ -49,6 +52,7 @@ class Entry {
this.content_type = initializationData.content_type;

this._data = initializationData.entry;
this._eventRegistry = eventRegistry

if (
(initializationData as IFieldModifierLocationInitData).changedData
Expand Down Expand Up @@ -226,6 +230,7 @@ class Entry {
onSave(callback: (arg0: EntryType) => void) {
const entryObj = this;
if (callback && typeof callback === "function") {
this._eventRegistry.insertEvent("extensionEvent", "entrySave");
entryObj._emitter.on("entrySave", (event: { data: EntryType }) => {
callback(event.data);
});
Expand All @@ -242,6 +247,7 @@ class Entry {
onChange(callback: IOnEntryChangeCallback) {
const entryObj = this;
if (callback && typeof callback === "function") {
this._eventRegistry.insertEvent("extensionEvent", "entryChange");
entryObj._emitter.on(
"entryChange",
(event: {
Expand All @@ -264,6 +270,7 @@ class Entry {
onPublish(callback: (arg0: PublishDetails) => void) {
const entryObj = this;
if (callback && typeof callback === "function") {
this._eventRegistry.insertEvent("extensionEvent", "entryPublish");
entryObj._emitter.on(
"entryPublish",
(event: { data: PublishDetails }) => {
Expand All @@ -283,6 +290,7 @@ class Entry {
onUnPublish(callback: (arg0: PublishDetails) => void) {
const entryObj = this;
if (callback && typeof callback === "function") {
this._eventRegistry.insertEvent("extensionEvent", "entryUnPublish");
entryObj._emitter.on(
"entryUnPublish",
(event: { data: PublishDetails }) => {
Expand Down
6 changes: 4 additions & 2 deletions src/fieldModifierLocation/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import { IFieldModifierLocationInitData } from "../types";
import { IGetTagsOptions } from "../types/entry.types";
import generateErrorMessages, { ERROR_MESSAGES } from "../utils/errorMessages";
import Field from "./field";
import RegisterEvents from '../registerEvents';

class FieldModifierLocationEntry extends Entry {
constructor(
initializationData: IFieldModifierLocationInitData,
connection: typeof postRobot,
emitter: EventEmitter
emitter: EventEmitter,
eventRegistry: RegisterEvents
) {
super(initializationData, connection, emitter, {
super(initializationData, connection, emitter,eventRegistry ,{
_internalFlags: {
FieldInstance: Field as any,
},
Expand Down
108 changes: 108 additions & 0 deletions src/registerEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import postRobot from "post-robot";
class RegisterEvents {
private events: { [key: string]: Set<string> } = {};
_connection: typeof postRobot;
installationUID: string;
appUID: string;
locationUID: string;
private debounceTimeout: number = 0;

constructor({
connection,
installationUID,
appUID,
locationUID,
}: {
connection: typeof postRobot;
installationUID: string;
appUID: string;
locationUID: string;
}) {
this.events = this.createObservable(this.events);
this._connection = connection;
this.installationUID = installationUID;
this.appUID = appUID;
this.locationUID = locationUID;
}

private createObservable(payload: { [key: string]: Set<string> }) {
return new Proxy(payload, {
set: (target, property, value) => {
// if (typeof property === "string") {
// target[property as string] = value;
this.onChange(target, "set");
// }
return true;
},
deleteProperty: (target, property) => {
if (property in target) {
if (typeof property === "string") {
delete target[property];
}
this.onChange(target, "delete");
}
return true;
},
});
}

private onChange(events: { [key: string]: Set<string> }, action: string) {
this._connection.sendToParent("registeredEvents", {
[this.installationUID]: {
appUID: this.appUID,
locationUID: this.locationUID,
// registeredEvents: this.events,
action,
},
});
}

// private debouncedOnChange = this.debounce(this.onChange.bind(this), 300);

// private debounce(callbackFunction: (...args: any[]) => void, wait: number) {
// return (...args: any[]) => {
// clearTimeout(this.debounceTimeout);
// this.debounceTimeout = window.setTimeout(() => callbackFunction(...args), wait);
// };
// }

insertEvent(eventName: string, eventType: string) {
console.log("eventName",eventName);
console.log("eventType",eventType);

if (!this.events[eventName]) {
this.events[eventName] = new Set();
}

const prevLength = this.events[eventName].size;
this.events[eventName].add(eventType);
// if (this.events[eventName].size !== prevLength) {
this.onChange(this.events, "insert");
// }
console.log("event",this.events);

}

hasEvent(eventName: string, eventType: string) {
return this.events[eventName]?.has(eventType);
}

removeEvent(eventName: string, eventType: string) {
if (this.events[eventName]) {
const prevSize = this.events[eventName].size;
this.events[eventName].delete(eventType);
if (this.events[eventName].size !== prevSize) {
if (this.events[eventName].size === 0) {
delete this.events[eventName];
}
this.onChange(this.events, "remove");
}
}
}

getRegisterEvents() {
return this.events;
}
}

export default RegisterEvents;
35 changes: 31 additions & 4 deletions src/uiLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import FieldModifierLocationField from "./fieldModifierLocation/field";
import FieldModifierLocationFrame from "./fieldModifierLocation/frame";
import Metadata from "./metadata";
import Modal from "./modal";
import RegisterEvents from "./registerEvents";
import Stack from "./stack";
import Store from "./store";
import {
Expand Down Expand Up @@ -95,6 +96,8 @@ class UiLocation {
*/
modal: Modal;

eventRegistry: RegisterEvents;

/**
* The Contentstack Region on which the app is running.
*/
Expand Down Expand Up @@ -139,6 +142,13 @@ class UiLocation {

this.metadata = new Metadata(postRobot);

this.eventRegistry = new RegisterEvents({
connection: this.postRobot,
installationUID: this.installationUID,
appUID: this.appUID,
locationUID: this.locationUID,
});

this.config = initializationData.config ?? {};

this.location = {
Expand Down Expand Up @@ -182,7 +192,12 @@ class UiLocation {
}
case LocationType.WIDGET: {
this.location.SidebarWidget = {
entry: new Entry(initializationData, postRobot, emitter),
entry: new Entry(
initializationData,
postRobot,
emitter,
this.eventRegistry
),
stack: new Stack(initializationData.stack, postRobot, {
currentBranch: initializationData.currentBranch,
}),
Expand Down Expand Up @@ -223,7 +238,8 @@ class UiLocation {
entry: new Entry(
initializationData as IRTEInitData,
postRobot,
emitter
emitter,
this.eventRegistry
),
};
});
Expand All @@ -236,7 +252,8 @@ class UiLocation {
entry: new FieldModifierLocationEntry(
initializationData,
postRobot,
emitter
emitter,
this.eventRegistry
),
stack: new Stack(initializationData.stack, postRobot, {
currentBranch: initializationData.currentBranch,
Expand Down Expand Up @@ -264,7 +281,12 @@ class UiLocation {
this.location.CustomField = {
field: new Field(initializationData, postRobot, emitter),
fieldConfig: initializationData.field_config,
entry: new Entry(initializationData, postRobot, emitter),
entry: new Entry(
initializationData,
postRobot,
emitter,
this.eventRegistry
),
stack: new Stack(initializationData.stack, postRobot, {
currentBranch: initializationData.currentBranch,
}),
Expand All @@ -281,6 +303,10 @@ class UiLocation {

try {
postRobot.on("extensionEvent", async (event) => {
// this.eventRegistry.insertEvent(
// "extensionEvent",
// event.data.name
// );
if (event.data.name === "entrySave") {
emitter.emitEvent("entrySave", [{ data: event.data.data }]);
emitter.emitEvent("updateFields", [
Expand Down Expand Up @@ -345,6 +371,7 @@ class UiLocation {
{ data: event.data.data },
]);
}
// this.registeredEvents();
});
} catch (err) {
console.error("Extension Event", err);
Expand Down
Loading

0 comments on commit 6ebe9d4

Please sign in to comment.