Webview: init api event registry
This commit is contained in:
parent
0cf86806b2
commit
149be249fa
|
@ -11,6 +11,10 @@ export type SuccessData<T extends OperationNames> = Extract<
|
||||||
OperationResponse<T>,
|
OperationResponse<T>,
|
||||||
{ status: "success" }
|
{ status: "success" }
|
||||||
>;
|
>;
|
||||||
|
export type ErrorData<T extends OperationNames> = Extract<
|
||||||
|
OperationResponse<T>,
|
||||||
|
{ status: "error" }
|
||||||
|
>;
|
||||||
|
|
||||||
export type ClanOperations = {
|
export type ClanOperations = {
|
||||||
[K in OperationNames]: (str: string) => void;
|
[K in OperationNames]: (str: string) => void;
|
||||||
|
@ -33,6 +37,27 @@ declare global {
|
||||||
// Make sure window.webkit is defined although the type is not correctly filled yet.
|
// Make sure window.webkit is defined although the type is not correctly filled yet.
|
||||||
window.clan = {} as ClanOperations;
|
window.clan = {} as ClanOperations;
|
||||||
|
|
||||||
|
const operations = schema.properties;
|
||||||
|
const operationNames = Object.keys(operations) as OperationNames[];
|
||||||
|
|
||||||
|
type ObserverRegistry = {
|
||||||
|
[K in OperationNames]: ((response: OperationResponse<K>) => void)[];
|
||||||
|
};
|
||||||
|
const obs: ObserverRegistry = operationNames.reduce(
|
||||||
|
(acc, opName) => ({
|
||||||
|
...acc,
|
||||||
|
[opName]: [],
|
||||||
|
}),
|
||||||
|
{} as ObserverRegistry
|
||||||
|
);
|
||||||
|
|
||||||
|
interface ReceiveOptions {
|
||||||
|
/**
|
||||||
|
* Calls only the registered function that has the same key as used with dispatch
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
fnKey: string;
|
||||||
|
}
|
||||||
function createFunctions<K extends OperationNames>(
|
function createFunctions<K extends OperationNames>(
|
||||||
operationName: K
|
operationName: K
|
||||||
): {
|
): {
|
||||||
|
@ -41,24 +66,27 @@ function createFunctions<K extends OperationNames>(
|
||||||
} {
|
} {
|
||||||
return {
|
return {
|
||||||
dispatch: (args: OperationArgs<K>) => {
|
dispatch: (args: OperationArgs<K>) => {
|
||||||
console.log(
|
// console.log(
|
||||||
`Operation: ${String(operationName)}, Arguments: ${JSON.stringify(args)}`
|
// `Operation: ${String(operationName)}, Arguments: ${JSON.stringify(args)}`
|
||||||
);
|
// );
|
||||||
// Send the data to the gtk app
|
// Send the data to the gtk app
|
||||||
window.webkit.messageHandlers.gtk.postMessage({
|
window.webkit.messageHandlers.gtk.postMessage({
|
||||||
method: operationName,
|
method: operationName,
|
||||||
data: args,
|
data: args,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
receive: (fn: (response: OperationResponse<K>) => void) => {
|
receive: (
|
||||||
window.clan[operationName] = deserialize(fn);
|
fn: (response: OperationResponse<K>) => void
|
||||||
|
// options?: ReceiveOptions
|
||||||
|
) => {
|
||||||
|
obs[operationName].push(fn);
|
||||||
|
window.clan[operationName] = (s: string) => {
|
||||||
|
obs[operationName].forEach((f) => deserialize(f)(s));
|
||||||
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const operations = schema.properties;
|
|
||||||
const operationNames = Object.keys(operations) as OperationNames[];
|
|
||||||
|
|
||||||
type PyApi = {
|
type PyApi = {
|
||||||
[K in OperationNames]: {
|
[K in OperationNames]: {
|
||||||
dispatch: (args: OperationArgs<K>) => void;
|
dispatch: (args: OperationArgs<K>) => void;
|
||||||
|
@ -70,7 +98,6 @@ const deserialize =
|
||||||
<T>(fn: (response: T) => void) =>
|
<T>(fn: (response: T) => void) =>
|
||||||
(str: string) => {
|
(str: string) => {
|
||||||
try {
|
try {
|
||||||
console.debug("Received data: ", str);
|
|
||||||
fn(JSON.parse(str) as T);
|
fn(JSON.parse(str) as T);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
alert(`Error parsing JSON: ${e}`);
|
alert(`Error parsing JSON: ${e}`);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user