util.js 750 B

12345678910111213141516171819202122
  1. import globalThis from "./globalThis.js";
  2. export function pick(obj, ...attr) {
  3. return attr.reduce((acc, k) => {
  4. if (obj.hasOwnProperty(k)) {
  5. acc[k] = obj[k];
  6. }
  7. return acc;
  8. }, {});
  9. }
  10. // Keep a reference to the real timeout functions so they can be used when overridden
  11. const NATIVE_SET_TIMEOUT = setTimeout;
  12. const NATIVE_CLEAR_TIMEOUT = clearTimeout;
  13. export function installTimerFunctions(obj, opts) {
  14. if (opts.useNativeTimers) {
  15. obj.setTimeoutFn = NATIVE_SET_TIMEOUT.bind(globalThis);
  16. obj.clearTimeoutFn = NATIVE_CLEAR_TIMEOUT.bind(globalThis);
  17. }
  18. else {
  19. obj.setTimeoutFn = setTimeout.bind(globalThis);
  20. obj.clearTimeoutFn = clearTimeout.bind(globalThis);
  21. }
  22. }