2021-05-28 00:24:08 +02:00
|
|
|
import type * as tg from "generic-type-guard";
|
2021-07-02 16:41:24 +02:00
|
|
|
import type {
|
|
|
|
IframeEvent,
|
2021-07-02 18:49:22 +02:00
|
|
|
IframeEventMap,
|
|
|
|
IframeQuery,
|
2021-07-02 16:41:24 +02:00
|
|
|
IframeQueryMap,
|
2021-07-02 18:49:22 +02:00
|
|
|
IframeResponseEventMap,
|
|
|
|
} from "../Events/IframeEvent";
|
|
|
|
import type { IframeQueryWrapper } from "../Events/IframeEvent";
|
2021-05-28 00:24:08 +02:00
|
|
|
|
|
|
|
export function sendToWorkadventure(content: IframeEvent<keyof IframeEventMap>) {
|
2021-07-02 18:49:22 +02:00
|
|
|
window.parent.postMessage(content, "*");
|
2021-05-28 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
2021-07-02 16:41:24 +02:00
|
|
|
let queryNumber = 0;
|
2021-05-28 00:24:08 +02:00
|
|
|
|
2021-07-02 18:49:22 +02:00
|
|
|
export const answerPromises = new Map<
|
|
|
|
number,
|
|
|
|
{
|
|
|
|
resolve: (
|
|
|
|
value:
|
|
|
|
| IframeQueryMap[keyof IframeQueryMap]["answer"]
|
|
|
|
| PromiseLike<IframeQueryMap[keyof IframeQueryMap]["answer"]>
|
|
|
|
) => void;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
reject: (reason?: any) => void;
|
|
|
|
}
|
|
|
|
>();
|
2021-07-02 16:41:24 +02:00
|
|
|
|
2021-07-02 18:49:22 +02:00
|
|
|
export function queryWorkadventure<T extends keyof IframeQueryMap>(
|
|
|
|
content: IframeQuery<T>
|
|
|
|
): Promise<IframeQueryMap[T]["answer"]> {
|
|
|
|
return new Promise<IframeQueryMap[T]["answer"]>((resolve, reject) => {
|
|
|
|
window.parent.postMessage(
|
|
|
|
{
|
|
|
|
id: queryNumber,
|
|
|
|
query: content,
|
|
|
|
} as IframeQueryWrapper<T>,
|
|
|
|
"*"
|
|
|
|
);
|
2021-07-02 16:41:24 +02:00
|
|
|
|
|
|
|
answerPromises.set(queryNumber, {
|
|
|
|
resolve,
|
2021-07-02 18:49:22 +02:00
|
|
|
reject,
|
2021-07-02 16:41:24 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
queryNumber++;
|
|
|
|
});
|
2021-05-28 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
2021-07-02 18:49:22 +02:00
|
|
|
type GuardedType<Guard extends tg.TypeGuard<unknown>> = Guard extends tg.TypeGuard<infer T> ? T : never;
|
2021-05-28 02:28:11 +02:00
|
|
|
|
2021-07-02 18:49:22 +02:00
|
|
|
export interface IframeCallback<
|
|
|
|
Key extends keyof IframeResponseEventMap,
|
|
|
|
T = IframeResponseEventMap[Key],
|
|
|
|
Guard = tg.TypeGuard<T>
|
|
|
|
> {
|
|
|
|
typeChecker: Guard;
|
|
|
|
callback: (payloadData: T) => void;
|
2021-05-28 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
2021-05-28 02:28:11 +02:00
|
|
|
export interface IframeCallbackContribution<Key extends keyof IframeResponseEventMap> extends IframeCallback<Key> {
|
2021-07-02 18:49:22 +02:00
|
|
|
type: Key;
|
2021-05-28 02:28:11 +02:00
|
|
|
}
|
|
|
|
|
2021-05-28 00:24:08 +02:00
|
|
|
/**
|
|
|
|
* !! be aware that the implemented attributes (addMethodsAtRoot and subObjectIdentifier) must be readonly
|
2021-06-18 17:22:56 +02:00
|
|
|
*
|
|
|
|
*
|
2021-05-28 00:24:08 +02:00
|
|
|
*/
|
|
|
|
|
2021-07-02 18:49:22 +02:00
|
|
|
export abstract class IframeApiContribution<
|
|
|
|
T extends {
|
|
|
|
callbacks: Array<IframeCallbackContribution<keyof IframeResponseEventMap>>;
|
|
|
|
}
|
|
|
|
> {
|
|
|
|
abstract callbacks: T["callbacks"];
|
2021-06-18 17:22:56 +02:00
|
|
|
}
|