@fbltd/async 1.0.16 → 1.0.18

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.
package/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # Async tools
2
- R&D async tools tree-shakable ES module
2
+ Tree-shakable ES module with async tools.
3
3
 
4
4
  ## Installation
5
5
  You can install this package with your favorite package manager.<br/>
package/dist/bin/delay.js CHANGED
@@ -1,4 +1,4 @@
1
- import { PromiseConfiguration } from "./get-promise.js";
1
+ import { PromiseConfiguration } from "./promise-configuration.js";
2
2
  export function delay(ms = 0, syncKey) {
3
3
  ms = Math.max(ms, 0);
4
4
  return delay.methods[typeof syncKey](ms);
@@ -1,8 +1,7 @@
1
- import { PromiseConfiguration } from "../get-promise.js";
2
- function baseComparer(prev, cur) {
3
- return prev === cur;
4
- }
5
- export class DependencyStream {
1
+ import { PromiseConfiguration } from "../promise-configuration.js";
2
+ import { DependencyStream } from "./dependency.stream.js";
3
+ import { baseComparer } from "./utils.js";
4
+ export class Dependency {
6
5
  _value;
7
6
  reactionPromise;
8
7
  abortPromise = new PromiseConfiguration();
@@ -31,6 +30,9 @@ export class DependencyStream {
31
30
  get value() {
32
31
  return this._value;
33
32
  }
33
+ getStream() {
34
+ return new DependencyStream(this);
35
+ }
34
36
  [Symbol.asyncIterator](thisStreamConfig = {}) {
35
37
  const totalDispose = this.abortPromise;
36
38
  const externalPromises = [totalDispose.promise];
@@ -0,0 +1,27 @@
1
+ import { PromiseConfiguration } from "../promise-configuration.js";
2
+ import { symAI } from "../constants.js";
3
+ export class DependencyStream {
4
+ owner;
5
+ selfDispose = new PromiseConfiguration();
6
+ iterator;
7
+ get isDisposed() {
8
+ return this.iterator.isDisposed;
9
+ }
10
+ constructor(owner) {
11
+ this.owner = owner;
12
+ this.selfDispose = new PromiseConfiguration();
13
+ this.iterator = owner[symAI]({ externalDispose: this.selfDispose });
14
+ }
15
+ [symAI]() {
16
+ return {
17
+ next: () => {
18
+ return this.iterator.next();
19
+ }
20
+ };
21
+ }
22
+ dispose() {
23
+ if (this.isDisposed)
24
+ return;
25
+ this.selfDispose.resolve();
26
+ }
27
+ }
@@ -1,4 +1,4 @@
1
- export * from "./dependency-stream.js";
1
+ export * from "./dependency.js";
2
+ export * from "./dependency.stream.js";
2
3
  export * from "./utils/index.js";
3
4
  export * from "./integrations/index.js";
4
- export * from "./contracts.js";
@@ -1,13 +1,22 @@
1
1
  import { symAI } from "../../constants.js";
2
- const StreamFinishError = new Error("Stream is done");
3
- export async function next(dep) {
4
- const res = await dep[symAI]().next();
5
- if (res.done) {
6
- throw StreamFinishError;
7
- }
8
- return res.value;
9
- }
2
+ import { PromiseConfiguration } from "../../promise-configuration.js";
10
3
  /**
11
4
  * @internal
12
5
  */
13
- export const InternalStreamFinishError = StreamFinishError;
6
+ export const StreamFinishError = new Error("Stream is done");
7
+ export function next(dep) {
8
+ const disposePromise = new PromiseConfiguration();
9
+ const resultPromise = new PromiseConfiguration();
10
+ dep[symAI]({ externalDispose: disposePromise })
11
+ .next()
12
+ .then((res) => {
13
+ if (res.done)
14
+ resultPromise.reject(StreamFinishError);
15
+ else
16
+ resultPromise.resolve(res.value);
17
+ });
18
+ return {
19
+ promise: resultPromise.promise,
20
+ dispose: () => disposePromise.resolve()
21
+ };
22
+ }
@@ -1,22 +1,18 @@
1
- import { PromiseConfiguration } from "../../get-promise.js";
2
1
  import { symAI } from "../../constants.js";
3
2
  export function onceStream(dep) {
4
- const externalDispose = new PromiseConfiguration();
5
- const iterator = dep[symAI]({ externalDispose });
6
- function isDisposed() {
7
- return externalDispose.isFulfilled || iterator.isDisposed;
8
- }
3
+ const stream = dep.getStream();
4
+ const iterator = stream[symAI]();
9
5
  return {
10
6
  get isDisposed() {
11
- return isDisposed();
7
+ return stream.isDisposed;
12
8
  },
13
- dispose: externalDispose.resolve,
9
+ dispose: () => stream.dispose(),
14
10
  [symAI]() {
15
11
  return {
16
12
  next: async () => {
17
13
  const result = await iterator.next();
18
- if (!isDisposed())
19
- externalDispose.resolve();
14
+ if (!result.done)
15
+ stream.dispose();
20
16
  return result;
21
17
  }
22
18
  };
@@ -1,5 +1,5 @@
1
1
  import { symAI } from "../../constants.js";
2
- import { PromiseConfiguration } from "../../get-promise.js";
2
+ import { PromiseConfiguration } from "../../promise-configuration.js";
3
3
  export function raceStream(...deps) {
4
4
  let selfDisposePromise = new PromiseConfiguration();
5
5
  let isDisposed = false;
@@ -0,0 +1,3 @@
1
+ export function baseComparer(prev, cur) {
2
+ return prev === cur;
3
+ }
package/dist/bin/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from "./dependency-stream/index.js";
2
2
  export * from "./dependency-stream/integrations/react/index.js";
3
3
  export * from "./delay.js";
4
- export * from "./get-promise.js";
4
+ export * from "./promise-configuration.js";
@@ -1,18 +1,24 @@
1
- export interface IIteratorOwner<TValue = any> {
2
- value: TValue;
3
- dispose(): void;
4
- }
5
- export type IStreamReturn = void;
6
- export type IStreamIteratorReturn = IteratorReturnResult<IStreamReturn>;
7
- export type IStreamIteratorYield<T> = IteratorYieldResult<T>;
1
+ import { PromiseConfiguration } from "../promise-configuration.js";
2
+ import { Dependency } from "./dependency.ts";
8
3
  export interface IStreamIterator<TValue = any> {
9
- owner: IIteratorOwner<TValue>;
4
+ owner: Dependency;
10
5
  dispose(): void;
11
6
  readonly isDisposed: boolean;
12
- next(): Promise<IteratorResult<TValue, IStreamReturn>>;
7
+ next(): Promise<IteratorResult<TValue, void>>;
13
8
  }
14
- export interface IStreamOwner<TValue = any> {
9
+ export interface IDependencyStream<TValue = any> {
15
10
  dispose(): void;
16
11
  readonly isDisposed: boolean;
17
- [Symbol.asyncIterator](): AsyncIterator<TValue, IStreamReturn>;
12
+ [Symbol.asyncIterator](): AsyncIterator<TValue, void>;
13
+ }
14
+ export interface IIsEquals<T> {
15
+ (prev: T, cur: T): boolean;
18
16
  }
17
+ export type IAllStreamConfig<T> = {
18
+ withCustomEquality: IIsEquals<T>;
19
+ withReactionOnSubscribe: boolean;
20
+ };
21
+ export type IThisStreamConfig = Partial<{
22
+ withReactionOnSubscribe: boolean;
23
+ externalDispose: PromiseConfiguration<any>;
24
+ }>;
@@ -0,0 +1,26 @@
1
+ import { IAllStreamConfig, IThisStreamConfig } from "./contracts.ts";
2
+ import { DependencyStream } from "./dependency.stream.ts";
3
+ export declare class Dependency<T = any> {
4
+ private _value;
5
+ private reactionPromise;
6
+ private abortPromise;
7
+ private config;
8
+ constructor(_value: T, config?: Partial<IAllStreamConfig<T>>);
9
+ private _set;
10
+ set value(v: T);
11
+ get value(): T;
12
+ getStream(this: Dependency<T>): DependencyStream<T>;
13
+ [Symbol.asyncIterator](this: Dependency<T>, thisStreamConfig?: IThisStreamConfig): {
14
+ owner: Dependency<T>;
15
+ dispose: () => void;
16
+ readonly isDisposed: boolean;
17
+ next: () => Promise<{
18
+ done: true;
19
+ value: void;
20
+ } | {
21
+ done: boolean;
22
+ readonly value: T;
23
+ }>;
24
+ };
25
+ dispose(this: Dependency<T>): void;
26
+ }
@@ -0,0 +1,14 @@
1
+ import { IDependencyStream } from "./contracts.ts";
2
+ import { symAI } from "../constants.ts";
3
+ import { Dependency } from "./dependency.ts";
4
+ export declare class DependencyStream<T = any> implements IDependencyStream {
5
+ readonly owner: Dependency;
6
+ private readonly selfDispose;
7
+ private readonly iterator;
8
+ get isDisposed(): boolean;
9
+ constructor(owner: Dependency);
10
+ [symAI](): {
11
+ next: () => Promise<IteratorResult<T, void>>;
12
+ };
13
+ dispose(): void;
14
+ }
@@ -1,4 +1,4 @@
1
- export * from './dependency-stream.ts';
1
+ export * from './dependency.ts';
2
+ export * from './dependency.stream.ts';
2
3
  export * from './utils/index.ts';
3
4
  export * from './integrations/index.ts';
4
- export * from './contracts.ts';
@@ -1,5 +1,5 @@
1
- import { DependencyStream } from "../../../index";
2
- export declare function useStream(...streams: DependencyStream[]): {
1
+ import { Dependency } from "../../../index";
2
+ export declare function useStream(...streams: Dependency[]): {
3
3
  value: Array<any> | undefined;
4
4
  dispose: () => void;
5
5
  };
@@ -1,6 +1,5 @@
1
- import { DependencyStream } from "../dependency-stream.ts";
2
- export declare function next<T>(dep: DependencyStream<T>): Promise<T>;
3
- /**
4
- * @internal
5
- */
6
- export declare const InternalStreamFinishError: Error;
1
+ import { Dependency } from "../dependency.ts";
2
+ export declare function next<T>(dep: Dependency<T>): {
3
+ promise: Promise<T>;
4
+ dispose: () => void;
5
+ };
@@ -1,3 +1,3 @@
1
- import { DependencyStream } from "../dependency-stream.ts";
2
- import { IStreamOwner } from "../contracts.ts";
3
- export declare function onceStream<T>(dep: DependencyStream<T>): IStreamOwner<T>;
1
+ import { Dependency } from "../dependency.ts";
2
+ import { IDependencyStream } from "../contracts.ts";
3
+ export declare function onceStream<T>(dep: Dependency<T>): IDependencyStream<T>;
@@ -1,12 +1,12 @@
1
- import { DependencyStream } from "../dependency-stream.ts";
1
+ import { Dependency } from "../dependency.ts";
2
2
  import { symAI } from "../../constants.ts";
3
- export declare function raceStream<TArray extends DependencyStream[]>(...deps: NoInfer<TArray>): {
3
+ export declare function raceStream<TArray extends Dependency[]>(...deps: NoInfer<TArray>): {
4
4
  dispose: () => void;
5
5
  readonly isDisposed: boolean;
6
6
  [Symbol.asyncIterator](): {
7
- next: () => Promise<IteratorReturnResult<void> | {
7
+ next: () => Promise<{
8
8
  done: boolean;
9
- value: any[];
9
+ readonly value: any;
10
10
  }>;
11
11
  };
12
12
  };
@@ -0,0 +1 @@
1
+ export declare function baseComparer<T>(prev: T, cur: T): boolean;
@@ -1,4 +1,4 @@
1
1
  export * from './dependency-stream/index.ts';
2
2
  export * from './dependency-stream/integrations/react/index.ts';
3
3
  export * from './delay.ts';
4
- export * from './get-promise.ts';
4
+ export * from './promise-configuration.ts';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fbltd/async",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "description": "Miscellaneous async utils",
5
5
  "homepage": "https://github.com/GlennMiller1991/async",
6
6
  "type": "module",
package/tsconfig.json CHANGED
@@ -4,6 +4,7 @@
4
4
  "compilerOptions": {
5
5
  "module": "NodeNext",
6
6
  "moduleResolution": "NodeNext",
7
+ "stripInternal": true,
7
8
  },
8
9
  }
9
10
  // @formatter:on
@@ -1,26 +0,0 @@
1
- import { IIteratorOwner, IStreamIterator } from "./contracts.ts";
2
- import { PromiseConfiguration } from "../get-promise.ts";
3
- interface IIsEquals<T> {
4
- (prev: T, cur: T): boolean;
5
- }
6
- type IAllStreamConfig<T> = {
7
- withCustomEquality: IIsEquals<T>;
8
- withReactionOnSubscribe: boolean;
9
- };
10
- type IThisStreamConfig = Partial<{
11
- withReactionOnSubscribe: boolean;
12
- externalDispose: PromiseConfiguration<any>;
13
- }>;
14
- export declare class DependencyStream<T = any> implements IIteratorOwner<T> {
15
- private _value;
16
- private reactionPromise;
17
- private abortPromise;
18
- private config;
19
- constructor(_value: T, config?: Partial<IAllStreamConfig<T>>);
20
- private _set;
21
- set value(v: T);
22
- get value(): T;
23
- [Symbol.asyncIterator](this: DependencyStream<T>, thisStreamConfig?: IThisStreamConfig): IStreamIterator<T>;
24
- dispose(this: DependencyStream<T>): void;
25
- }
26
- export {};