• In the related conversation about Module creation at runtime I learned about a superior solution for step 2:

    Modules.addCached(...) accepts the module code also as a function, something like this:

    Modules.addCached("MyObject", function(){
      // MyObject.js
      // Code of MyObject module implementing MyObject class/prototype.
      var MyObject = function(instanceName) { // constructor
        this.instanceName = instanceName;
        this.creationTimestamp = new Date().getTime();
      }
      MyObject.prototype.getInfo() {
        return this.instanceName + " created at " + this.creationTimestamp;
     }
     exports = MyObject;
    });
    

    Lines 2..11 represent the exact code that will become the 'outfactored' module. Keeping the module code as 'active' code in the wrapping, anonymous function passed into .addCached(..) keeps the code visible for the Espruino Web IDE editor and with that we get syntax coloring and code the Espruino specific code completion and easier debug-ability. With code as string none of these feature are available to the developer.

    The final code using this more useful approach looks then like this:

    // TemperatureDevTest2.js
    
    require("DS18B20"); // used by the dynamically loaded module
    Modules.addCached("Temperature", function() {
    // Temperature.js
    // 'class' (Prototype definition)
    var Temperature = function // module (class), for multiple instances
      ( name      // name hinting room / location of sensor\
      , oneWire   // one-wire; for example: new OneWire(B8);
      , addr      // address on the one-wire (required for multiples)
      , interval  // interval in milliseconds of measurements
      , preferred // optional, unit - "C" or "F" (default and not F is C)
      ) {
      // set givens
      this.name       = name;
      this.oneWire    = oneWire;
      this.addr       = addr;
      this.interval   = interval;
      this.preferred  = (preferred==="F") ? "F" : "C";
      this.enabled    = (typeof enabled === "undefined") || enabled;
      this.t = (this.preferred==="C") ? 0 : 32; // set current to 'frozen'
      this.intervalId = null; // later also used as indicator for enabled
      this.sensor     = null; // laster also used as indicatore for connected
      // get going
      this.sensor = require("DS18B20").connect(this.oneWire)­;
      this.intervalId = setInterval(function(_this) {
          _this.sensor.getTemp(function(t) {
          _this.t = t;
        });
      }, this.interval, this);
    };
    Temperature.prototype.getTemp = function
    ( unit // optional, unit "F" or "C", default "C"
    ) {
      var u = ((typeof unit !== "undefined") && (unit === "F")) ? "F" : "C";
      var d = (u === "F")
        ? (this.t * 1.8) + " Fahrenheit"
        : this.t + " Celsius";
      return d;
    };
    
    exports = Temperature;
    });
    
    
    // usage
    
    // setup oneWire
    var oneWire = new OneWire(B8);
    
    // setup 1st temperature sensor ts1
    var Temperature = require("Temperature");
    var ts1 = new Temperature
      ( "office" // name / location of temperatre sensor 1
      , oneWire
      , null     // addr on one-wire currently not implemented / used
      , 5000     // every 5 secs make a read (to keep it not booring)
      , "F"      // preferred unit is Fahrenheit
      );
    
    
    // for sample's sake, do everhthing deferred for more 
    // than 5 seconds in order to have value(s) to display
    setTimeout(function(){
    
    // get temp in preferred unit 
    console.log(ts1.getTemp());
    
    },6000);
    

    Lines 3..42 will become the - un-minified - source of the module file placed into the modules folder of the sandbox (or any other module repository).

About

Avatar for allObjects @allObjects started