@x-tech-laboratories/x-app 0.102.0 → 0.104.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 +5 -3
- package/dist/app/daemon.d.ts +7 -1
- package/dist/app/daemon.js +30 -4
- package/dist/app/define.d.ts +9 -1
- package/dist/app/desktop.d.ts +32 -2
- package/dist/app/desktop.js +81 -15
- package/dist/app/links.d.ts +2 -0
- package/dist/app/links.js +15 -0
- package/dist/app/protocol.d.ts +10 -1
- package/dist/app/protocol.js +5 -4
- package/dist/app/server.d.ts +6 -0
- package/dist/app/server.js +30 -2
- package/dist/app/services.d.ts +30 -0
- package/dist/app/services.js +37 -0
- package/dist/app/web/app_page.js +7 -1
- package/dist/app/web/hosts.js +29 -3
- package/dist/app/web/settings_page.d.ts +1 -1
- package/dist/app/web/settings_page.js +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,9 +10,11 @@ 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. |
|
|
14
|
-
| `app/
|
|
15
|
-
| `app/
|
|
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. |
|
|
15
|
+
| `app/links` | `app_link(scheme, route)` and `route_of_link(scheme, link)`: an app's links such as `x-notes://note/<id>` and the page route they open. |
|
|
16
|
+
| `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 (`default_size` fixed or from the screen's free area, the daemon told while one is in front for its services' `window_in_front`), the tray (`show_tray_icon`, the app's own `tray` rows above Show, Quit and Quit completely, closing the last window leaving the app in it, `--hidden` starting there), the app's `link_scheme` links opening the window on their page (`route_of_link` to map them its own way; the page follows the `open` event), the login entry, a second launch and `--daemon`; the app's own `desktop_settings` switches sit in Settings → Desktop app beside the base's; `run_preload(app, electron)` puts every bridge on the page. Electron is handed in, so neither imports it. |
|
|
17
|
+
| `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
18
|
| `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
19
|
| `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
20
|
| `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,14 @@ 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;
|
|
12
|
+
icon_path?: string | null;
|
|
9
13
|
}
|
|
10
14
|
export interface Daemon {
|
|
11
15
|
shut_down: () => Promise<void>;
|
|
12
16
|
}
|
|
13
17
|
export declare function start_daemon(app: App, options: DaemonOptions): Promise<Daemon | null>;
|
|
14
|
-
export declare function run_daemon(app: App, env?: NodeJS.ProcessEnv
|
|
18
|
+
export declare function run_daemon(app: App, env?: NodeJS.ProcessEnv, extra?: {
|
|
19
|
+
services?: StartServices;
|
|
20
|
+
}): 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 ───────────────────────────────────────────────────────────────────
|
|
@@ -45,13 +46,16 @@ export async function start_daemon(app, options) {
|
|
|
45
46
|
// ── Desktop settings ───────────────────────────────────────────────
|
|
46
47
|
const desktop_file = join(profile, "desktop.json");
|
|
47
48
|
function read_desktop() {
|
|
48
|
-
return read_desktop_settings(existsSync(desktop_file) ? JSON.parse(readFileSync(desktop_file, "utf8")) : null);
|
|
49
|
+
return read_desktop_settings(existsSync(desktop_file) ? JSON.parse(readFileSync(desktop_file, "utf8")) : null, app.desktop_settings);
|
|
49
50
|
}
|
|
51
|
+
// Told by the window as it comes and goes.
|
|
52
|
+
let window_in_front = false;
|
|
50
53
|
const daemon_methods = {
|
|
51
54
|
get_desktop_settings: read_desktop,
|
|
52
|
-
set_desktop_setting: (key, value) => writeFileSync(desktop_file, JSON.stringify(read_desktop_settings({ ...read_desktop(), [key]: value }), null, 2)),
|
|
55
|
+
set_desktop_setting: (key, value) => writeFileSync(desktop_file, JSON.stringify(read_desktop_settings({ ...read_desktop(), [key]: value }, app.desktop_settings), null, 2)),
|
|
53
56
|
// Stops once the answer has gone back.
|
|
54
57
|
stop_daemon: () => void setTimeout(() => void shut_down(), 0),
|
|
58
|
+
set_window_in_front: (in_front) => void (window_in_front = in_front === true),
|
|
55
59
|
};
|
|
56
60
|
// ── Calls ──────────────────────────────────────────────────────────
|
|
57
61
|
const daemon = fastify({ bodyLimit: call_body_limit, forceCloseConnections: true });
|
|
@@ -72,12 +76,16 @@ export async function start_daemon(app, options) {
|
|
|
72
76
|
// ── Lifecycle ──────────────────────────────────────────────────────
|
|
73
77
|
let stopping = null;
|
|
74
78
|
let unused_timer;
|
|
79
|
+
const sides = side_databases(database_path);
|
|
80
|
+
let services = no_services;
|
|
75
81
|
function shut_down() {
|
|
76
82
|
stopping ??= (async () => {
|
|
77
83
|
clearInterval(unused_timer);
|
|
84
|
+
await services.stop();
|
|
78
85
|
sync.stop();
|
|
79
86
|
await daemon.close();
|
|
80
87
|
await listening.stop();
|
|
88
|
+
sides.close_all();
|
|
81
89
|
opened.close();
|
|
82
90
|
options.on_stopped();
|
|
83
91
|
})();
|
|
@@ -96,6 +104,24 @@ export async function start_daemon(app, options) {
|
|
|
96
104
|
}
|
|
97
105
|
await listening.start();
|
|
98
106
|
sync.start();
|
|
107
|
+
if (options.services) {
|
|
108
|
+
services = await options.services({
|
|
109
|
+
kind: "desktop",
|
|
110
|
+
database: opened.database,
|
|
111
|
+
profile,
|
|
112
|
+
device_name: `${app.name} on ${hostname()}`,
|
|
113
|
+
config: {},
|
|
114
|
+
plugins_folder: join(profile, "plugins"),
|
|
115
|
+
declared_file: options.declared_file ?? `/etc/${app.id}/declared.json`,
|
|
116
|
+
announce: (client_id) => changes.announce_change(client_id),
|
|
117
|
+
emit: (event, data) => changes.send(JSON.stringify(data), { event }),
|
|
118
|
+
side_database: sides.open,
|
|
119
|
+
back_up: opened.back_up,
|
|
120
|
+
window_in_front: () => window_in_front,
|
|
121
|
+
icon_path: options.icon_path ?? null,
|
|
122
|
+
});
|
|
123
|
+
opened.on_restore({ before: () => services.before_restore?.(), after: () => services.after_restore?.() });
|
|
124
|
+
}
|
|
99
125
|
// Without "Keep running in the background", the daemon stops a while after its last window has gone.
|
|
100
126
|
let unused_since = Date.now();
|
|
101
127
|
unused_timer = setInterval(() => {
|
|
@@ -109,7 +135,7 @@ export async function start_daemon(app, options) {
|
|
|
109
135
|
// ── Running ──────────────────────────────────────────────────────────────────
|
|
110
136
|
// Starts the daemon on the database and socket the window names in <PREFIX>_DB and <PREFIX>_SOCKET, and ends the
|
|
111
137
|
// process once it stops.
|
|
112
|
-
export async function run_daemon(app, env = process.env) {
|
|
138
|
+
export async function run_daemon(app, env = process.env, extra = {}) {
|
|
113
139
|
const database_path = env[`${app.env_prefix}_DB`];
|
|
114
140
|
if (!database_path) {
|
|
115
141
|
console.error(`${app.id} daemon: ${app.env_prefix}_DB must name the database`);
|
|
@@ -118,7 +144,7 @@ export async function run_daemon(app, env = process.env) {
|
|
|
118
144
|
// A run on another database has a socket of its own.
|
|
119
145
|
const socket_path = env[`${app.env_prefix}_SOCKET`] || daemon_socket_path({ app: app.id, data_file: database_path });
|
|
120
146
|
try {
|
|
121
|
-
const daemon = await start_daemon(app, { database_path, socket_path, on_stopped: () => process.exit(0) });
|
|
147
|
+
const daemon = await start_daemon(app, { database_path, socket_path, on_stopped: () => process.exit(0), declared_file: declared_file_of(app, env), icon_path: env[`${app.env_prefix}_ICON`] ?? null, ...extra });
|
|
122
148
|
// Another daemon on this database got there first, which is all that was asked for.
|
|
123
149
|
if (!daemon)
|
|
124
150
|
process.exit(0);
|
package/dist/app/define.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { type SettingSpec, type SettingsMethods, type SettingsStore, type Settin
|
|
|
11
11
|
import { type MigrationStep, type SqlDatabase } from "../sql.ts";
|
|
12
12
|
import { type SyncedTable } from "../sync_engine.ts";
|
|
13
13
|
import { type PeerListeningMethods, type SyncDeviceMethods, type SyncStatus } from "../sync_protocol.ts";
|
|
14
|
-
import { type DesktopMethods } from "./protocol.ts";
|
|
14
|
+
import { type DesktopMethods, type DesktopSettingSpec } from "./protocol.ts";
|
|
15
15
|
export type StoreMethod = (database: SqlDatabase, ...args: never[]) => unknown;
|
|
16
16
|
export type KeptHistory = Pick<History<unknown>, "synced_table" | "install" | "forget_touched" | "record" | "prune">;
|
|
17
17
|
export interface Feature<Methods extends Record<string, StoreMethod> = Record<string, StoreMethod>> {
|
|
@@ -54,6 +54,8 @@ export interface AppOptions<Features extends readonly Feature[]> {
|
|
|
54
54
|
setting_key_example?: string;
|
|
55
55
|
page_only_refusal?: string;
|
|
56
56
|
token_prefix?: string;
|
|
57
|
+
desktop_settings?: Readonly<Record<string, DesktopSettingSpec>>;
|
|
58
|
+
link_scheme?: string;
|
|
57
59
|
}
|
|
58
60
|
export declare const base_setting_defaults: {
|
|
59
61
|
history_days: number;
|
|
@@ -66,6 +68,10 @@ export interface Restorable extends Backups {
|
|
|
66
68
|
export type AppEvents = {
|
|
67
69
|
changed: null;
|
|
68
70
|
sync_status: SyncStatus;
|
|
71
|
+
app_event: {
|
|
72
|
+
event: string;
|
|
73
|
+
data: unknown;
|
|
74
|
+
};
|
|
69
75
|
};
|
|
70
76
|
export interface AppBridges<Methods extends Record<keyof Methods, (...args: never[]) => unknown>> {
|
|
71
77
|
calls: Bridge<Methods>;
|
|
@@ -97,6 +103,8 @@ export declare function define_app<const Features extends readonly Feature[]>(op
|
|
|
97
103
|
setting_key_example?: string;
|
|
98
104
|
page_only_refusal?: string;
|
|
99
105
|
token_prefix?: string;
|
|
106
|
+
desktop_settings?: Readonly<Record<string, DesktopSettingSpec>>;
|
|
107
|
+
link_scheme?: string;
|
|
100
108
|
features: readonly Feature<Record<string, StoreMethod>>[];
|
|
101
109
|
settings_store: SettingsStore;
|
|
102
110
|
setting_defaults: Readonly<import("../settings.ts").Settings>;
|
package/dist/app/desktop.d.ts
CHANGED
|
@@ -2,13 +2,17 @@ import { type AutostartOptions } from "../autostart.ts";
|
|
|
2
2
|
import { type AppLike, type FocusableWindowLike } from "../electron/app.ts";
|
|
3
3
|
import type { IpcMainLike } from "../electron/bridge.ts";
|
|
4
4
|
import type { WindowLike } from "../electron/events.ts";
|
|
5
|
+
import { type TrayElectronLike, type TrayRow } from "../electron/tray.ts";
|
|
5
6
|
import { type BrowserWindowLike, type BrowserWindowOptionsLike, type NativeThemeLike, type ScreenLike, type ShellLike } from "../electron/window.ts";
|
|
6
7
|
import { call_daemon, start_daemon } from "../local_daemon.ts";
|
|
7
8
|
import type { App } from "./define.ts";
|
|
8
9
|
type DesktopWindow = BrowserWindowLike & FocusableWindowLike & WindowLike & {
|
|
9
10
|
webContents: {
|
|
10
11
|
reload(): void;
|
|
12
|
+
once(event: "did-finish-load", listener: () => void): unknown;
|
|
11
13
|
};
|
|
14
|
+
isFocused(): boolean;
|
|
15
|
+
isVisible(): boolean;
|
|
12
16
|
};
|
|
13
17
|
export interface ElectronLike {
|
|
14
18
|
app: AppLike & {
|
|
@@ -28,15 +32,39 @@ export interface ElectronLike {
|
|
|
28
32
|
};
|
|
29
33
|
ipcMain: IpcMainLike;
|
|
30
34
|
nativeTheme: NativeThemeLike;
|
|
31
|
-
screen: ScreenLike
|
|
35
|
+
screen: ScreenLike & {
|
|
36
|
+
getPrimaryDisplay(): {
|
|
37
|
+
workAreaSize: {
|
|
38
|
+
width: number;
|
|
39
|
+
height: number;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
};
|
|
32
43
|
shell: ShellLike;
|
|
44
|
+
Tray?: TrayElectronLike<unknown>["Tray"];
|
|
45
|
+
Menu?: TrayElectronLike<unknown>["Menu"];
|
|
46
|
+
nativeImage?: TrayElectronLike<unknown>["nativeImage"];
|
|
47
|
+
}
|
|
48
|
+
export interface TrayTools {
|
|
49
|
+
call: (method: string, args?: unknown[]) => Promise<unknown>;
|
|
50
|
+
open: (route: string) => void;
|
|
33
51
|
}
|
|
52
|
+
export type AppTrayRows = (tools: TrayTools) => Promise<{
|
|
53
|
+
tooltip?: string;
|
|
54
|
+
rows: TrayRow[];
|
|
55
|
+
}>;
|
|
34
56
|
export interface DesktopOptions {
|
|
35
57
|
main_dir: string;
|
|
36
58
|
default_size?: {
|
|
37
59
|
width: number;
|
|
38
60
|
height: number;
|
|
39
|
-
}
|
|
61
|
+
} | ((work_area: {
|
|
62
|
+
width: number;
|
|
63
|
+
height: number;
|
|
64
|
+
}) => {
|
|
65
|
+
width: number;
|
|
66
|
+
height: number;
|
|
67
|
+
});
|
|
40
68
|
min_size?: {
|
|
41
69
|
width: number;
|
|
42
70
|
height: number;
|
|
@@ -47,6 +75,8 @@ export interface DesktopOptions {
|
|
|
47
75
|
start_daemon?: typeof start_daemon;
|
|
48
76
|
call_daemon?: typeof call_daemon;
|
|
49
77
|
set_autostart?: (options: AutostartOptions, on: boolean) => void;
|
|
78
|
+
tray?: AppTrayRows;
|
|
79
|
+
route_of_link?: (link: string) => string | null;
|
|
50
80
|
}
|
|
51
81
|
export declare function run_desktop(app: App, electron: ElectronLike, options: DesktopOptions): void;
|
|
52
82
|
export {};
|
package/dist/app/desktop.js
CHANGED
|
@@ -13,10 +13,12 @@ import { connection_file_storage } from "../connection_file_storage.js";
|
|
|
13
13
|
import { create_connections } from "../connections.js";
|
|
14
14
|
import { create_daemon_link } from "../daemon_link.js";
|
|
15
15
|
import { create_daemon_router, default_local_tables } from "../daemon_router.js";
|
|
16
|
-
import { bring_to_front, hold_single_instance } from "../electron/app.js";
|
|
16
|
+
import { bring_to_front, first_argument, hold_single_instance } from "../electron/app.js";
|
|
17
|
+
import { app_rows, create_app_tray, tray_groups } from "../electron/tray.js";
|
|
17
18
|
import { create_window, is_app_page } from "../electron/window.js";
|
|
18
19
|
import { call_daemon, daemon_log_path, daemon_socket_path, start_daemon } from "../local_daemon.js";
|
|
19
20
|
import { peer_listening_method_names, sync_device_method_names } from "../sync_protocol.js";
|
|
21
|
+
import { route_of_link } from "./links.js";
|
|
20
22
|
// ── Desktop ──────────────────────────────────────────────────────────────────
|
|
21
23
|
export function run_desktop(app, electron, options) {
|
|
22
24
|
const { BrowserWindow, dialog, ipcMain } = electron;
|
|
@@ -41,7 +43,9 @@ export function run_desktop(app, electron, options) {
|
|
|
41
43
|
// The command that starts this app again, which the login entry runs.
|
|
42
44
|
const own_command = (args) => launch_command({ launcher: env[`${prefix}_LAUNCHER`] ?? null, is_packaged: electron.app.isPackaged, app_path: electron.app.getAppPath(), args });
|
|
43
45
|
function ensure_daemon() {
|
|
44
|
-
|
|
46
|
+
// Only the user's own daemon reads what this computer's configuration declares, unless a run names a file itself.
|
|
47
|
+
const declared = is_own_profile ? {} : { [`${prefix}_DECLARED_FILE`]: env[`${prefix}_DECLARED_FILE`] ?? "" };
|
|
48
|
+
return start({ socket_path, script: join(options.main_dir, "daemon.js"), env: { [`${prefix}_DB`]: database_path, [`${prefix}_SOCKET`]: socket_path, [`${prefix}_ICON`]: icon_path, ...declared }, log_file });
|
|
45
49
|
}
|
|
46
50
|
// ── Connections ──────────────────────────────────────────────────────
|
|
47
51
|
// Certificates the user trusted, read again whenever the daemon saves the list, so calls to a server honour them.
|
|
@@ -64,8 +68,11 @@ export function run_desktop(app, electron, options) {
|
|
|
64
68
|
on_change: () => {
|
|
65
69
|
app.bridges.events.emit(windows(), "changed", null);
|
|
66
70
|
void apply_window_settings().catch(() => undefined);
|
|
71
|
+
void tray?.refresh();
|
|
67
72
|
},
|
|
68
73
|
on_sync_status: (status) => app.bridges.events.emit(windows(), "sync_status", status),
|
|
74
|
+
// The app's own events, such as a reply as it is written, go to every window by name.
|
|
75
|
+
on_event: (event, data) => app.bridges.events.emit(windows(), "app_event", { event, data: data === "" ? null : JSON.parse(data) }),
|
|
69
76
|
on_switch: () => {
|
|
70
77
|
for (const window of windows())
|
|
71
78
|
window.webContents.reload();
|
|
@@ -76,11 +83,12 @@ export function run_desktop(app, electron, options) {
|
|
|
76
83
|
function get_desktop_settings() {
|
|
77
84
|
return call("daemon/get_desktop_settings");
|
|
78
85
|
}
|
|
79
|
-
//
|
|
86
|
+
// The tray as its setting says, and, for the user's own profile only, the login entry; a dev run never writes it.
|
|
80
87
|
async function apply_window_settings() {
|
|
88
|
+
const settings = await get_desktop_settings();
|
|
89
|
+
tray?.set_shown(settings.show_tray_icon);
|
|
81
90
|
if (!is_own_profile)
|
|
82
91
|
return;
|
|
83
|
-
const settings = await get_desktop_settings();
|
|
84
92
|
write_autostart({ id: `${app.id}-daemon`, name: `${app.name} in the background`, comment: `Sync and backups for ${app.name}`, icon: icon_path, command: own_command(["--daemon"]) }, settings.start_at_login && settings.keep_running_in_background);
|
|
85
93
|
}
|
|
86
94
|
// ── Window ───────────────────────────────────────────────────────────
|
|
@@ -88,30 +96,71 @@ export function run_desktop(app, electron, options) {
|
|
|
88
96
|
dev_url: electron.app.isPackaged ? undefined : env.ELECTRON_RENDERER_URL,
|
|
89
97
|
file: join(options.main_dir, "../renderer/index.html"),
|
|
90
98
|
};
|
|
99
|
+
// The daemon's services keep quiet, such as about notifications, while a window is in front.
|
|
100
|
+
function tell_in_front() {
|
|
101
|
+
const in_front = windows().some((each) => each.isFocused() && each.isVisible());
|
|
102
|
+
void call("daemon/set_window_in_front", [in_front]).catch(() => undefined);
|
|
103
|
+
}
|
|
91
104
|
function open_window() {
|
|
92
|
-
|
|
105
|
+
const size = options.default_size ?? { width: 1100, height: 760 };
|
|
106
|
+
const window = create_window(BrowserWindow, {
|
|
93
107
|
...page,
|
|
94
108
|
title: app.name,
|
|
95
109
|
icon: icon_path,
|
|
96
110
|
// electron-vite emits an ESM preload as .mjs; loading .js here silently yields no bridge.
|
|
97
111
|
preload: join(options.main_dir, "../preload/index.mjs"),
|
|
98
|
-
default_size:
|
|
112
|
+
default_size: typeof size === "function" ? size(electron.screen.getPrimaryDisplay().workAreaSize) : size,
|
|
99
113
|
min_size: options.min_size ?? { width: 360, height: 480 },
|
|
100
114
|
native_theme: electron.nativeTheme,
|
|
101
115
|
shell: electron.shell,
|
|
102
116
|
screen: electron.screen,
|
|
103
117
|
state_file: join(profile, "window.json"),
|
|
104
118
|
});
|
|
119
|
+
for (const event of ["focus", "blur", "show", "hide", "closed"])
|
|
120
|
+
window.on(event, tell_in_front);
|
|
121
|
+
return window;
|
|
105
122
|
}
|
|
106
|
-
|
|
107
|
-
|
|
123
|
+
// Brings the window forward, making one if it was closed, and hands it the route once its page listens.
|
|
124
|
+
function show_window(route) {
|
|
125
|
+
const existing = windows()[0];
|
|
126
|
+
const window = existing ?? open_window();
|
|
127
|
+
bring_to_front(window);
|
|
128
|
+
if (!route)
|
|
129
|
+
return;
|
|
130
|
+
const open = () => app.bridges.events.emit([window], "app_event", { event: "open", data: route });
|
|
131
|
+
if (existing)
|
|
132
|
+
open();
|
|
133
|
+
else
|
|
134
|
+
window.webContents.once("did-finish-load", open);
|
|
108
135
|
}
|
|
136
|
+
// The page an argument such as x-notes://note/<id> opens, if the app has links.
|
|
137
|
+
const link_route = options.route_of_link ?? (app.link_scheme ? (link) => route_of_link(app.link_scheme, link) : () => null);
|
|
138
|
+
const route_in = (args) => first_argument(args, link_route);
|
|
109
139
|
// Stops the daemon as well, so nothing runs until the app is opened again.
|
|
110
140
|
async function quit_completely() {
|
|
111
141
|
router.quitting();
|
|
112
142
|
await call("daemon/stop_daemon").catch(() => undefined);
|
|
113
143
|
electron.app.quit();
|
|
114
144
|
}
|
|
145
|
+
// ── Tray ───────────────────────────────────────────────────────────
|
|
146
|
+
// The app's rows above Show, Quit and Quit completely; its calls go to the daemon the window works on.
|
|
147
|
+
const { Tray, Menu, nativeImage } = electron;
|
|
148
|
+
const tray = Tray && Menu && nativeImage
|
|
149
|
+
? create_app_tray({
|
|
150
|
+
electron: { Tray, Menu, nativeImage },
|
|
151
|
+
icon_path,
|
|
152
|
+
on_click: () => show_window(),
|
|
153
|
+
on_error: (error) => console.error(error),
|
|
154
|
+
read: async () => {
|
|
155
|
+
const own = options.tray ? await options.tray({ call: (method, args = []) => call(`call/${method}`, args), open: (route) => show_window(route) }) : { rows: [] };
|
|
156
|
+
const quit = () => {
|
|
157
|
+
router.quitting();
|
|
158
|
+
electron.app.quit();
|
|
159
|
+
};
|
|
160
|
+
return { tooltip: own.tooltip ?? app.name, rows: tray_groups(own.rows, app_rows({ app_name: app.name, show: () => show_window(), quit, quit_completely: () => void quit_completely() })) };
|
|
161
|
+
},
|
|
162
|
+
})
|
|
163
|
+
: null;
|
|
115
164
|
// ── Bridges ──────────────────────────────────────────────────────────
|
|
116
165
|
// Only the app's own page may call in, never a site a link led to.
|
|
117
166
|
function trusted(event) {
|
|
@@ -121,7 +170,17 @@ export function run_desktop(app, electron, options) {
|
|
|
121
170
|
function register_bridges() {
|
|
122
171
|
const handle_options = { trusted, envelopes: true };
|
|
123
172
|
const { bridges } = app;
|
|
124
|
-
|
|
173
|
+
// A write the page makes shows in the tray at once, as the daemon tells this window of no change it made itself.
|
|
174
|
+
const calls = Object.fromEntries(Object.entries(forwarded(app.method_names, "call")).map(([name, forward]) => [
|
|
175
|
+
name,
|
|
176
|
+
async (...args) => {
|
|
177
|
+
const result = await forward(...args);
|
|
178
|
+
if (!app.is_read_only(name))
|
|
179
|
+
void tray?.refresh();
|
|
180
|
+
return result;
|
|
181
|
+
},
|
|
182
|
+
]));
|
|
183
|
+
bridges.calls.handle(ipcMain, calls, handle_options);
|
|
125
184
|
bridges.backups.handle(ipcMain, forwarded(backup_method_names, "backups"), handle_options);
|
|
126
185
|
bridges.device.handle(ipcMain, forwarded(sync_device_method_names, "device"), handle_options);
|
|
127
186
|
bridges.peer_listening.handle(ipcMain, forwarded(peer_listening_method_names, "peer_listening"), handle_options);
|
|
@@ -151,14 +210,20 @@ export function run_desktop(app, electron, options) {
|
|
|
151
210
|
register_bridges();
|
|
152
211
|
router.start();
|
|
153
212
|
await apply_window_settings().catch(() => undefined);
|
|
154
|
-
|
|
213
|
+
// `--hidden`, as a login entry may pass it, starts in the tray, unless there is no tray to open the window from.
|
|
214
|
+
if (!(argv.includes("--hidden") && tray?.is_shown()))
|
|
215
|
+
show_window(route_in(argv));
|
|
155
216
|
});
|
|
156
217
|
electron.app.on("will-quit", () => {
|
|
157
218
|
router.stop();
|
|
219
|
+
tray?.stop();
|
|
158
220
|
unwatchFile(certificates_file, trust_certificates);
|
|
159
221
|
});
|
|
160
|
-
// Closing the window ends only the window; the daemon runs on.
|
|
161
|
-
electron.app.on("window-all-closed", () =>
|
|
222
|
+
// Closing the window ends only the window, or leaves the app in the tray; the daemon runs on either way.
|
|
223
|
+
electron.app.on("window-all-closed", () => {
|
|
224
|
+
if (!tray?.is_shown())
|
|
225
|
+
electron.app.quit();
|
|
226
|
+
});
|
|
162
227
|
}
|
|
163
228
|
// `<app>-desktop --daemon`, as the login entry runs it, starts only the daemon.
|
|
164
229
|
async function start_daemon_alone() {
|
|
@@ -171,10 +236,11 @@ export function run_desktop(app, electron, options) {
|
|
|
171
236
|
electron.app.exit(1);
|
|
172
237
|
}
|
|
173
238
|
}
|
|
174
|
-
// A second launch brings the window forward, but one that only starts the daemon
|
|
239
|
+
// A second launch brings the window forward on the page its link names, but one that only starts the daemon or
|
|
240
|
+
// starts hidden leaves the window be.
|
|
175
241
|
function open_from_second_launch(second_argv) {
|
|
176
|
-
if (!second_argv.includes("--daemon"))
|
|
177
|
-
show_window();
|
|
242
|
+
if (!second_argv.includes("--daemon") && !second_argv.includes("--hidden"))
|
|
243
|
+
show_window(route_in(second_argv));
|
|
178
244
|
}
|
|
179
245
|
if (argv.includes("--daemon"))
|
|
180
246
|
void start_daemon_alone();
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Links such as x-notes://note/<id>, which open the desktop app on that page from a notification or any other program,
|
|
2
|
+
// in the scheme the app names.
|
|
3
|
+
// ── Links ────────────────────────────────────────────────────────────────────
|
|
4
|
+
// The link to a page, given its route such as "#/note/<id>".
|
|
5
|
+
export function app_link(scheme, route) {
|
|
6
|
+
return `${scheme}://${route.replace(/^#?\/?/, "")}`;
|
|
7
|
+
}
|
|
8
|
+
// The page's route a link names, such as "#/note/<id>", or null when the text is not one of the app's links.
|
|
9
|
+
export function route_of_link(scheme, text) {
|
|
10
|
+
const prefix = `${scheme.toLowerCase()}://`;
|
|
11
|
+
if (!text.toLowerCase().startsWith(prefix))
|
|
12
|
+
return null;
|
|
13
|
+
const path = text.slice(prefix.length).replace(/\/+$/, "");
|
|
14
|
+
return /^[a-z0-9/_-]*$/i.test(path) ? `#/${path}` : null;
|
|
15
|
+
}
|
package/dist/app/protocol.d.ts
CHANGED
|
@@ -4,11 +4,18 @@ import type { ConnectionClient } from "../connection_protocol.ts";
|
|
|
4
4
|
import type { Client } from "../electron/bridge.ts";
|
|
5
5
|
import type { PeerListeningClient, SyncDeviceClient } from "../sync_protocol.ts";
|
|
6
6
|
export interface DesktopSettings {
|
|
7
|
+
show_tray_icon: boolean;
|
|
7
8
|
keep_running_in_background: boolean;
|
|
8
9
|
start_at_login: boolean;
|
|
10
|
+
[key: string]: boolean;
|
|
9
11
|
}
|
|
10
12
|
export declare const desktop_setting_defaults: DesktopSettings;
|
|
11
|
-
export
|
|
13
|
+
export interface DesktopSettingSpec {
|
|
14
|
+
default: boolean;
|
|
15
|
+
label: string;
|
|
16
|
+
hint?: string;
|
|
17
|
+
}
|
|
18
|
+
export declare function read_desktop_settings(saved: unknown, specs?: Readonly<Record<string, DesktopSettingSpec>>): DesktopSettings;
|
|
12
19
|
export interface DesktopMethods {
|
|
13
20
|
get_desktop_settings: () => DesktopSettings;
|
|
14
21
|
set_desktop_setting: <Key extends keyof DesktopSettings>(key: Key, value: DesktopSettings[Key]) => void;
|
|
@@ -19,6 +26,7 @@ export interface DaemonMethods {
|
|
|
19
26
|
get_desktop_settings: () => DesktopSettings;
|
|
20
27
|
set_desktop_setting: (key: keyof DesktopSettings, value: boolean) => void;
|
|
21
28
|
stop_daemon: () => void;
|
|
29
|
+
set_window_in_front: (in_front: boolean) => void;
|
|
22
30
|
}
|
|
23
31
|
export type Host = "web" | "desktop" | "android";
|
|
24
32
|
export interface BackupFolder {
|
|
@@ -36,4 +44,5 @@ export interface PageApi<Methods extends Record<keyof Methods, (...args: never[]
|
|
|
36
44
|
peer_listening?: PeerListeningClient;
|
|
37
45
|
desktop?: Client<DesktopMethods>;
|
|
38
46
|
on_change: (listener: () => void) => () => void;
|
|
47
|
+
on_event?: (event: string, listener: (data: unknown) => void) => () => void;
|
|
39
48
|
}
|
package/dist/app/protocol.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
// What the base app's hosts and processes say to each other: the page's api on every host, and the desktop app's own
|
|
2
2
|
// settings, which its daemon keeps beside the database rather than syncing, since they belong to the computer.
|
|
3
3
|
export const desktop_setting_defaults = {
|
|
4
|
+
show_tray_icon: true,
|
|
4
5
|
keep_running_in_background: true,
|
|
5
6
|
start_at_login: true,
|
|
6
7
|
};
|
|
7
|
-
// Settings saved on the device, each default where it is missing or damaged.
|
|
8
|
-
export function read_desktop_settings(saved) {
|
|
8
|
+
// Settings saved on the device, the base's and the app's own, each default where it is missing or damaged.
|
|
9
|
+
export function read_desktop_settings(saved, specs = {}) {
|
|
9
10
|
const settings = (saved ?? {});
|
|
10
|
-
const
|
|
11
|
-
return
|
|
11
|
+
const defaults = { ...desktop_setting_defaults, ...Object.fromEntries(Object.entries(specs).map(([key, spec]) => [key, spec.default])) };
|
|
12
|
+
return Object.fromEntries(Object.entries(defaults).map(([key, fallback]) => [key, typeof settings[key] === "boolean" ? settings[key] : fallback]));
|
|
12
13
|
}
|
|
13
14
|
export const desktop_method_names = ["get_desktop_settings", "set_desktop_setting", "quit_completely"];
|
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,31 @@ 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
|
+
window_in_front: () => false,
|
|
228
|
+
icon_path: null,
|
|
229
|
+
})
|
|
230
|
+
: no_services;
|
|
231
|
+
opened.on_restore({ before: () => services.before_restore?.(), after: () => services.after_restore?.() });
|
|
206
232
|
console.info(`${app.name} is serving ${address}/`);
|
|
207
233
|
let stopping = null;
|
|
208
|
-
const stop = () => (stopping ??= server.close().then(() => {
|
|
234
|
+
const stop = () => (stopping ??= server.close().then(async () => {
|
|
235
|
+
await services.stop();
|
|
209
236
|
sync.stop();
|
|
237
|
+
sides.close_all();
|
|
210
238
|
opened.close();
|
|
211
239
|
accounts_database.close();
|
|
212
240
|
}));
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
window_in_front: () => boolean;
|
|
16
|
+
icon_path: string | null;
|
|
17
|
+
}
|
|
18
|
+
export interface HostServices {
|
|
19
|
+
before_restore?: () => void;
|
|
20
|
+
after_restore?: () => void;
|
|
21
|
+
stop: () => void | Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
export type StartServices = (context: HostContext) => HostServices | Promise<HostServices>;
|
|
24
|
+
export declare function declared_file_of(app: Pick<App, "id" | "env_prefix">, env: NodeJS.ProcessEnv): string;
|
|
25
|
+
export interface SideDatabases {
|
|
26
|
+
open: (name: string) => DatabaseSync;
|
|
27
|
+
close_all: () => void;
|
|
28
|
+
}
|
|
29
|
+
export declare function side_databases(database_path: string): SideDatabases;
|
|
30
|
+
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/app_page.js
CHANGED
|
@@ -76,6 +76,12 @@ async function host_api(app) {
|
|
|
76
76
|
}
|
|
77
77
|
// Starts the page on whichever host it runs: the desktop app, the phone app or a browser.
|
|
78
78
|
export async function render_app(pages, root = document.getElementById("app")) {
|
|
79
|
-
|
|
79
|
+
const api = await host_api(pages.app);
|
|
80
|
+
set_page_api(api);
|
|
81
|
+
// A link the desktop app was opened with, or a tray row, opens its page.
|
|
82
|
+
api.on_event?.("open", (route) => {
|
|
83
|
+
if (typeof route === "string" && route.startsWith("#/"))
|
|
84
|
+
location.hash = route;
|
|
85
|
+
});
|
|
80
86
|
createRoot(root).render(_jsx(AppView, { pages: pages }));
|
|
81
87
|
}
|
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 ────────────────────────────────────────────────────────────────────
|
|
@@ -11,7 +11,7 @@ export interface AppPage {
|
|
|
11
11
|
settings?: (props: PageSettingsProps) => SettingsSection[];
|
|
12
12
|
}
|
|
13
13
|
export interface AppPages {
|
|
14
|
-
app: Pick<App, "id" | "name" | "data_name" | "server_example" | "bridges"
|
|
14
|
+
app: Pick<App, "id" | "name" | "data_name" | "server_example" | "bridges"> & Partial<Pick<App, "desktop_settings">>;
|
|
15
15
|
pages: readonly AppPage[];
|
|
16
16
|
general_settings?: (props: PageSettingsProps) => SettingsSection[];
|
|
17
17
|
brand_icon?: string;
|
|
@@ -22,7 +22,7 @@ function FolderBackups({ backups, folder, data_name }) {
|
|
|
22
22
|
return _jsx(BackupSettings, { device: backups, data_name: data_name, on_restored: () => location.reload(), folder_row: folder_row, can_back_up: Boolean(name) }, name ?? "");
|
|
23
23
|
}
|
|
24
24
|
// The desktop app's own settings, which belong to this computer and do not sync.
|
|
25
|
-
function Desktop({ app_name }) {
|
|
25
|
+
function Desktop({ app_name, own }) {
|
|
26
26
|
const { desktop } = page();
|
|
27
27
|
const [settings, set_settings] = useState(null);
|
|
28
28
|
useEffect(() => void desktop?.get_desktop_settings().then(set_settings, () => undefined), [desktop]);
|
|
@@ -32,7 +32,7 @@ function Desktop({ app_name }) {
|
|
|
32
32
|
set_settings({ ...settings, [key]: on });
|
|
33
33
|
attempt(desktop.set_desktop_setting(key, on));
|
|
34
34
|
};
|
|
35
|
-
return (_jsxs(_Fragment, { children: [_jsx(SettingRow, { label: "Keep running in the background", hint: "Sync and backups carry on with the window closed. Turned off, they stop shortly after the last window closes.", as_label: true, children: _jsx(Switch, { label: "Keep running in the background", checked: settings.keep_running_in_background, on_change: (on) => change("keep_running_in_background", on) }) }), _jsx(SettingRow, { label: "Start at login", hint: `Starts ${app_name} in the background when you log in, without opening its window.`, as_label: true, children: _jsx(Switch, { label: "Start at login", checked: settings.start_at_login, disabled: !settings.keep_running_in_background, on_change: (on) => change("start_at_login", on) }) }), _jsx(SettingRow, { label: "Quit completely", hint: `Closes the window and stops ${app_name} in the background too, until you open it again.`, children: _jsx("button", { type: "button", onClick: () => attempt(desktop.quit_completely()), children: "Quit completely" }) })] }));
|
|
35
|
+
return (_jsxs(_Fragment, { children: [_jsx(SettingRow, { label: "Show the tray icon", hint: `Keeps ${app_name} in the tray, where closing the window leaves it.`, as_label: true, children: _jsx(Switch, { label: "Show the tray icon", checked: settings.show_tray_icon, on_change: (on) => change("show_tray_icon", on) }) }), _jsx(SettingRow, { label: "Keep running in the background", hint: "Sync and backups carry on with the window closed. Turned off, they stop shortly after the last window closes.", as_label: true, children: _jsx(Switch, { label: "Keep running in the background", checked: settings.keep_running_in_background, on_change: (on) => change("keep_running_in_background", on) }) }), _jsx(SettingRow, { label: "Start at login", hint: `Starts ${app_name} in the background when you log in, without opening its window.`, as_label: true, children: _jsx(Switch, { label: "Start at login", checked: settings.start_at_login, disabled: !settings.keep_running_in_background, on_change: (on) => change("start_at_login", on) }) }), Object.entries(own).map(([key, spec]) => (_jsx(SettingRow, { label: spec.label, ...(spec.hint ? { hint: spec.hint } : {}), as_label: true, children: _jsx(Switch, { label: spec.label, checked: settings[key] ?? spec.default, on_change: (on) => change(key, on) }) }, key))), _jsx(SettingRow, { label: "Quit completely", hint: `Closes the window and stops ${app_name} in the background too, until you open it again.`, children: _jsx("button", { type: "button", onClick: () => attempt(desktop.quit_completely()), children: "Quit completely" }) })] }));
|
|
36
36
|
}
|
|
37
37
|
// A desktop window on a server's daemon works on the server's data, as a browser does.
|
|
38
38
|
function storage_category(app, is_admin) {
|
|
@@ -44,7 +44,7 @@ function storage_category(app, is_admin) {
|
|
|
44
44
|
sections.push({ id: "connections", heading: "Connections", hint: `The ${data_name} this window works on: This computer's own, or an ${name} server's.`, content: _jsxs(_Fragment, { children: [_jsx(ConnectionSettings, { client: connections, certificates: certificates, placeholder: app.server_example }), !device && certificates && _jsx(TrustedCertificates, { certificates: certificates })] }) });
|
|
45
45
|
}
|
|
46
46
|
if (desktop)
|
|
47
|
-
sections.push({ id: "desktop", heading: "Desktop app", content: _jsx(Desktop, { app_name: name }) });
|
|
47
|
+
sections.push({ id: "desktop", heading: "Desktop app", content: _jsx(Desktop, { app_name: name, own: app.desktop_settings ?? {} }) });
|
|
48
48
|
if (device && (!on_server || is_admin)) {
|
|
49
49
|
const hint = on_server ? `Keep the server's ${data_name} alike with the devices paired with it.` : `Keep this device's ${data_name} alike with your other devices and your ${name} server.`;
|
|
50
50
|
sections.push({ id: "sync", heading: "Sync", hint, content: _jsx(SyncSettings, { device: device, subscribe: (listener) => data_watch.subscribe(listener), data_name: data_name, extra: peer_listening && _jsx(PeerListeningSettings, { listening: peer_listening }) }) });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-tech-laboratories/x-app",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.104.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": {
|