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

Use newer ES6 javascript and interop calls. #37

Merged
merged 1 commit into from
Aug 16, 2024
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
213 changes: 105 additions & 108 deletions js/Simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,119 +2,116 @@
* @class Simple Object
* @brief
*/
import {EventEmitter} from "events";

import {interop} from "../direct/Host";

// Node JS support
var EventEmitter;
if (typeof (require) !== "undefined") {
if (typeof (EventEmitter) == "undefined") {
EventEmitter = require("events");
class Simple extends EventEmitter {
constructor(instanceId) {
super();
this.instanceId = instanceId;
this.refCount = 1;
}
}

function Simple(instanceId) {
this.instanceId = instanceId;
}

Simple.prototype = Object.create(EventEmitter.prototype, {
constructor: {value: Simple, enumerable: false, writable: true, configurable: true}
});

Simple.prototype.release = function() {
this.emit("finalize");
this.releaseInstance();
};
Simple.prototype.invoke = function(methodBinding) {
return interop.invoke(this.instanceId, methodBinding);
};
Simple.prototype.releaseInstance = function() {
interop.releaseInstance(this.instanceId);
};
addRef() {
this.refCount++;
}
release() {
if (--this.refCount === 0) {
this.emit("release");

/**
* Gets an integer value
* @type int
*/
Simple.prototype.getIntProperty = function() {
return this.invoke({
"method": "getIntProperty"
});
};
/**
* Sets an integer value
*/
Simple.prototype.setIntProperty = function(value) {
return this.invoke({
"method": "setIntProperty",
"value": value
});
};
/**
* Gets a double value
* @type float
*/
Simple.prototype.getFloatProperty = function() {
return this.invoke({
"method": "getFloatProperty"
});
};
/**
* Sets a double value
*/
Simple.prototype.setFloatProperty = function(value) {
return this.invoke({
"method": "setFloatProperty",
"value": value
});
};
/**
* Gets a boolean value
* @type bool
*/
Simple.prototype.getBooleanProperty = function() {
return this.invoke({
"method": "getBooleanProperty"
});
};
/**
* Sets a boolean value
*/
Simple.prototype.setBooleanProperty = function(value) {
return this.invoke({
"method": "setBooleanProperty",
"value": value
});
};
/**
* Gets a string value
* @type string
*/
Simple.prototype.getStringProperty = function() {
return this.invoke({
"method": "getStringProperty"
});
};
/**
* Sets a string value
*/
Simple.prototype.setStringProperty = function(value) {
return this.invoke({
"method": "setStringProperty",
"value": value
});
};
/**
* Starts a request for a value
*/
Simple.prototype.startValueRequest = function() {
return this.invoke({
"method": "startValueRequest"
});
};
return interop.releaseInstance(this.instanceId);
}
}
releaseAsync() {
if (--this.refCount === 0) {
this.emit("release");

var createSimple = function(instanceId) {
return interop.createInstance("SSN.Simple", Simple, instanceId);
};
return interop.releaseInstanceAsync(this.instanceId);
}
return Promise.resolve();
}
invoke(method, methodArgs) {
return interop.invoke(this.instanceId, method, methodArgs);
}
invokeAsync(method, methodArgs) {
if (this.refCount === 0) throw Error("Unable to call 'invokeAsync' of null reference");
const self = this, releaseRef = () => self.release();
this.addRef();
return interop.invokeAsync(this.instanceId, method, methodArgs).then(releaseRef, releaseRef);
}
invokeMemoize(method, methodArgs) {
return interop.invokeMemoize(this.instanceId, method, methodArgs);
}
invokeMemoizeAsync(method, methodArgs) {
if (this.refCount === 0) throw Error("Unable to call 'invokeMemoizeAsync' of null reference");
const self = this, releaseRef = () => self.release();
this.addRef();
return interop.invokeMemoizeAsync(this.instanceId, method, methodArgs).then(releaseRef, releaseRef);
}
/**
* Gets an integer value
* @type int
*/
getIntProperty() {
return this.invoke("getIntProperty");
}
/**
* Sets an integer value
*/
setIntProperty(value) {
return this.invoke("setIntProperty", {value});
}
/**
* Gets a double value
* @type float
*/
getFloatProperty() {
return this.invoke("getFloatProperty");
}
/**
* Sets a double value
*/
setFloatProperty(value) {
return this.invoke("setFloatProperty", {value});
}
/**
* Gets a boolean value
* @type bool
*/
getBooleanProperty() {
return this.invoke("getBooleanProperty");
}
/**
* Sets a boolean value
*/
setBooleanProperty(value) {
return this.invoke("setBooleanProperty", {value});
}
/**
* Gets a string value
* @type string
*/
getStringProperty() {
return this.invoke("getStringProperty");
}
/**
* Sets a string value
*/
setStringProperty(value) {
return this.invoke("setStringProperty", {value});
}
/**
* Starts a request for a value
*/
startValueRequest() {
return this.invoke("startValueRequest");
}
}

export {createSimple};
export const createSimple = (instanceId) =>
interop.createInstance("SSN.Simple", Simple, instanceId);
export const createAnalyticsAsync = (instanceId) =>
interop.createInstanceAsync("SSN.Simple", Simple, instanceId);
export const createSimpleCopy = (obj) =>
new Simple(interop.getInstanceId(obj));
16 changes: 8 additions & 8 deletions js/SimpleExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {createSimple} from "./Simple";
var simple = null;
var observer = null;

function interopLoaded() {
const interopLoaded = () => {
//notificationCenter.verbose = true;
//interop.verbose = true;

simple = createSimple();

// Watch for changes in object
observer = notificationCenter.addInstanceObserver("Simple", "Changed", simple, function (sender, info) {
observer = notificationCenter.addInstanceObserver("Simple", "Changed", simple, (sender, info) => {
console.log("Simple Changed - {0} -> {1}".format(info.oldValue, info.newValue));
});

Expand All @@ -37,7 +37,7 @@ function interopLoaded() {
// and send a response through the notification center

// Listen for a request for a value
observer = notificationCenter.addInstanceObserver("Simple", "ValueRequest", simple, function (sender, info) {
observer = notificationCenter.addInstanceObserver("Simple", "ValueRequest", simple, (sender, info) => {
var response = {"String": "Item"};
console.log("Simple Value Request/Response - {0} -> {1}"
.format(JSON.stringify(info), JSON.stringify(response)));
Expand All @@ -50,9 +50,9 @@ function interopLoaded() {

// Trigger a request for a value, ideally would be called from C
simple.startValueRequest();
}
};

function interopUnloaded() {
const interopUnloaded = () => {
// Release our notification center instance observer
if (observer) {
observer.release();
Expand All @@ -63,9 +63,9 @@ function interopUnloaded() {
simple.release();
simple = null;
}
}
};

interop.on("load", function({name, successful}) {
interop.on("load", ({name, successful}) => {
if (name === "simple") {
if (successful) {
console.log("Simple interop loaded successfully");
Expand All @@ -76,7 +76,7 @@ interop.on("load", function({name, successful}) {
}
});

interop.on("unload", function({name}) {
interop.on("unload", ({name}) => {
if (name === "simple") {
interopUnloaded();
}
Expand Down