sepal 0.1.0-alpha.2 → 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.
Files changed (81) hide show
  1. package/README.md +16 -9
  2. package/cli.js +127 -10
  3. package/package.json +48 -21
  4. package/src/abort.js +129 -0
  5. package/src/app.js +13 -0
  6. package/src/dom.js +173 -60
  7. package/src/fetch.js +347 -80
  8. package/src/form-data.js +141 -0
  9. package/src/host-call.js +57 -0
  10. package/src/index.js +14 -12
  11. package/src/lru.js +45 -0
  12. package/src/native.js +32 -0
  13. package/src/notifications.js +60 -0
  14. package/src/os.js +35 -0
  15. package/src/process.js +164 -0
  16. package/src/protocol.js +3 -4
  17. package/src/renderer.js +140 -10
  18. package/src/secrets.js +42 -0
  19. package/src/settings.js +52 -0
  20. package/src/shims.js +111 -11
  21. package/src/stem.js +125 -0
  22. package/src/updater.js +3 -2
  23. package/src/url.js +65 -20
  24. package/src/web.js +86 -0
  25. package/src/window.js +6 -2
  26. package/types/abort.d.ts +46 -0
  27. package/types/app.d.ts +7 -0
  28. package/types/fetch.d.ts +118 -28
  29. package/types/form-data.d.ts +19 -0
  30. package/types/host-call.d.ts +18 -0
  31. package/types/index.d.ts +5 -2
  32. package/types/lru.d.ts +18 -0
  33. package/types/native.d.ts +22 -0
  34. package/types/notifications.d.ts +53 -0
  35. package/types/os.d.ts +53 -0
  36. package/types/process.d.ts +90 -0
  37. package/types/protocol.d.ts +0 -2
  38. package/types/renderer.d.ts +16 -1
  39. package/types/secrets.d.ts +22 -0
  40. package/types/settings.d.ts +29 -0
  41. package/types/shims.d.ts +13 -0
  42. package/types/stem.d.ts +21 -0
  43. package/types/updater.d.ts +6 -4
  44. package/types/url.d.ts +1 -1
  45. package/types/vite/adapter.d.ts +22 -0
  46. package/types/vite/add.d.ts +77 -0
  47. package/types/vite/app.d.ts +52 -1
  48. package/types/vite/licenses.d.ts +11 -0
  49. package/types/vite/native.d.ts +135 -0
  50. package/types/vite/page.d.ts +20 -0
  51. package/types/vite/permissions.d.ts +56 -0
  52. package/types/vite/sveltekit.d.ts +5 -34
  53. package/types/vite/vite.d.ts +86 -53
  54. package/types/web.d.ts +17 -0
  55. package/types/window.d.ts +5 -1
  56. package/vite/adapter.js +118 -0
  57. package/vite/add.js +254 -0
  58. package/vite/app.js +195 -27
  59. package/vite/head.js +2 -2
  60. package/vite/licenses.js +115 -0
  61. package/vite/native.js +535 -0
  62. package/vite/page.js +95 -0
  63. package/vite/permissions.d.ts +56 -0
  64. package/vite/sveltekit.js +11 -224
  65. package/vite/vite.js +377 -147
  66. package/ambient.d.ts +0 -20
  67. package/types/vite/navigation.d.ts +0 -1
  68. package/types/vite/router-entry.d.ts +0 -1
  69. package/types/vite/router.d.ts +0 -21
  70. package/types/vite/routes.d.ts +0 -41
  71. package/types/vite/routing.svelte.d.ts +0 -131
  72. package/types/vite/state.d.ts +0 -1
  73. package/vite/Router.svelte +0 -32
  74. package/vite/RouterError.svelte +0 -11
  75. package/vite/navigation.js +0 -3
  76. package/vite/router-entry.js +0 -12
  77. package/vite/router.js +0 -17
  78. package/vite/routes.js +0 -219
  79. package/vite/routing.svelte.js +0 -378
  80. package/vite/state.js +0 -2
  81. package/vite/virtual.d.ts +0 -9
package/README.md CHANGED
@@ -9,7 +9,7 @@ a Rust HTML/CSS engine; the JavaScript runs in an embedded QuickJS.
9
9
 
10
10
  An alpha, on macOS (Linux builds and passes its tests headless; its window has
11
11
  not been tried yet). Svelte 5 and Tailwind v4 as on the web, pages as files
12
- (Sepal's router or SvelteKit's), shadcn-svelte components through an opt-in
12
+ run by SvelteKit's router, shadcn-svelte components through an opt-in
13
13
  DOM layer, desktop APIs behind permissions, macOS `.app` bundles and
14
14
  self-update, and no frame loop: an idle app does no work.
15
15
 
@@ -24,7 +24,7 @@ runs; [how they were measured](https://github.com/etherCorps/sepal/blob/main/com
24
24
  | Idle memory, 1,000 issues | 107 MB RAM, 43 MB GPU memory |
25
25
  | Memory after wheel-scrolling the whole list, 1,000 issues | 305 MB RAM, 220 MB GPU memory |
26
26
 
27
- This one package is the runtime, the Vite preset and the `sepal` command; the
27
+ This one package is the runtime, the Vite presets and the `sepal` command; the
28
28
  prebuilt host comes with it. Start a project with the starter rather than by
29
29
  installing it directly:
30
30
 
@@ -41,17 +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, `sepal()`, and `defineConfig` for `sepal.config.ts` |
45
- | `sepal/sveltekit` | `sepalKit()`, which takes the place of both `sveltekit()` and the adapter |
46
- | `sepal/router`, `sepal/navigation`, `sepal/state` | the file-based router (`src/routes`, `$app/navigation`, `$app/state`) |
47
- | `sepal/ambient` | types for `$app/*`; add it to `types` in tsconfig.json |
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 |
48
46
 
49
- The preset compiles Svelte for the Blitz renderer, writes the one `app.js` and
50
- `app.css` the host loads, reads `sepal.config.ts`, and in `vite build --watch`
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`
51
50
  opens the app and reloads it on save.
52
51
 
53
52
  ```ts
54
- // 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
55
62
  import { sepal } from 'sepal/vite';
56
63
  export default { plugins: [sepal()] };
57
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 build`, and `vite build --watch` for dev).
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
- const [command, ...args] = process.argv.slice(2);
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
- start(bundled(calyx(), dist[0]), dist);
96
- case 'bundle':
97
- host(['--bundle', ...dist]);
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
- host(['--screenshot', args[0], ...(args.length > 1 ? args.slice(1) : ['dist'])]);
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.1.0-alpha.2",
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": {
@@ -22,7 +22,6 @@
22
22
  "gui",
23
23
  "vite",
24
24
  "vite-plugin",
25
- "router",
26
25
  "svelte-blitz"
27
26
  ],
28
27
  "type": "module",
@@ -58,6 +57,26 @@
58
57
  "types": "./types/clipboard.d.ts",
59
58
  "default": "./src/clipboard.js"
60
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
+ },
61
80
  "./shell": {
62
81
  "types": "./types/shell.d.ts",
63
82
  "default": "./src/shell.js"
@@ -66,6 +85,18 @@
66
85
  "types": "./types/window.d.ts",
67
86
  "default": "./src/window.js"
68
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
+ },
69
100
  "./dom": {
70
101
  "types": "./types/dom.d.ts",
71
102
  "default": "./src/dom.js"
@@ -74,41 +105,37 @@
74
105
  "types": "./types/vite/vite.d.ts",
75
106
  "default": "./vite/vite.js"
76
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
+ },
77
115
  "./sveltekit": {
78
116
  "types": "./types/vite/sveltekit.d.ts",
79
117
  "default": "./vite/sveltekit.js"
80
- },
81
- "./router": {
82
- "types": "./types/vite/router.d.ts",
83
- "default": "./vite/router.js"
84
- },
85
- "./navigation": {
86
- "types": "./types/vite/navigation.d.ts",
87
- "default": "./vite/navigation.js"
88
- },
89
- "./state": {
90
- "types": "./types/vite/state.d.ts",
91
- "default": "./vite/state.js"
92
- },
93
- "./ambient": {
94
- "types": "./ambient.d.ts"
95
118
  }
96
119
  },
97
120
  "files": [
98
121
  "src",
99
122
  "vite",
100
123
  "types",
101
- "ambient.d.ts",
102
124
  "cli.js"
103
125
  ],
104
126
  "dependencies": {
105
- "@ethercorps/calyx": "0.1.0-alpha.2"
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"
106
133
  },
107
134
  "peerDependencies": {
108
135
  "svelte": "^5.59.0",
109
136
  "@sveltejs/vite-plugin-svelte": ">=7",
110
137
  "vite": ">=8",
111
- "@sveltejs/kit": ">=3.4"
138
+ "@sveltejs/kit": ">=3.4 <3.5"
112
139
  },
113
140
  "peerDependenciesMeta": {
114
141
  "@sveltejs/kit": {
@@ -124,6 +151,6 @@
124
151
  "vite": "^8.3.8"
125
152
  },
126
153
  "scripts": {
127
- "check": "tsc && tsc -p tsconfig.vite.json && 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')\""
128
155
  }
129
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
+ }