Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

fromEvent.js 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var Observable_1 = require("../Observable");
  4. var isArray_1 = require("../util/isArray");
  5. var isFunction_1 = require("../util/isFunction");
  6. var map_1 = require("../operators/map");
  7. var toString = (function () { return Object.prototype.toString; })();
  8. function fromEvent(target, eventName, options, resultSelector) {
  9. if (isFunction_1.isFunction(options)) {
  10. resultSelector = options;
  11. options = undefined;
  12. }
  13. if (resultSelector) {
  14. return fromEvent(target, eventName, options).pipe(map_1.map(function (args) { return isArray_1.isArray(args) ? resultSelector.apply(void 0, args) : resultSelector(args); }));
  15. }
  16. return new Observable_1.Observable(function (subscriber) {
  17. function handler(e) {
  18. if (arguments.length > 1) {
  19. subscriber.next(Array.prototype.slice.call(arguments));
  20. }
  21. else {
  22. subscriber.next(e);
  23. }
  24. }
  25. setupSubscription(target, eventName, handler, subscriber, options);
  26. });
  27. }
  28. exports.fromEvent = fromEvent;
  29. function setupSubscription(sourceObj, eventName, handler, subscriber, options) {
  30. var unsubscribe;
  31. if (isEventTarget(sourceObj)) {
  32. var source_1 = sourceObj;
  33. sourceObj.addEventListener(eventName, handler, options);
  34. unsubscribe = function () { return source_1.removeEventListener(eventName, handler, options); };
  35. }
  36. else if (isJQueryStyleEventEmitter(sourceObj)) {
  37. var source_2 = sourceObj;
  38. sourceObj.on(eventName, handler);
  39. unsubscribe = function () { return source_2.off(eventName, handler); };
  40. }
  41. else if (isNodeStyleEventEmitter(sourceObj)) {
  42. var source_3 = sourceObj;
  43. sourceObj.addListener(eventName, handler);
  44. unsubscribe = function () { return source_3.removeListener(eventName, handler); };
  45. }
  46. else if (sourceObj && sourceObj.length) {
  47. for (var i = 0, len = sourceObj.length; i < len; i++) {
  48. setupSubscription(sourceObj[i], eventName, handler, subscriber, options);
  49. }
  50. }
  51. else {
  52. throw new TypeError('Invalid event target');
  53. }
  54. subscriber.add(unsubscribe);
  55. }
  56. function isNodeStyleEventEmitter(sourceObj) {
  57. return sourceObj && typeof sourceObj.addListener === 'function' && typeof sourceObj.removeListener === 'function';
  58. }
  59. function isJQueryStyleEventEmitter(sourceObj) {
  60. return sourceObj && typeof sourceObj.on === 'function' && typeof sourceObj.off === 'function';
  61. }
  62. function isEventTarget(sourceObj) {
  63. return sourceObj && typeof sourceObj.addEventListener === 'function' && typeof sourceObj.removeEventListener === 'function';
  64. }
  65. //# sourceMappingURL=fromEvent.js.map