import * as actor from "../src/actor" ; import { LocalContext } from "../src/localContext" ; import { Message } from "../src/messages" ; import { PID } from "../src/pid" ; class Request { constructor ( public Div , public Num , public Size ) { } } var myProps = actor . fromProducer ( ( ) => new MyActor ( ) ) ; class MyActor implements actor . IActor { Replies = 0 ; ReplyTo : PID | null = null ; Sum = 0 ; async Receive ( context ) { var message = context . Message ; if ( message instanceof Request ) { if ( message . Size == 1 ) { context . Respond ( message . Num ) ; context . Self . Stop ( ) ; return actor . done ; } this . Replies = message . Div ; this . ReplyTo = context . Sender ; for ( var i = 0 ; i < message . Div ; i ++ ) { var child = actor . spawn ( myProps ) ; var num = message . Num + i * ( message . Size / message . Div ) ; var size = message . Size / message . Div ; var div = message . Div ; child . Request ( new Request ( div , num , size ) , context . Self ) ; } return actor . done ; } if ( typeof ( message ) === 'number' ) { this . Sum += Number ( message ) ; this . Replies -- ; if ( this . Replies == 0 && this . ReplyTo ) { this . ReplyTo . Tell ( this . Sum ) ; } return actor . done ; } } } async function run ( ) { var pid = await actor . spawn ( myProps ) ; global . console . log ( 'starting - 10,000' ) ; var hrstart = process . hrtime ( ) ; var response = await pid . RequestPromise ( new Request ( 10 , 0 , 10 * 1000 ) ) ; global . console . log ( response ) ; var hr = process . hrtime ( hrstart ) ; var s = hr [ 0 ] + hr [ 1 ] / ( 1000 * 1000 * 1000 ) ; global . console . log ( s + ' seconds' ) ; } run ( ) ;