@tstdl/base 0.87.0 → 0.87.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,8 @@
1
- import type { BrowserContext } from 'playwright';
1
+ import type { BrowserContext, Page } from 'playwright';
2
2
  import type { AsyncDisposable } from '../disposable/disposable.js';
3
3
  import { disposeAsync } from '../disposable/disposable.js';
4
4
  import type { Resolvable } from '../injector/interfaces.js';
5
- import { resolveArgumentType } from '../injector/interfaces.js';
5
+ import { afterResolve, resolveArgumentType } from '../injector/interfaces.js';
6
6
  import type { Logger } from '../logger/logger.js';
7
7
  import type { Record } from '../types.js';
8
8
  import type { Tagged } from 'type-fest';
@@ -19,12 +19,22 @@ export type NewPageOptions = {
19
19
  export type BrowserContextState = Tagged<Record<string | number, unknown>, 'BrowserContextState'>;
20
20
  export type BrowserContextControllerArgument = NewBrowserContextOptions;
21
21
  export declare class BrowserContextController implements AsyncDisposable, Resolvable<BrowserContextControllerArgument> {
22
+ #private;
22
23
  /** @deprecated should be avoided */
23
24
  readonly context: BrowserContext;
24
25
  readonly options: BrowserContextControllerOptions;
25
26
  readonly [resolveArgumentType]: BrowserContextControllerArgument;
26
27
  constructor(context: BrowserContext, options?: BrowserContextControllerOptions);
28
+ [afterResolve](): void;
29
+ initialize(): void;
27
30
  [disposeAsync](): Promise<void>;
31
+ pages(): PageController[];
32
+ /**
33
+ * Get a controller for the page.
34
+ * @param page page to get controller for
35
+ * @param options options to use for the page controller *if* it is new. Ignored if there is already a controller associated.
36
+ */
37
+ getControllerByPage(page: Page, options?: PageControllerOptions): PageController;
28
38
  close(): Promise<void>;
29
39
  getState(): Promise<BrowserContextState>;
30
40
  setExtraHttpHeaders(headers: Record<string, string | undefined>): Promise<void>;
@@ -44,6 +44,7 @@ var __metadata = function(k, v) {
44
44
  return Reflect.metadata(k, v);
45
45
  };
46
46
  let BrowserContextController = class BrowserContextController2 {
47
+ #pageControllers = /* @__PURE__ */ new WeakMap();
47
48
  /** @deprecated should be avoided */
48
49
  context;
49
50
  options;
@@ -51,9 +52,38 @@ let BrowserContextController = class BrowserContextController2 {
51
52
  this.context = context;
52
53
  this.options = options;
53
54
  }
55
+ [import_interfaces.afterResolve]() {
56
+ this.initialize();
57
+ }
58
+ initialize() {
59
+ this.context.on("page", (page) => {
60
+ page.once("close", () => this.#pageControllers.delete(page));
61
+ });
62
+ }
54
63
  async [import_disposable.disposeAsync]() {
55
64
  await this.close();
56
65
  }
66
+ pages() {
67
+ const pages = this.context.pages();
68
+ return pages.map((page) => this.getControllerByPage(page));
69
+ }
70
+ /**
71
+ * Get a controller for the page.
72
+ * @param page page to get controller for
73
+ * @param options options to use for the page controller *if* it is new. Ignored if there is already a controller associated.
74
+ */
75
+ getControllerByPage(page, options) {
76
+ if (page.context() != this.context) {
77
+ throw new Error("Page is not from this context.");
78
+ }
79
+ const existingController = this.#pageControllers.get(page);
80
+ if ((0, import_type_guards.isDefined)(existingController)) {
81
+ return existingController;
82
+ }
83
+ const newController = new import_page_controller.PageController(page, this, { ...this.options.defaultNewPageOptions?.controllerOptions, ...options });
84
+ this.#pageControllers.set(page, newController);
85
+ return newController;
86
+ }
57
87
  async close() {
58
88
  await this.context.close();
59
89
  }
@@ -68,7 +98,8 @@ let BrowserContextController = class BrowserContextController2 {
68
98
  async newPage(options) {
69
99
  const page = await this.context.newPage();
70
100
  const mergedOptions = { ...this.options.defaultNewPageOptions, ...options };
71
- const controller = new import_page_controller.PageController(page, mergedOptions.controllerOptions);
101
+ const controller = new import_page_controller.PageController(page, this, mergedOptions.controllerOptions);
102
+ this.#pageControllers.set(page, controller);
72
103
  if ((0, import_type_guards.isDefined)(mergedOptions.extraHttpHeaders)) {
73
104
  await controller.setExtraHttpHeaders(mergedOptions.extraHttpHeaders);
74
105
  }
@@ -1,27 +1,33 @@
1
1
  import type { ElementHandle, Frame, FrameLocator, Page } from 'playwright';
2
+ import type { BrowserContextController } from './browser-context-controller.js';
2
3
  import type { Delay } from './element-controller.js';
3
4
  import { ElementController } from './element-controller.js';
4
5
  import type { FrameController, FrameControllerOptions } from './frame-controller.js';
5
6
  import { LocatorController } from './locator-controller.js';
6
- import type { PageControllerOptions } from './page-controller.js';
7
7
  export type DocumentControllerOptions = {
8
8
  defaultActionDelay?: Delay;
9
9
  defaultTypeDelay?: Delay;
10
10
  };
11
- export type DocumentControllerForwardOptions = {
12
- pageControllerOptions: PageControllerOptions;
13
- frameControllerOptions: FrameControllerOptions;
14
- };
11
+ export declare function setFrameControllerConstructor(constructor: typeof FrameController): void;
15
12
  export declare class DocumentController<T extends Page | Frame = Page | Frame> extends LocatorController<T> {
16
- protected readonly forwardOptions: DocumentControllerForwardOptions;
13
+ #private;
17
14
  /** @deprecated should be avoided */
18
15
  readonly document: T;
19
- constructor(document: T, options: DocumentControllerOptions, forwardOptions: DocumentControllerForwardOptions);
16
+ readonly context: BrowserContextController;
17
+ readonly options: DocumentControllerOptions;
18
+ constructor(document: T, context: BrowserContextController, options: DocumentControllerOptions);
19
+ frames(): FrameController[];
20
+ /**
21
+ * Get a controller for the frame.
22
+ * @param frame frame to get controller for
23
+ * @param options options to use for the frame controller *if* it is new. Ignored if there is already a controller associated.
24
+ */
25
+ getControllerByFrame(frame: Frame, options?: FrameControllerOptions): FrameController;
20
26
  setContent(...args: Parameters<Page['setContent']>): Promise<void>;
21
27
  navigate(...args: Parameters<Page['goto']>): Promise<void>;
22
28
  waitForLoadState(...args: Parameters<Page['waitForLoadState']>): Promise<void>;
23
29
  waitForUrl(...args: Parameters<Page['waitForURL']>): Promise<void>;
24
30
  waitForElement(selector: string, options?: Parameters<Page['waitForSelector']>[1]): Promise<ElementController<ElementHandle<SVGElement | HTMLElement>>>;
25
31
  locateInFrame(frameSelector: string): LocatorController<FrameLocator>;
26
- waitForFrame(selector: string, options?: Parameters<Page['waitForSelector']>[1]): Promise<FrameController>;
32
+ waitForFrame(selector: string, options?: Parameters<Page['waitForSelector']>[1], controllerOptions?: FrameControllerOptions): Promise<FrameController>;
27
33
  }
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,31 +15,54 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
  var document_controller_exports = {};
30
20
  __export(document_controller_exports, {
31
- DocumentController: () => DocumentController
21
+ DocumentController: () => DocumentController,
22
+ setFrameControllerConstructor: () => setFrameControllerConstructor
32
23
  });
33
24
  module.exports = __toCommonJS(document_controller_exports);
34
25
  var import_type_guards = require("../utils/type-guards.js");
35
26
  var import_element_controller = require("./element-controller.js");
36
27
  var import_locator_controller = require("./locator-controller.js");
28
+ var import_utils = require("./utils.js");
29
+ let frameControllerConstructor;
30
+ function setFrameControllerConstructor(constructor) {
31
+ frameControllerConstructor = constructor;
32
+ }
37
33
  class DocumentController extends import_locator_controller.LocatorController {
38
- forwardOptions;
34
+ #frameControllers = /* @__PURE__ */ new WeakMap();
39
35
  /** @deprecated should be avoided */
40
36
  document;
41
- constructor(document, options, forwardOptions) {
37
+ context;
38
+ options;
39
+ constructor(document, context, options) {
42
40
  super(document, { actionDelay: options.defaultActionDelay, typeDelay: options.defaultTypeDelay });
43
41
  this.document = document;
44
- this.forwardOptions = forwardOptions;
42
+ this.context = context;
43
+ this.options = options;
44
+ }
45
+ frames() {
46
+ const frames = (0, import_utils.isPage)(this.document) ? this.document.frames() : this.document.childFrames();
47
+ return frames.map((page) => this.getControllerByFrame(page));
48
+ }
49
+ /**
50
+ * Get a controller for the frame.
51
+ * @param frame frame to get controller for
52
+ * @param options options to use for the frame controller *if* it is new. Ignored if there is already a controller associated.
53
+ */
54
+ getControllerByFrame(frame, options) {
55
+ const documentPage = (0, import_utils.isPage)(this.document) ? this.document : this.document.page();
56
+ if (frame.page() != documentPage) {
57
+ throw new Error("Frame is not from this page.");
58
+ }
59
+ const existingController = this.#frameControllers.get(frame);
60
+ if ((0, import_type_guards.isDefined)(existingController)) {
61
+ return existingController;
62
+ }
63
+ const newController = new frameControllerConstructor(frame, this.context, { ...this.options, ...options });
64
+ this.#frameControllers.set(frame, newController);
65
+ return newController;
45
66
  }
46
67
  async setContent(...args) {
47
68
  await this.document.setContent(...args);
@@ -66,24 +87,12 @@ class DocumentController extends import_locator_controller.LocatorController {
66
87
  const locator = this.document.frameLocator(frameSelector);
67
88
  return new import_locator_controller.LocatorController(locator, this.elementControllerOptions);
68
89
  }
69
- async waitForFrame(selector, options) {
90
+ async waitForFrame(selector, options, controllerOptions) {
70
91
  const element = await this.waitForElement(selector, options);
71
92
  const frame = await element.locatorOrHandle.contentFrame();
72
93
  if ((0, import_type_guards.isNull)(frame)) {
73
94
  throw new Error("Element is not a frame.");
74
95
  }
75
- return newFrameController(frame, this.forwardOptions.frameControllerOptions, { pageControllerOptions: this.forwardOptions.pageControllerOptions });
96
+ return this.getControllerByFrame(frame, controllerOptions);
76
97
  }
77
98
  }
78
- let frameControllerConstructor;
79
- async function newFrameController(...args) {
80
- if ((0, import_type_guards.isUndefined)(frameControllerConstructor)) {
81
- frameControllerConstructor = importFrameController();
82
- void frameControllerConstructor.then((constructor) => frameControllerConstructor = constructor);
83
- }
84
- return new (await frameControllerConstructor)(...args);
85
- }
86
- async function importFrameController() {
87
- const module2 = await import("./frame-controller.js");
88
- return module2.FrameController;
89
- }
@@ -1,23 +1,19 @@
1
1
  import type { Frame, Page } from 'playwright';
2
+ import type { BrowserContextController } from './browser-context-controller.js';
2
3
  import type { DocumentControllerOptions } from './document-controller.js';
3
4
  import { DocumentController } from './document-controller.js';
4
- import type { PageControllerOptions } from './page-controller.js';
5
- import { PageController } from './page-controller.js';
5
+ import type { PageController, PageControllerOptions } from './page-controller.js';
6
6
  export type FrameControllerOptions = DocumentControllerOptions;
7
- export type FrameControllerForwardOptions = {
8
- pageControllerOptions: PageControllerOptions;
9
- };
10
7
  export declare class FrameController extends DocumentController<Frame> {
11
- private readonly frameControllerForwardOptions;
12
8
  /** @deprecated should be avoided */
13
9
  readonly frame: Frame;
14
10
  readonly options: FrameControllerOptions;
15
- constructor(frame: Frame, options: FrameControllerOptions, forwardOptions: FrameControllerForwardOptions);
11
+ constructor(frame: Frame, context: BrowserContextController, options: FrameControllerOptions);
16
12
  /** Get the page containing this frame */
17
- getPage(): PageController;
13
+ getPage(options?: PageControllerOptions): PageController;
18
14
  /**
19
15
  * @param frameSelector frame name, url or url predicate
20
16
  * @returns
21
17
  */
22
- getFrame(frameSelector: Parameters<Page['frame']>[0]): FrameController;
18
+ getFrame(frameSelector: Parameters<Page['frame']>[0], options?: FrameControllerOptions): FrameController;
23
19
  }
@@ -23,30 +23,27 @@ __export(frame_controller_exports, {
23
23
  module.exports = __toCommonJS(frame_controller_exports);
24
24
  var import_type_guards = require("../utils/type-guards.js");
25
25
  var import_document_controller = require("./document-controller.js");
26
- var import_page_controller = require("./page-controller.js");
27
26
  class FrameController extends import_document_controller.DocumentController {
28
- frameControllerForwardOptions;
29
27
  /** @deprecated should be avoided */
30
28
  frame;
31
29
  options;
32
- constructor(frame, options, forwardOptions) {
33
- super(frame, options, { frameControllerOptions: options, pageControllerOptions: forwardOptions.pageControllerOptions });
30
+ constructor(frame, context, options) {
31
+ super(frame, context, options);
34
32
  this.options = options;
35
- this.frameControllerForwardOptions = forwardOptions;
36
33
  }
37
34
  /** Get the page containing this frame */
38
- getPage() {
39
- return new import_page_controller.PageController(this.frame.page(), this.frameControllerForwardOptions.pageControllerOptions);
35
+ getPage(options) {
36
+ return this.context.getControllerByPage(this.frame.page(), options);
40
37
  }
41
38
  /**
42
39
  * @param frameSelector frame name, url or url predicate
43
40
  * @returns
44
41
  */
45
- getFrame(frameSelector) {
42
+ getFrame(frameSelector, options) {
46
43
  const frame = this.frame.page().frame(frameSelector);
47
44
  if ((0, import_type_guards.isNull)(frame)) {
48
45
  throw new Error("Frame not found.");
49
46
  }
50
- return new FrameController(frame, this.options, this.frameControllerForwardOptions);
47
+ return this.getControllerByFrame(frame, { ...this.options, ...options });
51
48
  }
52
49
  }
package/browser/module.js CHANGED
@@ -28,6 +28,8 @@ var import_injector = require("../injector/injector.js");
28
28
  var import_object = require("../utils/object/object.js");
29
29
  var import_type_guards = require("../utils/type-guards.js");
30
30
  var import_browser_service = require("./browser.service.js");
31
+ var import_document_controller = require("./document-controller.js");
32
+ var import_frame_controller = require("./frame-controller.js");
31
33
  const browserTypes = {
32
34
  chromium: import_playwright.chromium,
33
35
  firefox: import_playwright.firefox,
@@ -49,3 +51,4 @@ function getBrowserType(type) {
49
51
  }
50
52
  return browserType;
51
53
  }
54
+ (0, import_document_controller.setFrameControllerConstructor)(import_frame_controller.FrameController);
@@ -2,22 +2,23 @@ import type { Page } from 'playwright';
2
2
  import type { AsyncDisposable } from '../disposable/disposable.js';
3
3
  import { disposeAsync } from '../disposable/disposable.js';
4
4
  import type { Logger } from '../logger/logger.js';
5
+ import type { BrowserContextController } from './browser-context-controller.js';
5
6
  import type { DocumentControllerOptions } from './document-controller.js';
6
7
  import { DocumentController } from './document-controller.js';
7
- import type { FrameControllerOptions } from './frame-controller.js';
8
- import { FrameController } from './frame-controller.js';
8
+ import type { FrameController, FrameControllerOptions } from './frame-controller.js';
9
9
  import type { PdfRenderOptions } from './pdf-options.js';
10
10
  import type { Abortable } from './types.js';
11
- export type PageControllerOptions = DocumentControllerOptions & {
12
- defaultFrameControllerOptions?: FrameControllerOptions;
13
- };
11
+ export type PageControllerOptions = DocumentControllerOptions;
14
12
  export declare class PageController extends DocumentController<Page> implements AsyncDisposable {
15
13
  /** @deprecated should be avoided */
16
14
  readonly page: Page;
17
15
  readonly options: PageControllerOptions;
18
- constructor(page: Page, options?: PageControllerOptions);
16
+ constructor(page: Page, context: BrowserContextController, options?: PageControllerOptions);
19
17
  [disposeAsync](): Promise<void>;
20
18
  close(): Promise<void>;
19
+ /** finds pages opened by this page (having opener set to this page) */
20
+ opened(): Promise<PageController[]>;
21
+ opener(): Promise<PageController | null>;
21
22
  setExtraHttpHeaders(headers: Record<string, string>): Promise<void>;
22
23
  waitForClose(): Promise<void>;
23
24
  url(): string;
@@ -25,7 +26,7 @@ export declare class PageController extends DocumentController<Page> implements
25
26
  * @param frameSelector frame name, url or url predicate
26
27
  * @returns
27
28
  */
28
- frame(frameSelector: Parameters<Page['frame']>[0]): FrameController;
29
+ frame(frameSelector: Parameters<Page['frame']>[0], options?: FrameControllerOptions): FrameController;
29
30
  renderPdf(options?: PdfRenderOptions & Abortable): Promise<Uint8Array>;
30
31
  renderPdfStream(options?: PdfRenderOptions & Abortable): ReadableStream<Uint8Array>;
31
32
  attachLogger(logger: Logger): void;
@@ -28,14 +28,13 @@ var import_timing = require("../utils/timing.js");
28
28
  var import_type_guards = require("../utils/type-guards.js");
29
29
  var import_units = require("../utils/units.js");
30
30
  var import_document_controller = require("./document-controller.js");
31
- var import_frame_controller = require("./frame-controller.js");
32
31
  var import_utils = require("./utils.js");
33
32
  class PageController extends import_document_controller.DocumentController {
34
33
  /** @deprecated should be avoided */
35
34
  page;
36
35
  options;
37
- constructor(page, options = {}) {
38
- super(page, options, { pageControllerOptions: options, frameControllerOptions: options.defaultFrameControllerOptions ?? options });
36
+ constructor(page, context, options = {}) {
37
+ super(page, context, options);
39
38
  this.page = page;
40
39
  this.options = options;
41
40
  }
@@ -45,6 +44,23 @@ class PageController extends import_document_controller.DocumentController {
45
44
  async close() {
46
45
  await this.page.close();
47
46
  }
47
+ /** finds pages opened by this page (having opener set to this page) */
48
+ async opened() {
49
+ const openedPages = [];
50
+ for (const page of this.context.pages()) {
51
+ if (await page.opener() == this) {
52
+ openedPages.push(page);
53
+ }
54
+ }
55
+ return openedPages;
56
+ }
57
+ async opener() {
58
+ const opener = await this.page.opener();
59
+ if ((0, import_type_guards.isNull)(opener)) {
60
+ return null;
61
+ }
62
+ return this.context.getControllerByPage(opener);
63
+ }
48
64
  async setExtraHttpHeaders(headers) {
49
65
  const filtered = (0, import_object.filterUndefinedFromRecord)(headers);
50
66
  await this.page.setExtraHTTPHeaders(filtered);
@@ -65,12 +81,12 @@ class PageController extends import_document_controller.DocumentController {
65
81
  * @param frameSelector frame name, url or url predicate
66
82
  * @returns
67
83
  */
68
- frame(frameSelector) {
84
+ frame(frameSelector, options) {
69
85
  const frame = this.page.frame(frameSelector);
70
86
  if ((0, import_type_guards.isNull)(frame)) {
71
87
  throw new Error("Frame not found.");
72
88
  }
73
- return new import_frame_controller.FrameController(frame, this.options, this.forwardOptions);
89
+ return this.getControllerByFrame(frame, { ...this.options, ...options });
74
90
  }
75
91
  async renderPdf(options = {}) {
76
92
  const createPdfOptions = convertPdfOptions(options);
@@ -1 +1 @@
1
- export {};
1
+ import '../../polyfills.js';
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
+ var import_polyfills = require("../../polyfills.js");
2
3
  var import_promises = require("node:fs/promises");
3
4
  var import_application = require("../../application/index.js");
4
5
  var import_browser_service = require("../../browser/browser.service.js");
package/function/log.d.ts CHANGED
@@ -3,4 +3,4 @@ export type WrapLogOptions = {
3
3
  fnName?: string;
4
4
  logger?: Logger;
5
5
  };
6
- export declare function wrapLog(fn: Function, options?: WrapLogOptions): Function;
6
+ export declare function wrapLog(fn: Function, { fnName, logger }?: WrapLogOptions): Function;
package/function/log.js CHANGED
@@ -23,15 +23,21 @@ __export(log_exports, {
23
23
  module.exports = __toCommonJS(log_exports);
24
24
  var import_type_guards = require("../utils/type-guards.js");
25
25
  var import_type_of = require("../utils/type-of.js");
26
- function wrapLog(fn, options) {
27
- const fnName = options?.fnName ?? fn.name;
28
- const log = options?.logger?.trace.bind(options.logger) ?? console.log.bind(console);
26
+ function wrapLog(fn, { fnName = fn.name, logger } = {}) {
27
+ const log = logger?.trace.bind(logger) ?? console.log.bind(console);
29
28
  const wrapped = {
30
29
  [fnName](...args) {
31
- const argString = args.map((arg) => (0, import_type_guards.isPrimitive)(arg) ? (0, import_type_guards.isString)(arg) ? `"${arg}"` : String(arg) : (0, import_type_of.typeOf)(arg)).join(", ");
30
+ const argString = args.map((arg) => stringifyArg(arg)).join(", ");
32
31
  log(`[call: ${fnName}(${argString})]`);
33
32
  return Reflect.apply(fn, this, args);
34
33
  }
35
34
  };
36
35
  return wrapped[fnName];
37
36
  }
37
+ function stringifyArg(arg, depth = 1) {
38
+ if ((0, import_type_guards.isArray)(arg) && depth > 0) {
39
+ const argString = arg.map((innerArg) => stringifyArg(innerArg, depth - 1)).join(", ");
40
+ return `[${argString}]`;
41
+ }
42
+ return (0, import_type_guards.isPrimitive)(arg) ? (0, import_type_guards.isString)(arg) ? `"${arg}"` : String(arg) : (0, import_type_of.typeOf)(arg);
43
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.87.0",
3
+ "version": "0.87.2",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -1,5 +1,3 @@
1
1
  import type { AnyIterable } from '../any-iterable-iterator.js';
2
2
  import type { IterableItemMetadata } from '../iterable-helpers/types.js';
3
3
  export declare function metadataAsync<T>(iterable: AnyIterable<T>): AsyncIterableIterator<IterableItemMetadata<T>>;
4
- export declare function sync<T>(iterable: Iterable<T>): AsyncIterableIterator<IterableItemMetadata<T>>;
5
- export declare function async<T>(iterable: AnyIterable<T>): AsyncIterableIterator<IterableItemMetadata<T>>;
@@ -18,9 +18,7 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var metadata_exports = {};
20
20
  __export(metadata_exports, {
21
- async: () => async,
22
- metadataAsync: () => metadataAsync,
23
- sync: () => sync
21
+ metadataAsync: () => metadataAsync
24
22
  });
25
23
  module.exports = __toCommonJS(metadata_exports);
26
24
  var import_is_async_iterable = require("./is-async-iterable.js");
@@ -21,7 +21,7 @@ __export(skip_exports, {
21
21
  skipAsync: () => skipAsync
22
22
  });
23
23
  module.exports = __toCommonJS(skip_exports);
24
- var import_take_while = require("./take-while.js");
24
+ var import_filter = require("./filter.js");
25
25
  function skipAsync(iterable, count) {
26
- return (0, import_take_while.takeWhileAsync)(iterable, false, (_item, index) => index >= count);
26
+ return (0, import_filter.filterAsync)(iterable, (_item, index) => index >= count);
27
27
  }
@@ -21,7 +21,23 @@ __export(take_exports, {
21
21
  takeAsync: () => takeAsync
22
22
  });
23
23
  module.exports = __toCommonJS(take_exports);
24
- async function* takeAsync(iterable, count) {
24
+ var import_is_async_iterable = require("./is-async-iterable.js");
25
+ function takeAsync(iterable, count) {
26
+ return (0, import_is_async_iterable.isAsyncIterable)(iterable) ? async(iterable, count) : sync(iterable, count);
27
+ }
28
+ async function* sync(iterable, count) {
29
+ if (count <= 0) {
30
+ return;
31
+ }
32
+ let counter = 0;
33
+ for (const item of iterable) {
34
+ yield item;
35
+ if (++counter >= count) {
36
+ break;
37
+ }
38
+ }
39
+ }
40
+ async function* async(iterable, count) {
25
41
  if (count <= 0) {
26
42
  return;
27
43
  }