@fbltd/async 1.0.15 → 1.0.16

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 "./get-promise.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,11 @@
1
- import { getPromise } from "../get-promise.js";
1
+ import { PromiseConfiguration } from "../get-promise.js";
2
2
  function baseComparer(prev, cur) {
3
3
  return prev === cur;
4
4
  }
5
5
  export class DependencyStream {
6
6
  _value;
7
7
  reactionPromise;
8
- abortPromise = getPromise();
8
+ abortPromise = new PromiseConfiguration();
9
9
  config;
10
10
  constructor(_value, config = {}) {
11
11
  this._value = _value;
@@ -37,7 +37,7 @@ export class DependencyStream {
37
37
  let firstPromise;
38
38
  const withReactionOnSubscribe = this.config.withReactionOnSubscribe || thisStreamConfig.withReactionOnSubscribe;
39
39
  if (withReactionOnSubscribe) {
40
- firstPromise = getPromise();
40
+ firstPromise = new PromiseConfiguration();
41
41
  firstPromise.resolve(this.value);
42
42
  externalPromises.push(firstPromise.promise);
43
43
  }
@@ -54,7 +54,7 @@ export class DependencyStream {
54
54
  },
55
55
  next: async () => {
56
56
  if (!this.reactionPromise) {
57
- this.reactionPromise = getPromise();
57
+ this.reactionPromise = new PromiseConfiguration();
58
58
  this._set(this._value);
59
59
  }
60
60
  await Promise.race([
@@ -80,7 +80,7 @@ export class DependencyStream {
80
80
  }
81
81
  dispose() {
82
82
  this.abortPromise.resolve();
83
- this.abortPromise = getPromise();
83
+ this.abortPromise = new PromiseConfiguration();
84
84
  this.reactionPromise = undefined;
85
85
  }
86
86
  }
@@ -1,7 +1,7 @@
1
- import { getPromise } from "../../get-promise.js";
1
+ import { PromiseConfiguration } from "../../get-promise.js";
2
2
  import { symAI } from "../../constants.js";
3
3
  export function onceStream(dep) {
4
- const externalDispose = getPromise();
4
+ const externalDispose = new PromiseConfiguration();
5
5
  const iterator = dep[symAI]({ externalDispose });
6
6
  function isDisposed() {
7
7
  return externalDispose.isFulfilled || iterator.isDisposed;
@@ -1,7 +1,7 @@
1
1
  import { symAI } from "../../constants.js";
2
- import { getPromise } from "../../get-promise.js";
2
+ import { PromiseConfiguration } from "../../get-promise.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
@@ -1,29 +1,30 @@
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;
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
+ };
11
16
  });
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
- };
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
+ }
29
30
  }
@@ -1,5 +1,5 @@
1
- import { IPromiseConfiguration } from "../get-promise.ts";
2
1
  import { IIteratorOwner, IStreamIterator } from "./contracts.ts";
2
+ import { PromiseConfiguration } from "../get-promise.ts";
3
3
  interface IIsEquals<T> {
4
4
  (prev: T, cur: T): boolean;
5
5
  }
@@ -9,7 +9,7 @@ type IAllStreamConfig<T> = {
9
9
  };
10
10
  type IThisStreamConfig = Partial<{
11
11
  withReactionOnSubscribe: boolean;
12
- externalDispose: IPromiseConfiguration<any>;
12
+ externalDispose: PromiseConfiguration<any>;
13
13
  }>;
14
14
  export declare class DependencyStream<T = any> implements IIteratorOwner<T> {
15
15
  private _value;
@@ -1,8 +1,11 @@
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>>;
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.16",
4
4
  "description": "Miscellaneous async utils",
5
5
  "homepage": "https://github.com/GlennMiller1991/async",
6
6
  "type": "module",