1
- import { Path } from "uix/utils/path.ts" ;
2
1
import { UIX } from "uix" ;
3
- import { spawnThreads , spawnThread , disposeThread } from "unyt_core/threads/threads.ts" ;
2
+ import { spawnThreads , spawnThread } from "unyt_core/threads/threads.ts" ;
3
+ import { always , map } from "unyt_core/functions.ts" ;
4
+ import type { AddressData } from "common/TOR-Worker.ts" ;
4
5
5
6
@UIX . template ( function ( this : MainPage ) {
6
7
return < div >
7
- < div class = "tor" >
8
- < h2 > Multi-Threading TOR Address</ h2 >
9
- < input id = "torAddress" maxlength = { 3 } type = { "text" } placeholder = { "Prefix of vanity address" } />
8
+ < div id = "tor" class = { always ( ( ) => this . calculatingAddress ? 'hidden' : '' ) } >
9
+ < h2 > Create TOR Address</ h2 >
10
+ < input id = "torAddress" maxlength = "3" type = "text" placeholder = "Prefix of vanity address" value = { this . $ . addressPrefix } />
10
11
< div onclick = { ( ) => this . createVanityAddress ( ) } class = "button" > Compute</ div >
11
- < section class = "results" > </ section >
12
+ < section class = "results" >
13
+ { map ( this . resultAddresses , address =>
14
+ < span >
15
+ < a > { address . address } </ a >
16
+ < b > Pub: { address . public . b64 } </ b >
17
+ < b > Priv: { address . private . b64 } </ b >
18
+ </ span >
19
+ ) }
20
+ </ section >
12
21
</ div >
13
- < div class = "pi" >
14
- < h2 > How many digits of PI to calculate? </ h2 >
15
- < input id = "inputPiDigits" type = { "number" } placeholder = { "Input number" } />
22
+ < div id = "pi" class = { always ( ( ) => this . calculatingPI ? 'hidden' : '' ) } >
23
+ < h2 > Calculate PI </ h2 >
24
+ < input id = "inputPiDigits" type = "number" placeholder = "Number of digits" value = { this . $ . piDigits } />
16
25
< div onclick = { ( ) => this . computePI ( ) } class = "button" > Compute</ div >
17
- < section class = "results" > </ section >
26
+ < section class = "results" >
27
+ { map ( this . resultPIs , pi =>
28
+ < span > { pi } </ span >
29
+ ) }
30
+ </ section >
18
31
</ div >
19
32
</ div >
20
33
} )
21
34
export class MainPage extends UIX . BaseComponent {
22
- @id declare inputPiDigits : HTMLInputElement ;
23
- @id declare torAddress : HTMLInputElement ;
35
+
36
+ // references properties to read input values
37
+ @property piDigits = 5 ;
38
+ @property addressPrefix = "" ;
39
+
40
+ // reference properties to get calculation state
41
+ @property calculatingPI = false
42
+ @property calculatingAddress = false
43
+
44
+ // arrays containing history of calculated results
45
+ @property resultPIs :string [ ] = [ ]
46
+ @property resultAddresses :AddressData [ ] = [ ]
24
47
25
48
async createVanityAddress ( ) {
26
- const parent = this . torAddress . parentElement ! ;
27
- if ( parent . classList . contains ( "hidden" ) )
28
- return ;
29
- parent . classList . add ( "hidden" ) ;
30
-
31
- const threads = await spawnThreads < typeof import ( '../TOR-Worker.ts' ) > ( new Path ( '../TOR-Worker.ts' ) , 10 ) ;
32
- const calculations = threads . map ( thread => thread . generateVanityAddress ( this . torAddress . value ) ) ;
33
- const result = await Promise . any ( calculations ) ;
34
- console . log ( "Found address" , result ) ;
35
- parent . querySelector ( "section" ) ! . prepend ( < span >
36
- < a > { result . address } </ a >
37
- < b > Pub: { result . public . b64 } </ b >
38
- < b > Priv: { result . private . b64 } </ b >
39
- </ span > )
40
- parent . classList . remove ( "hidden" ) ;
41
- disposeThread ( ...threads ) ;
49
+ this . calculatingAddress = true ;
50
+
51
+ // spawn 10 new threads
52
+ using threads = await spawnThreads < typeof import ( '../TOR-Worker.ts' ) > ( '../TOR-Worker.ts' , 10 ) ;
53
+ // try to find an address in parallel
54
+ const address = await Promise . any (
55
+ threads . map ( thread => thread . generateVanityAddress ( this . addressPrefix ) )
56
+ ) ;
57
+ this . resultAddresses . unshift ( address ) ;
58
+
59
+ this . calculatingAddress = false ;
42
60
}
43
61
44
62
async computePI ( ) {
45
- const parent = this . inputPiDigits . parentElement ! ;
46
- if ( parent . classList . contains ( "hidden" ) )
47
- return ;
48
- parent . classList . add ( "hidden" ) ;
49
-
50
- const thread = await spawnThread < typeof import ( '../PI-Worker.ts' ) > ( new Path ( '../PI-Worker.ts' ) ) ;
51
- const pi = await thread . calculatePI ( + this . inputPiDigits . value || 10 ) ;
52
- parent . querySelector ( "section" ) ! . prepend ( < span > { pi } </ span > )
53
- parent . classList . remove ( "hidden" ) ;
54
- disposeThread ( thread ) ;
63
+ this . calculatingPI = true ;
64
+
65
+ // spawn a new thread
66
+ using thread = await spawnThread < typeof import ( '../PI-Worker.ts' ) > ( '../PI-Worker.ts' ) ;
67
+ // calculate pi in the thread
68
+ const pi = await thread . calculatePI ( this . piDigits ) ;
69
+ this . resultPIs . unshift ( pi ) ;
70
+
71
+ this . calculatingPI = false ;
72
+
55
73
}
56
- }
74
+ }
0 commit comments