@photon-os/sdk 1.0.0-alpha.1 → 1.0.0-alpha.3

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/dist/index.mjs CHANGED
@@ -1219,6 +1219,71 @@ var AppManager = class {
1219
1219
  }
1220
1220
  };
1221
1221
 
1222
+ // src/framework/DeviceManager.ts
1223
+ var DeviceManager = class {
1224
+ constructor(os) {
1225
+ this.messageCallbacks = {};
1226
+ this.subscribed = false;
1227
+ this.os = os;
1228
+ }
1229
+ /** Get all registered Second Life devices for the current user */
1230
+ async getRegisteredDevices() {
1231
+ const api2 = await this.os.getRPCAPI();
1232
+ return await api2.devices_getRegistered();
1233
+ }
1234
+ /** Send a message to a registered device */
1235
+ async sendMessage(deviceId, type, payload = {}) {
1236
+ const api2 = await this.os.getRPCAPI();
1237
+ return await api2.devices_sendMessage(deviceId, type, payload);
1238
+ }
1239
+ /** Unregister a device */
1240
+ async unregisterDevice(deviceId) {
1241
+ const api2 = await this.os.getRPCAPI();
1242
+ await api2.devices_unregister(deviceId);
1243
+ }
1244
+ /** Subscribe to messages from all registered devices */
1245
+ subscribeToMessages(deviceId, callback) {
1246
+ this.messageCallbacks[deviceId] = this.messageCallbacks[deviceId] ?? [];
1247
+ this.messageCallbacks[deviceId].add(callback);
1248
+ if (!this.subscribed) {
1249
+ this.startSubscription();
1250
+ }
1251
+ return () => {
1252
+ if (this.messageCallbacks[deviceId]) {
1253
+ this.messageCallbacks[deviceId].delete(callback);
1254
+ }
1255
+ const totalSubscriptions = Object.values(this.messageCallbacks).reduce(
1256
+ (acc, cv) => acc + cv.size,
1257
+ 0
1258
+ );
1259
+ if (totalSubscriptions === 0 && this.subscribed) {
1260
+ this.stopSubscription();
1261
+ }
1262
+ };
1263
+ }
1264
+ /** Internal: Start the message subscription via RPC */
1265
+ async startSubscription() {
1266
+ const api2 = await this.os.getRPCAPI();
1267
+ await api2.devices_subscribe((message) => {
1268
+ const callbacks = this.messageCallbacks[message.deviceId] ?? [];
1269
+ for (const callback of callbacks) {
1270
+ try {
1271
+ callback(message);
1272
+ } catch (error) {
1273
+ console.error("Error in device message callback:", error);
1274
+ }
1275
+ }
1276
+ });
1277
+ this.subscribed = true;
1278
+ }
1279
+ /** Internal: Stop the message subscription */
1280
+ async stopSubscription() {
1281
+ const api2 = await this.os.getRPCAPI();
1282
+ await api2.devices_unsubscribe();
1283
+ this.subscribed = false;
1284
+ }
1285
+ };
1286
+
1222
1287
  // src/framework/PreferencesManager.ts
1223
1288
  var PreferencesManager = class {
1224
1289
  constructor(os) {
@@ -1290,6 +1355,7 @@ var OS = class {
1290
1355
  this.config = config;
1291
1356
  this.accounts = new AccountManager(this);
1292
1357
  this.apps = new AppManager(this);
1358
+ this.devices = new DeviceManager(this);
1293
1359
  this.prefs = new PreferencesManager(this);
1294
1360
  this.user = new UserManager(this);
1295
1361
  }