Skip to content

Commit 793de50

Browse files
author
Gabriel Schulhof
committed
Add N-API thread-safe function round trip example
Closes: gabrielschulhof/abi-stable-node-addon-examples#1 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> PR-URL: #67
1 parent cf02f73 commit 793de50

4 files changed

Lines changed: 435 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'round_trip',
5+
'sources': [ 'round_trip.c' ]
6+
}
7+
]
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const bindings = require('bindings')('round_trip');
2+
3+
bindings.startThread((item, thePrime) => {
4+
console.log('The prime: ' + thePrime);
5+
6+
// Answer the call with a 90% probability of returning true somewhere between
7+
// 200 and 400 ms from now.
8+
setTimeout(() => {
9+
const theAnswer = (Math.random() > 0.1);
10+
console.log(thePrime + ': answering with ' + theAnswer);
11+
bindings.registerReturnValue(item, theAnswer);
12+
}, Math.random() * 200 + 200);
13+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "thread_safe_function_round_trip",
3+
"version": "0.0.0",
4+
"description": "Thread-safe Function Example With JavaScript Round Trip",
5+
"main": "index.js",
6+
"private": true,
7+
"dependencies": {
8+
"bindings": "~1.2.1"
9+
},
10+
"scripts": {
11+
"test": "node index.js"
12+
},
13+
"gypfile": true
14+
}

0 commit comments

Comments
 (0)