Skip to content
Snippets Groups Projects
Select Git revision
  • ae1e15fb8def0e23662a611309277673039ffe30
  • master default protected
2 results

prob.md

Blame
  • template.js 2.11 KiB
    /*
    
    hunk template
    
    */
    
    // these are ES6 modules
    import {
      Hunkify,
      Input,
      Output,
      State
    } from './hunks.js'
    
    function Name() {
      // this fn attaches handles to our function-object,
      Hunkify(this)
    
      // inputs, outputs, and state are objects. they have a type (string identifier)
      // see 'typeset.js'
      // a name (doesn't have to be unique), and we pass them a handle to ourselves...
      let inA = new Input('number', 'name', this)
      // inputs, outputs and state are all kept locally in these arrays,
      // if we don't include them here, the manager will have a hard time finding them ...
      this.inputs.push(inA)
    
      let outB = new Output('number', 'name', this)
      this.outputs.push(outB)
    
      let stateItem = new State('string', 'name', 'startupValue')
      this.states.push(stateItem)
    
      // State items also have change handlers,
      stateItem.onChange = (value) => {
        // at this point, something external (probably a human)
        // has requested that we change this state variable,
        console.log('requests:', value)
        // we can reject that, by doing nothing here, or we can
        stateItem.set(value)
      }
    
      // hunks can choose to- or not- have init code.
      // at init, the module has been loaded and state variables have been
      // recalled from any program save - so this is a good point
      // to check any of those, and setup accordingly ...
      this.init = () => {
        this.log('hello template world')
      }
    
      let internalVariable = 'local globals'
    
      function internalFunc(data) {
        // scoped function, not accessible externally
        // do work,
        return (data)
      }
    
      // to divide time between hunks, each has a loop function
      // this is the hunks' runtime: a manager calls this once-per-round
      // here is where we check inputs, put to outputs, do work, etc
      this.loop = () => {
        // typically we check inputs and outputs first,
        // making sure we are clear to run,
        if (inA.io() && !outB.io()) {
          // an input is occupied, and the exit path is empty
          let output = internalFunc(inA.get())
          // put 'er there
          outB.put(output)
        }
      }
    }
    
    // the hunk is also an ES6 module, this is how we export those:
    export default Name