@x-tech-laboratories/x-app 0.102.0 → 0.103.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 +3 -2
- package/dist/app/daemon.d.ts +6 -1
- package/dist/app/daemon.js +23 -2
- package/dist/app/define.d.ts +4 -0
- package/dist/app/desktop.js +2 -0
- package/dist/app/protocol.d.ts +1 -0
- package/dist/app/server.d.ts +6 -0
- package/dist/app/server.js +28 -2
- package/dist/app/services.d.ts +28 -0
- package/dist/app/services.js +37 -0
- package/dist/app/web/hosts.js +29 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,9 +10,10 @@ an app moves over by changing the package name in its imports and its `x-libs-*`
|
|
|
10
10
|
| Import | What it holds |
|
|
11
11
|
| ---------------------------------------- | ----------------------------------------------------------------------------- |
|
|
12
12
|
| `app/define` | The base app: `define_app` puts an X-app's name, ports, files, its own settings and its `define_feature` features (tables, migrations, calls, `api_spec`, settings, histories) together into one store every device keeps (`open_store` calling each feature's `on_open`, `needs_upgrade`, `bind_methods(database, on_change, caller)` running each call as its `caller`, calling each feature's `after_write`, recording versions and announcing writes, `is_read_only`), `page_only` (the specs' `page_only` calls and the writes of `user_only` settings, refused with `page_only_refusal`), its sync engine, its backups' names and schedule, and its bridges named after the app; `AppMethodsOf<typeof app>` types the app's calls. |
|
|
13
|
-
| `app/server`, `app/daemon` | `run_server(app, { web_folder })`: the web server from `<PREFIX>_CONFIG` with accounts in `accounts.db` (moved there once from the data's own file, tokens starting with the app's `token_prefix`), the calls, OpenAPI, MCP, the server's backups and its sync peer, a token or MCP client refused the app's `page_only` calls and the `user_only_backup_calls` (restore, remove, schedule) and its calls run as the `program` caller; `run_daemon(app)`: the desktop daemon from `<PREFIX>_DB` with sync, letting devices connect, backups, certificates and the desktop settings; `build_server`, `start_daemon`, `open_app_database` and `register_app_calls` for tests and apps that add their own. |
|
|
13
|
+
| `app/server`, `app/daemon` | `run_server(app, { web_folder })`: the web server from `<PREFIX>_CONFIG` with accounts in `accounts.db` (moved there once from the data's own file, tokens starting with the app's `token_prefix`), the calls, OpenAPI, MCP, the server's backups and its sync peer, a token or MCP client refused the app's `page_only` calls and the `user_only_backup_calls` (restore, remove, schedule) and its calls run as the `program` caller; `run_daemon(app)`: the desktop daemon from `<PREFIX>_DB` with sync, letting devices connect, backups, certificates and the desktop settings; Both take the app's `services` (see `app/services`), `run_daemon(app, env, { services })`. `build_server`, `start_daemon`, `open_app_database` (with `on_restore` hooks) and `register_app_calls` for tests and apps that add their own. |
|
|
14
|
+
| `app/services` | An app's own work beside its calls, run by both hosts: `StartServices` gets a `HostContext` (`kind`, `database()`, `profile`, `device_name`, the server's raw `config`, `plugins_folder`, `declared_file` from `<PREFIX>_DECLARED_FILE` or `/etc/<id>/declared.json`, `announce`, `emit(event, data)` for the page's `on_event`, `side_database(name)` for a never-backed-up file such as `<base>-index.db`, `back_up`) and returns `HostServices` (`before_restore`, `after_restore`, `stop`), stopped before sync and the database. |
|
|
14
15
|
| `app/desktop`, `app/preload` | `run_desktop(app, electron, { main_dir })`: Electron's main process as a portal onto this computer's daemon or a server, with the window, the login entry, a second launch and `--daemon`; `run_preload(app, electron)` puts every bridge on the page. Electron is handed in, so neither imports it. |
|
|
15
|
-
| `app/web/app_page`, `app/web/page` | `render_app({ app, pages, general_settings })`: the page on the web, the desktop or Android in the `Shell`, with sign-in in a browser, Your account, Settings (the app's general sections, each page's own, Connections, Desktop app, Sync and Backups) and `SyncButton`; `page_hooks<Methods>()` gives the app's typed `calls`, `use_data` and `use_settings`. The phone's host (`app/web/android`) loads only on Android. |
|
|
16
|
+
| `app/web/app_page`, `app/web/page` | `render_app({ app, pages, general_settings })`: the page on the web, the desktop or Android in the `Shell`, with sign-in in a browser, Your account, Settings (the app's general sections, each page's own, Connections, Desktop app, Sync and Backups) and `SyncButton`; `page_hooks<Methods>()` gives the app's typed `calls`, `use_data` and `use_settings`. The phone's host (`app/web/android`) loads only on Android. The page API's `on_event(name, listener)` hears the named events the app's services `emit`, on the web and the desktop. |
|
|
16
17
|
| `config/vite`, `config/electron_vite`, `config/vitest`, `config/tsconfig/*.json` | An X-app's configs as presets: `web_vite_config({ port })` and `android_vite_config()` for the page, `desktop_vite_config()` for electron-vite's window, daemon, preload and page, `vitest_config()` for tests in `tests/` in UTC with coverage of `src/` to the terminal, merged with the app's own floor so `autoUpdate` still raises it, and `base`, `node`, `web` and `tests` tsconfigs to extend. |
|
|
17
18
|
| `app_new` | The `x-app-new <X-Name>` command: a new X-app's files written once, laid out as the Application Standard asks, on the base app, the presets, X-App's Nix lib and its CI actions, with an example notes feature; `--port`, `--folder`, `--no-desktop` and `--no-android`. |
|
|
18
19
|
| `durations` | `parse_duration` for settings such as `"2h30m"`, `format_duration` in months down to seconds and "less than a second" below one, `format_age`, and `format_clock` for a playing time such as `"3:07"`, with `{ hours: "always" }` writing `"0:03:07"` so a column of times lines up and `empty` for no time. |
|
package/dist/app/daemon.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { App } from "./define.ts";
|
|
2
|
+
import { type StartServices } from "./services.ts";
|
|
2
3
|
export interface DaemonOptions {
|
|
3
4
|
database_path: string;
|
|
4
5
|
socket_path: string;
|
|
@@ -6,9 +7,13 @@ export interface DaemonOptions {
|
|
|
6
7
|
unused_exit_ms?: number;
|
|
7
8
|
unused_check_ms?: number;
|
|
8
9
|
default_peer_port?: number;
|
|
10
|
+
services?: StartServices;
|
|
11
|
+
declared_file?: string;
|
|
9
12
|
}
|
|
10
13
|
export interface Daemon {
|
|
11
14
|
shut_down: () => Promise<void>;
|
|
12
15
|
}
|
|
13
16
|
export declare function start_daemon(app: App, options: DaemonOptions): Promise<Daemon | null>;
|
|
14
|
-
export declare function run_daemon(app: App, env?: NodeJS.ProcessEnv
|
|
17
|
+
export declare function run_daemon(app: App, env?: NodeJS.ProcessEnv, extra?: {
|
|
18
|
+
services?: StartServices;
|
|
19
|
+
}): Promise<void>;
|
package/dist/app/daemon.js
CHANGED
|
@@ -14,6 +14,7 @@ import { register_error_replies, register_lenient_json_body } from "../server/re
|
|
|
14
14
|
import { file_sync_storage } from "../sync_file_storage.js";
|
|
15
15
|
import { create_sync_peer, sync_device_methods } from "../sync_peer.js";
|
|
16
16
|
import { keep_listening_for_peers } from "../sync_peer_listening.js";
|
|
17
|
+
import { declared_file_of, no_services, side_databases } from "./services.js";
|
|
17
18
|
import { read_desktop_settings } from "./protocol.js";
|
|
18
19
|
import { call_body_limit, open_app_database, register_app_calls } from "./server.js";
|
|
19
20
|
// ── Daemon ───────────────────────────────────────────────────────────────────
|
|
@@ -72,12 +73,16 @@ export async function start_daemon(app, options) {
|
|
|
72
73
|
// ── Lifecycle ──────────────────────────────────────────────────────
|
|
73
74
|
let stopping = null;
|
|
74
75
|
let unused_timer;
|
|
76
|
+
const sides = side_databases(database_path);
|
|
77
|
+
let services = no_services;
|
|
75
78
|
function shut_down() {
|
|
76
79
|
stopping ??= (async () => {
|
|
77
80
|
clearInterval(unused_timer);
|
|
81
|
+
await services.stop();
|
|
78
82
|
sync.stop();
|
|
79
83
|
await daemon.close();
|
|
80
84
|
await listening.stop();
|
|
85
|
+
sides.close_all();
|
|
81
86
|
opened.close();
|
|
82
87
|
options.on_stopped();
|
|
83
88
|
})();
|
|
@@ -96,6 +101,22 @@ export async function start_daemon(app, options) {
|
|
|
96
101
|
}
|
|
97
102
|
await listening.start();
|
|
98
103
|
sync.start();
|
|
104
|
+
if (options.services) {
|
|
105
|
+
services = await options.services({
|
|
106
|
+
kind: "desktop",
|
|
107
|
+
database: opened.database,
|
|
108
|
+
profile,
|
|
109
|
+
device_name: `${app.name} on ${hostname()}`,
|
|
110
|
+
config: {},
|
|
111
|
+
plugins_folder: join(profile, "plugins"),
|
|
112
|
+
declared_file: options.declared_file ?? `/etc/${app.id}/declared.json`,
|
|
113
|
+
announce: (client_id) => changes.announce_change(client_id),
|
|
114
|
+
emit: (event, data) => changes.send(JSON.stringify(data), { event }),
|
|
115
|
+
side_database: sides.open,
|
|
116
|
+
back_up: opened.back_up,
|
|
117
|
+
});
|
|
118
|
+
opened.on_restore({ before: () => services.before_restore?.(), after: () => services.after_restore?.() });
|
|
119
|
+
}
|
|
99
120
|
// Without "Keep running in the background", the daemon stops a while after its last window has gone.
|
|
100
121
|
let unused_since = Date.now();
|
|
101
122
|
unused_timer = setInterval(() => {
|
|
@@ -109,7 +130,7 @@ export async function start_daemon(app, options) {
|
|
|
109
130
|
// ── Running ──────────────────────────────────────────────────────────────────
|
|
110
131
|
// Starts the daemon on the database and socket the window names in <PREFIX>_DB and <PREFIX>_SOCKET, and ends the
|
|
111
132
|
// process once it stops.
|
|
112
|
-
export async function run_daemon(app, env = process.env) {
|
|
133
|
+
export async function run_daemon(app, env = process.env, extra = {}) {
|
|
113
134
|
const database_path = env[`${app.env_prefix}_DB`];
|
|
114
135
|
if (!database_path) {
|
|
115
136
|
console.error(`${app.id} daemon: ${app.env_prefix}_DB must name the database`);
|
|
@@ -118,7 +139,7 @@ export async function run_daemon(app, env = process.env) {
|
|
|
118
139
|
// A run on another database has a socket of its own.
|
|
119
140
|
const socket_path = env[`${app.env_prefix}_SOCKET`] || daemon_socket_path({ app: app.id, data_file: database_path });
|
|
120
141
|
try {
|
|
121
|
-
const daemon = await start_daemon(app, { database_path, socket_path, on_stopped: () => process.exit(0) });
|
|
142
|
+
const daemon = await start_daemon(app, { database_path, socket_path, on_stopped: () => process.exit(0), declared_file: declared_file_of(app, env), ...extra });
|
|
122
143
|
// Another daemon on this database got there first, which is all that was asked for.
|
|
123
144
|
if (!daemon)
|
|
124
145
|
process.exit(0);
|
package/dist/app/define.d.ts
CHANGED
|
@@ -66,6 +66,10 @@ export interface Restorable extends Backups {
|
|
|
66
66
|
export type AppEvents = {
|
|
67
67
|
changed: null;
|
|
68
68
|
sync_status: SyncStatus;
|
|
69
|
+
app_event: {
|
|
70
|
+
event: string;
|
|
71
|
+
data: unknown;
|
|
72
|
+
};
|
|
69
73
|
};
|
|
70
74
|
export interface AppBridges<Methods extends Record<keyof Methods, (...args: never[]) => unknown>> {
|
|
71
75
|
calls: Bridge<Methods>;
|
package/dist/app/desktop.js
CHANGED
|
@@ -66,6 +66,8 @@ export function run_desktop(app, electron, options) {
|
|
|
66
66
|
void apply_window_settings().catch(() => undefined);
|
|
67
67
|
},
|
|
68
68
|
on_sync_status: (status) => app.bridges.events.emit(windows(), "sync_status", status),
|
|
69
|
+
// The app's own events, such as a reply as it is written, go to every window by name.
|
|
70
|
+
on_event: (event, data) => app.bridges.events.emit(windows(), "app_event", { event, data: data === "" ? null : JSON.parse(data) }),
|
|
69
71
|
on_switch: () => {
|
|
70
72
|
for (const window of windows())
|
|
71
73
|
window.webContents.reload();
|
package/dist/app/protocol.d.ts
CHANGED
|
@@ -36,4 +36,5 @@ export interface PageApi<Methods extends Record<keyof Methods, (...args: never[]
|
|
|
36
36
|
peer_listening?: PeerListeningClient;
|
|
37
37
|
desktop?: Client<DesktopMethods>;
|
|
38
38
|
on_change: (listener: () => void) => () => void;
|
|
39
|
+
on_event?: (event: string, listener: (data: unknown) => void) => () => void;
|
|
39
40
|
}
|
package/dist/app/server.d.ts
CHANGED
|
@@ -6,10 +6,15 @@ import { EventStream } from "../server/events.ts";
|
|
|
6
6
|
import type { SqlDatabase } from "../sql.ts";
|
|
7
7
|
import { type SyncPeer } from "../sync_peer.ts";
|
|
8
8
|
import type { App } from "./define.ts";
|
|
9
|
+
import { type StartServices } from "./services.ts";
|
|
9
10
|
export interface AppDatabase {
|
|
10
11
|
database: () => DatabaseSync;
|
|
11
12
|
back_up: () => Promise<void>;
|
|
12
13
|
backup_methods: BackupMethods;
|
|
14
|
+
on_restore: (hooks: {
|
|
15
|
+
before?: () => void;
|
|
16
|
+
after?: () => void;
|
|
17
|
+
}) => void;
|
|
13
18
|
close: () => void;
|
|
14
19
|
}
|
|
15
20
|
export declare function open_app_database(app: App, path: string, backups_folder: string): AppDatabase;
|
|
@@ -30,6 +35,7 @@ export interface RunServerOptions {
|
|
|
30
35
|
web_folder: URL;
|
|
31
36
|
argv?: string[];
|
|
32
37
|
env?: NodeJS.ProcessEnv;
|
|
38
|
+
services?: StartServices;
|
|
33
39
|
}
|
|
34
40
|
export interface RunningServer {
|
|
35
41
|
server: FastifyInstance;
|
package/dist/app/server.js
CHANGED
|
@@ -24,21 +24,27 @@ import { register_sync_peer_routes } from "../server/sync_peer_routes.js";
|
|
|
24
24
|
import { register_web_folder } from "../server/web_folder.js";
|
|
25
25
|
import { file_sync_storage } from "../sync_file_storage.js";
|
|
26
26
|
import { create_sync_peer, sync_device_methods } from "../sync_peer.js";
|
|
27
|
+
import { declared_file_of, no_services, side_databases } from "./services.js";
|
|
27
28
|
// The app's database as the server and the desktop daemon keep it: a file opened and brought up to date, backed up
|
|
28
29
|
// before its schema is upgraded, before another copy replaces it, and daily, and restored from a backup.
|
|
29
30
|
export function open_app_database(app, path, backups_folder) {
|
|
30
31
|
mkdirSync(dirname(path), { recursive: true });
|
|
31
32
|
const existed = existsSync(path);
|
|
32
33
|
let database = new DatabaseSync(path);
|
|
34
|
+
let restore_hooks = {};
|
|
33
35
|
const kept = sqlite_backups({
|
|
34
36
|
folder: () => (mkdirSync(backups_folder, { recursive: true }), backups_folder),
|
|
35
37
|
naming: app.backup_naming,
|
|
36
38
|
database: () => database,
|
|
37
39
|
database_path: () => path,
|
|
38
|
-
close: () =>
|
|
40
|
+
close: () => {
|
|
41
|
+
restore_hooks.before?.();
|
|
42
|
+
database.close();
|
|
43
|
+
},
|
|
39
44
|
open: () => {
|
|
40
45
|
database = new DatabaseSync(path);
|
|
41
46
|
start(database);
|
|
47
|
+
restore_hooks.after?.();
|
|
42
48
|
},
|
|
43
49
|
// The restored data no longer matches other devices', so sync asks again which copy wins.
|
|
44
50
|
after_restore: (restored) => app.sync_engine.forget_history(restored),
|
|
@@ -57,6 +63,7 @@ export function open_app_database(app, path, backups_folder) {
|
|
|
57
63
|
database: () => database,
|
|
58
64
|
back_up: async () => void (await kept.create()),
|
|
59
65
|
backup_methods: app.backup_methods(kept, () => database, () => void keeper.check()),
|
|
66
|
+
on_restore: (hooks) => void (restore_hooks = hooks),
|
|
60
67
|
close: () => {
|
|
61
68
|
keeper.stop();
|
|
62
69
|
database.close();
|
|
@@ -203,10 +210,29 @@ export async function run_server(app, options) {
|
|
|
203
210
|
const server = await build_server(app, { database: opened.database, accounts, web_folder: server_config.web_folder, sync, changes, backup_methods: opened.backup_methods });
|
|
204
211
|
const address = await server.listen({ host: server_config.host, port: server_config.port });
|
|
205
212
|
sync.start();
|
|
213
|
+
const sides = side_databases(database_path);
|
|
214
|
+
const services = options.services
|
|
215
|
+
? await options.services({
|
|
216
|
+
kind: "server",
|
|
217
|
+
database: opened.database,
|
|
218
|
+
profile: dirname(database_path),
|
|
219
|
+
device_name: `${app.name} server on ${hostname()}`,
|
|
220
|
+
config: config,
|
|
221
|
+
plugins_folder: config.plugins_folder ? resolve(config.plugins_folder) : null,
|
|
222
|
+
declared_file: declared_file_of(app, options.env ?? process.env),
|
|
223
|
+
announce: (client_id) => changes.announce_change(client_id),
|
|
224
|
+
emit: (event, data) => changes.send(JSON.stringify(data), { event }),
|
|
225
|
+
side_database: sides.open,
|
|
226
|
+
back_up: opened.back_up,
|
|
227
|
+
})
|
|
228
|
+
: no_services;
|
|
229
|
+
opened.on_restore({ before: () => services.before_restore?.(), after: () => services.after_restore?.() });
|
|
206
230
|
console.info(`${app.name} is serving ${address}/`);
|
|
207
231
|
let stopping = null;
|
|
208
|
-
const stop = () => (stopping ??= server.close().then(() => {
|
|
232
|
+
const stop = () => (stopping ??= server.close().then(async () => {
|
|
233
|
+
await services.stop();
|
|
209
234
|
sync.stop();
|
|
235
|
+
sides.close_all();
|
|
210
236
|
opened.close();
|
|
211
237
|
accounts_database.close();
|
|
212
238
|
}));
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { DatabaseSync } from "node:sqlite";
|
|
2
|
+
import type { App } from "./define.ts";
|
|
3
|
+
export interface HostContext {
|
|
4
|
+
kind: "server" | "desktop";
|
|
5
|
+
database: () => DatabaseSync;
|
|
6
|
+
profile: string;
|
|
7
|
+
device_name: string;
|
|
8
|
+
config: Readonly<Record<string, unknown>>;
|
|
9
|
+
plugins_folder: string | null;
|
|
10
|
+
declared_file: string;
|
|
11
|
+
announce: (client_id?: string) => void;
|
|
12
|
+
emit: (event: string, data: unknown) => void;
|
|
13
|
+
side_database: (name: string) => DatabaseSync;
|
|
14
|
+
back_up: () => Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
export interface HostServices {
|
|
17
|
+
before_restore?: () => void;
|
|
18
|
+
after_restore?: () => void;
|
|
19
|
+
stop: () => void | Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
export type StartServices = (context: HostContext) => HostServices | Promise<HostServices>;
|
|
22
|
+
export declare function declared_file_of(app: Pick<App, "id" | "env_prefix">, env: NodeJS.ProcessEnv): string;
|
|
23
|
+
export interface SideDatabases {
|
|
24
|
+
open: (name: string) => DatabaseSync;
|
|
25
|
+
close_all: () => void;
|
|
26
|
+
}
|
|
27
|
+
export declare function side_databases(database_path: string): SideDatabases;
|
|
28
|
+
export declare const no_services: HostServices;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// An app's own work beside its calls, which the web server and the desktop daemon both run: background jobs, an
|
|
2
|
+
// index kept in a file of its own, collectors reading other apps, an assistant. The host hands the services what it
|
|
3
|
+
// has, starts them once the database is open, tells them around a restore, and stops them before sync and the
|
|
4
|
+
// database close.
|
|
5
|
+
// ── Imports ──────────────────────────────────────────────────────────────────
|
|
6
|
+
import { DatabaseSync } from "node:sqlite";
|
|
7
|
+
// ── Files ────────────────────────────────────────────────────────────────────
|
|
8
|
+
// The declarations file a host reads: <PREFIX>_DECLARED_FILE when it is set, even empty for none, else the NixOS
|
|
9
|
+
// module's /etc/<id>/declared.json.
|
|
10
|
+
export function declared_file_of(app, env) {
|
|
11
|
+
return env[`${app.env_prefix}_DECLARED_FILE`] ?? `/etc/${app.id}/declared.json`;
|
|
12
|
+
}
|
|
13
|
+
// The side databases beside the main file, named after it, such as notes-index.db beside notes.db; each opens once.
|
|
14
|
+
export function side_databases(database_path) {
|
|
15
|
+
const opened = new Map();
|
|
16
|
+
const base = database_path.replace(/\.db$/, "");
|
|
17
|
+
return {
|
|
18
|
+
open: (name) => {
|
|
19
|
+
if (!/^[a-z0-9_-]+$/.test(name))
|
|
20
|
+
throw new Error(`A side database's name is letters, digits, - and _, not '${name}'.`);
|
|
21
|
+
let database = opened.get(name);
|
|
22
|
+
if (!database) {
|
|
23
|
+
database = new DatabaseSync(`${base}-${name}.db`);
|
|
24
|
+
database.exec("PRAGMA journal_mode = WAL");
|
|
25
|
+
opened.set(name, database);
|
|
26
|
+
}
|
|
27
|
+
return database;
|
|
28
|
+
},
|
|
29
|
+
close_all: () => {
|
|
30
|
+
for (const database of opened.values())
|
|
31
|
+
database.close();
|
|
32
|
+
opened.clear();
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
// The services when the app has none.
|
|
37
|
+
export const no_services = { stop: () => undefined };
|
package/dist/app/web/hosts.js
CHANGED
|
@@ -13,10 +13,21 @@ const events_path = "api/events";
|
|
|
13
13
|
const client_id_header = "x-client-id";
|
|
14
14
|
const reconnect_delay_ms = 5000;
|
|
15
15
|
// A greeting after the first means the stream reconnected and may have missed changes, so the page reloads too.
|
|
16
|
-
|
|
16
|
+
// Returns how to listen for one of the app's own events on the stream as it is now and after every reconnect.
|
|
17
|
+
function follow_changes(on_change, on_client_id, named) {
|
|
17
18
|
let has_connected = false;
|
|
19
|
+
let current = null;
|
|
20
|
+
const listen = (events, event) => events.addEventListener(event, (message) => {
|
|
21
|
+
const text = message.data;
|
|
22
|
+
const data = text === "" ? null : JSON.parse(text);
|
|
23
|
+
for (const listener of named.get(event) ?? [])
|
|
24
|
+
listener(data);
|
|
25
|
+
});
|
|
18
26
|
const connect = () => {
|
|
19
27
|
const events = new EventSource(events_path);
|
|
28
|
+
current = events;
|
|
29
|
+
for (const event of named.keys())
|
|
30
|
+
listen(events, event);
|
|
20
31
|
events.addEventListener("hello", (event) => {
|
|
21
32
|
on_client_id(event.data);
|
|
22
33
|
if (has_connected)
|
|
@@ -31,6 +42,10 @@ function follow_changes(on_change, on_client_id) {
|
|
|
31
42
|
});
|
|
32
43
|
};
|
|
33
44
|
connect();
|
|
45
|
+
return (event) => {
|
|
46
|
+
if (current)
|
|
47
|
+
listen(current, event);
|
|
48
|
+
};
|
|
34
49
|
}
|
|
35
50
|
export function web_api(app) {
|
|
36
51
|
// Given by the server when the page connects, so the page is not told about its own changes.
|
|
@@ -50,10 +65,11 @@ export function web_api(app) {
|
|
|
50
65
|
return text === "" ? undefined : JSON.parse(text);
|
|
51
66
|
}
|
|
52
67
|
const listeners = new Set();
|
|
53
|
-
|
|
68
|
+
const named = new Map();
|
|
69
|
+
const listen_for = follow_changes(() => {
|
|
54
70
|
for (const listener of listeners)
|
|
55
71
|
listener();
|
|
56
|
-
}, (given) => (client_id = given));
|
|
72
|
+
}, (given) => (client_id = given), named);
|
|
57
73
|
const { bridges } = app;
|
|
58
74
|
return {
|
|
59
75
|
host: "web",
|
|
@@ -65,6 +81,15 @@ export function web_api(app) {
|
|
|
65
81
|
listeners.add(listener);
|
|
66
82
|
return () => listeners.delete(listener);
|
|
67
83
|
},
|
|
84
|
+
on_event: (event, listener) => {
|
|
85
|
+
let kept = named.get(event);
|
|
86
|
+
if (!kept) {
|
|
87
|
+
named.set(event, (kept = new Set()));
|
|
88
|
+
listen_for(event);
|
|
89
|
+
}
|
|
90
|
+
kept.add(listener);
|
|
91
|
+
return () => void kept.delete(listener);
|
|
92
|
+
},
|
|
68
93
|
};
|
|
69
94
|
}
|
|
70
95
|
// ── Desktop ──────────────────────────────────────────────────────────────────
|
|
@@ -89,6 +114,7 @@ export async function desktop_api(app) {
|
|
|
89
114
|
connections,
|
|
90
115
|
desktop: exposed[bridges.desktop.api_name],
|
|
91
116
|
on_change: (listener) => events.on("changed", listener),
|
|
117
|
+
on_event: (event, listener) => events.on("app_event", (sent) => sent.event === event && listener(sent.data)),
|
|
92
118
|
};
|
|
93
119
|
}
|
|
94
120
|
// ── Phone ────────────────────────────────────────────────────────────────────
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-tech-laboratories/x-app",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.103.0",
|
|
4
4
|
"description": "Code the X-apps share: agents and their long runs, passwords and sign-in, the Fastify server pieces, durations, and the web page's helpers and widgets",
|
|
5
5
|
"license": "GPL-3.0-or-later",
|
|
6
6
|
"repository": {
|