Newer
Older
import { search } from "./commands/util";
import { CallbackOptions, CommandType, OnCallback, OnIdent, SubscriptionCallback } from "./types";
/**
* Makes a router for storing resolve/reject for a message
*/
export const makeRouter = () => {
const routes = new Map();
return {
get: (id: string, cmd: CommandType) => {
const res = { ...routes.get(`${id}--${cmd}`) };
routes.delete(id);
return res;
},
add: (id: string, cmd: CommandType, data: any) => {
routes.set(`${id}--${cmd}`, { ...data });
},
};
};
const toId = (id: OnIdent): string => id.toString();
const isRegExp = (v: OnIdent): v is RegExp => typeof v === "object";
const satisfies = (id: OnIdent, matches: string): boolean =>
isRegExp(id) ? matches.match(id) !== null : id === matches;
export const makeCallbackRouter = () => {
const routes = new Map<
string,
>();
return {
return Array.from(routes.values())
.filter(({ ident }) => satisfies(ident, id))
.map((v) => v.callback);
},
add: (ident: OnIdent, callback: OnCallback) => {
routes.set(toId(ident), { callback, ident });
},
del: (id: OnIdent) => routes.delete(toId(id)),
};
};
export const parseCallbackOptions = (msg: Record<string, any>): CallbackOptions | null => {
const sendResp = search(msg, "sendResp")
if (typeof sendResp === "boolean") return { sendResp }
return null
}
export const knownUSPVersions = ["1.0", "1.1", "1.2"] as const;