forked from contentstack/app-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Amitkanswal/feat/MKT-7848-register-events
Feat/mkt 7848 register events
- Loading branch information
Showing
9 changed files
with
1,510 additions
and
1,342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.