generated from rweich/streamdeck-ts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.ts
70 lines (53 loc) · 1.75 KB
/
Plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { Streamdeck } from "@rweich/streamdeck-ts";
import { Settings } from "./Settings";
import { Action } from "./Action";
import { streamDurationAction } from "./actions/streamDurationAction";
import { liveChatAction } from "./actions/liveChatAction";
import { subscribersAction } from "./actions/subscribersAction";
let didSettingsLoad = false;
const plugin = new Streamdeck().plugin();
let settings: Settings = { apiEndpoint: "", apiKey: "" };
function loadSettings(context: string, actionId: string, event: string) {
didSettingsLoad = false;
const action = getAction(actionId);
action.prepare({ context, settings, plugin, event });
plugin.getGlobalSettings(plugin.pluginUUID!);
const timer = () => {
setTimeout(() => {
if (didSettingsLoad) {
action.run({ context, settings, plugin, event });
} else {
timer();
}
}, 100);
};
timer();
}
plugin.on("willAppear", ({ context, action }) => {
loadSettings(context, action, "willAppear");
});
plugin.on("keyDown", ({ context, action }) => {
loadSettings(context, action, "keyDown");
});
plugin.on("didReceiveGlobalSettings", ({ settings: rawSettings }) => {
settings = {
apiEndpoint: (rawSettings as Settings).apiEndpoint ?? "",
apiKey: (rawSettings as Settings).apiKey ?? "",
};
console.log("did receive settings", { settings });
didSettingsLoad = true;
});
function getAction(actionId: string): Action {
const name: string = actionId.split(".").pop() ?? "";
if (name === "stream-duration") {
return streamDurationAction;
}
if (name === "subscribers") {
return subscribersAction;
}
if (name === "live-chat") {
return liveChatAction;
}
throw new Error(`No action found for ${name} (${actionId})`);
}
export default plugin;