Skip to content

Commit

Permalink
Fix instance of Item checks don't work (#290)
Browse files Browse the repository at this point in the history
Fixes an issue, where the webpacked, globals injected
`time.toZDT(items.Item)` did not work,
because #283 did revert
#273 and therefore constructor
names are not kept.

This introduces a `_isItem` helper method, which checks both constructor
name or unique properties (`rawItem`).

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
  • Loading branch information
florian-h05 authored Aug 12, 2023
1 parent be9daef commit ee2e51f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
17 changes: 16 additions & 1 deletion helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ function _getItemName (itemOrName) {
return itemOrName;
}

/**
* Checks whether the given object is an instance of {@link items.Item}.
*
* To be used when instanceof checks don't work because of circular dependencies.
* Checks constructor name or unique properties, because constructor name does not work for the webpacked globals injection.
*
* @param o {*}
* @returns {boolean}
* @private
*/
function _isItem (o) {
return ((o.constructor && o.constructor.name === 'Item') || typeof o.rawItem === 'object');
}

module.exports = {
_getItemName
_getItemName,
_isItem
};
5 changes: 3 additions & 2 deletions quantity.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { _isItem } = require('./helpers');
const QuantityType = Java.type('org.openhab.core.library.types.QuantityType');
/**
* @type {JavaBigDecimal}
Expand All @@ -20,7 +21,7 @@ const BigDecimal = Java.type('java.math.BigDecimal');
* @private
*/
function _toBigDecimalOrQtyType (value) {
if (value.constructor && value.constructor.name === 'Item' && value.rawState.getClass().getSimpleName() === 'DecimalType') {
if (_isItem(value) && value.rawState.getClass().getSimpleName() === 'DecimalType') {
try {
value = value.rawState.toBigDecimal();
} catch (e) {
Expand Down Expand Up @@ -48,7 +49,7 @@ function _toBigDecimalOrQtyType (value) {
* @private
*/
function _toQtyType (value, errorMsg = 'Argument of wrong type provided, required Item, string or Quantity.') {
if (value.constructor && value.constructor.name === 'Item') {
if (_isItem(value)) {
if (value.rawState.getClass().getSimpleName() === 'QuantityType') {
value = value.rawState;
} else {
Expand Down
5 changes: 3 additions & 2 deletions time.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require('@js-joda/timezone');
const time = require('@js-joda/core');
const items = require('./items');
const utils = require('./utils');
const { _isItem } = require('./helpers');

const javaZDT = Java.type('java.time.ZonedDateTime');
const javaDuration = Java.type('java.time.Duration');
Expand Down Expand Up @@ -254,11 +255,11 @@ function toZDT (when) {

// GenericItem
if (when instanceof ohItem) {
return _convertItem(items.getItem(when.name));
return _convertItem(items.getItem(when.getName()));
}

// items.Item
if (when.constructor.name === 'Item') {
if (_isItem(when)) {
return _convertItem(when);
}

Expand Down
2 changes: 1 addition & 1 deletion types/items/items.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion types/quantity.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion types/time.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ee2e51f

Please sign in to comment.