Skip to content
Open
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
19 changes: 15 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,6 @@ client.find(path)

Promise containing requested INode object when fulfilled.

- Restriction

The requested node must reside in the application client was connected to.

- Usage

The provided path must contain dot separated path to target node. **Root node is not considered part of the path.**
Expand Down Expand Up @@ -357,6 +353,21 @@ node.info()
| | | - eNodeIsImportant |
+------------------+------------------------------+---------------------------------------------------------------+

node.requestValue()
^^^^^^^^^^^^^^^^^^^

- Returns

Promise containing fetched value when fulfilled.

- Example

.. code:: javascript

client.requestValue().then(function (value) {

});

node.lastValue()
^^^^^^^^^^^^^^^^

Expand Down
52 changes: 46 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ studio.protocol = (function(ProtoBuf) {
case obj.CDPValueType.eSTRING:
return variantValue.str_value;
default:
return 0;
return undefined;
}
};

Expand Down Expand Up @@ -337,10 +337,11 @@ studio.internal = (function(proto) {
var childMap = new Map();
var givenPromises = new Map();
var childIterators = [];
var valuePromises = [];
var valueSubscriptions = [];
var structureSubscriptions = [];
var eventSubscriptions = [];
var lastValue;
var lastValue = null;
var lastInfo = null; //when we get this, if there are any child requests we need to fetch child fetch too
var valid = true;

Expand Down Expand Up @@ -386,7 +387,7 @@ studio.internal = (function(proto) {
};

this.lastValue = function() {
return lastValue;
return lastValue.value;
};

this.forEachChild = function(iteratorFunction) {
Expand All @@ -402,7 +403,12 @@ studio.internal = (function(proto) {
parent = nodeParent;
lastInfo = protoInfo;
id = protoInfo.node_id;
this.async._makeGetterRequest();
if (valueSubscriptions.length) {
this.async._makeGetterRequest();
}
if (valuePromises.length) {
app.makeGetterRequest(id, 0, 0, false);
}
for (var i = 0; i < eventSubscriptions.length; i++)
app.makeEventRequest(id, eventSubscriptions[i][1], false);
};
Expand Down Expand Up @@ -445,7 +451,11 @@ studio.internal = (function(proto) {
};

this.receiveValue = function (nodeValue, nodeTimestamp) {
lastValue = nodeValue;
lastValue = {value: nodeValue, time: nodeTimestamp};
for (var i = 0; i < valuePromises.length; i++) {
valuePromises[i](nodeValue);
valuePromises.splice(i, 1);
}
for (var i = 0; i < valueSubscriptions.length; i++) {
valueSubscriptions[i][0](nodeValue, nodeTimestamp);
}
Expand Down Expand Up @@ -490,6 +500,9 @@ studio.internal = (function(proto) {
};

this.async.subscribeToValues = function(valueConsumer, fs, sampleRate) {
if (lastValue) {
valueConsumer(lastValue.value, lastValue.time);
}
valueSubscriptions.push([valueConsumer, fs, sampleRate]);
this._makeGetterRequest();
};
Expand Down Expand Up @@ -534,6 +547,14 @@ studio.internal = (function(proto) {
//when offline must queue or update pending set request and call set callbacks ...???
};

this.async.requestValue = function() {
let promise = new Promise(function (resolve, reject) {
valuePromises.push(resolve);
});
app.makeGetterRequest(id, 0, 0, false);
return promise;
};

this.async._makeGetterRequest = function() {
if (valueSubscriptions.length > 0) {
var maxFs = Math.max.apply(Math, valueSubscriptions.map(v => v[1]));
Expand Down Expand Up @@ -658,6 +679,10 @@ studio.internal = (function(proto) {
}
};

this.async.requestValue = function() {

};

this.async.subscribeToValues = function(valueConsumer, fs=5, sampleRate=0) {

};
Expand Down Expand Up @@ -830,7 +855,9 @@ studio.internal = (function(proto) {
var msg = new proto.Container();
var request = new proto.ValueRequest();
request.node_id = id;
request.fs = fs;
if (fs) {
request.fs = fs;
}
if (sampleRate) {
request.sample_rate = sampleRate;
}
Expand Down Expand Up @@ -1107,6 +1134,7 @@ studio.api = (function(internal) {
this.info = function() {
return node.info();
};

/**
* Access the last known value.
*
Expand Down Expand Up @@ -1190,6 +1218,15 @@ studio.api = (function(internal) {
* @param {number} timestamp
*/

/**
* Fetch current value.
*
* @returns {Promise.<number>} A promise containing fetched value when fulfilled.
*/
this.requestValue = function() {
return node.async.requestValue();
};

/**
* Subscribe to value changes on this node.
*
Expand Down Expand Up @@ -1433,3 +1470,6 @@ studio.api = (function(internal) {
export default studio





2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cdp-client",
"version": "2.1.1",
"version": "2.2.1",
"description": "A simple Javascript interface for the CDP Studio development platform that allows Javascript applications to interact with",
"main": "index.js",
"scripts": {
Expand Down