@fbltd/async 1.0.15 → 1.0.17

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
@@ -19,15 +19,15 @@ await delay(number);
19
19
  delay(number, 'sync');
20
20
  ```
21
21
 
22
- ### getPromise
23
- getPromise is a function that creates and returns promise, its fulfillment functions
24
- and status flags.
25
- Returned type is:
22
+ ### PromiseConfiguration
23
+ PromiseConfiguration is a class that creates and provide promise,
24
+ its fulfillment functions and status flags.
25
+ Instance of the class is represented by the following type:
26
26
  ```typescript
27
27
  type IPromiseConfiguration<T> = {
28
- resolve(arg: T): void, // function that resolves promise
29
- reject(err: Error): void, // function that rejects promise
30
- promise: Promise<T>, // promise itself
28
+ readonly resolve: (arg: T) => void, // function that resolves promise
29
+ readonly reject: (err: Error) => void, // function that rejects promise
30
+ readonly promise: Promise<T>, // promise itself
31
31
  readonly isPending: boolean, // promise status flag
32
32
  readonly isFulfilled: boolean, // reverted promise status flag
33
33
  }
package/dist/bin/delay.js CHANGED
@@ -1,4 +1,4 @@
1
- import { getPromise } 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);
@@ -14,7 +14,7 @@ Object.defineProperty(delay, 'methods', {
14
14
  }
15
15
  },
16
16
  undefined: (ms) => {
17
- const { promise, resolve } = getPromise();
17
+ const { promise, resolve } = new PromiseConfiguration();
18
18
  setTimeout(() => {
19
19
  resolve();
20
20
  }, ms);
@@ -1,11 +1,10 @@
1
- import { getPromise } 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
- abortPromise = getPromise();
7
+ abortPromise = new PromiseConfiguration();
9
8
  config;
10
9
  constructor(_value, config = {}) {
11
10
  this._value = _value;
@@ -31,13 +30,16 @@ 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];
37
39
  let firstPromise;
38
40
  const withReactionOnSubscribe = this.config.withReactionOnSubscribe || thisStreamConfig.withReactionOnSubscribe;
39
41
  if (withReactionOnSubscribe) {
40
- firstPromise = getPromise();
42
+ firstPromise = new PromiseConfiguration();
41
43
  firstPromise.resolve(this.value);
42
44
  externalPromises.push(firstPromise.promise);
43
45
  }
@@ -54,7 +56,7 @@ export class DependencyStream {
54
56
  },
55
57
  next: async () => {
56
58
  if (!this.reactionPromise) {
57
- this.reactionPromise = getPromise();
59
+ this.reactionPromise = new PromiseConfiguration();
58
60
  this._set(this._value);
59
61
  }
60
62
  await Promise.race([
@@ -80,7 +82,7 @@ export class DependencyStream {
80
82
  }
81
83
  dispose() {
82
84
  this.abortPromise.resolve();
83
- this.abortPromise = getPromise();
85
+ this.abortPromise = new PromiseConfiguration();
84
86
  this.reactionPromise = undefined;
85
87
  }
86
88
  }
@@ -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,22 +1,18 @@
1
- import { getPromise } from "../../get-promise.js";
2
1
  import { symAI } from "../../constants.js";
3
2
  export function onceStream(dep) {
4
- const externalDispose = getPromise();
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,7 +1,7 @@
1
1
  import { symAI } from "../../constants.js";
2
- import { getPromise } from "../../get-promise.js";
2
+ import { PromiseConfiguration } from "../../promise-configuration.js";
3
3
  export function raceStream(...deps) {
4
- let selfDisposePromise = getPromise();
4
+ let selfDisposePromise = new PromiseConfiguration();
5
5
  let isDisposed = false;
6
6
  const streams = deps.map((dep) => dep[symAI]({
7
7
  externalDispose: selfDisposePromise
@@ -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";
@@ -0,0 +1,30 @@
1
+ export class PromiseConfiguration {
2
+ promise;
3
+ _resolve;
4
+ _reject;
5
+ _isFulfilled = false;
6
+ constructor() {
7
+ this.promise = new Promise((res, rej) => {
8
+ this._resolve = ((value) => {
9
+ res(value);
10
+ this._isFulfilled = true;
11
+ });
12
+ this._reject = (error) => {
13
+ rej(error);
14
+ this._isFulfilled = true;
15
+ };
16
+ });
17
+ }
18
+ get reject() {
19
+ return this._reject;
20
+ }
21
+ get resolve() {
22
+ return this._resolve;
23
+ }
24
+ get isFulfilled() {
25
+ return this._isFulfilled;
26
+ }
27
+ get isPending() {
28
+ return !this.isFulfilled;
29
+ }
30
+ }
@@ -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,2 @@
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>): Promise<T>;
@@ -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';
@@ -0,0 +1,11 @@
1
+ export declare class PromiseConfiguration<TReturn = void> {
2
+ readonly promise: Promise<TReturn>;
3
+ _resolve: (value: TReturn) => void;
4
+ _reject: (value: Error) => void;
5
+ _isFulfilled: boolean;
6
+ constructor();
7
+ get reject(): (value: Error) => void;
8
+ get resolve(): (value: TReturn) => void;
9
+ get isFulfilled(): boolean;
10
+ get isPending(): boolean;
11
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fbltd/async",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
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,29 +0,0 @@
1
- export function getPromise() {
2
- let resolve;
3
- let reject;
4
- let isPending = true;
5
- let isFulfilled = false;
6
- const promise = new Promise((res, rej) => {
7
- resolve = ((value) => {
8
- res(value);
9
- isPending = false;
10
- isFulfilled = true;
11
- });
12
- reject = (error) => {
13
- rej(error);
14
- isPending = false;
15
- isFulfilled = true;
16
- };
17
- });
18
- return {
19
- resolve,
20
- reject,
21
- get isPending() {
22
- return isPending;
23
- },
24
- get isFulfilled() {
25
- return isFulfilled;
26
- },
27
- promise
28
- };
29
- }
@@ -1,26 +0,0 @@
1
- import { IPromiseConfiguration } from "../get-promise.ts";
2
- import { IIteratorOwner, IStreamIterator } from "./contracts.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: IPromiseConfiguration<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 {};
@@ -1,8 +0,0 @@
1
- export declare function getPromise<TReturn = void>(): {
2
- resolve: (value: TReturn) => void;
3
- reject: (value: Error) => void;
4
- readonly isPending: boolean;
5
- readonly isFulfilled: boolean;
6
- promise: Promise<TReturn>;
7
- };
8
- export type IPromiseConfiguration<T = void> = ReturnType<typeof getPromise<T>>;