Skip to content

Commit 1ef799c

Browse files
committed
Updates
1 parent 1d56f4b commit 1ef799c

72 files changed

Lines changed: 2121 additions & 917 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Directory.Packages.props‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<Project>
2-
32
<PropertyGroup>
43
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
54
<MauiVersion>10.0.101</MauiVersion>
65
<MauiLabsVersion>0.1.0-preview.12.26421.1</MauiLabsVersion>
76
</PropertyGroup>
8-
97
<ItemGroup>
8+
<PackageVersion Include="GirCore.Gtk-4.0" Version="0.8.1"/>
109
<PackageVersion Include="Nerdbank.GitVersioning" Version="3.10.91"/>
1110
<PackageVersion Include="Shiny.Net.HttpServer" Version="1.2.0"/>
1211
<PackageVersion Include="Shiny.Net.HttpServer.Ssh" Version="1.2.0"/>
@@ -15,25 +14,25 @@
1514
<PackageVersion Include="Shiny.Extensions.Stores" Version="5.2.1"/>
1615
<PackageVersion Include="Shiny.Maui.Controls.Desktop" Version="1.4.0"/>
1716
<PackageVersion Include="Shiny.Maui.Controls.Camera" Version="1.4.0"/>
18-
<PackageVersion Include="Shiny.Hosting.Maui" Version="5.6.4"/>
19-
<PackageVersion Include="Shiny.Jobs" Version="5.6.4"/>
20-
<PackageVersion Include="Shiny.Push" Version="5.6.4"/>
21-
<PackageVersion Include="Shiny.Notifications" Version="5.6.4"/>
22-
<PackageVersion Include="Shiny.Notifications.Linux" Version="5.6.4"/>
23-
<PackageVersion Include="Shiny.Net.Wifi" Version="5.6.4"/>
24-
<PackageVersion Include="Shiny.Net.Wifi.Linux" Version="5.6.4"/>
25-
<PackageVersion Include="Shiny.Net.Discovery" Version="5.6.4"/>
17+
<PackageVersion Include="Shiny.Hosting.Maui" Version="5.7.0"/>
18+
<PackageVersion Include="Shiny.Jobs" Version="5.7.0"/>
19+
<PackageVersion Include="Shiny.Push" Version="5.7.0"/>
20+
<PackageVersion Include="Shiny.Notifications" Version="5.7.0"/>
21+
<PackageVersion Include="Shiny.Notifications.Linux" Version="5.7.0"/>
22+
<PackageVersion Include="Shiny.Net.Wifi" Version="5.7.0"/>
23+
<PackageVersion Include="Shiny.Net.Wifi.Linux" Version="5.7.0"/>
24+
<PackageVersion Include="Shiny.Net.Discovery" Version="5.7.0"/>
2625
<PackageVersion Include="Shiny.Obd" Version="1.2.0"/>
2726
<PackageVersion Include="Shiny.Obd.Ble" Version="1.2.0"/>
2827
<PackageVersion Include="Shiny.Obd.Wifi" Version="1.2.0"/>
29-
<PackageVersion Include="Shiny.Locations" Version="5.6.4"/>
30-
<PackageVersion Include="Shiny.BluetoothLE" Version="5.6.4"/>
28+
<PackageVersion Include="Shiny.Locations" Version="5.7.0"/>
29+
<PackageVersion Include="Shiny.BluetoothLE" Version="5.7.0"/>
3130
<PackageVersion Include="Shiny.Health" Version="2.0.1"/>
32-
<PackageVersion Include="Shiny.Contacts" Version="5.6.4"/>
33-
<PackageVersion Include="Shiny.Calendar" Version="5.6.4"/>
31+
<PackageVersion Include="Shiny.Contacts" Version="5.7.0"/>
32+
<PackageVersion Include="Shiny.Calendar" Version="5.7.0"/>
3433
<PackageVersion Include="Shiny.Speech" Version="3.0.0-beta-0030"/>
35-
<PackageVersion Include="Shiny.Net.Http" Version="5.6.4"/>
36-
<PackageVersion Include="Shiny.Core.Linux" Version="5.6.4"/>
34+
<PackageVersion Include="Shiny.Net.Http" Version="5.7.0"/>
35+
<PackageVersion Include="Shiny.Core.Linux" Version="5.7.0"/>
3736
<PackageVersion Include="Xamarin.AndroidX.DocumentFile" Version="1.1.0.3"/>
3837
<PackageVersion Include="GirCore.Gtk-4.0" Version="0.7.0"/>
3938
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="10.0.12"/>
@@ -51,11 +50,9 @@
5150
<PackageVersion Include="Microsoft.Maui.Platforms.Linux.Gtk4" Version="$(MauiLabsVersion)"/>
5251
<PackageVersion Include="Microsoft.Maui.Platforms.Linux.Gtk4.Essentials" Version="$(MauiLabsVersion)"/>
5352
</ItemGroup>
54-
5553
<ItemGroup>
5654
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="10.0.12"/>
5755
<PackageVersion Include="xunit.v3" Version="3.2.2"/>
5856
<PackageVersion Include="Microsoft.Testing.Extensions.TrxReport" Version="1.9.1"/>
5957
</ItemGroup>
60-
61-
</Project>
58+
</Project>

‎clients/typescript/src/app-support.ts‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -347,37 +347,37 @@ export class AppBridge {
347347
}
348348

349349
/** The orientation changed. */
350-
onOrientationChanged(handler: (payload: OrientationChanged) => void): () => void {
350+
onOrientationChanged(handler: (payload: OrientationChanged) => void): Promise<() => void> {
351351
return this.transport.subscribe("app.orientation", json => handler(JSON.parse(json) as OrientationChanged));
352352
}
353353

354354
/** The device's culture changed. */
355-
onCultureChanged(handler: (payload: CultureChanged) => void): () => void {
355+
onCultureChanged(handler: (payload: CultureChanged) => void): Promise<() => void> {
356356
return this.transport.subscribe("app.culture", json => handler(JSON.parse(json) as CultureChanged));
357357
}
358358

359359
/** The device's time zone changed. */
360-
onTimeZoneChanged(handler: (payload: TimeZoneChanged) => void): () => void {
360+
onTimeZoneChanged(handler: (payload: TimeZoneChanged) => void): Promise<() => void> {
361361
return this.transport.subscribe("app.timezone", json => handler(JSON.parse(json) as TimeZoneChanged));
362362
}
363363

364364
/** Network access or the connections in use changed. */
365-
onConnectivityChanged(handler: (payload: ConnectivityInfo) => void): () => void {
365+
onConnectivityChanged(handler: (payload: ConnectivityInfo) => void): Promise<() => void> {
366366
return this.transport.subscribe("app.connectivity", json => handler(JSON.parse(json) as ConnectivityInfo));
367367
}
368368

369369
/** The charge, charging state or power source changed. */
370-
onBatteryChanged(handler: (payload: BatteryChanged) => void): () => void {
370+
onBatteryChanged(handler: (payload: BatteryChanged) => void): Promise<() => void> {
371371
return this.transport.subscribe("app.battery", json => handler(JSON.parse(json) as BatteryChanged));
372372
}
373373

374374
/** Energy saver was turned on or off. */
375-
onEnergySaverChanged(handler: (payload: EnergySaverChanged) => void): () => void {
375+
onEnergySaverChanged(handler: (payload: EnergySaverChanged) => void): Promise<() => void> {
376376
return this.transport.subscribe("app.energysaver", json => handler(JSON.parse(json) as EnergySaverChanged));
377377
}
378378
}
379379

380-
/** Motion and environment sensors — accelerometer, gyroscope, magnetometer, compass, barometer and orientation — as live events. Start a sensor, listen for its event, stop it when done. Every sensor stops on its own when the page's last event stream closes, so a closed page never keeps one running. A sensor the device lacks fails with 501. */
380+
/** Motion and environment sensors — accelerometer, gyroscope, magnetometer, compass, barometer and orientation — as live events. Start a sensor, listen for its event, stop it when done. A sensor stops on its own once nothing listens to its event any more — the accelerometer while shakes are listened to counts — so a closed page never keeps one running. A sensor the device lacks fails with 501. */
381381
export class SensorsBridge {
382382
constructor(private readonly transport: BridgeTransport = browserTransport()) {}
383383

@@ -401,32 +401,32 @@ export class SensorsBridge {
401401
return callVoid(this.transport, "DELETE", `sensors`, { signal: options?.signal });
402402
}
403403

404-
onAccelerometer(handler: (payload: VectorReading) => void): () => void {
404+
onAccelerometer(handler: (payload: VectorReading) => void): Promise<() => void> {
405405
return this.transport.subscribe("sensors.accelerometer", json => handler(JSON.parse(json) as VectorReading));
406406
}
407407

408-
onGyroscope(handler: (payload: VectorReading) => void): () => void {
408+
onGyroscope(handler: (payload: VectorReading) => void): Promise<() => void> {
409409
return this.transport.subscribe("sensors.gyroscope", json => handler(JSON.parse(json) as VectorReading));
410410
}
411411

412-
onMagnetometer(handler: (payload: VectorReading) => void): () => void {
412+
onMagnetometer(handler: (payload: VectorReading) => void): Promise<() => void> {
413413
return this.transport.subscribe("sensors.magnetometer", json => handler(JSON.parse(json) as VectorReading));
414414
}
415415

416-
onCompass(handler: (payload: CompassReading) => void): () => void {
416+
onCompass(handler: (payload: CompassReading) => void): Promise<() => void> {
417417
return this.transport.subscribe("sensors.compass", json => handler(JSON.parse(json) as CompassReading));
418418
}
419419

420-
onBarometer(handler: (payload: BarometerReading) => void): () => void {
420+
onBarometer(handler: (payload: BarometerReading) => void): Promise<() => void> {
421421
return this.transport.subscribe("sensors.barometer", json => handler(JSON.parse(json) as BarometerReading));
422422
}
423423

424-
onOrientation(handler: (payload: OrientationReading) => void): () => void {
424+
onOrientation(handler: (payload: OrientationReading) => void): Promise<() => void> {
425425
return this.transport.subscribe("sensors.orientation", json => handler(JSON.parse(json) as OrientationReading));
426426
}
427427

428428
/** The device was shaken. Raised only while the accelerometer is running. */
429-
onShake(handler: (payload: ShakeDetected) => void): () => void {
429+
onShake(handler: (payload: ShakeDetected) => void): Promise<() => void> {
430430
return this.transport.subscribe("sensors.shake", json => handler(JSON.parse(json) as ShakeDetected));
431431
}
432432
}

‎clients/typescript/src/bluetooth-le.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export class BluetoothLEBridge {
109109
return call<BleStatus>(this.transport, "POST", `ble/access`, { signal: options?.signal });
110110
}
111111

112-
/** Starts scanning; results arrive through `onScanResult`. Starting again replaces the running scan. */
112+
/** Starts scanning; results arrive through `onScanResult`, so listen first: without a listener it fails with 409. Starting again replaces the running scan, and the scan stops once nothing listens to it. */
113113
startScan(request: BleScanRequest, options?: { signal?: AbortSignal }): Promise<void> {
114114
return callVoid(this.transport, "POST", `ble/scan`, { json: request, signal: options?.signal });
115115
}
@@ -164,7 +164,7 @@ export class BluetoothLEBridge {
164164
return callVoid(this.transport, "PUT", `ble/peripherals/${segment(uuid)}/services/${segment(service)}/characteristics/${segment(characteristic)}`, { json: request, signal: options?.signal });
165165
}
166166

167-
/** Subscribes to a characteristic; values arrive through `onNotification`. */
167+
/** Subscribes to a characteristic; values arrive through `onNotification`, so listen first: without a listener it fails with 409. Every subscription ends once that has no listener left; peripherals stay connected. */
168168
startNotifications(uuid: string, service: string, characteristic: string, options?: { signal?: AbortSignal }): Promise<void> {
169169
return callVoid(this.transport, "POST", `ble/peripherals/${segment(uuid)}/services/${segment(service)}/characteristics/${segment(characteristic)}/notifications`, { signal: options?.signal });
170170
}
@@ -175,22 +175,22 @@ export class BluetoothLEBridge {
175175
}
176176

177177
/** A peripheral seen by the running scan. Raised for every advertisement. */
178-
onScanResult(handler: (payload: BleScanResult) => void): () => void {
178+
onScanResult(handler: (payload: BleScanResult) => void): Promise<() => void> {
179179
return this.transport.subscribe("ble.scan", json => handler(JSON.parse(json) as BleScanResult));
180180
}
181181

182182
/** A peripheral this page connected to changed connection state. */
183-
onStatus(handler: (payload: BlePeripheralStatus) => void): () => void {
183+
onStatus(handler: (payload: BlePeripheralStatus) => void): Promise<() => void> {
184184
return this.transport.subscribe("ble.status", json => handler(JSON.parse(json) as BlePeripheralStatus));
185185
}
186186

187187
/** A subscribed characteristic changed. */
188-
onNotification(handler: (payload: BleNotification) => void): () => void {
188+
onNotification(handler: (payload: BleNotification) => void): Promise<() => void> {
189189
return this.transport.subscribe("ble.notification", json => handler(JSON.parse(json) as BleNotification));
190190
}
191191

192192
/** A scan or a notification subscription failed and has ended. */
193-
onError(handler: (payload: BleError) => void): () => void {
193+
onError(handler: (payload: BleError) => void): Promise<() => void> {
194194
return this.transport.subscribe("ble.error", json => handler(JSON.parse(json) as BleError));
195195
}
196196
}

‎clients/typescript/src/camera.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export class CameraBridge {
155155
}
156156

157157
/** The camera changed — opened, closed, switched mode, filed a capture. Raised with the whole state. */
158-
onStatus(handler: (payload: CameraStatus) => void): () => void {
158+
onStatus(handler: (payload: CameraStatus) => void): Promise<() => void> {
159159
return this.transport.subscribe("camera.status", json => handler(JSON.parse(json) as CameraStatus));
160160
}
161161
}

‎clients/typescript/src/core.ts‎

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ export interface BridgeTransport {
55
/** Sends a request whose path is relative to the bridge prefix, e.g. `calendar/events/42`. */
66
send(method: string, path: string, init?: BridgeRequestInit): Promise<Response>;
77

8-
/** Delivers each occurrence of a native event as its JSON text. Returns a function that stops delivery. */
9-
subscribe(eventName: string, handler: (json: string) => void): () => void;
8+
/**
9+
* Delivers each occurrence of a native event as its JSON text. Resolves, once the host is delivering it, to a function
10+
* that stops delivery — so a page can start what it listens to right after, without missing the first events.
11+
*/
12+
subscribe(eventName: string, handler: (json: string) => void): Promise<() => void>;
1013
}
1114

1215
export interface BridgeRequestInit {
@@ -46,7 +49,9 @@ export function browserTransport(): BridgeTransport {
4649

4750
function createBrowserTransport(): BridgeTransport {
4851
let prefix: Promise<string> | undefined;
49-
let source: EventSource | undefined;
52+
let streamId: string | undefined;
53+
let syncing: Promise<void> | undefined;
54+
let waiting: (() => void)[] = [];
5055
const listeners = new Map<string, Set<(json: string) => void>>();
5156

5257
const bridgePrefix = () =>
@@ -55,6 +60,54 @@ function createBrowserTransport(): BridgeTransport {
5560
.then((p: { bridge: string }) => (p.bridge.endsWith("/") ? p.bridge : p.bridge + "/"))
5661
.catch(() => "/_bridge/"));
5762

63+
// The host only runs the native source behind an event while a stream names it, so the stream always carries
64+
// exactly the events something here listens to.
65+
const topics = () => [...listeners].filter(([, handlers]) => handlers.size > 0).map(([name]) => name);
66+
67+
// Resolves once the host has the current topics. Several changes in a row become one request.
68+
const sync = (): Promise<void> => {
69+
const done = new Promise<void>(resolve => waiting.push(resolve));
70+
71+
syncing ??= Promise.resolve().then(async () => {
72+
syncing = undefined;
73+
74+
// No stream id yet: the stream's open event syncs, and settles these.
75+
if (!streamId)
76+
return;
77+
78+
const settled = waiting;
79+
waiting = [];
80+
81+
await fetch((await bridgePrefix()) + "events/" + streamId, {
82+
method: "PUT",
83+
headers: { "Content-Type": "application/json" },
84+
body: JSON.stringify({ topics: topics() }),
85+
credentials: "same-origin"
86+
}).catch(() => {
87+
// Reconnecting; the next open event sends the topics again.
88+
});
89+
90+
settled.forEach(resolve => resolve());
91+
});
92+
93+
// A host that is not there never opens the stream; do not hold the caller forever.
94+
return Promise.race([done, new Promise<void>(resolve => setTimeout(resolve, 5000))]);
95+
};
96+
97+
let opening: Promise<EventSource> | undefined;
98+
const events = () =>
99+
(opening ??= bridgePrefix().then(p => {
100+
const source = new EventSource(p + "events?topics=" + encodeURIComponent(topics().join(",")));
101+
102+
// First on every connection, reconnects included, which start over with the topics of the first URL.
103+
source.addEventListener("bridge.stream", e => {
104+
streamId = (JSON.parse((e as MessageEvent<string>).data) as { id: string }).id;
105+
void sync();
106+
});
107+
108+
return source;
109+
}));
110+
58111
return {
59112
async send(method, path, init) {
60113
const headers: Record<string, string> = {};
@@ -77,25 +130,33 @@ function createBrowserTransport(): BridgeTransport {
77130
});
78131
},
79132

80-
subscribe(eventName, handler) {
133+
async subscribe(eventName, handler) {
81134
let handlers = listeners.get(eventName);
135+
const known = handlers !== undefined;
82136

83137
if (!handlers) {
84138
handlers = new Set();
85139
listeners.set(eventName, handlers);
86-
87-
// One DOM listener per event name, added once the prefix is known; handlers come and go behind it.
88-
void bridgePrefix().then(p => {
89-
source ??= new EventSource(p + "events");
90-
source.addEventListener(eventName, e => {
91-
for (const h of listeners.get(eventName) ?? [])
92-
h((e as MessageEvent<string>).data);
93-
});
94-
});
95140
}
96141

142+
const added = handlers.size === 0;
97143
handlers.add(handler);
98-
return () => handlers!.delete(handler);
144+
145+
// One DOM listener per event name for the life of the page; handlers come and go behind it.
146+
if (!known)
147+
(await events()).addEventListener(eventName, e => {
148+
for (const h of listeners.get(eventName) ?? [])
149+
h((e as MessageEvent<string>).data);
150+
});
151+
152+
if (added)
153+
await sync();
154+
155+
const subscribed = handlers;
156+
return () => {
157+
if (subscribed.delete(handler) && subscribed.size === 0)
158+
void sync();
159+
};
99160
}
100161
};
101162
}

0 commit comments

Comments
 (0)