@sdeck/plugin 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bridge.d.ts +48 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/connection.d.ts +44 -0
- package/dist/connection.d.ts.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +774 -0
- package/dist/index.js.map +17 -0
- package/dist/manifest.d.ts +45 -0
- package/dist/manifest.d.ts.map +1 -0
- package/dist/permissions.d.ts +31 -0
- package/dist/permissions.d.ts.map +1 -0
- package/dist/plugin.d.ts +332 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/settings.d.ts +135 -0
- package/dist/settings.d.ts.map +1 -0
- package/dist/shared.d.ts +9 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/ui.d.ts +4 -0
- package/dist/ui.d.ts.map +1 -0
- package/package.json +41 -4
- package/src/bridge.ts +134 -0
- package/src/connection.ts +206 -0
- package/src/index.ts +93 -0
- package/src/manifest.ts +85 -0
- package/src/permissions.ts +119 -0
- package/src/plugin.ts +571 -0
- package/src/settings.ts +293 -0
- package/src/shared.ts +34 -0
- package/src/ui.ts +3 -0
- package/README.md +0 -3
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Action settings are described by a plain-data schema. The host turns it into a form in the
|
|
3
|
+
* configuration UI, and `resolveSettings` turns whatever is stored into well-typed values.
|
|
4
|
+
*/
|
|
5
|
+
type Primitive = string | number | boolean;
|
|
6
|
+
interface FieldBase {
|
|
7
|
+
/** Label shown in the configuration UI. */
|
|
8
|
+
label: string;
|
|
9
|
+
/** Help text under the field. */
|
|
10
|
+
description?: string;
|
|
11
|
+
/**
|
|
12
|
+
* It only changes how the button looks. It is asked for on a button, but not in a step of a
|
|
13
|
+
* workflow or gesture, where nothing is drawn.
|
|
14
|
+
*/
|
|
15
|
+
appearance?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Shown only while other fields have these values: `{ to: "page" }` or
|
|
18
|
+
* `{ mode: ["a", "b"] }`. Hidden fields keep their value.
|
|
19
|
+
*/
|
|
20
|
+
visibleIf?: Readonly<Record<string, Primitive | readonly Primitive[]>>;
|
|
21
|
+
}
|
|
22
|
+
export interface TextField extends FieldBase {
|
|
23
|
+
type: "text";
|
|
24
|
+
default?: string;
|
|
25
|
+
placeholder?: string;
|
|
26
|
+
multiline?: boolean;
|
|
27
|
+
/** Suggestions from one of the plugin's `optionSources` (any text is still allowed). */
|
|
28
|
+
source?: string;
|
|
29
|
+
/** Other fields the suggestions depend on, passed to the source. */
|
|
30
|
+
dependsOn?: readonly string[];
|
|
31
|
+
}
|
|
32
|
+
export interface NumberField extends FieldBase {
|
|
33
|
+
type: "number";
|
|
34
|
+
default?: number;
|
|
35
|
+
min?: number;
|
|
36
|
+
max?: number;
|
|
37
|
+
step?: number;
|
|
38
|
+
}
|
|
39
|
+
export interface BooleanField extends FieldBase {
|
|
40
|
+
type: "boolean";
|
|
41
|
+
default?: boolean;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A password or token, only for a plugin's own settings. It is kept in the host's secrets file
|
|
45
|
+
* (never in the config, so it is safe to share or commit) and the UI never reads it back.
|
|
46
|
+
*/
|
|
47
|
+
export interface SecretField extends FieldBase {
|
|
48
|
+
type: "secret";
|
|
49
|
+
default?: string;
|
|
50
|
+
placeholder?: string;
|
|
51
|
+
}
|
|
52
|
+
/** A `#rrggbb` colour. */
|
|
53
|
+
export interface ColorField extends FieldBase {
|
|
54
|
+
type: "color";
|
|
55
|
+
default?: string;
|
|
56
|
+
}
|
|
57
|
+
export interface SelectOption {
|
|
58
|
+
value: string;
|
|
59
|
+
label: string;
|
|
60
|
+
}
|
|
61
|
+
export interface SelectField extends FieldBase {
|
|
62
|
+
type: "select";
|
|
63
|
+
/** Fixed options. With `source`, shown before the fetched ones. */
|
|
64
|
+
options: readonly SelectOption[];
|
|
65
|
+
/**
|
|
66
|
+
* Options fetched from one of the plugin's `optionSources` when the form opens (OBS
|
|
67
|
+
* scenes, audio devices). Any stored string is then kept, since the list can change.
|
|
68
|
+
*/
|
|
69
|
+
source?: string;
|
|
70
|
+
/**
|
|
71
|
+
* Other fields the fetched options depend on, passed to the source. The form asks for them
|
|
72
|
+
* first, and clears this field when one of them changes.
|
|
73
|
+
*/
|
|
74
|
+
dependsOn?: readonly string[];
|
|
75
|
+
default?: string;
|
|
76
|
+
}
|
|
77
|
+
/** The name of one of the user's pages (the UI shows a page picker). */
|
|
78
|
+
export interface PageField extends FieldBase {
|
|
79
|
+
type: "page";
|
|
80
|
+
default?: string;
|
|
81
|
+
}
|
|
82
|
+
/** An image: an `http(s)` URL or a `data:` URI (the UI accepts a URL or a file). */
|
|
83
|
+
export interface ImageField extends FieldBase {
|
|
84
|
+
type: "image";
|
|
85
|
+
default?: string;
|
|
86
|
+
}
|
|
87
|
+
/** A named set the user made of options, from a `groups` field. */
|
|
88
|
+
export interface OptionGroup {
|
|
89
|
+
/** Stays the same when the group is renamed; refer to the group by it. */
|
|
90
|
+
id: string;
|
|
91
|
+
name: string;
|
|
92
|
+
/** The chosen options' values. */
|
|
93
|
+
members: readonly string[];
|
|
94
|
+
}
|
|
95
|
+
/** Named sets of options the user puts together (rooms of lights), only for a plugin's own settings. */
|
|
96
|
+
export interface GroupsField extends FieldBase {
|
|
97
|
+
type: "groups";
|
|
98
|
+
/** The plugin's `optionSources` entry that lists what a group can contain. */
|
|
99
|
+
source: string;
|
|
100
|
+
/** What a group contains, for the form, e.g. `lights` (default: `items`). */
|
|
101
|
+
noun?: string;
|
|
102
|
+
default?: readonly OptionGroup[];
|
|
103
|
+
}
|
|
104
|
+
export type SettingField = TextField | NumberField | BooleanField | ColorField | SelectField | PageField | ImageField | GroupsField | SecretField;
|
|
105
|
+
export type SettingsSchema = Readonly<Record<string, SettingField>>;
|
|
106
|
+
/** The value type of one field. */
|
|
107
|
+
export type FieldValue<F extends SettingField> = F extends NumberField ? number : F extends BooleanField ? boolean : F extends GroupsField ? readonly OptionGroup[] : F extends SelectField ? F extends {
|
|
108
|
+
source: string;
|
|
109
|
+
} ? string : F["options"][number]["value"] : string;
|
|
110
|
+
/** A value of any field. */
|
|
111
|
+
export type SettingValue = Primitive | readonly OptionGroup[];
|
|
112
|
+
/** The settings object an action receives for a schema. */
|
|
113
|
+
export type InferSettings<S extends SettingsSchema> = {
|
|
114
|
+
-readonly [K in keyof S]: FieldValue<S[K]>;
|
|
115
|
+
};
|
|
116
|
+
/** The value a field takes when nothing (valid) is stored. */
|
|
117
|
+
export declare function defaultValue(field: SettingField): SettingValue;
|
|
118
|
+
/** The valid groups in a stored value, or `undefined` if it isn't a list. */
|
|
119
|
+
export declare function optionGroups(value: unknown): OptionGroup[] | undefined;
|
|
120
|
+
/**
|
|
121
|
+
* Stored settings, made safe: every field of `schema` is present and of the right type (invalid
|
|
122
|
+
* or missing values get the default, numbers are clamped), and unknown keys are dropped.
|
|
123
|
+
*/
|
|
124
|
+
export declare function resolveSettings<S extends SettingsSchema>(schema: S, stored: Readonly<Record<string, unknown>> | undefined): InferSettings<S>;
|
|
125
|
+
/** The fields that change what an action does, without those for how its button looks. */
|
|
126
|
+
export declare function behaviorFields(schema: SettingsSchema): SettingsSchema;
|
|
127
|
+
/** Whether `field` is shown for these (resolved) settings. */
|
|
128
|
+
export declare function isFieldVisible(field: SettingField, settings: Readonly<Record<string, unknown>>): boolean;
|
|
129
|
+
/**
|
|
130
|
+
* Problems with a schema (empty when it is fine). Used when a plugin is loaded. `sources` are
|
|
131
|
+
* the plugin's option sources, checked against fields that name one.
|
|
132
|
+
*/
|
|
133
|
+
export declare function checkSchema(schema: SettingsSchema, sources?: Readonly<Record<string, unknown>>): string[];
|
|
134
|
+
export {};
|
|
135
|
+
//# sourceMappingURL=settings.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"settings.d.ts","sourceRoot":"","sources":["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MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAC1C,OAAO,CAMT;AAED;;;GAGG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,cAAc,EACtB,OAAO,GAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAM,GAC9C,MAAM,EAAE,CA0CV"}
|
package/dist/shared.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** A source started on the first listener and stopped after the last one leaves. */
|
|
2
|
+
export declare class Shared<T> {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(start: (emit: (value: T) => void) => () => void);
|
|
5
|
+
/** The latest value, if any arrived since it started. */
|
|
6
|
+
get last(): T | undefined;
|
|
7
|
+
listen(listener: (value: T) => void): () => void;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,qBAAa,MAAM,CAAC,CAAC;;IAMnB,YAAY,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,KAAK,MAAM,IAAI,EAE1D;IAED,yDAAyD;IACzD,IAAI,IAAI,IAAI,CAAC,GAAG,SAAS,CAExB;IAED,MAAM,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,GAAG,MAAM,IAAI,CAgB/C;CACF"}
|
package/dist/ui.d.ts
ADDED
package/dist/ui.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ui.d.ts","sourceRoot":"","sources":["../src/ui.ts"],"names":[],"mappings":"AAAA,qGAAqG;AACrG,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdeck/plugin",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "SDK for sdeck plugins: actions that run headless or draw with React, live state, triggers and option sources.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"src"
|
|
11
|
+
],
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"bun": "./src/index.ts",
|
|
18
|
+
"import": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "bun ../../scripts/build-package.ts",
|
|
24
|
+
"clean": "rm -rf dist",
|
|
25
|
+
"test": "bun test"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@sdeck/app": "0.2.0",
|
|
29
|
+
"@sdeck/engine": "0.2.0",
|
|
30
|
+
"@sdeck/schema": "0.2.0"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"react": ">=19.3"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/ivanoliverfabra/sdeck.git",
|
|
38
|
+
"directory": "packages/plugin"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
}
|
|
43
|
+
}
|
package/src/bridge.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live state for action components. The host provides a `HostBridge` around every slot; these
|
|
3
|
+
* hooks subscribe to exactly what they read and re-render when it changes.
|
|
4
|
+
*/
|
|
5
|
+
import { compile, type Scope, sameValue, template } from "@sdeck/engine";
|
|
6
|
+
import {
|
|
7
|
+
createContext,
|
|
8
|
+
useCallback,
|
|
9
|
+
useContext,
|
|
10
|
+
useEffect,
|
|
11
|
+
useMemo,
|
|
12
|
+
useRef,
|
|
13
|
+
useState,
|
|
14
|
+
useSyncExternalStore,
|
|
15
|
+
} from "react";
|
|
16
|
+
|
|
17
|
+
export interface HostBridge {
|
|
18
|
+
/**
|
|
19
|
+
* Watches state keys and variables behind expression paths (`system.cpu`, `vars.mode`).
|
|
20
|
+
* Starts providers as needed. Returns a function that stops watching.
|
|
21
|
+
*/
|
|
22
|
+
watch(paths: readonly string[], listener: () => void): () => void;
|
|
23
|
+
/** The current expression scope (state namespaces, `vars`, `self`…). */
|
|
24
|
+
scope(): Scope;
|
|
25
|
+
/** Runs a plugin action as a workflow step would (recorded in the history). */
|
|
26
|
+
run(plugin: string, action: string, settings?: Record<string, unknown>): Promise<unknown>;
|
|
27
|
+
/** Sets a global variable. */
|
|
28
|
+
setVariable(name: string, value: unknown): void;
|
|
29
|
+
/**
|
|
30
|
+
* What to draw for an image source (an icon reference, a URL or a `data:` URI), or
|
|
31
|
+
* `undefined` while it loads. `color` applies to icons.
|
|
32
|
+
*/
|
|
33
|
+
image(src: string, color?: string): string | undefined;
|
|
34
|
+
/** Called when an image finishes loading. */
|
|
35
|
+
watchImages(listener: () => void): () => void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const HostBridgeContext = createContext<HostBridge | undefined>(undefined);
|
|
39
|
+
|
|
40
|
+
function useBridge(): HostBridge {
|
|
41
|
+
const bridge = useContext(HostBridgeContext);
|
|
42
|
+
if (!bridge)
|
|
43
|
+
throw new Error("sdeck live-state hooks must be used inside an action drawn by the host");
|
|
44
|
+
return bridge;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Re-renders every second while `on` (for `now()`/`time()` in templates). */
|
|
48
|
+
function useClock(on: boolean): number {
|
|
49
|
+
const [tick, setTick] = useState(0);
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
if (!on) return;
|
|
52
|
+
const timer = setInterval(() => setTick((t) => t + 1), 1000);
|
|
53
|
+
return () => clearInterval(timer);
|
|
54
|
+
}, [on]);
|
|
55
|
+
return tick;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function useWatched<T>(paths: readonly string[], read: () => T): T {
|
|
59
|
+
const bridge = useBridge();
|
|
60
|
+
const key = paths.join("\n");
|
|
61
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: `key` stands for `paths`.
|
|
62
|
+
const subscribe = useCallback(
|
|
63
|
+
(listener: () => void) => bridge.watch(paths, listener),
|
|
64
|
+
[bridge, key],
|
|
65
|
+
);
|
|
66
|
+
// Expressions can build fresh arrays/objects; keep the previous one while equal so React does
|
|
67
|
+
// not see a new snapshot on every read.
|
|
68
|
+
const last = useRef<{ value: T } | undefined>(undefined);
|
|
69
|
+
const snapshot = () => {
|
|
70
|
+
const value = read();
|
|
71
|
+
if (last.current && sameValue(last.current.value, value)) return last.current.value;
|
|
72
|
+
last.current = { value };
|
|
73
|
+
return value;
|
|
74
|
+
};
|
|
75
|
+
return useSyncExternalStore(subscribe, snapshot);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The value of an expression (`system.cpu > 80`, `vars.mode`), kept up to date. Values the plugin
|
|
80
|
+
* is not allowed to read (`state`, `variables`) are missing.
|
|
81
|
+
*/
|
|
82
|
+
export function useExpression(source: string): unknown {
|
|
83
|
+
const bridge = useBridge();
|
|
84
|
+
const expression = useMemo(() => compile(source), [source]);
|
|
85
|
+
useClock(expression.timeDependent);
|
|
86
|
+
return useWatched(expression.paths, () => expression.evaluate(bridge.scope()));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** A `{{template}}` rendered to text, kept up to date: `useTemplate("CPU {{system.cpu}}%")`. */
|
|
90
|
+
export function useTemplate(source: string): string {
|
|
91
|
+
const bridge = useBridge();
|
|
92
|
+
const compiled = useMemo(() => template(source), [source]);
|
|
93
|
+
useClock(compiled.timeDependent);
|
|
94
|
+
return useWatched(compiled.paths, () => compiled.render(bridge.scope()));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** A live state value by key (`system.cpu`). */
|
|
98
|
+
export function useHostState(key: string): unknown {
|
|
99
|
+
const bridge = useBridge();
|
|
100
|
+
const paths = useMemo(() => [key], [key]);
|
|
101
|
+
return useWatched(paths, () => {
|
|
102
|
+
const dot = key.indexOf(".");
|
|
103
|
+
const ns = bridge.scope()[key.slice(0, dot)] as Record<string, unknown> | undefined;
|
|
104
|
+
return dot > 0 ? ns?.[key.slice(dot + 1)] : undefined;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** A global variable and a setter for it. */
|
|
109
|
+
export function useVariable(name: string): [unknown, (value: unknown) => void] {
|
|
110
|
+
const bridge = useBridge();
|
|
111
|
+
const paths = useMemo(() => [`vars.${name}`], [name]);
|
|
112
|
+
const value = useWatched(paths, () => (bridge.scope().vars as Record<string, unknown>)[name]);
|
|
113
|
+
const set = useCallback((v: unknown) => bridge.setVariable(name, v), [bridge, name]);
|
|
114
|
+
return [value, set];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Runs an action from a component (`run("@sdeck/plugin-system", "open", {...})`).
|
|
119
|
+
* Other plugins' actions need the `actions` permission.
|
|
120
|
+
*/
|
|
121
|
+
export function useRunAction(): HostBridge["run"] {
|
|
122
|
+
return useBridge().run;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* An image ready to draw with `<img src>`: an icon (`icon:lucide/play`, `icon:si/SiDiscord`) in
|
|
127
|
+
* `color`, a URL or a `data:` URI. `undefined` until it has loaded.
|
|
128
|
+
*/
|
|
129
|
+
export function useImage(src: string, color?: string): string | undefined {
|
|
130
|
+
const bridge = useBridge();
|
|
131
|
+
return useSyncExternalStore(bridge.watchImages, () =>
|
|
132
|
+
src ? bridge.image(src, color) : undefined,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A connection to another program or service, opened only while something needs it and reopened
|
|
3
|
+
* when it drops. Plugins hold it while their state is watched or a trigger listens, and borrow it
|
|
4
|
+
* to run actions.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export interface Link<C> {
|
|
8
|
+
/** What callers use. */
|
|
9
|
+
value: C;
|
|
10
|
+
close(): void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Retrying cannot help (a wrong password, nothing configured) until something changes. */
|
|
14
|
+
export class PermanentError extends Error {}
|
|
15
|
+
|
|
16
|
+
export type ConnectionState = "connecting" | "connected" | "disconnected";
|
|
17
|
+
|
|
18
|
+
export interface ConnectionOptions<C> {
|
|
19
|
+
/**
|
|
20
|
+
* Opens the connection. Call `lost` if it drops later. `signal` aborts when nobody wants it
|
|
21
|
+
* any more.
|
|
22
|
+
*/
|
|
23
|
+
open(lost: (error: Error) => void, signal: AbortSignal): Promise<Link<C>>;
|
|
24
|
+
onStatus?: (state: ConnectionState, error?: Error) => void;
|
|
25
|
+
/** How long it stays open after the last holder leaves (default 3000). */
|
|
26
|
+
lingerMs?: number;
|
|
27
|
+
/** First and longest pause between attempts (default 1000 and 30000). */
|
|
28
|
+
retry?: { minMs: number; maxMs: number };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface Waiter<C> {
|
|
32
|
+
resolve: (value: C) => void;
|
|
33
|
+
reject: (error: Error) => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export class ManagedConnection<C> {
|
|
37
|
+
readonly #options: ConnectionOptions<C>;
|
|
38
|
+
readonly #waiters = new Set<Waiter<C>>();
|
|
39
|
+
#holders = 0;
|
|
40
|
+
#link: Link<C> | undefined;
|
|
41
|
+
#attempt: AbortController | undefined;
|
|
42
|
+
#retry: ReturnType<typeof setTimeout> | undefined;
|
|
43
|
+
#linger: ReturnType<typeof setTimeout> | undefined;
|
|
44
|
+
#delay: number;
|
|
45
|
+
/** Set by a {@link PermanentError}; cleared by {@link restart}. */
|
|
46
|
+
#failed: Error | undefined;
|
|
47
|
+
|
|
48
|
+
constructor(options: ConnectionOptions<C>) {
|
|
49
|
+
this.#options = options;
|
|
50
|
+
this.#delay = options.retry?.minMs ?? 1000;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The open connection, if there is one. */
|
|
54
|
+
get current(): C | undefined {
|
|
55
|
+
return this.#link?.value;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Keeps the connection wanted until the returned function is called. */
|
|
59
|
+
hold(): () => void {
|
|
60
|
+
this.#holders++;
|
|
61
|
+
clearTimeout(this.#linger);
|
|
62
|
+
this.#connect();
|
|
63
|
+
let released = false;
|
|
64
|
+
return () => {
|
|
65
|
+
if (released) return;
|
|
66
|
+
released = true;
|
|
67
|
+
this.#holders--;
|
|
68
|
+
if (this.#holders > 0) return;
|
|
69
|
+
this.#linger = setTimeout(() => this.#drop(), this.#options.lingerMs ?? 3000);
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Runs `use` with the connection, opening it first if needed. */
|
|
74
|
+
async with<T>(use: (value: C) => T | Promise<T>, timeoutMs = 5000): Promise<T> {
|
|
75
|
+
const release = this.hold();
|
|
76
|
+
try {
|
|
77
|
+
return await use(await this.#ready(timeoutMs));
|
|
78
|
+
} finally {
|
|
79
|
+
release();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Starts over (the settings changed). */
|
|
84
|
+
restart(): void {
|
|
85
|
+
this.#failed = undefined;
|
|
86
|
+
this.#delay = this.#options.retry?.minMs ?? 1000;
|
|
87
|
+
this.#drop();
|
|
88
|
+
if (this.#holders > 0) this.#connect();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Closes it for good, whoever holds it. */
|
|
92
|
+
close(): void {
|
|
93
|
+
this.#holders = 0;
|
|
94
|
+
clearTimeout(this.#linger);
|
|
95
|
+
this.#drop();
|
|
96
|
+
this.#reject(new Error("the connection was closed"));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
#ready(timeoutMs: number): Promise<C> {
|
|
100
|
+
if (this.#link) return Promise.resolve(this.#link.value);
|
|
101
|
+
if (this.#failed) return Promise.reject(this.#failed);
|
|
102
|
+
// Someone is waiting: no point sitting out the pause between attempts.
|
|
103
|
+
if (this.#retry) {
|
|
104
|
+
clearTimeout(this.#retry);
|
|
105
|
+
this.#retry = undefined;
|
|
106
|
+
this.#connect();
|
|
107
|
+
}
|
|
108
|
+
return new Promise<C>((resolve, reject) => {
|
|
109
|
+
let last: Error | undefined;
|
|
110
|
+
const waiter: Waiter<C> = {
|
|
111
|
+
resolve: (value) => {
|
|
112
|
+
clearTimeout(timer);
|
|
113
|
+
resolve(value);
|
|
114
|
+
},
|
|
115
|
+
reject: (error) => {
|
|
116
|
+
last = error;
|
|
117
|
+
clearTimeout(timer);
|
|
118
|
+
this.#waiters.delete(waiter);
|
|
119
|
+
reject(last);
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
const timer = setTimeout(
|
|
123
|
+
() => waiter.reject(new Error("could not connect in time")),
|
|
124
|
+
timeoutMs,
|
|
125
|
+
);
|
|
126
|
+
this.#waiters.add(waiter);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
#connect(): void {
|
|
131
|
+
if (this.#link || this.#attempt || this.#retry || this.#failed) return;
|
|
132
|
+
const attempt = new AbortController();
|
|
133
|
+
this.#attempt = attempt;
|
|
134
|
+
this.#options.onStatus?.("connecting");
|
|
135
|
+
let lostEarly: Error | undefined;
|
|
136
|
+
let link: Link<C> | undefined;
|
|
137
|
+
const lost = (error: Error) => {
|
|
138
|
+
if (!link) {
|
|
139
|
+
lostEarly = error;
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (this.#link !== link) return;
|
|
143
|
+
this.#link = undefined;
|
|
144
|
+
link.close();
|
|
145
|
+
this.#options.onStatus?.("disconnected", error);
|
|
146
|
+
this.#schedule(error);
|
|
147
|
+
};
|
|
148
|
+
const open = async () => {
|
|
149
|
+
const opened = await this.#options.open(lost, attempt.signal);
|
|
150
|
+
if (attempt.signal.aborted) {
|
|
151
|
+
opened.close();
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (lostEarly) {
|
|
155
|
+
opened.close();
|
|
156
|
+
throw lostEarly;
|
|
157
|
+
}
|
|
158
|
+
link = opened;
|
|
159
|
+
this.#attempt = undefined;
|
|
160
|
+
this.#link = opened;
|
|
161
|
+
this.#delay = this.#options.retry?.minMs ?? 1000;
|
|
162
|
+
this.#options.onStatus?.("connected");
|
|
163
|
+
for (const waiter of this.#waiters) waiter.resolve(opened.value);
|
|
164
|
+
this.#waiters.clear();
|
|
165
|
+
};
|
|
166
|
+
open().catch((error: Error) => {
|
|
167
|
+
if (attempt.signal.aborted || this.#attempt !== attempt) return;
|
|
168
|
+
this.#attempt = undefined;
|
|
169
|
+
this.#options.onStatus?.("disconnected", error);
|
|
170
|
+
this.#reject(error);
|
|
171
|
+
this.#schedule(error);
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
#schedule(error: Error): void {
|
|
176
|
+
if (error instanceof PermanentError) {
|
|
177
|
+
this.#failed = error;
|
|
178
|
+
this.#reject(error);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (this.#holders <= 0) return;
|
|
182
|
+
clearTimeout(this.#retry);
|
|
183
|
+
this.#retry = setTimeout(() => {
|
|
184
|
+
this.#retry = undefined;
|
|
185
|
+
if (this.#holders > 0) this.#connect();
|
|
186
|
+
}, this.#delay);
|
|
187
|
+
this.#delay = Math.min(this.#delay * 2, this.#options.retry?.maxMs ?? 30_000);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
#reject(error: Error): void {
|
|
191
|
+
for (const waiter of [...this.#waiters]) waiter.reject(error);
|
|
192
|
+
this.#waiters.clear();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
#drop(): void {
|
|
196
|
+
clearTimeout(this.#retry);
|
|
197
|
+
this.#retry = undefined;
|
|
198
|
+
this.#attempt?.abort();
|
|
199
|
+
this.#attempt = undefined;
|
|
200
|
+
const link = this.#link;
|
|
201
|
+
this.#link = undefined;
|
|
202
|
+
if (!link) return;
|
|
203
|
+
link.close();
|
|
204
|
+
this.#options.onStatus?.("disconnected");
|
|
205
|
+
}
|
|
206
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@sdeck/plugin`: everything a plugin needs. Define actions that run headless (from keys,
|
|
3
|
+
* triggers and workflows) or draw themselves with React, publish live state, offer trigger types,
|
|
4
|
+
* and use the `@sdeck/app` hooks and components (re-exported here) for input and navigation.
|
|
5
|
+
*/
|
|
6
|
+
export * from "@sdeck/app";
|
|
7
|
+
export {
|
|
8
|
+
type HostBridge,
|
|
9
|
+
HostBridgeContext,
|
|
10
|
+
useExpression,
|
|
11
|
+
useHostState,
|
|
12
|
+
useImage,
|
|
13
|
+
useRunAction,
|
|
14
|
+
useTemplate,
|
|
15
|
+
useVariable,
|
|
16
|
+
} from "./bridge.ts";
|
|
17
|
+
export {
|
|
18
|
+
type ConnectionOptions,
|
|
19
|
+
type ConnectionState,
|
|
20
|
+
type Link,
|
|
21
|
+
ManagedConnection,
|
|
22
|
+
PermanentError,
|
|
23
|
+
} from "./connection.ts";
|
|
24
|
+
export {
|
|
25
|
+
HOST_MODULES,
|
|
26
|
+
type ManifestResult,
|
|
27
|
+
PLUGIN_KEYWORD,
|
|
28
|
+
type PluginManifest,
|
|
29
|
+
parseManifest,
|
|
30
|
+
SUPPORTED_API_VERSIONS,
|
|
31
|
+
} from "./manifest.ts";
|
|
32
|
+
export {
|
|
33
|
+
PERMISSION_TEXTS,
|
|
34
|
+
PERMISSIONS,
|
|
35
|
+
type Permission,
|
|
36
|
+
PermissionError,
|
|
37
|
+
type PermissionText,
|
|
38
|
+
} from "./permissions.ts";
|
|
39
|
+
export {
|
|
40
|
+
type Action,
|
|
41
|
+
type ActionDef,
|
|
42
|
+
type ActionProps,
|
|
43
|
+
checkPlugin,
|
|
44
|
+
type Deactivate,
|
|
45
|
+
type DeckControls,
|
|
46
|
+
defineAction,
|
|
47
|
+
definePlugin,
|
|
48
|
+
defineTrigger,
|
|
49
|
+
type Onboarding,
|
|
50
|
+
type OnboardingStep,
|
|
51
|
+
type OptionSource,
|
|
52
|
+
PLUGIN_API_VERSION,
|
|
53
|
+
type PluginCommand,
|
|
54
|
+
type PluginContext,
|
|
55
|
+
type PluginDef,
|
|
56
|
+
pluginNamespace,
|
|
57
|
+
type RunContext,
|
|
58
|
+
type RunFn,
|
|
59
|
+
type RunOutcome,
|
|
60
|
+
type RunResult,
|
|
61
|
+
type SlotKind,
|
|
62
|
+
type StarterPage,
|
|
63
|
+
type StateDef,
|
|
64
|
+
type StatusLevel,
|
|
65
|
+
type Trigger,
|
|
66
|
+
type TriggerContext,
|
|
67
|
+
type TriggerDef,
|
|
68
|
+
} from "./plugin.ts";
|
|
69
|
+
export {
|
|
70
|
+
type BooleanField,
|
|
71
|
+
behaviorFields,
|
|
72
|
+
type ColorField,
|
|
73
|
+
checkSchema,
|
|
74
|
+
defaultValue,
|
|
75
|
+
type FieldValue,
|
|
76
|
+
type GroupsField,
|
|
77
|
+
type ImageField,
|
|
78
|
+
type InferSettings,
|
|
79
|
+
isFieldVisible,
|
|
80
|
+
type NumberField,
|
|
81
|
+
type OptionGroup,
|
|
82
|
+
optionGroups,
|
|
83
|
+
type PageField,
|
|
84
|
+
resolveSettings,
|
|
85
|
+
type SecretField,
|
|
86
|
+
type SelectField,
|
|
87
|
+
type SelectOption,
|
|
88
|
+
type SettingField,
|
|
89
|
+
type SettingsSchema,
|
|
90
|
+
type SettingValue,
|
|
91
|
+
type TextField,
|
|
92
|
+
} from "./settings.ts";
|
|
93
|
+
export { Shared } from "./shared.ts";
|