Child objects within modules

Posted on
  • I wondering the best way to handle this, and the best way to set modules up for child and parent relationships.

    Using a filesystem as an example, say we have 
    var myfs=require('FileSystem');
    
    var file=myfs.createFile('test.txt');
    file.write( 'data');
    file.close();
    

    Do we have a single module that contains both the FileSystem and File objects?

    I want file to have a reference to it's parent, and have it's own methods.

    Should this be set up as two modules, where Filesystem is a list of File objects?

    I've not seen any modules that contain two or related classes.

  • I'd put them both in one module:

    function File() {
    }
    File.prototype.foo = ...;
    
    exports.createFile = ...;
    exports.File = File;
    
    // ...
    var myfs=require('FileSystem');
    myfs.File.prototype.mySpecialFunction = ...;
    var file=myfs.createFile('test.txt');
    
    

    In fact, in a lot of cases you don't actually need access to the File class directly, since it'll only be created by things like createFile.

    If you're doing this, please can you make your API the same as the existing filesystem - specifically the existing E.openFile function? It'd make life much easier for everyone.

    ... IMO it'd be ideal if someone could just compile all the existing FAT filesystem code into the ESP8266 port, and then it could even work with external SD cards if needed. While not perfect for flash memory, I'm sure it'd be good enough.

  • Thanks Gordon.
    I used the file analogy as an example.
    I'm working on an object store that uses the esp8266 flash. I'm storing the mime types of the objects, as well as the data. This means that css/js/html can all be stored in flash and piped out to an http.response object.

    I'm intending to have a decent looking ui that is self contained, so that you can connect to the esp8266 as an access point, and then select a wifi ssid, and enter a password to join the network. From there the front end (bootstrap etc) can be delivered via cdn.

    I like the idea of having a self contained web server, and with the large flash size on the esp8266, local resources can be stored.

    I'll post code once I've got it more refined.

  • That'd be great to see when you get it done!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Child objects within modules

Posted by Avatar for Wilberforce @Wilberforce

Actions