Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

async-iterators.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { r as replaceTraps, i as instanceOfAny, a as reverseTransformCache, u as unwrap } from './wrap-idb-value.js';
  2. const advanceMethodProps = ['continue', 'continuePrimaryKey', 'advance'];
  3. const methodMap = {};
  4. const advanceResults = new WeakMap();
  5. const ittrProxiedCursorToOriginalProxy = new WeakMap();
  6. const cursorIteratorTraps = {
  7. get(target, prop) {
  8. if (!advanceMethodProps.includes(prop))
  9. return target[prop];
  10. let cachedFunc = methodMap[prop];
  11. if (!cachedFunc) {
  12. cachedFunc = methodMap[prop] = function (...args) {
  13. advanceResults.set(this, ittrProxiedCursorToOriginalProxy.get(this)[prop](...args));
  14. };
  15. }
  16. return cachedFunc;
  17. },
  18. };
  19. async function* iterate(...args) {
  20. // tslint:disable-next-line:no-this-assignment
  21. let cursor = this;
  22. if (!(cursor instanceof IDBCursor)) {
  23. cursor = await cursor.openCursor(...args);
  24. }
  25. if (!cursor)
  26. return;
  27. cursor = cursor;
  28. const proxiedCursor = new Proxy(cursor, cursorIteratorTraps);
  29. ittrProxiedCursorToOriginalProxy.set(proxiedCursor, cursor);
  30. // Map this double-proxy back to the original, so other cursor methods work.
  31. reverseTransformCache.set(proxiedCursor, unwrap(cursor));
  32. while (cursor) {
  33. yield proxiedCursor;
  34. // If one of the advancing methods was not called, call continue().
  35. cursor = await (advanceResults.get(proxiedCursor) || cursor.continue());
  36. advanceResults.delete(proxiedCursor);
  37. }
  38. }
  39. function isIteratorProp(target, prop) {
  40. return ((prop === Symbol.asyncIterator &&
  41. instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) ||
  42. (prop === 'iterate' && instanceOfAny(target, [IDBIndex, IDBObjectStore])));
  43. }
  44. replaceTraps((oldTraps) => ({
  45. ...oldTraps,
  46. get(target, prop, receiver) {
  47. if (isIteratorProp(target, prop))
  48. return iterate;
  49. return oldTraps.get(target, prop, receiver);
  50. },
  51. has(target, prop) {
  52. return isIteratorProp(target, prop) || oldTraps.has(target, prop);
  53. },
  54. }));