Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix instance of Item checks don't work #290

Merged
merged 2 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.