Skip to content
Draft
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
2 changes: 1 addition & 1 deletion declaration/openspace-api-js.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ declare module 'openspace-api-js' {
* @param property The URI of the property to set.
* @param value - The value to set the property to.
*/
setProperty(property: string, value: unknown): void;
setProperty(property: string, value: unknown): Promise<void>;

/**
* Get a property
Expand Down
2 changes: 1 addition & 1 deletion script/openspace-api-static.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ declare module 'openspace-api-js' {
* @param property The URI of the property to set.
* @param value - The value to set the property to.
*/
setProperty(property: string, value: unknown): void;
setProperty(property: string, value: unknown): Promise<void>;

/**
* Get a property
Expand Down
36 changes: 22 additions & 14 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class OpenSpaceApi {
*/
startTopic(type, payload) {
if (typeof type !== 'string') {
throw("Topic type must be a string")
throw "Topic type must be a string";
}

const topic = this._nextTopicId++;
Expand Down Expand Up @@ -132,25 +132,33 @@ class OpenSpaceApi {
* @param {string} property - The URI of the property to set.
* @param {*} value - The value to set the property to.
*/
setProperty(property, value) {
async setProperty(property, value) {
if (typeof property !== 'string') {
throw("Property must be a valid uri string")
throw "Property must be a valid uri string";
}
const topic = this.startTopic('set', {
property,
value
});
topic.cancel();
property,
value,
});
try {
const response = await topic.iterator().next();
topic.cancel();
return response.value;
} catch (e) {
throw "Error setting property. \n" + e;
}
}



/**
* Get a property
* @param {string} property - The URI of the property to set.
* @return {*} The value of the property.
*/
async getProperty(property) {
if (typeof property !== 'string') {
throw("Property must be a valid uri string")
throw "Property must be a valid uri string";
}
const topic = this.startTopic('get', {
property,
Expand Down Expand Up @@ -193,7 +201,7 @@ class OpenSpaceApi {
*/
subscribeToProperty(property) {
if (typeof property !== 'string') {
throw("Property must be a valid uri string")
throw "Property must be a valid uri string";
}
const topic = this.startTopic('subscribe', {
event: 'start_subscription',
Expand Down Expand Up @@ -221,7 +229,7 @@ class OpenSpaceApi {
*/
async executeLuaScript(script, getReturnValue = true, shouldBeSynchronized = true) {
if (typeof script !== 'string') {
throw("Script must be a string")
throw "Script must be a string";
}
const topic = this.startTopic('luascript', {
script,
Expand Down Expand Up @@ -250,7 +258,7 @@ class OpenSpaceApi {
*/
async executeLuaFunction(fun, args, getReturnValue = true) {
if (typeof fun !== 'string') {
throw("Function must be a string")
throw "Function must be a string";
}
const topic = this.startTopic('luascript', {
function: fun,
Expand All @@ -264,7 +272,7 @@ class OpenSpaceApi {
topic.cancel();
return response.value;
} catch (e) {
throw "Error executing lua function: \n" + e
throw "Error executing lua function: \n" + e;
}
} else {
topic.cancel();
Expand All @@ -287,7 +295,7 @@ class OpenSpaceApi {
try {
return await this.executeLuaFunction(functionName, args);
} catch (e) {
throw "Lua execution error: \n" + e
throw "Lua execution error: \n" + e;
}
}
};
Expand All @@ -301,7 +309,7 @@ class OpenSpaceApi {
}
return null;
} catch (e) {
throw "Lua execution error: \n" + e
throw "Lua execution error: \n" + e;
}
}
};
Expand Down