|
1 | | -import type { AppSettings, AppConfig, Capabilities } from "../../types"; |
| 1 | +import type { AppSettings, AppConfig, AppSetting, Capabilities, ExtendedStreamType } from "../../types"; |
2 | 2 | import { MentraSession } from "../MentraSession"; |
3 | 3 | import { _V2AudioStreamShim } from "./_V2AudioStreamShim"; |
4 | 4 | import { _V2CameraShim, type _V2PhotoRequestBridge } from "./_V2CameraShim"; |
@@ -286,6 +286,101 @@ export class _V2SessionShim { |
286 | 286 |
|
287 | 287 | // ─── Low-Level Message Sending ────────────────────────────────────────── |
288 | 288 |
|
| 289 | + // ─── Settings-Based Subscriptions ─────────────────────────────────────── |
| 290 | + |
| 291 | + /** |
| 292 | + * @deprecated Use v3 manager-based subscriptions instead. |
| 293 | + * |
| 294 | + * Configure automatic subscription updates when settings change. |
| 295 | + * The handler function receives the current settings and returns |
| 296 | + * the desired subscription list. When any of the `updateOnChange` |
| 297 | + * keys change, the handler is re-evaluated and subscriptions updated. |
| 298 | + */ |
| 299 | + private _subscriptionSettingsHandler?: (settings: AppSettings) => ExtendedStreamType[]; |
| 300 | + private _subscriptionUpdateTriggers: string[] = []; |
| 301 | + |
| 302 | + setSubscriptionSettings(options: { |
| 303 | + updateOnChange: string[]; |
| 304 | + handler: (settings: AppSettings) => ExtendedStreamType[]; |
| 305 | + }): void { |
| 306 | + this._subscriptionUpdateTriggers = options.updateOnChange; |
| 307 | + this._subscriptionSettingsHandler = options.handler; |
| 308 | + |
| 309 | + // If we already have settings, evaluate immediately |
| 310 | + if (this.session.settingsData.length > 0) { |
| 311 | + this._updateSubscriptionsFromSettings(); |
| 312 | + } |
| 313 | + |
| 314 | + // Listen for settings changes to re-evaluate |
| 315 | + this.session.onSettings((settings) => { |
| 316 | + const shouldUpdate = this._subscriptionUpdateTriggers.some((key) => { |
| 317 | + return settings.some((s: any) => s.key === key); |
| 318 | + }); |
| 319 | + if (shouldUpdate) { |
| 320 | + this._updateSubscriptionsFromSettings(); |
| 321 | + } |
| 322 | + }); |
| 323 | + } |
| 324 | + |
| 325 | + private _updateSubscriptionsFromSettings(): void { |
| 326 | + if (!this._subscriptionSettingsHandler) return; |
| 327 | + try { |
| 328 | + this._subscriptionSettingsHandler(this.session.settingsData); |
| 329 | + // Note: with Bug 007 fix, subscriptions are derived from handlers. |
| 330 | + // The handler should register event handlers that correspond to the |
| 331 | + // desired subscriptions. This call just triggers the re-evaluation. |
| 332 | + } catch (error) { |
| 333 | + this.session.logger.error(error, "Error updating subscriptions from settings"); |
| 334 | + } |
| 335 | + } |
| 336 | + |
| 337 | + // ─── Config Loading ───────────────────────────────────────────────────── |
| 338 | + |
| 339 | + /** |
| 340 | + * @deprecated Use appConfig property directly. |
| 341 | + * Load app configuration from a JSON string. |
| 342 | + */ |
| 343 | + loadConfigFromJson(jsonData: string): AppConfig { |
| 344 | + try { |
| 345 | + const parsed = JSON.parse(jsonData); |
| 346 | + // Basic validation |
| 347 | + if (!parsed || typeof parsed !== "object") { |
| 348 | + throw new Error("Invalid App configuration format"); |
| 349 | + } |
| 350 | + this.session.appConfig = parsed as AppConfig; |
| 351 | + return parsed as AppConfig; |
| 352 | + } catch (error) { |
| 353 | + const msg = error instanceof Error ? error.message : String(error); |
| 354 | + throw new Error(`Failed to load App configuration: ${msg}`); |
| 355 | + } |
| 356 | + } |
| 357 | + |
| 358 | + /** |
| 359 | + * @deprecated Read from appConfig.settings directly. |
| 360 | + * Get default settings from the loaded app configuration. |
| 361 | + */ |
| 362 | + getDefaultSettings(): AppSettings { |
| 363 | + if (!this.session.appConfig) { |
| 364 | + throw new Error("App configuration not loaded. Call loadConfigFromJson first."); |
| 365 | + } |
| 366 | + return (this.session.appConfig.settings || []) |
| 367 | + .filter((s: any) => s.type !== "group" && "key" in s) |
| 368 | + .map((s: any) => ({ ...s, value: s.defaultValue })); |
| 369 | + } |
| 370 | + |
| 371 | + /** |
| 372 | + * @deprecated Read from appConfig.settings directly. |
| 373 | + * Get the schema for a specific setting key. |
| 374 | + */ |
| 375 | + getSettingSchema(key: string): AppSetting | undefined { |
| 376 | + if (!this.session.appConfig) return undefined; |
| 377 | + return (this.session.appConfig.settings || []).find( |
| 378 | + (s: any) => s.type !== "group" && "key" in s && s.key === key, |
| 379 | + ) as AppSetting | undefined; |
| 380 | + } |
| 381 | + |
| 382 | + // ─── Messaging ────────────────────────────────────────────────────────── |
| 383 | + |
289 | 384 | /** Send an arbitrary JSON message over the WebSocket. */ |
290 | 385 | sendMessage(message: unknown): void { |
291 | 386 | this.session.sendMessage(message); |
|
0 commit comments