在另一个方法Javascript上调用变​​量

时间:2017-10-24 15:17:43

标签: javascript node.js


    "use strict";
    var fs = require("fs"),
        child_process = require('child_process'),
        util = require('util'),
        logger = require("./logger.js"),
        Capture = require("./capture.js").Capture,
        RateLimiter = require("./utils.js").RateLimiter;
    class FileCapture extends Capture {
        constructor(options) {
            super(options); 
        }
        capture() {
            if (!this.options){
                throw new Error("Options is null for FileCapture");
            }
            var path = this.options.file;
            if (!path) {
                logger.error("File missing from config");
                return;
            }
            if (!fs.existsSync(path)) {
                logger.debug("File not found: " + path);
                return;
            }
            var limiter = new RateLimiter(this.options.rate);
            logger.info("current rate = " + this.options.rate);
            var lastmtime = 0;
            logger.info("Watching: " + path);
            var self = this;
            this.killwatcher();
            this.streamwatcher = fs.watch(this.options.file, (function(eventType, filename){
                fs.stat(this.options.file, function(err, stat) {
                    if (!err) {
                        if (stat.mtime.getTime() != lastmtime) {

                            limiter.emit((function() {
                                var buffer = fs.readFileSync(path);
                                if (buffer && buffer.length > 0) {
                                    self.receivedFrame({
                                        camera: self.options.camera,
                                        time: new Date().getTime(),
                                        data: buffer
                                    });
                                } else {
                                    logger.warn("Failed to read " + path);
                                }
                                logger.info("file changed current mtime:" + stat.mtime.getTime() +", lastmtime:"+lastmtime);
                                lastmtime = stat.mtime.getTime();
                            }).bind(this));
                        }
                    } else {
                        logger.warn("Failed to read " + path);
                    }
                });
            });
        }
        killwatcher() {
            logger.info("watcher: " +this.streamwatcher);
            if(this.streamwatcher) {
                logger.info("Closing watcher again");
                this.streamwatcher.close();
                this.streamwatcher = null;
            }
        }
    }
    module.exports = FileCapture;

大家好,我想在killwatacher()方法中调用 this.streamwatcher 。关闭fs.watch()但 this.streamwathcer 始终为null。我的问题是,如何访问 this.streamwatcher 来关闭fs.watch()?

任何意见/建议都非常感谢。谢谢。

0 个答案:

没有答案