@@ -64,6 +64,10 @@ class OpenSpaceApi {
6464 * @return {Topic } - An object representing the topic.
6565 */
6666 startTopic ( type , payload ) {
67+ if ( typeof type !== 'string' ) {
68+ throw ( "Topic type must be a string" )
69+ }
70+
6771 const topic = this . _nextTopicId ++ ;
6872 const messageObject = {
6973 topic,
@@ -129,7 +133,10 @@ class OpenSpaceApi {
129133 * @param {* } value - The value to set the property to.
130134 */
131135 setProperty ( property , value ) {
132- const topic = this . startTopic ( 'set' , {
136+ if ( typeof property !== 'string' ) {
137+ throw ( "Property must be a valid uri string" )
138+ }
139+ const topic = this . startTopic ( 'set' , {
133140 property,
134141 value
135142 } ) ;
@@ -142,6 +149,9 @@ class OpenSpaceApi {
142149 * @return {* } The value of the property.
143150 */
144151 async getProperty ( property ) {
152+ if ( typeof property !== 'string' ) {
153+ throw ( "Property must be a valid uri string" )
154+ }
145155 const topic = this . startTopic ( 'get' , {
146156 property,
147157 } ) ;
@@ -182,6 +192,9 @@ class OpenSpaceApi {
182192 * When cancelled, this object will unsubscribe to the property.
183193 */
184194 subscribeToProperty ( property ) {
195+ if ( typeof property !== 'string' ) {
196+ throw ( "Property must be a valid uri string" )
197+ }
185198 const topic = this . startTopic ( 'subscribe' , {
186199 event : 'start_subscription' ,
187200 property
@@ -202,10 +215,13 @@ class OpenSpaceApi {
202215 /**
203216 * Execute a lua script
204217 * @param {string } script - The lua script to execute.
205- * @param {string } getReturnValue - Specified whether the return value should be collected.
218+ * @param {bool } getReturnValue - Specified whether the return value should be collected.
206219 * @return {* } The return value of the script, if `getReturnValue` is true, otherwise undefined.
207220 */
208221 async executeLuaScript ( script , getReturnValue = true ) {
222+ if ( typeof script !== 'string' ) {
223+ throw ( "Script must be a string" )
224+ }
209225 const topic = this . startTopic ( 'luascript' , {
210226 script,
211227 return : getReturnValue
@@ -231,6 +247,9 @@ class OpenSpaceApi {
231247 * @return {* } The return value of the script, if `getReturnValue` is true, otherwise undefined.
232248 */
233249 async executeLuaFunction ( fun , args , getReturnValue = true ) {
250+ if ( typeof fun !== 'string' ) {
251+ throw ( "Function must be a string" )
252+ }
234253 const topic = this . startTopic ( 'luascript' , {
235254 function : fun ,
236255 arguments : args ,
@@ -252,10 +271,16 @@ class OpenSpaceApi {
252271
253272 /**
254273 * Get an object representing the OpenSpace lua library.
274+ * @param {bool } multiReturn - whether the library should return the raw lua tables.
275+ * If this value is true, the 1-indexed lua table will be returned as a JavaScript object.
276+ * if the value is false, the only the first return value will be returned
255277 * @return {Object } The lua library, mapped to async JavaScript functions.
256278 */
257- async library ( ) {
258- const generateAsyncFunction = ( functionName ) => {
279+ async library ( multiReturn ) {
280+ if ( multiReturn === undefined ) {
281+ multiReturn = true ;
282+ }
283+ const generateAsyncMultiRetFunction = functionName => {
259284 return async ( ...args ) => {
260285 try {
261286 return await this . executeLuaFunction ( functionName , args ) ;
@@ -265,6 +290,17 @@ class OpenSpaceApi {
265290 }
266291 } ;
267292
293+ const generateAsyncSingleRetFunction = functionName => {
294+ return async ( ...args ) => {
295+ try {
296+ const luaTable = await this . executeLuaFunction ( functionName , args ) ;
297+ return luaTable [ 1 ] ;
298+ } catch ( e ) {
299+ throw "Lua execution error: \n" + e
300+ }
301+ }
302+ } ;
303+
268304 let documentation ;
269305 try {
270306 documentation = await this . getDocumentation ( 'lua' ) ;
@@ -273,26 +309,47 @@ class OpenSpaceApi {
273309 }
274310 const jsLibrary = { } ;
275311
276- documentation . forEach ( ( lib ) => {
312+ documentation . forEach ( lib => {
277313 let subJsLibrary = undefined ;
278314 if ( lib . library === '' ) {
279315 subJsLibrary = jsLibrary ;
280316 } else {
281317 subJsLibrary = jsLibrary [ lib . library ] = { } ;
282318 }
283319
284- lib . functions . forEach ( ( f ) => {
320+ lib . functions . forEach ( f => {
285321 const fullFunctionName =
286322 'openspace.' +
287323 ( subJsLibrary === jsLibrary ? '' : ( lib . library + '.' ) ) +
288324 f . name ;
289325
290- subJsLibrary [ f . name ] = generateAsyncFunction ( fullFunctionName ) ;
326+ subJsLibrary [ f . name ] = multiReturn ?
327+ generateAsyncMultiRetFunction ( fullFunctionName ) :
328+ generateAsyncSingleRetFunction ( fullFunctionName ) ;
291329 } ) ;
292330 } ) ;
293331
294332 return jsLibrary ;
295333 }
334+
335+ /**
336+ * Get an object representing the OpenSpace lua library.
337+ * @return {Object } The lua library, mapped to async JavaScript functions.
338+ * This method only returns the first return value.
339+ */
340+ async singleReturnLibrary ( ) {
341+ return await this . library ( true ) ;
342+ }
343+
344+ /**
345+ * Get an object representing the OpenSpace lua library.
346+ * @return {Object } The lua library, mapped to async JavaScript functions.
347+ * The values returned by the async functions will be the entire lua tables,
348+ * with 1-indexed values.
349+ */
350+ async multiReturnLibrary ( ) {
351+ return await this . library ( false ) ;
352+ }
296353}
297354
298355module . exports = OpenSpaceApi ;
0 commit comments