homey-api 1.5.12 → 1.5.13

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.
@@ -119,6 +119,7 @@ class Manager extends EventEmitter {
119
119
  const query = { ...$query };
120
120
  const headers = { ...$headers };
121
121
 
122
+ // Verify & Transform parameters
122
123
  if (operation.parameters) {
123
124
  // Parse Parameters
124
125
  for (const [parameterId, parameter] of Object.entries(operation.parameters)) {
@@ -194,8 +195,9 @@ class Manager extends EventEmitter {
194
195
  let result;
195
196
  const benchmark = Util.benchmark();
196
197
 
197
- // Try to get the CRUD Item from Cache.
198
- if (operation.crud && $cache === true) {
198
+ // If connected to Socket.io,
199
+ // try to get the CRUD Item from Cache.
200
+ if (this.isConnected() && operation.crud && $cache === true) {
199
201
  const itemId = items[operation.crud.item].id;
200
202
  const itemType = items[operation.crud.item].type;
201
203
 
@@ -234,8 +236,8 @@ class Manager extends EventEmitter {
234
236
  }
235
237
  }
236
238
 
237
- // If Homey is connected to Socket.io
238
- // Send API request to socket.io
239
+ // If Homey is connected to Socket.io,
240
+ // send the API request to socket.io.
239
241
  // This is about ~2x faster than HTTP
240
242
  if (this.homey.isConnected()) {
241
243
  result = await Util.timeout(new Promise((resolve, reject) => {
@@ -273,13 +275,17 @@ class Manager extends EventEmitter {
273
275
  case 'getOne': {
274
276
  const key = getItemKey(result);
275
277
 
276
- result = this.__cache[itemId][key] = new ItemClass({
278
+ result = new ItemClass({
277
279
  key,
278
280
  homey: this.homey,
279
281
  manager: this,
280
282
  properties: { ...result },
281
283
  });
282
284
 
285
+ if (this.isConnected()) {
286
+ this.__cache[itemId][key] = result;
287
+ }
288
+
283
289
  break;
284
290
  }
285
291
  case 'getAll': {
@@ -290,16 +296,20 @@ class Manager extends EventEmitter {
290
296
  if (this.__cache[itemId][key]) {
291
297
  result[resultKey] = this.__cache[itemId][key].__update(item);
292
298
  } else {
293
- result[resultKey] = this.__cache[itemId][key] = new ItemClass({
299
+ result[resultKey] = new ItemClass({
294
300
  key,
295
301
  homey: this.homey,
296
302
  manager: this,
297
303
  properties: { ...item },
298
304
  });
305
+
306
+ if (this.isConnected()) {
307
+ this.__cache[itemId][key] = result[resultKey];
308
+ }
299
309
  }
300
310
  }
301
311
 
302
- // Find and delete deleted from cache
312
+ // Find and delete deleted items from cache
303
313
  if (this.__cache[itemId]) {
304
314
  for (const cachedItem of Object.values(this.__cache[itemId])) {
305
315
  const key = getItemKey(cachedItem);
@@ -321,11 +331,15 @@ class Manager extends EventEmitter {
321
331
  if (this.__cache[itemId][key]) {
322
332
  result = this.__cache[itemId][key].__update(result);
323
333
  } else {
324
- result = this.__cache[itemId][key] = new ItemClass({
334
+ result = new ItemClass({
325
335
  key,
326
336
  manager: this,
327
337
  properties: result,
328
338
  });
339
+
340
+ if (this.isConnected()) {
341
+ this.__cache[itemId][key] = result;
342
+ }
329
343
  }
330
344
  break;
331
345
  }
@@ -354,6 +368,14 @@ class Manager extends EventEmitter {
354
368
  this.homey.__debug(`[${this.name}]`, ...props);
355
369
  }
356
370
 
371
+ /**
372
+ * If this manager's namespace is connected to Socket.io.
373
+ * @returns {Boolean}
374
+ */
375
+ isConnected() {
376
+ return this.__connected === true;
377
+ }
378
+
357
379
  /**
358
380
  * Connect to the realtime namespace.
359
381
  * @returns {Promise<void>}
@@ -467,6 +467,10 @@ class HomeyAPIV2 extends HomeyAPI {
467
467
  this.__token = null;
468
468
  }
469
469
 
470
+ /**
471
+ * If Homey is connected to Socket.io.
472
+ * @returns {Boolean}
473
+ */
470
474
  isConnected() {
471
475
  return this.__connected === true;
472
476
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homey-api",
3
- "version": "1.5.12",
3
+ "version": "1.5.13",
4
4
  "description": "Homey API",
5
5
  "main": "src/index.js",
6
6
  "types": "assets/types/homey-api.d.ts",