util.js 912 B

123456789101112131415161718192021222324252627
  1. /** @module env/util */
  2. 'use strict';
  3. const GroupedQueue = require('grouped-queue');
  4. /**
  5. * Create a "sloppy" copy of an initial Environment object. The focus of this method is on
  6. * performance rather than correctly deep copying every property or recreating a correct
  7. * instance. Use carefully and don't rely on `hasOwnProperty` of the copied environment.
  8. *
  9. * Every property are shared except the runLoop which is regenerated.
  10. *
  11. * @param {Environment} initialEnv - an Environment instance
  12. * @return {Environment} sloppy copy of the initial Environment
  13. */
  14. exports.duplicateEnv = initialEnv => {
  15. const queues = require('../environment').queues;
  16. // Hack: Create a clone of the environment with a new instance of `runLoop`
  17. const env = Object.create(initialEnv);
  18. env.runLoop = new GroupedQueue(queues);
  19. return env;
  20. };
  21. /**
  22. * Log utility
  23. * @see {@link env/log}
  24. */
  25. exports.log = require('./log');