Table of Contents

HipHop

The "hiphop" module contains utilities for creating and running HipHop reactive machines.

Use require( "hiphop" ) to use it.

Creating HipHop reactive machines

new hiphop.ReactiveMachine( hhprgm, options )

const hh = require( "hiphop" );
const m = new hh.ReactiveMachine( require( "prgm.hh.js" ) );

Running HipHop reactive machines

mach.react( sigset )

The react function machine reactions. If called with no argument, it proceed to one step. If called with one or several arguments, it proceeds as follows for each argument:

The react function returns the machine itself.

Example:

// proceed to one reaction
m.react(); 

// proceed to 4 reactions, with first the signal O emitded with value 24,
// P with value 53, then a second reaction with only o emitted with value 56,
// ...
m.react( { O: 24, P: 53 } );
m.react( { O: 56 } );
m.react( { O: 77 } );
m.react( { P: 10 } );

After a reaction, each output signal of the main program module is bound in the machine as a JavaScript property. For instance, is a reaction emits the signal O with value 1 at the first reaction of the machine m and O with value 2 at the second reaction, checking m.O after that reaction would return:

{ nowval: 2, preval: 1, now: true, pre: true }

mach.input( sigset )

The input function emits signal in the machine but does not triggers the reaction. For instance,

m.input( { O: 24 } );
m.input( { P: 53 } )
m.react();

Is equivalent to

m.react( { O: 24, P: 53 } )
m.react();

Interacing with HipHop reactive machines

mach.addEventListener( signame, listener )

Associate a listener to the machine event signame.

Listeners are invoked with one object with one or two fields:

The stopPropagation() is a method that, if called within the listener, will inhibit the call of others callback mapped on this signal.

Example:

reactfunc.js

"use hiphop"
"use hopscript"

const hh = require( "hiphop" );

function foo( evt ) {
   console.log( "hi from foo signal", evt.type, "is set!" )
}

function bar( evt ) {
   console.log( "hi from bar signal", evt.type, "is set!" )
}

function foo2( evt ) {
   console.log( "hi from foo2 signal", evt.type, "is set with", evt.nowval, "!" )
}

function bar2( evt ) {
   console.log( "hi from bar2 signal", evt.type, "is set with", evt.nowval, "!" )
}

hiphop module prg( in I1, in I2, O1, O11, O2 ) {
   loop {
      if( I1.now ) {
         emit O1();
         emit O11();
      }
      if( I2.now ) {
         emit O2( I2.nowval );
      }
      yield;
   }
}

const m = new hh.ReactiveMachine( prg, "reactfunc" );

m.addEventListener( "O1", foo );
m.addEventListener( "O11", foo );
m.addEventListener( "O11", bar );
m.addEventListener( "O2", foo2 )
m.addEventListener( "O2", bar2 );

exports.prg = m;

mach.removeEventListener( signame, listener )

Remove the listener from the machine.