callback.js 760 B

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * Create a callback container
  3. * @return {Object} that wraps callbacks and returns a one-time id.
  4. */
  5. exports.create = function() {
  6. var lastId = 1,
  7. callbacks = {};
  8. return Object.create({}, {
  9. wrap: {
  10. value: function(callback) {
  11. var callbackId = lastId++;
  12. callbacks[callbackId] = callback || function() {};
  13. return callbackId;
  14. }
  15. },
  16. processResponse: {
  17. value: function(callbackId, args) {
  18. var callback = callbacks[callbackId];
  19. if (callback) {
  20. callback.apply(null, args);
  21. }
  22. delete callbacks[callbackId];
  23. }
  24. },
  25. removeResponseCallbackEntry: {
  26. value: function(callbackId) {
  27. delete callbacks[callbackId];
  28. }
  29. }
  30. });
  31. };