sepal 0.2.0-alpha.0 → 0.3.0-alpha.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/README.md +14 -5
- package/cli.js +127 -10
- package/package.json +48 -4
- package/src/abort.js +129 -0
- package/src/app.js +13 -0
- package/src/dom.js +173 -60
- package/src/fetch.js +347 -80
- package/src/form-data.js +141 -0
- package/src/host-call.js +57 -0
- package/src/index.js +9 -11
- package/src/lru.js +45 -0
- package/src/native.js +32 -0
- package/src/notifications.js +60 -0
- package/src/os.js +35 -0
- package/src/process.js +164 -0
- package/src/protocol.js +3 -4
- package/src/renderer.js +140 -10
- package/src/secrets.js +42 -0
- package/src/settings.js +52 -0
- package/src/shims.js +30 -8
- package/src/stem.js +125 -0
- package/src/updater.js +3 -2
- package/src/url.js +65 -20
- package/src/web.js +86 -0
- package/src/window.js +6 -2
- package/types/abort.d.ts +46 -0
- package/types/app.d.ts +7 -0
- package/types/fetch.d.ts +118 -28
- package/types/form-data.d.ts +19 -0
- package/types/host-call.d.ts +18 -0
- package/types/index.d.ts +5 -2
- package/types/lru.d.ts +18 -0
- package/types/native.d.ts +22 -0
- package/types/notifications.d.ts +53 -0
- package/types/os.d.ts +53 -0
- package/types/process.d.ts +90 -0
- package/types/protocol.d.ts +0 -2
- package/types/renderer.d.ts +16 -1
- package/types/secrets.d.ts +22 -0
- package/types/settings.d.ts +29 -0
- package/types/shims.d.ts +1 -0
- package/types/stem.d.ts +21 -0
- package/types/updater.d.ts +6 -4
- package/types/url.d.ts +1 -1
- package/types/vite/adapter.d.ts +22 -0
- package/types/vite/add.d.ts +77 -0
- package/types/vite/app.d.ts +52 -1
- package/types/vite/licenses.d.ts +11 -0
- package/types/vite/native.d.ts +135 -0
- package/types/vite/page.d.ts +20 -0
- package/types/vite/permissions.d.ts +56 -0
- package/types/vite/sveltekit.d.ts +5 -42
- package/types/vite/vite.d.ts +86 -29
- package/types/web.d.ts +17 -0
- package/types/window.d.ts +5 -1
- package/vite/adapter.js +118 -0
- package/vite/add.js +254 -0
- package/vite/app.js +195 -27
- package/vite/head.js +2 -2
- package/vite/licenses.js +115 -0
- package/vite/native.js +535 -0
- package/vite/page.js +95 -0
- package/vite/permissions.d.ts +56 -0
- package/vite/sveltekit.js +11 -263
- package/vite/vite.js +378 -109
package/README.md
CHANGED
|
@@ -41,15 +41,24 @@ What is in here:
|
|
|
41
41
|
| `sepal/updater` | self-update |
|
|
42
42
|
| `sepal/dom` | a small DOM for component libraries written for the web (`dom: true`) |
|
|
43
43
|
| `sepal/renderer`, `sepal/protocol` | what the Vite preset and the host are built against |
|
|
44
|
-
| `sepal/vite` | the Vite preset for
|
|
45
|
-
| `sepal/
|
|
44
|
+
| `sepal/vite` | the Vite preset, `sepal()`; `withSepal()`, SvelteKit's options for an app; `sepalPreprocess()`; and `defineConfig` for `sepal.config.ts` |
|
|
45
|
+
| `sepal/adapter` | the SvelteKit adapter |
|
|
46
46
|
|
|
47
|
-
The preset compiles Svelte for the Blitz renderer
|
|
48
|
-
|
|
47
|
+
The preset compiles Svelte for the Blitz renderer and writes a web build the
|
|
48
|
+
host loads: `index.html`, its ES module chunks and stylesheets, the static
|
|
49
|
+
files and `app.json`. It reads `sepal.config.ts`, and in `vite build --watch`
|
|
49
50
|
opens the app and reloads it on save.
|
|
50
51
|
|
|
51
52
|
```ts
|
|
52
|
-
// vite.config.ts
|
|
53
|
+
// vite.config.ts, for an app with pages in src/routes: SvelteKit's own plugin
|
|
54
|
+
import { sveltekit } from '@sveltejs/kit/vite';
|
|
55
|
+
import adapter from 'sepal/adapter';
|
|
56
|
+
import { sepal, withSepal } from 'sepal/vite';
|
|
57
|
+
export default { plugins: [sveltekit(withSepal({ adapter: adapter() })), sepal()] };
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
// vite.config.ts, for an app without routes, which starts from index.html
|
|
53
62
|
import { sepal } from 'sepal/vite';
|
|
54
63
|
export default { plugins: [sepal()] };
|
|
55
64
|
```
|
package/cli.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// The sepal command: run, bundle and screenshot an app
|
|
3
|
-
// Building is Vite's job (`vite
|
|
2
|
+
// The sepal command: run, bundle and screenshot an app, and write and build
|
|
3
|
+
// its own host (`native/`). Building the JavaScript is Vite's job (`vite
|
|
4
|
+
// build`, and `vite build --watch` for dev).
|
|
4
5
|
|
|
5
6
|
import { spawnSync } from 'node:child_process';
|
|
6
|
-
import { readFileSync } from 'node:fs';
|
|
7
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
7
8
|
import { basename, join, resolve } from 'node:path';
|
|
8
9
|
import { find_host, platform_package } from '@ethercorps/calyx';
|
|
9
10
|
|
|
11
|
+
// What native/ needs is loaded only for an app that has it.
|
|
12
|
+
const native_tools = () => import('./vite/native.js');
|
|
13
|
+
|
|
10
14
|
const own = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
|
|
11
15
|
|
|
12
16
|
const USAGE = `usage:
|
|
@@ -14,11 +18,21 @@ const USAGE = `usage:
|
|
|
14
18
|
sepal bundle [dist] [out-dir] [--sign <identity>] [--notarize <keychain-profile>]
|
|
15
19
|
[--update-key <private-key-file>] [--update-notes <text>]
|
|
16
20
|
build a macOS .app; with a key, also the signed update
|
|
21
|
+
(SEPAL_SIGN names the identity when --sign does not;
|
|
22
|
+
dev signs an app's own host with it too)
|
|
17
23
|
sepal signer generate <private-key-file> make the key pair updates are signed with
|
|
18
24
|
sepal screenshot <out.png> [dist] [WIDTHxHEIGHT]
|
|
25
|
+
sepal native init write native/: the app's own host, in Rust
|
|
26
|
+
sepal native build [--profile dev|dist] build it (dev and bundle do this too); prints the binary
|
|
27
|
+
sepal native update point native/ at this release of sepal
|
|
28
|
+
sepal native clean remove the build cache every app on this machine shares
|
|
29
|
+
sepal add <name>… add a plugin (watch, or a package by its full name); anything
|
|
30
|
+
else is handed to \`sv add\`
|
|
31
|
+
sepal check check that the plugins, native/ and permissions agree, then svelte-check
|
|
19
32
|
|
|
20
33
|
Build first with \`vite build\`. For dev, \`vite build --watch\` opens the app
|
|
21
|
-
and reloads it on every save.
|
|
34
|
+
and reloads it on every save. With native/, run, screenshot, bundle and dev
|
|
35
|
+
build the app's own host first, with cargo.
|
|
22
36
|
To start a project: pnpm create sepal my-app`;
|
|
23
37
|
|
|
24
38
|
/** @param {string} message @returns {never} */
|
|
@@ -77,7 +91,7 @@ function bundled(bin, dir) {
|
|
|
77
91
|
return bin;
|
|
78
92
|
}
|
|
79
93
|
if (!name) return bin;
|
|
80
|
-
const made = spawnSync(bin, ['--bundle', dir, resolve('node_modules/.sepal')], { encoding: 'utf8' });
|
|
94
|
+
const made = spawnSync(bin, ['--bundle', dir, resolve('node_modules/.sepal'), ...(process.env.SEPAL_SIGN ? ['--dev', '--sign', process.env.SEPAL_SIGN] : ['--dev'])], { encoding: 'utf8' });
|
|
81
95
|
if (made.status !== 0) {
|
|
82
96
|
console.warn(`sepal: running unbundled: ${made.stderr.trim()}`);
|
|
83
97
|
return bin;
|
|
@@ -86,21 +100,124 @@ function bundled(bin, dir) {
|
|
|
86
100
|
return join(bundle, 'Contents/MacOS', basename(bundle, '.app'));
|
|
87
101
|
}
|
|
88
102
|
|
|
89
|
-
|
|
103
|
+
/** The crate name for native/: from the app's id, else its package's name. */
|
|
104
|
+
async function app_crate_name() {
|
|
105
|
+
const { load_config } = await import('./vite/app.js');
|
|
106
|
+
const { config } = await load_config(process.cwd(), { command: 'build', mode: 'production' });
|
|
107
|
+
/** @type {{ name?: string }} */
|
|
108
|
+
let pkg = {};
|
|
109
|
+
try {
|
|
110
|
+
pkg = JSON.parse(readFileSync('package.json', 'utf8'));
|
|
111
|
+
} catch {}
|
|
112
|
+
const { crate_name } = await native_tools();
|
|
113
|
+
return { name: crate_name(config.app?.id, pkg.name), config };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** `sepal native …`. @param {string[]} args @returns {Promise<never>} */
|
|
117
|
+
async function native(args) {
|
|
118
|
+
const { build_or_exit, clean, init, native_manifest, update_tag, VERSION } = await native_tools();
|
|
119
|
+
const manifest = native_manifest(process.cwd());
|
|
120
|
+
switch (args[0]) {
|
|
121
|
+
case 'init': {
|
|
122
|
+
const { name } = await app_crate_name();
|
|
123
|
+
try {
|
|
124
|
+
const written = init(process.cwd(), name);
|
|
125
|
+
console.log(`sepal: wrote ${written.join(', ')}. \`vite build --watch\` builds it from now on.`);
|
|
126
|
+
} catch (error) {
|
|
127
|
+
fail(`sepal: ${/** @type {Error} */ (error).message}`);
|
|
128
|
+
}
|
|
129
|
+
process.exit(0);
|
|
130
|
+
}
|
|
131
|
+
case 'build': {
|
|
132
|
+
if (!manifest) fail('sepal: there is no native/Cargo.toml here; `sepal native init` writes one');
|
|
133
|
+
const profile = args[1] === '--profile' ? args[2] : 'dev';
|
|
134
|
+
if (profile !== 'dev' && profile !== 'dist') fail(USAGE);
|
|
135
|
+
console.log(await build_or_exit(manifest, profile));
|
|
136
|
+
process.exit(0);
|
|
137
|
+
}
|
|
138
|
+
case 'update': {
|
|
139
|
+
if (!manifest) fail('sepal: there is no native/Cargo.toml here');
|
|
140
|
+
const changed = update_tag(manifest, VERSION);
|
|
141
|
+
console.log(
|
|
142
|
+
changed > 0 ? `sepal: native/Cargo.toml now builds from sepal v${VERSION} (${changed} lines)` : `sepal: native/Cargo.toml already builds from sepal v${VERSION}`
|
|
143
|
+
);
|
|
144
|
+
process.exit(0);
|
|
145
|
+
}
|
|
146
|
+
case 'clean':
|
|
147
|
+
console.log(`sepal: removed ${clean()}. The next build of any app's native/ compiles everything again.`);
|
|
148
|
+
process.exit(0);
|
|
149
|
+
default:
|
|
150
|
+
fail(USAGE);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Before `run`, `screenshot` and `bundle`: an app with native/ runs its own
|
|
156
|
+
* host, built now, unless `SEPAL_HOST` names one already.
|
|
157
|
+
* @param {'dev' | 'dist'} profile
|
|
158
|
+
* @returns {Promise<string | null>} the native/ manifest, if it was built
|
|
159
|
+
*/
|
|
160
|
+
async function own_host(profile) {
|
|
161
|
+
const manifest = resolve('native/Cargo.toml');
|
|
162
|
+
if (process.env.SEPAL_HOST || !existsSync(manifest)) return null;
|
|
163
|
+
await (await native_tools()).build_or_exit(manifest, profile);
|
|
164
|
+
return manifest;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const [command, ...given] = process.argv.slice(2);
|
|
168
|
+
// The identity the app is signed with, said once: `bundle` signs with it
|
|
169
|
+
// unless told otherwise, and dev signs an app's own host with it.
|
|
170
|
+
const args = command === 'bundle' && process.env.SEPAL_SIGN && !given.includes('--sign') ? [...given, '--sign', process.env.SEPAL_SIGN] : given;
|
|
90
171
|
// `bundle --sign x` with no directory still means `dist`.
|
|
91
172
|
const dist = args[0] !== undefined && !args[0].startsWith('--') ? args : ['dist', ...args];
|
|
92
173
|
|
|
93
174
|
switch (command) {
|
|
175
|
+
case 'native':
|
|
176
|
+
await native(args);
|
|
177
|
+
case 'add': {
|
|
178
|
+
if (args.length === 0) fail(USAGE);
|
|
179
|
+
const { add } = await import('./vite/add.js');
|
|
180
|
+
try {
|
|
181
|
+
add(process.cwd(), args, { crate_name: (await app_crate_name()).name });
|
|
182
|
+
} catch (error) {
|
|
183
|
+
fail(`sepal: ${/** @type {Error} */ (error).message}`);
|
|
184
|
+
}
|
|
185
|
+
process.exit(0);
|
|
186
|
+
}
|
|
187
|
+
case 'check': {
|
|
188
|
+
const { check } = await import('./vite/add.js');
|
|
189
|
+
const { config } = await app_crate_name();
|
|
190
|
+
const problems = check(process.cwd(), /** @type {any} */ (config.permissions));
|
|
191
|
+
for (const problem of problems) console.error(`sepal: ${problem}`);
|
|
192
|
+
const { process_warnings } = await import('./vite/app.js');
|
|
193
|
+
for (const warning of process_warnings(/** @type {any} */ (config.permissions))) console.warn(`sepal: warning: ${warning}`);
|
|
194
|
+
if (problems.length === 0) console.log('sepal: the plugins, native/ and permissions agree');
|
|
195
|
+
const svelte_check = resolve('node_modules/.bin/svelte-check');
|
|
196
|
+
const checked = existsSync(svelte_check) ? spawnSync(svelte_check, [], { stdio: 'inherit' }).status : 0;
|
|
197
|
+
process.exit(problems.length > 0 || checked !== 0 ? 1 : 0);
|
|
198
|
+
}
|
|
94
199
|
case 'run':
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
200
|
+
await own_host('dev');
|
|
201
|
+
// `--dev`: what this command runs from the project is dev's (its
|
|
202
|
+
// secrets are `<id>.dev`), on every OS; what ships is the app.
|
|
203
|
+
start(bundled(calyx(), dist[0]), ['--dev', ...dist]);
|
|
204
|
+
case 'bundle': {
|
|
205
|
+
const manifest = await own_host('dist');
|
|
206
|
+
if (manifest === null) host(['--bundle', ...dist]);
|
|
207
|
+
// The notices of this binary, plugins included: calyx's own file is not it.
|
|
208
|
+
const notices = resolve('node_modules/.sepal/THIRD-PARTY-LICENSES.txt');
|
|
209
|
+
mkdirSync(resolve('node_modules/.sepal'), { recursive: true });
|
|
210
|
+
const { third_party_licenses } = await import('./vite/licenses.js');
|
|
211
|
+
writeFileSync(notices, third_party_licenses(manifest).text);
|
|
212
|
+
host(['--bundle', ...dist, '--licenses', notices]);
|
|
213
|
+
}
|
|
98
214
|
case 'signer':
|
|
99
215
|
if (args[0] !== 'generate' || !args[1]) fail(USAGE);
|
|
100
216
|
host(['--updater-keygen', args[1]]);
|
|
101
217
|
case 'screenshot':
|
|
102
218
|
if (!args[0]) fail(USAGE);
|
|
103
|
-
|
|
219
|
+
await own_host('dev');
|
|
220
|
+
host(['--dev', '--screenshot', args[0], ...(args.length > 1 ? args.slice(1) : ['dist'])]);
|
|
104
221
|
default:
|
|
105
222
|
fail(USAGE);
|
|
106
223
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sepal",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0-alpha.0",
|
|
4
4
|
"description": "Native Svelte apps, drawn by Blitz: a Svelte custom renderer for the Blitz HTML/CSS engine, its Vite preset and the sepal command",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -57,6 +57,26 @@
|
|
|
57
57
|
"types": "./types/clipboard.d.ts",
|
|
58
58
|
"default": "./src/clipboard.js"
|
|
59
59
|
},
|
|
60
|
+
"./notifications": {
|
|
61
|
+
"types": "./types/notifications.d.ts",
|
|
62
|
+
"default": "./src/notifications.js"
|
|
63
|
+
},
|
|
64
|
+
"./os": {
|
|
65
|
+
"types": "./types/os.d.ts",
|
|
66
|
+
"default": "./src/os.js"
|
|
67
|
+
},
|
|
68
|
+
"./process": {
|
|
69
|
+
"types": "./types/process.d.ts",
|
|
70
|
+
"default": "./src/process.js"
|
|
71
|
+
},
|
|
72
|
+
"./secrets": {
|
|
73
|
+
"types": "./types/secrets.d.ts",
|
|
74
|
+
"default": "./src/secrets.js"
|
|
75
|
+
},
|
|
76
|
+
"./settings": {
|
|
77
|
+
"types": "./types/settings.d.ts",
|
|
78
|
+
"default": "./src/settings.js"
|
|
79
|
+
},
|
|
60
80
|
"./shell": {
|
|
61
81
|
"types": "./types/shell.d.ts",
|
|
62
82
|
"default": "./src/shell.js"
|
|
@@ -65,6 +85,18 @@
|
|
|
65
85
|
"types": "./types/window.d.ts",
|
|
66
86
|
"default": "./src/window.js"
|
|
67
87
|
},
|
|
88
|
+
"./app": {
|
|
89
|
+
"types": "./types/app.d.ts",
|
|
90
|
+
"default": "./src/app.js"
|
|
91
|
+
},
|
|
92
|
+
"./stem": {
|
|
93
|
+
"types": "./types/stem.d.ts",
|
|
94
|
+
"default": "./src/stem.js"
|
|
95
|
+
},
|
|
96
|
+
"./native": {
|
|
97
|
+
"types": "./types/native.d.ts",
|
|
98
|
+
"default": "./src/native.js"
|
|
99
|
+
},
|
|
68
100
|
"./dom": {
|
|
69
101
|
"types": "./types/dom.d.ts",
|
|
70
102
|
"default": "./src/dom.js"
|
|
@@ -73,6 +105,13 @@
|
|
|
73
105
|
"types": "./types/vite/vite.d.ts",
|
|
74
106
|
"default": "./vite/vite.js"
|
|
75
107
|
},
|
|
108
|
+
"./permissions": {
|
|
109
|
+
"types": "./types/vite/permissions.d.ts"
|
|
110
|
+
},
|
|
111
|
+
"./adapter": {
|
|
112
|
+
"types": "./types/vite/adapter.d.ts",
|
|
113
|
+
"default": "./vite/adapter.js"
|
|
114
|
+
},
|
|
76
115
|
"./sveltekit": {
|
|
77
116
|
"types": "./types/vite/sveltekit.d.ts",
|
|
78
117
|
"default": "./vite/sveltekit.js"
|
|
@@ -85,13 +124,18 @@
|
|
|
85
124
|
"cli.js"
|
|
86
125
|
],
|
|
87
126
|
"dependencies": {
|
|
88
|
-
"@ethercorps/calyx": "0.
|
|
127
|
+
"@ethercorps/calyx": "0.3.0-alpha.0",
|
|
128
|
+
"@sveltejs/acorn-typescript": "^1.0.13",
|
|
129
|
+
"acorn": "^8.18.0",
|
|
130
|
+
"formdata-node": "6.0.3",
|
|
131
|
+
"magic-string": "^0.30.11",
|
|
132
|
+
"web-streams-polyfill": "4.5.1"
|
|
89
133
|
},
|
|
90
134
|
"peerDependencies": {
|
|
91
135
|
"svelte": "^5.59.0",
|
|
92
136
|
"@sveltejs/vite-plugin-svelte": ">=7",
|
|
93
137
|
"vite": ">=8",
|
|
94
|
-
"@sveltejs/kit": ">=3.4"
|
|
138
|
+
"@sveltejs/kit": ">=3.4 <3.5"
|
|
95
139
|
},
|
|
96
140
|
"peerDependenciesMeta": {
|
|
97
141
|
"@sveltejs/kit": {
|
|
@@ -107,6 +151,6 @@
|
|
|
107
151
|
"vite": "^8.3.8"
|
|
108
152
|
},
|
|
109
153
|
"scripts": {
|
|
110
|
-
"check": "tsc && tsc -p tsconfig.node.json"
|
|
154
|
+
"check": "tsc && tsc -p tsconfig.node.json && tsc -p tsconfig.host.json && node -e \"require('fs').copyFileSync('vite/permissions.d.ts', 'types/vite/permissions.d.ts')\""
|
|
111
155
|
}
|
|
112
156
|
}
|
package/src/abort.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// `AbortController` and `AbortSignal`, for `fetch`, streams and listeners
|
|
2
|
+
// (`addEventListener(..., { signal })`). Installed as globals by `web.js`.
|
|
3
|
+
//
|
|
4
|
+
// A signal is an event target of its own here, not the DOM layer's
|
|
5
|
+
// `EventTarget`: the DOM layer's `Event` and `EventTarget` know about the
|
|
6
|
+
// renderer's events, and with the DOM layer loaded its globals stay its own.
|
|
7
|
+
|
|
8
|
+
/** An `abort` event: what a signal hands its listeners. */
|
|
9
|
+
class AbortEvent {
|
|
10
|
+
/** @param {AbortSignal} target */
|
|
11
|
+
constructor(target) {
|
|
12
|
+
this.type = 'abort';
|
|
13
|
+
this.target = this.currentTarget = target;
|
|
14
|
+
this.bubbles = false;
|
|
15
|
+
this.cancelable = false;
|
|
16
|
+
this.defaultPrevented = false;
|
|
17
|
+
this.timeStamp = /** @type {any} */ (globalThis).performance?.now?.() ?? 0;
|
|
18
|
+
}
|
|
19
|
+
preventDefault() {}
|
|
20
|
+
stopPropagation() {}
|
|
21
|
+
stopImmediatePropagation() {}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Why a signal was aborted, when no reason was given: the error `fetch` rejects with. @param {string} message @param {string} name */
|
|
25
|
+
export function abort_error(message, name = 'AbortError') {
|
|
26
|
+
const DOMException = /** @type {any} */ (globalThis).DOMException;
|
|
27
|
+
if (typeof DOMException === 'function') return new DOMException(message, name);
|
|
28
|
+
const error = new Error(message);
|
|
29
|
+
error.name = name;
|
|
30
|
+
return error;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class AbortSignal {
|
|
34
|
+
/** @type {Map<string, Set<Function>>} */
|
|
35
|
+
#listeners = new Map();
|
|
36
|
+
|
|
37
|
+
constructor() {
|
|
38
|
+
this.aborted = false;
|
|
39
|
+
/** @type {any} */
|
|
40
|
+
this.reason = undefined;
|
|
41
|
+
/** @type {((event: AbortEvent) => void) | null} */
|
|
42
|
+
this.onabort = null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
throwIfAborted() {
|
|
46
|
+
if (this.aborted) throw this.reason;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** @param {string} type @param {any} handler @param {any} [options] */
|
|
50
|
+
addEventListener(type, handler, options) {
|
|
51
|
+
if (handler == null) return;
|
|
52
|
+
let set = this.#listeners.get(type);
|
|
53
|
+
if (set === undefined) this.#listeners.set(type, (set = new Set()));
|
|
54
|
+
const once = typeof options === 'object' && options !== null && options.once;
|
|
55
|
+
const listener = once
|
|
56
|
+
? (/** @type {AbortEvent} */ event) => {
|
|
57
|
+
this.removeEventListener(type, listener);
|
|
58
|
+
call(handler, this, event);
|
|
59
|
+
}
|
|
60
|
+
: handler;
|
|
61
|
+
if (once) set.add(listener);
|
|
62
|
+
else set.add(handler);
|
|
63
|
+
const signal = typeof options === 'object' && options !== null ? options.signal : null;
|
|
64
|
+
if (signal instanceof AbortSignal) signal.addEventListener('abort', () => this.removeEventListener(type, listener), { once: true });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** @param {string} type @param {any} handler */
|
|
68
|
+
removeEventListener(type, handler) {
|
|
69
|
+
this.#listeners.get(type)?.delete(handler);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** @param {{ type: string }} event */
|
|
73
|
+
dispatchEvent(event) {
|
|
74
|
+
if (event.type === 'abort' && typeof this.onabort === 'function') this.onabort(/** @type {AbortEvent} */ (event));
|
|
75
|
+
for (const handler of [...(this.#listeners.get(event.type) ?? [])]) call(handler, this, event);
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** @param {any} [reason] */
|
|
80
|
+
static abort(reason) {
|
|
81
|
+
const controller = new AbortController();
|
|
82
|
+
controller.abort(reason);
|
|
83
|
+
return controller.signal;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** A signal that aborts after `ms` milliseconds, with a `TimeoutError`. @param {number} ms */
|
|
87
|
+
static timeout(ms) {
|
|
88
|
+
const controller = new AbortController();
|
|
89
|
+
/** @type {any} */ (globalThis).setTimeout(() => controller.abort(abort_error('the operation timed out', 'TimeoutError')), ms);
|
|
90
|
+
return controller.signal;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** A signal that aborts when the first of `signals` does, with its reason. @param {Iterable<AbortSignal>} signals */
|
|
94
|
+
static any(signals) {
|
|
95
|
+
const controller = new AbortController();
|
|
96
|
+
for (const signal of signals) {
|
|
97
|
+
if (signal.aborted) {
|
|
98
|
+
controller.abort(signal.reason);
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
signal.addEventListener('abort', () => controller.abort(signal.reason), { once: true });
|
|
102
|
+
}
|
|
103
|
+
return controller.signal;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** @param {any} handler @param {AbortSignal} target @param {any} event */
|
|
108
|
+
function call(handler, target, event) {
|
|
109
|
+
try {
|
|
110
|
+
if (typeof handler === 'function') handler.call(target, event);
|
|
111
|
+
else handler.handleEvent(event);
|
|
112
|
+
} catch (error) {
|
|
113
|
+
/** @type {any} */ (globalThis).console?.error?.('Error in an abort listener:', error);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export class AbortController {
|
|
118
|
+
constructor() {
|
|
119
|
+
this.signal = new AbortSignal();
|
|
120
|
+
}
|
|
121
|
+
/** @param {any} [reason] */
|
|
122
|
+
abort(reason) {
|
|
123
|
+
const signal = this.signal;
|
|
124
|
+
if (signal.aborted) return;
|
|
125
|
+
signal.aborted = true;
|
|
126
|
+
signal.reason = reason === undefined ? abort_error('signal is aborted without reason') : reason;
|
|
127
|
+
signal.dispatchEvent(new AbortEvent(signal));
|
|
128
|
+
}
|
|
129
|
+
}
|
package/src/app.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// The app as a whole, beyond its window.
|
|
2
|
+
|
|
3
|
+
import { host_call } from './host-call.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Quit the app: its window, its server code, the process. Needs no
|
|
7
|
+
* permission. An app with `background: true` keeps running when its last
|
|
8
|
+
* window closes, and ends only through this (or the system's Quit).
|
|
9
|
+
* @returns {Promise<void>}
|
|
10
|
+
*/
|
|
11
|
+
export function quit() {
|
|
12
|
+
return host_call('app.quit');
|
|
13
|
+
}
|