purge-abandoned.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict'
  2. const { AbandonedError } = require('../../errors')
  3. const geWindow = require('./ge-window')
  4. /**
  5. * Given a `msgID` and a set of `abandoned` messages, remove any abandoned
  6. * messages that existed _prior_ to the specified `msgID`. For example, let's
  7. * assume the server has sent 3 messages:
  8. *
  9. * 1. A search message.
  10. * 2. An abandon message for the search message.
  11. * 3. A new search message.
  12. *
  13. * When the response for message #1 comes in, if it does, it will be processed
  14. * normally due to the specification. Message #2 will not receive a response, or
  15. * if the server does send one since the spec sort of allows it, we won't do
  16. * anything with it because we just discard that listener. Now the response
  17. * for message #3 comes in. At this point, we will issue a purge of responses
  18. * by passing in `msgID = 3`. This result is that we will remove the tracking
  19. * for message #1.
  20. *
  21. * @param {integer} msgID An upper bound for the messages to be purged.
  22. * @param {Map} abandoned A set of abandoned messages. Each message is an object
  23. * `{ age: <id>, cb: <func> }` where `age` was the current message id when the
  24. * abandon message was sent.
  25. */
  26. module.exports = function purgeAbandoned (msgID, abandoned) {
  27. abandoned.forEach((val, key) => {
  28. if (geWindow(val.age, msgID) === false) return
  29. val.cb(new AbandonedError('client request abandoned'))
  30. abandoned.delete(key)
  31. })
  32. }