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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. const EventEmitter = require('events').EventEmitter;
  18. const MAX_LISTENERS = 20;
  19. const INSTANCE_KEY = Symbol.for('org.apache.cordova.common.CordovaEvents');
  20. let EVENTS_RECEIVER = null;
  21. class CordovaEventEmitter extends EventEmitter {
  22. /**
  23. * Sets up current instance to forward emitted events to another EventEmitter
  24. * instance.
  25. *
  26. * @param {EventEmitter} [eventEmitter] The emitter instance to forward
  27. * events to. Falsy value, when passed, disables forwarding.
  28. */
  29. forwardEventsTo (eventEmitter) {
  30. // If no argument is specified disable events forwarding
  31. if (!eventEmitter) {
  32. EVENTS_RECEIVER = undefined;
  33. return;
  34. }
  35. if (!(eventEmitter instanceof EventEmitter)) {
  36. throw new Error('Cordova events can be redirected to another EventEmitter instance only');
  37. }
  38. // CB-10940 Skipping forwarding to self to avoid infinite recursion.
  39. // This is the case when the modules are npm-linked.
  40. if (this !== eventEmitter) {
  41. EVENTS_RECEIVER = eventEmitter;
  42. } else {
  43. // Reset forwarding if we are subscribing to self
  44. EVENTS_RECEIVER = undefined;
  45. }
  46. }
  47. /**
  48. * Sets up current instance to forward emitted events to another EventEmitter
  49. * instance.
  50. *
  51. * @param {EventEmitter} [eventEmitter] The emitter instance to forward
  52. * events to. Falsy value, when passed, disables forwarding.
  53. */
  54. emit (eventName, ...args) {
  55. if (EVENTS_RECEIVER) {
  56. EVENTS_RECEIVER.emit(eventName, ...args);
  57. }
  58. return super.emit(eventName, ...args);
  59. }
  60. }
  61. // This singleton instance pattern is based on the ideas from
  62. // https://derickbailey.com/2016/03/09/creating-a-true-singleton-in-node-js-with-es6-symbols/
  63. if (Object.getOwnPropertySymbols(global).indexOf(INSTANCE_KEY) === -1) {
  64. const events = new CordovaEventEmitter();
  65. events.setMaxListeners(MAX_LISTENERS);
  66. global[INSTANCE_KEY] = events;
  67. }
  68. module.exports = global[INSTANCE_KEY];