Skip to content

Commit 973c076

Browse files
committed
📌 Nodepp | Stable Release | V1.3.0 📌
1 parent 1a2eccb commit 973c076

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

examples/17-task-resolver.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <nodepp/nodepp.h>
2+
#include <nodepp/promise.h>
3+
4+
using namespace nodepp;
5+
6+
void onMain() {
7+
8+
ptr_t<uint> value = new uint(0);
9+
10+
process::resolve( coroutine::add( COROUTINE(){
11+
coBegin
12+
13+
coDelay( 1000 );
14+
*value = 10;
15+
16+
coFinish
17+
}))
18+
19+
.then([=]( null_t ){
20+
console::log( *value );
21+
})
22+
23+
.fail([=]( except_t err ){
24+
console::error( err.what() );
25+
});
26+
27+
console::log( "Hello!" );
28+
29+
}

include/nodepp/coroutine.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ namespace nodepp { class coroutine_t {
6060

6161
/*─······································································─*/
6262

63-
coEmit() const noexcept { return next(); }
63+
coEmit() const { return next(); }
6464

65-
int next() const noexcept {
66-
if( !obj->alive ){ return -1; }
65+
int next() const {
66+
if ( !obj->alive ){ return -1; }
6767
return obj->callback( obj->state, obj->time );
6868
}
6969

include/nodepp/promise.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,29 @@ namespace nodepp { template< class T, class V > class promise_t {
208208

209209
/*────────────────────────────────────────────────────────────────────────────*/
210210

211+
namespace nodepp { namespace promise {
212+
213+
template< class T, class... V >
214+
promise_t<null_t,except_t> resolve( T cb, const V&... args ) {
215+
return promise_t<null_t,except_t>([=]( res_t<null_t> res, rej_t<except_t> rej ){
216+
217+
process::add( coroutine::add( COROUTINE(){
218+
coBegin try {
219+
220+
while( cb( args... )>=0 ){ return 1; }
221+
res( nullptr );
222+
223+
} catch( except_t err ) {
224+
rej( err );
225+
} coFinish
226+
} ));
227+
228+
}); }
229+
230+
}}
231+
232+
/*────────────────────────────────────────────────────────────────────────────*/
233+
211234
namespace nodepp { namespace promise {
212235

213236
template< class V >

test/task.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <nodepp/nodepp.h>
2+
#include <nodepp/promise.h>
23
#include <nodepp/test.h>
34

45
using namespace nodepp;
@@ -48,6 +49,24 @@ namespace TEST { namespace TASK {
4849
} catch ( ... ) { TEST_FAIL(); }
4950
});
5051

52+
TEST_ADD( test, "TEST 3 | task resolver testing", [](){
53+
try { ptr_t<int> x = new int(0);
54+
ptr_t<int> y = new int(3);
55+
56+
promise::resolve( coroutine::add( COROUTINE(){
57+
coBegin
58+
59+
while( *y>0 ){ *x += 10; *y-=1; coNext; }
60+
61+
coFinish
62+
})).emit();
63+
64+
while( *y!=0 ){ process::next(); }
65+
if( *x != 30 ){ TEST_FAIL(); }
66+
TEST_DONE();
67+
} catch ( ... ) { TEST_FAIL(); }
68+
});
69+
5170
test.onClose.once([=](){
5271
console::log("\nRESULT | total:", *totl, "| passed:", *done, "| error:", *err, "| skipped:", *skp );
5372
});

0 commit comments

Comments
 (0)