简体   繁体   中英

How to properly structure class in Node.js

I have a class called TileStreamer that I am currently defining as follows:

function TileStreamer {

};

This class has constants, which I define as follows:

// Tiles are 256 x 256 pixels 
TileStreamer.prototype.TILE_SIZE = 256; 

// Header size in bytes
TileStreamer.prototype.HEADER_SIZE = 28; 

// Various table entry sizes in bytes 
TileStreamer.prototype.RESOLUTION_ENTRY_SIZE = 12; 
TileStreamer.prototype.TILE_COUNT_SIZE = 4; 
TileStreamer.prototype.TILE_ENTRY_SIZE = 12; 

// Offsets within header 
TileStreamer.prototype.WIDTH_OFFSET = 3; 
TileStreamer.prototype.HEIGHT_OFFSET = 4; 
TileStreamer.prototype.NUM_TABLES_OFFSET = 7; 
TileStreamer.prototype.UNPOPULATED_OFFSET = 12092;

There also other variables. These variables are important because they need to be accessible from other classes . They get their values within the methods of this class. This is what I am unsure of as far as structure. What I'm currently trying is:

TileStreamer.prototype.header; 
TileStreamer.prototype.resolutionEntry; 
TileStreamer.prototype.resolutionTable; 
TileStreamer.prototype.filepath; 
TileStreamer.prototype.s3; 
TileStreamer.prototype.level; 
TileStreamer.prototype.ncols; 
TileStreamer.prototype.nrows; 
TileStreamer.prototype.nlevels; 
TileStreamer.prototype.toffset; 
TileStreamer.prototype.tsize; 
TileStreamer.prototype.modifiedTime; 
TileStreamer.prototype.tile; 
TileStreamer.prototype.host; 
TileStreamer.prototype.bucket; 

This class also has methods such as:

TileStreamer.prototype.Init = function(filepath, index, s3config){
   var retval = false; 
   AWS.config.update({accessKeyId: s3config.access_key, secretAccessKey: s3config.secret_key});
   var blc = new BlockLibraryConfigs(); 
   var awsConfig = blc.awsConfig; 
   AWS.config.update({region: awsConfig.region}); 

   var aws = new AWS.S3(); 
   var params = {
       Bucket: s3config.bucket, 
       Key: s3config.tile_directory + filepath, 
       Range: 'bytes=0-' + (this.HEADER_SIZE - 1)
   };

    aws.getObject(params, function(err, data){
       if(err == null){
            TileStreamer.modifiedTime = data.LastModified; 
            var header = bufferpack.unpack('<7I', data.Body);
            TileStreamer.header = header; 
            TileStreamer.nlevels = header[TileStreamer.NUM_TABLES_OFFSET]; 

            if(TileStreamer.nlevels == 5){
                TileStreamer.level = 0; 
                TileStreamer.ncols = Math.ceil((header[TileStreamer.WIDTH_OFFSET] * 1.0) / TileStreamer.TILE_SIZE); 
                TileStreamer.nrows = Math.ceil((header[TileStreamer.HEIGHT_OFFSET] * 1.0) / TileStreamer.TILE_SIZE);   

            }  
       }

    });     

}; 

The method above should set some of the values of the variables, such as modifiedTime so that I can access it in another class such as:

TileStreamer = require('tilestreamer.js'); 
var ts = new TileStreamer();
ts.Init(parPath, index, config); 
var last_modified = ts.modifiedTime; 

Just put any public properties you want to initialise when the object is created, directly in the init function. Here's a small example...

function TileStreamer() {
};

TileStreamer.prototype.Init = function() {
    this.modifiedTime = new Date();
}; 

var ts = new TileStreamer();
ts.Init();
console.log(ts);

jsfiddle example

https://jsfiddle.net/v6muohyk/

To get around the issue you're having with setting the object properties in a callback from an asynchronous function, just create a locally accessible variable to reference the object that you are creating at that time...

TileStreamer.prototype.Init = function() {
    var thisTileStreamer = this;
    asynchFunction(function(err, data) {
        thisTileStreamer.modifiedTime = data.lastModified;
    });
};

To take it one step further, if you need to execute some code after the init function has completed, then that will require waiting for the asynchronous function to complete, as well. For that, pass a further parameter to init , that is a function to be executed after all the work is done...

TileStreamer.prototype.Init = function(callback) {
    var thisTileStreamer = this;
    asynchFunction(function(err, data) {
        thisTileStreamer.modifiedTime = data.lastModified;
        callback();
    });
};

var ts = new TileStreamer();
ts.Init(function() {
    // put code here that needs to be executed *after* the init function has completed
    alert(ts.modifiedTime);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM