Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/core/cri/deno-cri/api.js
3587 views
1
/*
2
* api.js
3
*
4
* Copyright (c) 2021 Andrea Cardaci <[email protected]>
5
*
6
* Deno port Copyright (C) 2022 Posit Software, PBC
7
*/
8
9
function arrayToObject(parameters) {
10
const keyValue = {};
11
parameters.forEach((parameter) => {
12
const name = parameter.name;
13
delete parameter.name;
14
keyValue[name] = parameter;
15
});
16
return keyValue;
17
}
18
19
function decorate(to, category, object) {
20
to.category = category;
21
Object.keys(object).forEach((field) => {
22
// skip the 'name' field as it is part of the function prototype
23
if (field === "name") {
24
return;
25
}
26
// commands and events have parameters whereas types have properties
27
if (
28
(category === "type" && field === "properties") ||
29
field === "parameters"
30
) {
31
to[field] = arrayToObject(object[field]);
32
} else {
33
to[field] = object[field];
34
}
35
});
36
}
37
38
function addCommand(chrome, domainName, command) {
39
const commandName = `${domainName}.${command.name}`;
40
const handler = (params, sessionId, callback) => {
41
return chrome.send(commandName, params, sessionId, callback);
42
};
43
decorate(handler, "command", command);
44
chrome[commandName] = chrome[domainName][command.name] = handler;
45
}
46
47
function addEvent(chrome, domainName, event) {
48
const eventName = `${domainName}.${event.name}`;
49
const handler = (sessionId, handler) => {
50
if (typeof sessionId === "function") {
51
handler = sessionId;
52
sessionId = undefined;
53
}
54
const rawEventName = sessionId ? `${eventName}.${sessionId}` : eventName;
55
if (typeof handler === "function") {
56
chrome.on(rawEventName, handler);
57
return () => chrome.removeListener(rawEventName, handler);
58
} else {
59
return new Promise((fulfill, _reject) => {
60
chrome.once(rawEventName, fulfill);
61
});
62
}
63
};
64
decorate(handler, "event", event);
65
chrome[eventName] = chrome[domainName][event.name] = handler;
66
}
67
68
function addType(chrome, domainName, type) {
69
const typeName = `${domainName}.${type.id}`;
70
const help = {};
71
decorate(help, "type", type);
72
chrome[typeName] = chrome[domainName][type.id] = help;
73
}
74
75
export function prepare(object, protocol) {
76
// assign the protocol and generate the shorthands
77
object.protocol = protocol;
78
protocol.domains.forEach((domain) => {
79
const domainName = domain.domain;
80
object[domainName] = {};
81
// add commands
82
(domain.commands || []).forEach((command) => {
83
addCommand(object, domainName, command);
84
});
85
// add events
86
(domain.events || []).forEach((event) => {
87
addEvent(object, domainName, event);
88
});
89
// add types
90
(domain.types || []).forEach((type) => {
91
addType(object, domainName, type);
92
});
93
// add utility listener for each domain
94
object[domainName].on = (eventName, handler) => {
95
return object[domainName][eventName](handler);
96
};
97
});
98
}
99
100