From 794b05d1429c715b7489a5d7dcb4aaef22e6f039 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Thu, 1 Dec 2022 23:03:53 +0100 Subject: [PATCH 1/3] [rules] Remove unused rule providers Removes methods to select/register another provider for rules. Those are removed, because they go far beyond the use case of most users. As they were not documented, it is not expected that anyone is using them. Signed-off-by: Florian Hotze --- rules/rules.js | 56 ++++++++------------------------------------------ 1 file changed, 9 insertions(+), 47 deletions(-) diff --git a/rules/rules.js b/rules/rules.js index 2fb47a059..52797e718 100644 --- a/rules/rules.js +++ b/rules/rules.js @@ -60,7 +60,7 @@ const factory = require('@runtime/rules').factory; /** * Generates an item name given it's configuration. * - * @memberOf rules + * @memberof rules * @private * @param {object} ruleConfig The rule config * @param {string} userInfo.name The name of the rule. @@ -72,7 +72,7 @@ const itemNameForRule = function (ruleConfig) { /** * Links an item to a rule. When the item is switched on or off, so will the rule be. * - * @memberOf rules + * @memberof rules * @private * @param {HostRule} rule The rule to link to the item. * @param {items.Item} item the item to link to the rule. @@ -100,7 +100,7 @@ const linkItemToRule = function (rule, item) { /** * Gets the groups that an rule-toggling-item should be a member of. Will create the group item if necessary. * - * @memberOf rules + * @memberof rules * @private * @param {RuleConfig} ruleConfig The rule config describing the rule * @returns {String[]} the group names to put the item in @@ -126,7 +126,7 @@ const getGroupsForItem = function (ruleConfig) { * Check whether a rule exists. * Only works for rules created in the same file. * - * @memberOf rules + * @memberof rules * @private * @param {string} uid the UID of the rule * @returns {boolean} whether the rule exists @@ -139,7 +139,7 @@ const ruleExists = function (uid) { * Remove a rule when it exists. The rule will be immediately removed. * Only works for rules created in the same file. * - * @memberOf rules + * @memberof rules * @param {string} uid the UID of the rule * @returns {boolean} whether the rule was actually removed */ @@ -157,7 +157,7 @@ const removeRule = function (uid) { * Runs the rule with the given UID. Throws errors when the rule doesn't exist * or is unable to run (e.g. it's disabled). * - * @memberOf rules + * @memberof rules * @param {string} uid the UID of the rule to run * @param {Map} [args={}] args optional dict of data to pass to the called rule * @param {boolean} [cond=true] when true, the called rule will only run if it's conditions are met @@ -220,7 +220,7 @@ const setEnabled = function (uid, isEnabled) { * execute: (event) => { // do stuff } * }); * - * @memberOf rules + * @memberof rules * @param {RuleConfig} ruleConfig The rule config describing the rule * @returns {HostRule} the created rule * @throws Will throw an error if the rule with the passed in uid already exists @@ -280,54 +280,17 @@ const JSRule = function (ruleConfig) { // Register rule here if (triggers && triggers.length > 0) { rule.setTriggers(triggers); - rule = registerRule(rule); + rule = automationManager.addRule(rule); } return rule; }; -let currentProvider = automationManager; - -const withNewRuleProvider = function (fn) { - const cachedRules = []; - currentProvider = { - addRule: r => { - // r = factory.processRule(r); - // r.setConfigurationDescriptions(null); - // r.setConfiguration(null); - cachedRules.push(factory.processRule(r)); - } - }; - - try { - fn(); - const provider = factory.newRuleProvider(cachedRules); - osgi.registerService(provider, Java.type('org.openhab.core.automation.RuleProvider').class.getName()); - } finally { - currentProvider = automationManager; - } -}; - -const withManagedProvider = function (fn) { // eslint-disable-line no-unused-vars - const previousProvider = currentProvider; - currentProvider = automationManager; - - try { - fn(); - } finally { - currentProvider = previousProvider; - } -}; - -const registerRule = function (rule) { - return currentProvider.addRule(rule); -}; - /** * Creates a rule, with an associated SwitchItem that can be used to toggle the rule's enabled state. * The rule will be created and immediately available. * - * @memberOf rules + * @memberof rules * @param {RuleConfig} ruleConfig The rule config describing the rule * @returns {HostRule} the created rule * @throws Will throw an error is a rule with the given UID already exists. @@ -484,7 +447,6 @@ const getTriggeredData = function (input) { }; module.exports = { - withNewRuleProvider, removeRule, runRule, isEnabled, From 70a2033a0776a9ba8ca97bef291d1edf695a7dce Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Fri, 2 Dec 2022 19:11:32 +0100 Subject: [PATCH 2/3] [rules] Fix linting errors & Update types Signed-off-by: Florian Hotze --- rules/rules.js | 1 - types/rules/rules.d.ts | 9 ++++----- types/rules/rules.d.ts.map | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/rules/rules.js b/rules/rules.js index 52797e718..580dbb1b2 100644 --- a/rules/rules.js +++ b/rules/rules.js @@ -55,7 +55,6 @@ const triggers = require('../triggers'); const { automationManager, ruleRegistry } = require('@runtime/RuleSupport'); const RuleManager = osgi.getService('org.openhab.core.automation.RuleManager'); -const factory = require('@runtime/rules').factory; /** * Generates an item name given it's configuration. diff --git a/types/rules/rules.d.ts b/types/rules/rules.d.ts index 57cc68149..7a2c842fb 100644 --- a/types/rules/rules.d.ts +++ b/types/rules/rules.d.ts @@ -100,12 +100,11 @@ export type RuleConfig = { */ overwrite?: boolean; }; -export function withNewRuleProvider(fn: any): void; /** * Remove a rule when it exists. The rule will be immediately removed. * Only works for rules created in the same file. * - * @memberOf rules + * @memberof rules * @param {string} uid the UID of the rule * @returns {boolean} whether the rule was actually removed */ @@ -114,7 +113,7 @@ export function removeRule(uid: string): boolean; * Runs the rule with the given UID. Throws errors when the rule doesn't exist * or is unable to run (e.g. it's disabled). * - * @memberOf rules + * @memberof rules * @param {string} uid the UID of the rule to run * @param {Map} [args={}] args optional dict of data to pass to the called rule * @param {boolean} [cond=true] when true, the called rule will only run if it's conditions are met @@ -154,7 +153,7 @@ export function setEnabled(uid: string, isEnabled: boolean): void; * execute: (event) => { // do stuff } * }); * - * @memberOf rules + * @memberof rules * @param {RuleConfig} ruleConfig The rule config describing the rule * @returns {HostRule} the created rule * @throws Will throw an error if the rule with the passed in uid already exists @@ -164,7 +163,7 @@ export function JSRule(ruleConfig: RuleConfig): HostRule; * Creates a rule, with an associated SwitchItem that can be used to toggle the rule's enabled state. * The rule will be created and immediately available. * - * @memberOf rules + * @memberof rules * @param {RuleConfig} ruleConfig The rule config describing the rule * @returns {HostRule} the created rule * @throws Will throw an error is a rule with the given UID already exists. diff --git a/types/rules/rules.d.ts.map b/types/rules/rules.d.ts.map index 0123601f4..fd2970135 100644 --- a/types/rules/rules.d.ts.map +++ b/types/rules/rules.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../../rules/rules.js"],"names":[],"mappings":";;;;;;;;;;;cAec,MAAM;;;;cACN,MAAM;;;;mBACN,MAAM;;;;qBACN,MAAM;;;;cACN,MAAM;;;;mBACN,MAAM;;;;gBACN,MAAM;;;;eACN,MAAM;;;;eACN,MAAM;;;;YACN,MAAM;;;;eACN,MAAM;;;;iBACN,MAAM;;;;;;;;;mCAMT,WAAW;;;;;;;;UAKR,MAAM;;;;kBACN,MAAM;;;;cACN,kBAAS,mBAAU;;;;aACnB,YAAY;;;;SACZ,MAAM;;;;WACN,QAAQ;;;;gBACR,MAAM;;;;gBACN,OAAO;;AAsPrB,mDAkBC;AA3KD;;;;;;;IAOI;AACJ,gCAHY,MAAM,GACJ,OAAO,CAUpB;AAED;;;;;;;;;IASI;AACJ,6BALY,MAAM,+BAEN,OAAO,QAalB;AAED;;;;;;;;IAQI;AACJ,+BAJY,MAAM,GACJ,OAAO,CAQpB;AAED;;;;;;;;IAQI;AACJ,gCAJY,MAAM,aACN,OAAO,QAQlB;AAED;;;;;;;;;;;;;;;;;IAiBI;AACJ,mCAJY,UAAU,YA+DrB;AAuCD;;;;;;;;IAQI;AACJ,6CAJY,UAAU,YAqCrB"} \ No newline at end of file +{"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../../rules/rules.js"],"names":[],"mappings":";;;;;;;;;;;cAec,MAAM;;;;cACN,MAAM;;;;mBACN,MAAM;;;;qBACN,MAAM;;;;cACN,MAAM;;;;mBACN,MAAM;;;;gBACN,MAAM;;;;eACN,MAAM;;;;eACN,MAAM;;;;YACN,MAAM;;;;eACN,MAAM;;;;iBACN,MAAM;;;;;;;;;mCAMT,WAAW;;;;;;;;UAKR,MAAM;;;;kBACN,MAAM;;;;cACN,kBAAS,mBAAU;;;;aACnB,YAAY;;;;SACZ,MAAM;;;;WACN,QAAQ;;;;gBACR,MAAM;;;;gBACN,OAAO;;AA4FrB;;;;;;;IAOI;AACJ,gCAHY,MAAM,GACJ,OAAO,CAUpB;AAED;;;;;;;;;IASI;AACJ,6BALY,MAAM,+BAEN,OAAO,QAalB;AAED;;;;;;;;IAQI;AACJ,+BAJY,MAAM,GACJ,OAAO,CAQpB;AAED;;;;;;;;IAQI;AACJ,gCAJY,MAAM,aACN,OAAO,QAQlB;AAED;;;;;;;;;;;;;;;;;IAiBI;AACJ,mCAJY,UAAU,YA+DrB;AAED;;;;;;;;IAQI;AACJ,6CAJY,UAAU,YAqCrB"} \ No newline at end of file From 12a053d102e720a0d9957d4d546cb2cbe0870248 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Fri, 2 Dec 2022 19:14:18 +0100 Subject: [PATCH 3/3] Update CHANGELOG Signed-off-by: Florian Hotze --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd4c0ec06..d3739be9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ | Type | Namespace | Description | Reference | Breaking | |-------------|-----------|------------------------------------------------------------|--------------------------------------------------------|----------| +| Cleanup | `rules` | Remove unused rule providers | [#183](https://github.com/openhab/openhab-js/pull/183) | Yes | Also see the [Release Milestone](https://github.com/openhab/openhab-js/milestone/8).