...
 
Commits (2)
  • lolimay's avatar
    Apps.engine.ui.sdk (#132) · 658c4686
    lolimay authored
    * update IWebHooks interface
    
    * Add util randomString
    
    * add AppEmbeddedSDK
    
    * move constants to src/client/constants directory
    
    * separate initListener from call method
    * rename initListener to init
    658c4686
  • Douglas Gubert's avatar
    Bump alpha version · 8d67b452
    Douglas Gubert authored
    8d67b452
{
"name": "@rocket.chat/apps-engine",
"version": "1.5.2",
"version": "1.6.0-alpha.2",
"description": "The engine code for the Rocket.Chat Apps which manages, runs, translates, coordinates and all of that.",
"main": "index",
"typings": "index",
......
import { ACTION_ID_LENGTH } from './constants';
import { randomString } from './utils';
export class AppEmbeddedSDK {
private listener: (this: Window, ev: MessageEvent) => any;
private callbacks: Map<string, (response: any) => any>;
constructor() {
this.listener = () => console.log('init');
this.callbacks = new Map();
}
public getUserInfo(): Promise<any> {
return this.call(AppEmbeddedSDKActions.GET_USER_INFO);
}
public getRoomInfo(): Promise<any> {
return this.call(AppEmbeddedSDKActions.GET_ROOM_INFO);
}
/**
* Initialize the App Embedded SDK for communicating with Rocket.Chat
*/
public init(): void {
this.listener = ({ data }) => {
if (!data.hasOwnProperty('rcEmbeddedSDK')) {
return;
}
const { rcEmbeddedSDK: { id, payload } } = data;
if (this.callbacks.has(id)) {
const resolve = this.callbacks.get(id);
if (typeof resolve === 'function') {
resolve(payload);
}
this.callbacks.delete(id);
}
};
window.addEventListener('message', this.listener);
}
private call(action: string, payload?: any ): Promise<any> {
return new Promise((resolve) => {
const id = randomString(ACTION_ID_LENGTH);
window.parent.postMessage({ rcEmbeddedSDK: { action, payload, id } }, '*');
this.callbacks.set(id, resolve);
});
}
}
export enum AppEmbeddedSDKActions {
GET_USER_INFO = 'getUserInfo',
GET_ROOM_INFO = 'getRoomInfo',
}
/**
* The id length of each action.
*/
export const ACTION_ID_LENGTH = 80;
/**
* Generate a random string with the specified length.
* @param length the length for the generated random string.
*/
export function randomString(length: number): string {
const buffer: Array<string> = [];
const chars: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < length; i++) {
buffer.push(chars[getRandomInt(chars.length)]);
}
return buffer.join('');
}
function getRandomInt(max: number): number {
return Math.floor(Math.random() * Math.floor(max));
}
......@@ -5,5 +5,6 @@
export interface IWebHooks {
// When a game is closed, Rocket.Chat will post all
// relevant information to that URL.
sessionStarts?: string;
sessionEnds?: string;
}
{
"name": "@rocket.chat/apps-ts-definition",
"version": "1.4.2",
"version": "1.5.2",
"description": "Contains the TypeScript definitions for the Rocket.Chat Applications.",
"main": "index.js",
"typings": "index",
......