Skip to content

Commit

Permalink
Merge pull request #1343 from klein0r/master
Browse files Browse the repository at this point in the history
Blockly improvements, messageToAsync, documentation
  • Loading branch information
GermanBluefox committed Jul 7, 2023
2 parents cbcdfe2 + 0205688 commit 2fa88e3
Show file tree
Hide file tree
Showing 24 changed files with 435 additions and 399 deletions.
251 changes: 145 additions & 106 deletions docs/en/javascript.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/javascript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,7 @@ declare global {
* @return ID of the subscription. It could be used for un-subscribe.
*/
function messageTo(target: iobJS.MessageTarget | string, data: any, options?: any, callback?: SimpleCallback<any>): iobJS.MessageSubscribeID;
function messageToAsync(target: iobJS.MessageTarget | string, data: any, options?: any): Promise<iobJS.MessageCallback | iobJS.MessageCallbackInfo>;

/**
* Process message from other script.
Expand Down
24 changes: 21 additions & 3 deletions lib/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ function sandBox(script, name, verbose, debug, context) {
let ts = mods.suncalc.getTimes(date, adapter.config.latitude, adapter.config.longitude)[pattern];

if (ts === undefined || ts.getTime().toString() === 'NaN') {
adapter.log.error(`Cannot get astro date for "${pattern}"`);
adapter.log.warn(`Cannot calculate astro date "${pattern}" for ${adapter.config.latitude}, ${adapter.config.longitude}`);
}

sandbox.verbose && sandbox.log(`getAstroDate(pattern=${pattern}, date=${date}) => ${ts}`, 'info');
Expand Down Expand Up @@ -1926,7 +1926,7 @@ function sandBox(script, name, verbose, debug, context) {
} else {
if (adapter.config.subscribe) {
sandbox.log('The "getState" method cannot be used synchronously, because the adapter setting "Do not subscribe to all states on start" is enabled.', 'error');
sandbox.log(`Please disable that setting or use "getState" with a callback, e.g.: getState("${id}", (err, state) => { ... });`, 'error');
sandbox.log(`Please disable that setting or use "getState" with a callback, e.g.: getState('${id}', (err, state) => { ... });`, 'error');
} else {
if (states[id]) {
sandbox.verbose && sandbox.log(`getState(id=${id}, timerId=${timers[id]}) => ${JSON.stringify(states[id])}`, 'info');
Expand Down Expand Up @@ -1978,7 +1978,12 @@ function sandBox(script, name, verbose, debug, context) {
}
});
} else {
return !!states[id];
if (adapter.config.subscribe) {
sandbox.log('The "existsState" method cannot be used synchronously, because the adapter setting "Do not subscribe to all states on start" is enabled.', 'error');
sandbox.log(`Please disable that setting or use "existsState" with a callback, e.g.: existsState('${id}', (err, stateExists) => { ... });`, 'error');
} else {
return !!states[id];
}
}
},
existsObject: function (id, callback) {
Expand Down Expand Up @@ -3489,6 +3494,7 @@ function sandBox(script, name, verbose, debug, context) {
}
});
} else {
// Send to all instances
context.adapter.getObjectView('system', 'instance', {startkey: 'system.adapter.javascript.', endkey: 'system.adapter.javascript.\u9999'}, options, (err, res) => {
if (err || !res) {
sandbox.log(`messageTo failed: ${err.message}`, 'error');
Expand Down Expand Up @@ -3516,6 +3522,18 @@ function sandBox(script, name, verbose, debug, context) {
});
}
},
messageToAsync: function (target, data, options) {
return new Promise((resolve, reject) => {
sandbox.messageTo(target, data, options, res => {
sandbox.verbose && sandbox.log(`messageTo result => ${JSON.stringify(res)}`, 'debug');
if (!res || res.error) {
reject(res ? res.error : new Error('Unknown error'));
} else {
resolve(res);
}
});
});
},
onMessage: function (messageName, callback) {
if (typeof callback !== 'function') {
sandbox.log('onMessage callback is not a function', 'error');
Expand Down
30 changes: 15 additions & 15 deletions lib/typescriptTools.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const fs = require('fs');
const path = require('path');
const fs = require('node:fs');
const path = require('node:path');
const ts = require('typescript');
const {matchAll} = require('./tools');

Expand Down Expand Up @@ -213,15 +213,15 @@ function addExportModifier(s) {
if (ts.isVariableStatement(s)) {
return ts.factory.updateVariableStatement(s, modifiers, s.declarationList);
} else if (ts.isTypeAliasDeclaration(s)) {
return ts.factory.updateTypeAliasDeclaration(s, s.decorators, modifiers, s.name, s.typeParameters, s.type);
return ts.factory.updateTypeAliasDeclaration(s, modifiers, s.name, s.typeParameters, s.type);
} else if (ts.isInterfaceDeclaration(s)) {
return ts.factory.updateInterfaceDeclaration(s, s.decorators, modifiers, s.name, s.typeParameters, s.heritageClauses, s.members);
return ts.factory.updateInterfaceDeclaration(s, modifiers, s.name, s.typeParameters, s.heritageClauses, s.members);
} else if (ts.isEnumDeclaration(s)) {
return ts.factory.updateEnumDeclaration(s, s.decorators, modifiers, s.name, s.members);
return ts.factory.updateEnumDeclaration(s, modifiers, s.name, s.members);
} else if (ts.isClassDeclaration(s)) {
return ts.factory.updateClassDeclaration(s, s.decorators, modifiers, s.name, s.typeParameters, s.heritageClauses, s.members);
return ts.factory.updateClassDeclaration(s, modifiers, s.name, s.typeParameters, s.heritageClauses, s.members);
} else if (ts.isFunctionDeclaration(s)) {
return ts.factory.updateFunctionDeclaration(s, s.decorators, modifiers, s.asteriskToken, s.name, s.typeParameters, s.parameters, s.type, s.body);
return ts.factory.updateFunctionDeclaration(s, modifiers, s.asteriskToken, s.name, s.typeParameters, s.parameters, s.type, s.body);
}
return s;
}
Expand All @@ -242,15 +242,15 @@ function removeDeclareModifier(s) {
if (ts.isVariableStatement(s)) {
return ts.factory.updateVariableStatement(s, modifiers, s.declarationList);
} else if (ts.isTypeAliasDeclaration(s)) {
return ts.factory.updateTypeAliasDeclaration(s, s.decorators, modifiers, s.name, s.typeParameters, s.type);
return ts.factory.updateTypeAliasDeclaration(s, modifiers, s.name, s.typeParameters, s.type);
} else if (ts.isInterfaceDeclaration(s)) {
return ts.factory.updateInterfaceDeclaration(s, s.decorators, modifiers, s.name, s.typeParameters, s.heritageClauses, s.members);
return ts.factory.updateInterfaceDeclaration(s, modifiers, s.name, s.typeParameters, s.heritageClauses, s.members);
} else if (ts.isEnumDeclaration(s)) {
return ts.factory.updateEnumDeclaration(s, s.decorators, modifiers, s.name, s.members);
return ts.factory.updateEnumDeclaration(s, modifiers, s.name, s.members);
} else if (ts.isClassDeclaration(s)) {
return ts.factory.updateClassDeclaration(s, s.decorators, modifiers, s.name, s.typeParameters, s.heritageClauses, s.members);
return ts.factory.updateClassDeclaration(s, modifiers, s.name, s.typeParameters, s.heritageClauses, s.members);
} else if (ts.isFunctionDeclaration(s)) {
return ts.factory.updateFunctionDeclaration(s, s.decorators, modifiers, s.asteriskToken, s.name, s.typeParameters, s.parameters, s.type, s.body);
return ts.factory.updateFunctionDeclaration(s, modifiers, s.asteriskToken, s.name, s.typeParameters, s.parameters, s.type, s.body);
}
return s;
}
Expand Down Expand Up @@ -398,7 +398,7 @@ function transformScriptBeforeCompilation(source, isGlobal) {
? // If there is a top-level await, wrap all non-hoisted statements in (async () => { ... })();
[
ts.factory.createExpressionStatement(
ts.createCall(
ts.factory.createCallExpression(
ts.factory.createArrowFunction(
[
ts.factory.createModifier(
Expand Down Expand Up @@ -457,8 +457,8 @@ function transformGlobalDeclarations(decl) {
* @type {import("typescript").TransformerFactory<import("typescript").SourceFile>}
*/
// eslint-disable-next-line no-unused-vars
const transformer = (context) => {
return (sourceFile) =>
const transformer = context => {
return sourceFile =>
ts.visitNode(sourceFile, (node) => {
if (ts.isSourceFile(node)) {
// All non-export-statements stay at the root level, the rest is wrapped in `declare global`
Expand Down
37 changes: 19 additions & 18 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
/* jshint shadow: true */
'use strict';

const vm = require('vm');
const nodeFS = require('fs');
const nodePath = require('path');
const vm = require('node:vm');
const nodeFS = require('node:fs');
const nodePath = require('node:path');
const CoffeeScript = require('coffeescript');
const tsc = require('virtual-tsc');
const Mirror = require('./lib/mirror');
Expand All @@ -26,21 +26,21 @@ const { astroList } = require('./lib/consts');

const mods = {
fs: {},
dgram: require('dgram'),
crypto: require('crypto'),
dns: require('dns'),
events: require('events'),
http: require('http'),
https: require('https'),
http2: require('http2'),
net: require('net'),
os: require('os'),
path: require('path'),
util: require('util'),
child_process: require('child_process'),
stream: require('stream'),
url: require('url'),
zlib: require('zlib'),
dgram: require('node:dgram'),
crypto: require('node:crypto'),
dns: require('node:dns'),
events: require('node:events'),
http: require('node:http'),
https: require('node:https'),
http2: require('node:http2'),
net: require('node:net'),
os: require('node:os'),
path: require('node:path'),
util: require('node:util'),
child_process: require('node:child_process'),
stream: require('node:stream'),
url: require('node:url'),
zlib: require('node:zlib'),
suncalc: require('suncalc2'),
request: require('./lib/request'),
wake_on_lan: require('wake_on_lan'),
Expand Down Expand Up @@ -2183,6 +2183,7 @@ async function getData(callback) {
await adapter.subscribeForeignStatesAsync('*');
} else {
await adapter.subscribeStatesAsync('debug.to');
await adapter.subscribeStatesAsync('scriptEnabled.*');
}

adapter.log.info('requesting all states');
Expand Down
17 changes: 0 additions & 17 deletions src/public/google-blockly/msg/js/de.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/* eslint-disable */
;(function(root, factory) {
if (typeof define === 'function' && define.amd) { // AMD
define([], factory);
} else if (typeof exports === 'object') { // Node.js
module.exports = factory();
} else { // Browser
var messages = factory();
for (var key in messages) {
root.Blockly.Msg[key] = messages[key];
}
}
}(this, function() {
// This file was automatically generated. Do not modify.

'use strict';

var Blockly = Blockly || { Msg: Object.create(null) };
Expand Down Expand Up @@ -436,5 +421,3 @@ Blockly.Msg["TEXTS_HUE"] = "160";
Blockly.Msg["PROCEDURES_HUE"] = "290";
Blockly.Msg["COLOUR_HUE"] = "20";
Blockly.Msg["VARIABLES_DYNAMIC_HUE"] = "310";
return Blockly.Msg;
}));
17 changes: 0 additions & 17 deletions src/public/google-blockly/msg/js/en.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/* eslint-disable */
;(function(root, factory) {
if (typeof define === 'function' && define.amd) { // AMD
define([], factory);
} else if (typeof exports === 'object') { // Node.js
module.exports = factory();
} else { // Browser
var messages = factory();
for (var key in messages) {
root.Blockly.Msg[key] = messages[key];
}
}
}(this, function() {
// This file was automatically generated. Do not modify.

'use strict';

var Blockly = Blockly || { Msg: Object.create(null) };
Expand Down Expand Up @@ -436,5 +421,3 @@ Blockly.Msg["TEXTS_HUE"] = "160";
Blockly.Msg["PROCEDURES_HUE"] = "290";
Blockly.Msg["COLOUR_HUE"] = "20";
Blockly.Msg["VARIABLES_DYNAMIC_HUE"] = "310";
return Blockly.Msg;
}));
17 changes: 0 additions & 17 deletions src/public/google-blockly/msg/js/es.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/* eslint-disable */
;(function(root, factory) {
if (typeof define === 'function' && define.amd) { // AMD
define([], factory);
} else if (typeof exports === 'object') { // Node.js
module.exports = factory();
} else { // Browser
var messages = factory();
for (var key in messages) {
root.Blockly.Msg[key] = messages[key];
}
}
}(this, function() {
// This file was automatically generated. Do not modify.

'use strict';

var Blockly = Blockly || { Msg: Object.create(null) };
Expand Down Expand Up @@ -436,5 +421,3 @@ Blockly.Msg["TEXTS_HUE"] = "160";
Blockly.Msg["PROCEDURES_HUE"] = "290";
Blockly.Msg["COLOUR_HUE"] = "20";
Blockly.Msg["VARIABLES_DYNAMIC_HUE"] = "310";
return Blockly.Msg;
}));
17 changes: 0 additions & 17 deletions src/public/google-blockly/msg/js/fr.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/* eslint-disable */
;(function(root, factory) {
if (typeof define === 'function' && define.amd) { // AMD
define([], factory);
} else if (typeof exports === 'object') { // Node.js
module.exports = factory();
} else { // Browser
var messages = factory();
for (var key in messages) {
root.Blockly.Msg[key] = messages[key];
}
}
}(this, function() {
// This file was automatically generated. Do not modify.

'use strict';

var Blockly = Blockly || { Msg: Object.create(null) };
Expand Down Expand Up @@ -436,5 +421,3 @@ Blockly.Msg["TEXTS_HUE"] = "160";
Blockly.Msg["PROCEDURES_HUE"] = "290";
Blockly.Msg["COLOUR_HUE"] = "20";
Blockly.Msg["VARIABLES_DYNAMIC_HUE"] = "310";
return Blockly.Msg;
}));
17 changes: 0 additions & 17 deletions src/public/google-blockly/msg/js/it.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/* eslint-disable */
;(function(root, factory) {
if (typeof define === 'function' && define.amd) { // AMD
define([], factory);
} else if (typeof exports === 'object') { // Node.js
module.exports = factory();
} else { // Browser
var messages = factory();
for (var key in messages) {
root.Blockly.Msg[key] = messages[key];
}
}
}(this, function() {
// This file was automatically generated. Do not modify.

'use strict';

var Blockly = Blockly || { Msg: Object.create(null) };
Expand Down Expand Up @@ -436,5 +421,3 @@ Blockly.Msg["TEXTS_HUE"] = "160";
Blockly.Msg["PROCEDURES_HUE"] = "290";
Blockly.Msg["COLOUR_HUE"] = "20";
Blockly.Msg["VARIABLES_DYNAMIC_HUE"] = "310";
return Blockly.Msg;
}));
17 changes: 0 additions & 17 deletions src/public/google-blockly/msg/js/nl.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/* eslint-disable */
;(function(root, factory) {
if (typeof define === 'function' && define.amd) { // AMD
define([], factory);
} else if (typeof exports === 'object') { // Node.js
module.exports = factory();
} else { // Browser
var messages = factory();
for (var key in messages) {
root.Blockly.Msg[key] = messages[key];
}
}
}(this, function() {
// This file was automatically generated. Do not modify.

'use strict';

var Blockly = Blockly || { Msg: Object.create(null) };
Expand Down Expand Up @@ -436,5 +421,3 @@ Blockly.Msg["TEXTS_HUE"] = "160";
Blockly.Msg["PROCEDURES_HUE"] = "290";
Blockly.Msg["COLOUR_HUE"] = "20";
Blockly.Msg["VARIABLES_DYNAMIC_HUE"] = "310";
return Blockly.Msg;
}));
17 changes: 0 additions & 17 deletions src/public/google-blockly/msg/js/pl.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/* eslint-disable */
;(function(root, factory) {
if (typeof define === 'function' && define.amd) { // AMD
define([], factory);
} else if (typeof exports === 'object') { // Node.js
module.exports = factory();
} else { // Browser
var messages = factory();
for (var key in messages) {
root.Blockly.Msg[key] = messages[key];
}
}
}(this, function() {
// This file was automatically generated. Do not modify.

'use strict';

var Blockly = Blockly || { Msg: Object.create(null) };
Expand Down Expand Up @@ -436,5 +421,3 @@ Blockly.Msg["TEXTS_HUE"] = "160";
Blockly.Msg["PROCEDURES_HUE"] = "290";
Blockly.Msg["COLOUR_HUE"] = "20";
Blockly.Msg["VARIABLES_DYNAMIC_HUE"] = "310";
return Blockly.Msg;
}));
17 changes: 0 additions & 17 deletions src/public/google-blockly/msg/js/pt.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/* eslint-disable */
;(function(root, factory) {
if (typeof define === 'function' && define.amd) { // AMD
define([], factory);
} else if (typeof exports === 'object') { // Node.js
module.exports = factory();
} else { // Browser
var messages = factory();
for (var key in messages) {
root.Blockly.Msg[key] = messages[key];
}
}
}(this, function() {
// This file was automatically generated. Do not modify.

'use strict';

var Blockly = Blockly || { Msg: Object.create(null) };
Expand Down Expand Up @@ -436,5 +421,3 @@ Blockly.Msg["TEXTS_HUE"] = "160";
Blockly.Msg["PROCEDURES_HUE"] = "290";
Blockly.Msg["COLOUR_HUE"] = "20";
Blockly.Msg["VARIABLES_DYNAMIC_HUE"] = "310";
return Blockly.Msg;
}));
Loading

0 comments on commit 2fa88e3

Please sign in to comment.