@kelnishi/satmouse-client 0.18.0 → 0.18.2

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.
@@ -263,6 +263,7 @@ var ExtensionAdapter = class {
263
263
  onClose = null;
264
264
  onError = null;
265
265
  messageHandler = null;
266
+ pendingDeviceFetch = null;
266
267
  static isAvailable() {
267
268
  return !!globalThis.__satmouseExtensionAvailable;
268
269
  }
@@ -294,11 +295,27 @@ var ExtensionAdapter = class {
294
295
  if (msg.type === "deviceStatus" && msg.data) {
295
296
  this.onDeviceStatus?.(msg.data.event, msg.data.device);
296
297
  }
298
+ if (msg.type === "deviceList" && this.pendingDeviceFetch) {
299
+ this.pendingDeviceFetch(Array.isArray(msg.data) ? msg.data : []);
300
+ this.pendingDeviceFetch = null;
301
+ }
297
302
  };
298
303
  globalThis.addEventListener("message", this.messageHandler);
299
304
  globalThis.postMessage({ target: "satmouse-extension", action: "connect" }, "*");
300
305
  });
301
306
  }
307
+ fetchDeviceInfo() {
308
+ return new Promise((resolve) => {
309
+ this.pendingDeviceFetch = resolve;
310
+ globalThis.postMessage({ target: "satmouse-extension", action: "fetchDevices" }, "*");
311
+ setTimeout(() => {
312
+ if (this.pendingDeviceFetch) {
313
+ this.pendingDeviceFetch = null;
314
+ resolve([]);
315
+ }
316
+ }, 3e3);
317
+ });
318
+ }
302
319
  close() {
303
320
  if (this.messageHandler) {
304
321
  globalThis.removeEventListener("message", this.messageHandler);
@@ -360,8 +377,8 @@ var SatMouseConnection = class extends TypedEmitter {
360
377
  if (!wtUrl && !wsUrl) {
361
378
  const tdUrl = this.options.tdUrl;
362
379
  const tdUrls = tdUrl ? [tdUrl] : [
363
- "https://127.0.0.1:18947/td.json",
364
- "http://127.0.0.1:18945/td.json"
380
+ "http://127.0.0.1:18945/td.json",
381
+ "https://127.0.0.1:18947/td.json"
365
382
  ];
366
383
  let resolved = false;
367
384
  for (const url of tdUrls) {
@@ -379,8 +396,7 @@ var SatMouseConnection = class extends TypedEmitter {
379
396
  }
380
397
  if (!resolved) {
381
398
  this.emit("error", new Error("Failed to fetch Thing Description"));
382
- const isSecurePage = typeof globalThis.location !== "undefined" && globalThis.location.protocol === "https:";
383
- wsUrl = isSecurePage ? "wss://127.0.0.1:18947/spatial" : "ws://127.0.0.1:18945/spatial";
399
+ wsUrl = "ws://127.0.0.1:18945/spatial";
384
400
  }
385
401
  }
386
402
  for (const proto of this.options.transports) {
@@ -428,11 +444,20 @@ var SatMouseConnection = class extends TypedEmitter {
428
444
  this.setState("disconnected", "none");
429
445
  }
430
446
  async fetchDeviceInfo() {
431
- if (!this.deviceInfoUrl) return [];
432
- const res = await globalThis.fetch(this.deviceInfoUrl);
433
- if (!res.ok) return [];
434
- const data = await res.json();
435
- return data.devices ?? [];
447
+ if (this.deviceInfoUrl) {
448
+ try {
449
+ const res = await globalThis.fetch(this.deviceInfoUrl);
450
+ if (res.ok) {
451
+ const data = await res.json();
452
+ return data.devices ?? [];
453
+ }
454
+ } catch {
455
+ }
456
+ }
457
+ if (this.transport && "fetchDeviceInfo" in this.transport) {
458
+ return this.transport.fetchDeviceInfo();
459
+ }
460
+ return [];
436
461
  }
437
462
  async tryTransport(adapter) {
438
463
  adapter.onSpatialData = (data) => this.emit("spatialData", data);