Merge branch 'next' into next-reply

This commit is contained in:
Chi Vinh Le
2018-09-06 21:38:20 +02:00
15 changed files with 18828 additions and 54 deletions
@@ -23,7 +23,7 @@ export interface TalkContext {
/** formatter for timeago. */
timeagoFormatter?: Formatter;
/** Session Storage */
/** Local Storage */
localStorage: Storage;
/** Session storage */
@@ -1,10 +1,71 @@
import { Child, Parent } from "pym.js";
import uuid from "uuid/v4";
import { PromisifiedStorage } from "./PromisifiedStorage";
type Pym = Child | Parent;
class PymStorage implements PromisifiedStorage {
/** Instance to pym */
private pym: Pym;
/** Requested storage type */
private type: string;
/** A Map of requestID => {resolve, reject} */
private requests: Record<
string,
{ resolve: ((v: any) => void); reject: ((v: any) => void) }
> = {};
/** Requests method with parameters over pym. */
private call<T>(
method: string,
parameters: { key: string; value?: string }
): Promise<T> {
const id = uuid();
return new Promise((resolve, reject) => {
this.requests[id] = { resolve, reject };
this.pym.sendMessage(
`pymStorage.${this.type}.request`,
JSON.stringify({ id, method, parameters })
);
});
}
/** Listen to pym responses */
private listen() {
// Receive successful responses.
this.pym.onMessage(`pymStorage.${this.type}.response`, (msg: string) => {
const { id, result } = JSON.parse(msg);
this.requests[id].resolve(result);
delete this.requests[id];
});
// Receive error responses.
this.pym.onMessage(`pymStorage.${this.type}.error`, (msg: string) => {
const { id, error } = JSON.parse(msg);
this.requests[id].reject(new Error(error));
delete this.requests[id];
});
}
constructor(pym: Pym, type: string) {
this.pym = pym;
this.type = type;
this.listen();
}
public setItem(key: string, value: string) {
return this.call<void>("setItem", { key, value });
}
public getItem(key: string) {
return this.call<string | null>("getItem", { key });
}
public removeItem(key: string) {
return this.call<void>("removeItem", { key });
}
}
/**
* Creates a storage that put requests onto pym.
* This is the counterpart of `connectStorageToPym`.
@@ -14,45 +75,6 @@ type Pym = Child | Parent;
export default function createPymStorage(
pym: Pym,
type: "localStorage" | "sessionStorage"
): PromisifiedStorage {
// A Map of requestID => {resolve, reject}
const requests: Record<
string,
{ resolve: ((v: any) => void); reject: ((v: any) => void) }
> = {};
// Requests method with parameters over pym.
const call = <T>(
method: string,
parameters: { key: string; value?: string }
): Promise<T> => {
const id = uuid();
return new Promise((resolve, reject) => {
requests[id] = { resolve, reject };
pym.sendMessage(
`pymStorage.${type}.request`,
JSON.stringify({ id, method, parameters })
);
});
};
// Receive successful responses.
pym.onMessage(`pymStorage.${type}.response`, (msg: string) => {
const { id, result } = JSON.parse(msg);
requests[id].resolve(result);
delete requests[id];
});
// Receive error responses.
pym.onMessage(`pymStorage.${type}.error`, (msg: string) => {
const { id, error } = JSON.parse(msg);
requests[id].reject(new Error(error));
delete requests[id];
});
return {
setItem: (key: string, value: string) => call("setItem", { key, value }),
getItem: (key: string) => call("getItem", { key }),
removeItem: (key: string) => call("removeItem", { key }),
};
): PymStorage {
return new PymStorage(pym, type);
}