File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { executeExample } from '../../test-utils/run_test.ts'
2+ import { assertEquals } from "jsr:@std/assert@^1"
3+
4+ Deno . test ( "background_tasks" , async ( ) => {
5+ const result = await executeExample ( "background_tasks" )
6+ assertEquals ( result , [
7+ `Task [rs]: Started` ,
8+ `Task [js]: Message` ,
9+ `Task [rs]: Ended` ,
10+ ] . join ( '\n' ) )
11+ } ) ;
Original file line number Diff line number Diff line change @@ -25,34 +25,34 @@ pub fn main() -> anyhow::Result<()> {
2525 let ctx = worker. create_context ( ) ?;
2626
2727 // Execute some JavaScript in the context
28- ctx. exec ( |env| {
28+ ctx. exec_blocking ( |env| {
29+ // Tell the runtime that it must remain alive
30+ // Safety: This must be manually incremented and decremented
31+ // if the ref count is greater than 0, the runtime
32+ // will remain alive until it's 0
2933 env. inc_ref ( ) ;
3034
3135 env. spawn_background ( {
3236 let env = env. as_async ( ) ;
3337 async move {
34- println ! ( "Background Task Started" ) ;
38+ println ! ( "Task [rs]: Started" ) ;
3539 tokio:: time:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
36- println ! ( "Background Task Ended" ) ;
3740
3841 env. exec_async ( |env| {
39- println ! ( "hi" ) ;
42+ println ! ( "Task [js]: Message" ) ;
43+ // Tell the runtime that this task will no longer require keeping it alive
4044 env. dec_ref ( ) ;
4145 Ok ( ( ) )
4246 } )
4347 . await ?;
48+
49+ println ! ( "Task [rs]: Ended" ) ;
4450 Ok ( ( ) )
4551 }
4652 } ) ?;
4753
4854 Ok ( ( ) )
4955 } ) ?;
5056
51- println ! ( "Context Dropping" ) ;
52- drop ( ctx) ;
53- drop ( worker) ;
54-
55- println ! ( "Context Dropped" ) ;
56-
5757 Ok ( ( ) )
5858}
Original file line number Diff line number Diff line change 1+ import { executeExample } from '../../test-utils/run_test.ts'
2+ import { assertEquals } from "jsr:@std/assert@^1"
3+
4+ Deno . test ( "external_value" , async ( ) => {
5+ const result = await executeExample ( "external_value" )
6+ assertEquals ( result , "42" )
7+ } ) ;
Original file line number Diff line number Diff line change 1+ use ion:: * ;
2+
3+ pub fn main ( ) -> anyhow:: Result < ( ) > {
4+ let runtime = JsRuntime :: initialize_once ( JsRuntimeOptions {
5+ extensions : vec ! [
6+ ion:: extensions:: console( ) ,
7+ ion:: extensions:: set_interval( ) ,
8+ ion:: extensions:: set_timeout( ) ,
9+ ] ,
10+ transformers : vec ! [
11+ ion:: transformers:: json( ) ,
12+ ion:: transformers:: ts( ) ,
13+ ion:: transformers:: tsx( ) ,
14+ ] ,
15+ ..Default :: default ( )
16+ } ) ?;
17+
18+ let worker = runtime. spawn_worker ( JsWorkerOptions :: default ( ) ) ?;
19+ let ctx = worker. create_context ( ) ?;
20+
21+ // Set External Value
22+ ctx. exec_blocking ( |env| {
23+ let external = 42 ;
24+ let external_js = JsExternal :: new ( env, external) ?;
25+ env. global_this ( ) ?
26+ . set_named_property ( "external" , external_js) ?;
27+ Ok ( ( ) )
28+ } ) ?;
29+
30+ // Use External Value
31+ ctx. exec_blocking ( |env| {
32+ let Some ( external_js) = env
33+ . global_this ( ) ?
34+ . get_named_property :: < JsExternal < i32 > > ( "external" ) ?
35+ else {
36+ panic ! ( "Could not revive external" )
37+ } ;
38+ let external = external_js. as_inner ( ) ?;
39+ println ! ( "{}" , external) ;
40+ Ok ( ( ) )
41+ } ) ?;
42+
43+ Ok ( ( ) )
44+ }
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ mod custom_extension;
77mod custom_resolver;
88mod deferred;
99mod eval;
10+ mod external_value;
1011mod http_server;
1112mod memory_usage_context;
1213mod memory_usage_module;
@@ -53,6 +54,7 @@ fn main() -> anyhow::Result<()> {
5354 "memory_usage_worker" => memory_usage_worker:: main ( ) ,
5455 "memory_usage_module" => memory_usage_module:: main ( ) ,
5556 "memory_usage_value" => memory_usage_value:: main ( ) ,
57+ "external_value" => external_value:: main ( ) ,
5658 _ => Err ( anyhow:: anyhow!( "No example for: \" {}\" " , example) ) ,
5759 }
5860}
You can’t perform that action at this time.
0 commit comments