How to properly implement pause / resume in node.js for custom EventEmitter

I create a report service in which one process sends a request, retrieves a token, and then passes the token to another process, from which we request a report service and return the results.

Given this code, how to implement a blocking call until it is suspended?

var   util = require('util')
    , events = require('events')
    , pg = require('pg')

// QueryHandler is an EventEmitter
function QueryHandler(sql) {
  this.paused = true

  pg.connect(connectionString, function(err, client) {
    // error handling ignored for sake of illustration
    var query = client.query(sql)

    query.on('row', function(row) {
      if (this.paused) {
        // Somehow block until paused === false
      }

      this.emit(row)
    }.bind(this))
  }.bind(this))
}

util.inherits(QueryHandler, events.EventEmitter)

QueryHandler.prototype.resume = function() {
  this.paused = false
}

Here is an interaction diagram explaining what I'm trying to achieve:

http://teksol.info.s3.amazonaws.com/reporting-service.png

  • Web browser requests frontend web server for report
  • Frontend web server requests a reporting service for a token related to a specific request
  • At the same time, the reporting service connects to PostgreSQL and sends a query
  • The frontend web server returns the report service URL and token back to the web browser.
  • - Ajax ( )
  • -.

, 3 -, . , , , . , , , - ? , !

+3
1

, , , - api (, ) .

I think storing lines coming from pg is a better idea.

+2
source

All Articles