Skip to content

Commit 6734d0c

Browse files
committed
Add fetchValue method
1 parent 2d38255 commit 6734d0c

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

README.rst

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,6 @@ client.find(path)
253253

254254
Promise containing requested INode object when fulfilled.
255255

256-
- Restriction
257-
258-
The requested node must reside in the application client was connected to.
259-
260256
- Usage
261257

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

356+
node.requestValue()
357+
^^^^^^^^^^^^^^^^^^^
358+
359+
- Returns
360+
361+
Promise containing fetched value when fulfilled.
362+
363+
- Example
364+
365+
.. code:: javascript
366+
367+
client.requestValue().then(function (value) {
368+
369+
});
370+
360371
node.lastValue()
361372
^^^^^^^^^^^^^^^^
362373

index.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ studio.internal = (function(proto) {
337337
var childMap = new Map();
338338
var givenPromises = new Map();
339339
var childIterators = [];
340+
var valuePromises = [];
340341
var valueSubscriptions = [];
341342
var structureSubscriptions = [];
342343
var eventSubscriptions = [];
@@ -446,6 +447,10 @@ studio.internal = (function(proto) {
446447

447448
this.receiveValue = function (nodeValue, nodeTimestamp) {
448449
lastValue = nodeValue;
450+
for (var i = 0; i < valuePromises.length; i++) {
451+
valuePromises[i](lastValue);
452+
valuePromises.splice(i, 1);
453+
}
449454
for (var i = 0; i < valueSubscriptions.length; i++) {
450455
valueSubscriptions[i][0](nodeValue, nodeTimestamp);
451456
}
@@ -534,6 +539,14 @@ studio.internal = (function(proto) {
534539
//when offline must queue or update pending set request and call set callbacks ...???
535540
};
536541

542+
this.async.requestValue = function() {
543+
let promise = new Promise(function (resolve, reject) {
544+
valuePromises.push(resolve);
545+
});
546+
app.makeGetterRequest(id, 0, 0, false);
547+
return promise;
548+
};
549+
537550
this.async._makeGetterRequest = function() {
538551
if (valueSubscriptions.length > 0) {
539552
var maxFs = Math.max.apply(Math, valueSubscriptions.map(v => v[1]));
@@ -658,6 +671,10 @@ studio.internal = (function(proto) {
658671
}
659672
};
660673

674+
this.async.requestValue = function() {
675+
676+
};
677+
661678
this.async.subscribeToValues = function(valueConsumer, fs=5, sampleRate=0) {
662679

663680
};
@@ -830,7 +847,9 @@ studio.internal = (function(proto) {
830847
var msg = new proto.Container();
831848
var request = new proto.ValueRequest();
832849
request.node_id = id;
833-
request.fs = fs;
850+
if (fs) {
851+
request.fs = fs;
852+
}
834853
if (sampleRate) {
835854
request.sample_rate = sampleRate;
836855
}
@@ -1107,6 +1126,7 @@ studio.api = (function(internal) {
11071126
this.info = function() {
11081127
return node.info();
11091128
};
1129+
11101130
/**
11111131
* Access the last known value.
11121132
*
@@ -1190,6 +1210,15 @@ studio.api = (function(internal) {
11901210
* @param {number} timestamp
11911211
*/
11921212

1213+
/**
1214+
* Fetch current value.
1215+
*
1216+
* @returns {Promise.<number>} A promise containing fetched value when fulfilled.
1217+
*/
1218+
this.requestValue = function() {
1219+
return node.async.requestValue();
1220+
};
1221+
11931222
/**
11941223
* Subscribe to value changes on this node.
11951224
*
@@ -1433,3 +1462,4 @@ studio.api = (function(internal) {
14331462
export default studio
14341463

14351464

1465+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cdp-client",
3-
"version": "2.1.1",
3+
"version": "2.2.1",
44
"description": "A simple Javascript interface for the CDP Studio development platform that allows Javascript applications to interact with",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)