11import { template } from "uix/html/template.ts" ;
22import { Component } from "uix/components/Component.ts" ;
3- import { spawnThreads , spawnThread } from "unyt_core/threads/threads.ts" ;
4- import { always , map } from "unyt_core/functions.ts" ;
3+ import { spawnThreads , spawnThread , run } from "datex-core-legacy/threads/threads.ts" ;
54import type { AddressData } from "common/TOR-Worker.ts" ;
65
76@template ( function ( this : MainPage ) {
8- return < div >
9- < div id = "tor" class = { always ( ( ) => this . calculatingAddress ?'hidden' :'' ) } >
7+ return < >
8+ < div id = "run" class = { this . calculatingPI ? 'hidden' : '' } >
9+ < h2 > Use Console</ h2 >
10+ < div onclick = { ( ) => this . runConsole ( ) } class = "button" >
11+ Run in Thread
12+ </ div >
13+ </ div >
14+ < div id = "pi" class = { this . calculatingPI ? 'hidden' : '' } >
15+ < h2 > Calculate PI</ h2 >
16+ < input id = "inputPiDigits"
17+ type = "number"
18+ placeholder = "Number of digits"
19+ value = { this . piDigits } />
20+ < div onclick = { ( ) => this . computePI ( ) } class = "button" > {
21+ this . calculatingPI ? "Waiting" : "Calculate"
22+ } </ div >
23+ < section class = "results" >
24+ { this . resultPIs . map ( pi =>
25+ < span > { pi } </ span >
26+ ) }
27+ </ section >
28+ </ div >
29+ < div id = "tor" class = { this . calculatingAddress ? 'hidden' : '' } >
1030 < h2 > Create TOR Address</ h2 >
11- < input id = "torAddress" maxlength = "3" type = "text" placeholder = "Prefix of vanity address" value = { this . $ . addressPrefix } />
12- < div onclick = { ( ) => this . createVanityAddress ( ) } class = "button" > Compute</ div >
31+ < input id = "torAddress"
32+ maxlength = "3"
33+ type = "text"
34+ placeholder = "Prefix of vanity address"
35+ value = { this . addressPrefix } />
36+ < div onclick = { ( ) => this . createVanityAddress ( ) } class = "button" > {
37+ this . calculatingAddress ? "Waiting" : "Calculate"
38+ } </ div >
1339 < section class = "results" >
14- { map ( this . resultAddresses , ( address : AddressData ) =>
40+ { this . resultAddresses . map ( address =>
1541 < span >
1642 < a > { address . address } </ a >
1743 < b > Pub: { address . public . b64 } </ b >
@@ -20,44 +46,26 @@ import type { AddressData } from "common/TOR-Worker.ts";
2046 ) }
2147 </ section >
2248 </ div >
23- < div id = "pi" class = { always ( ( ) => this . calculatingPI ?'hidden' :'' ) } >
24- < h2 > Calculate PI</ h2 >
25- < input id = "inputPiDigits" type = "number" placeholder = "Number of digits" value = { this . $ . piDigits } />
26- < div onclick = { ( ) => this . computePI ( ) } class = "button" > Compute</ div >
27- < section class = "results" >
28- { map ( this . resultPIs , ( pi : string ) =>
29- < span > { pi } </ span >
30- ) }
31- </ section >
32- </ div >
33- </ div >
49+ </ >
3450} )
3551export class MainPage extends Component {
36-
3752 // reference properties for input values
3853 @property piDigits = 5 ;
39- @property addressPrefix = "" ;
54+ @property addressPrefix = '' ;
4055
4156 // reference properties for calculation state
4257 @property calculatingPI = false
4358 @property calculatingAddress = false
4459
4560 // arrays containing history of calculated results
46- @property resultPIs :string [ ] = [ ]
47- @property resultAddresses :AddressData [ ] = [ ]
48-
49- async createVanityAddress ( ) {
50- this . calculatingAddress = true ;
61+ @property resultPIs : string [ ] = [ ]
62+ @property resultAddresses : AddressData [ ] = [ ]
5163
52- // spawn 10 new threads
53- using threads = await spawnThreads < typeof import ( '../TOR-Worker.ts' ) > ( '../TOR-Worker.ts' , 10 ) ;
54- // try to find an address in parallel
55- const address = await Promise . any (
56- threads . map ( thread => thread . generateVanityAddress ( this . addressPrefix ) )
57- ) ;
58- this . resultAddresses . unshift ( address ) ;
59-
60- this . calculatingAddress = false ;
64+ runConsole ( ) {
65+ run ( ( ) => {
66+ console . log ( "Hello, main thread!" ) ;
67+ globalThis . console . log ( "Hello, worker!" ) ;
68+ } ) ;
6169 }
6270
6371 async computePI ( ) {
@@ -71,4 +79,18 @@ export class MainPage extends Component {
7179
7280 this . calculatingPI = false ;
7381 }
82+
83+ async createVanityAddress ( ) {
84+ this . calculatingAddress = true ;
85+
86+ // spawn 10 new threads
87+ using threads = await spawnThreads < typeof import ( '../TOR-Worker.ts' ) > ( '../TOR-Worker.ts' , 10 ) ;
88+ // try to find an address in parallel
89+ const address = await Promise . any (
90+ threads . map ( thread => thread . generateVanityAddress ( this . addressPrefix ) )
91+ ) ;
92+ this . resultAddresses . unshift ( address ) ;
93+
94+ this . calculatingAddress = false ;
95+ }
7496}
0 commit comments