@fbltd/async 1.0.19 → 1.0.23

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.
Files changed (43) hide show
  1. package/dist/bin/{dependency-stream → dependency}/dependency.js +2 -0
  2. package/dist/bin/dependency/global.js +24 -0
  3. package/dist/bin/dependency/integrations/react/index.js +1 -0
  4. package/dist/bin/{dependency-stream/integrations/react/src → dependency/integrations/react}/race-stream.controller.js +5 -9
  5. package/dist/bin/dependency/integrations/react/use-race.stream.js +17 -0
  6. package/dist/bin/dependency/integrations/react/utils.js +7 -0
  7. package/dist/bin/{dependency-stream → dependency}/stream-utils/index.js +1 -0
  8. package/dist/bin/dependency/stream-utils/reaction.js +21 -0
  9. package/dist/bin/index.js +2 -2
  10. package/dist/types/dependency/global.d.ts +11 -0
  11. package/dist/types/dependency/integrations/react/index.d.ts +1 -0
  12. package/dist/types/{dependency-stream/integrations/react/src → dependency/integrations/react}/race-stream.controller.d.ts +1 -1
  13. package/dist/types/{dependency-stream/integrations/react/src → dependency/integrations/react}/use-race.stream.d.ts +1 -1
  14. package/dist/types/{dependency-stream → dependency}/stream-utils/index.d.ts +1 -0
  15. package/dist/types/dependency/stream-utils/reaction.d.ts +11 -0
  16. package/dist/types/index.d.ts +2 -2
  17. package/package.json +8 -4
  18. package/dist/bin/dependency-stream/integrations/react/index.js +0 -17
  19. package/dist/bin/dependency-stream/integrations/react/src/index.js +0 -22
  20. package/dist/bin/dependency-stream/integrations/react/src/use-race.stream.js +0 -20
  21. package/dist/bin/dependency-stream/integrations/react/src/utils.js +0 -10
  22. package/dist/types/dependency-stream/integrations/react/index.d.ts +0 -1
  23. package/dist/types/dependency-stream/integrations/react/src/index.d.ts +0 -3
  24. /package/dist/bin/{dependency-stream → dependency}/contracts.js +0 -0
  25. /package/dist/bin/{dependency-stream → dependency}/dependency.stream.js +0 -0
  26. /package/dist/bin/{dependency-stream → dependency}/index.js +0 -0
  27. /package/dist/bin/{dependency-stream → dependency}/integrations/index.js +0 -0
  28. /package/dist/bin/{dependency-stream → dependency}/stream-utils/contracts.js +0 -0
  29. /package/dist/bin/{dependency-stream → dependency}/stream-utils/next.js +0 -0
  30. /package/dist/bin/{dependency-stream → dependency}/stream-utils/once.stream.js +0 -0
  31. /package/dist/bin/{dependency-stream → dependency}/stream-utils/race.stream.js +0 -0
  32. /package/dist/bin/{dependency-stream → dependency}/utils.js +0 -0
  33. /package/dist/types/{dependency-stream → dependency}/contracts.d.ts +0 -0
  34. /package/dist/types/{dependency-stream → dependency}/dependency.d.ts +0 -0
  35. /package/dist/types/{dependency-stream → dependency}/dependency.stream.d.ts +0 -0
  36. /package/dist/types/{dependency-stream → dependency}/index.d.ts +0 -0
  37. /package/dist/types/{dependency-stream → dependency}/integrations/index.d.ts +0 -0
  38. /package/dist/types/{dependency-stream/integrations/react/src → dependency/integrations/react}/utils.d.ts +0 -0
  39. /package/dist/types/{dependency-stream → dependency}/stream-utils/contracts.d.ts +0 -0
  40. /package/dist/types/{dependency-stream → dependency}/stream-utils/next.d.ts +0 -0
  41. /package/dist/types/{dependency-stream → dependency}/stream-utils/once.stream.d.ts +0 -0
  42. /package/dist/types/{dependency-stream → dependency}/stream-utils/race.stream.d.ts +0 -0
  43. /package/dist/types/{dependency-stream → dependency}/utils.d.ts +0 -0
@@ -1,6 +1,7 @@
1
1
  import { PromiseConfiguration } from "../promise-configuration.js";
2
2
  import { DependencyStream } from "./dependency.stream.js";
3
3
  import { baseComparer } from "./utils.js";
4
+ import { setDep } from "./global.js";
4
5
  export class Dependency {
5
6
  _value;
6
7
  reactionPromise;
@@ -28,6 +29,7 @@ export class Dependency {
28
29
  this._set(v);
29
30
  }
30
31
  get value() {
32
+ setDep(this);
31
33
  return this._value;
32
34
  }
33
35
  getStream() {
@@ -0,0 +1,24 @@
1
+ const global = {
2
+ watchFlag: false,
3
+ dependencies: null
4
+ };
5
+ const notImplemented = new Error('Watching while watching is not implemented');
6
+ export function watchDeps(fn) {
7
+ if (global.watchFlag) {
8
+ throw notImplemented;
9
+ }
10
+ global.watchFlag = true;
11
+ global.dependencies = new Set();
12
+ let result = fn();
13
+ let deps = global.dependencies;
14
+ global.dependencies = null;
15
+ global.watchFlag = false;
16
+ return {
17
+ result, deps
18
+ };
19
+ }
20
+ export function setDep(dep) {
21
+ if (!global.watchFlag)
22
+ return;
23
+ global.dependencies.add(dep);
24
+ }
@@ -0,0 +1 @@
1
+ export * from "./use-race.stream.js";
@@ -1,9 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RaceStreamController = void 0;
4
- const utils_1 = require("./utils");
5
- const stream_utils_1 = require("../../../stream-utils");
6
- class RaceStreamController {
1
+ import { isObjectsContentEqual } from "./utils.js";
2
+ import { raceStream } from "../../stream-utils/index.js";
3
+ export class RaceStreamController {
7
4
  _value;
8
5
  isFirstWas = false;
9
6
  rerenderTrigger;
@@ -17,7 +14,7 @@ class RaceStreamController {
17
14
  return this._value;
18
15
  }
19
16
  async init() {
20
- this.iterator = (0, stream_utils_1.raceStream)(this.deps);
17
+ this.iterator = raceStream(this.deps);
21
18
  this._value = Object.entries(this.deps)
22
19
  .reduce((acc, [key, dep]) => {
23
20
  acc[key] = dep.value;
@@ -25,7 +22,7 @@ class RaceStreamController {
25
22
  }, {});
26
23
  for await (let value of this.iterator) {
27
24
  this.rerenderTrigger?.(prev => !prev);
28
- if (!this.isFirstWas && !(0, utils_1.isObjectsContentEqual)(this._value, value)) {
25
+ if (!this.isFirstWas && !isObjectsContentEqual(this._value, value)) {
29
26
  this._value = value;
30
27
  }
31
28
  this.isFirstWas = true;
@@ -35,4 +32,3 @@ class RaceStreamController {
35
32
  this.iterator.dispose();
36
33
  };
37
34
  }
38
- exports.RaceStreamController = RaceStreamController;
@@ -0,0 +1,17 @@
1
+ import { useEffect, useState } from "react";
2
+ import { RaceStreamController } from "./race-stream.controller.js";
3
+ export function useRaceStream(deps) {
4
+ const [obj] = useState(() => ({ controller: new RaceStreamController(deps) }));
5
+ const [, setValue] = useState(false);
6
+ obj.controller.rerenderTrigger = setValue;
7
+ useEffect(() => {
8
+ return () => {
9
+ obj.controller.dispose();
10
+ obj.controller = undefined;
11
+ };
12
+ }, []);
13
+ return {
14
+ value: obj.controller.value,
15
+ dispose: obj.controller.dispose,
16
+ };
17
+ }
@@ -0,0 +1,7 @@
1
+ export function isObjectsContentEqual(obj1, obj2) {
2
+ for (let key in obj1) {
3
+ if (obj1[key] !== obj2[key])
4
+ return false;
5
+ }
6
+ return true;
7
+ }
@@ -2,3 +2,4 @@ export * from "./once.stream.js";
2
2
  export * from "./race.stream.js";
3
3
  export * from "./next.js";
4
4
  export * from "./contracts.js";
5
+ export * from "./reaction.js";
@@ -0,0 +1,21 @@
1
+ import { watchDeps } from "../global.js";
2
+ import { symAI } from "../../constants.js";
3
+ /**
4
+ * @deprecated
5
+ */
6
+ export function reaction(fn) {
7
+ return {
8
+ [Symbol.asyncIterator]: () => {
9
+ let { result, deps } = watchDeps(fn);
10
+ return {
11
+ next: async () => {
12
+ const dependencies = Array.from(deps);
13
+ const streams = dependencies.map(dep => dep[symAI]());
14
+ await Promise.race(streams.map(s => s.next()));
15
+ ({ result, deps } = watchDeps(fn));
16
+ return { done: false, value: result };
17
+ }
18
+ };
19
+ }
20
+ };
21
+ }
package/dist/bin/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export * from "./dependency-stream/index.js";
2
- export * from "./dependency-stream/integrations/react/index.js";
1
+ export * from "./dependency/index.js";
2
+ export * from "./dependency/integrations/react/index.js";
3
3
  export * from "./delay.js";
4
4
  export * from "./promise-configuration.js";
@@ -0,0 +1,11 @@
1
+ import { Dependency } from "./dependency.js";
2
+ declare const global: {
3
+ watchFlag: boolean;
4
+ dependencies: Set<Dependency>;
5
+ };
6
+ export declare function watchDeps<T>(fn: () => T): {
7
+ result: T;
8
+ deps: typeof global.dependencies;
9
+ };
10
+ export declare function setDep(dep: Dependency): void;
11
+ export {};
@@ -0,0 +1 @@
1
+ export * from './use-race.stream.ts';
@@ -1,5 +1,5 @@
1
1
  import { Dispatch, SetStateAction } from "react";
2
- import { IDepObjectArgument, IDepObjectReturn, raceStream } from "../../../stream-utils";
2
+ import { IDepObjectArgument, IDepObjectReturn, raceStream } from "../../stream-utils/index.ts";
3
3
  export declare class RaceStreamController<T extends IDepObjectArgument> {
4
4
  private _value;
5
5
  private isFirstWas;
@@ -1,4 +1,4 @@
1
- import { IDepObjectArgument, IDepObjectReturn } from "../../../index";
1
+ import { IDepObjectArgument, IDepObjectReturn } from "../../index.ts";
2
2
  export declare function useRaceStream<T extends IDepObjectArgument>(deps: T): {
3
3
  value: IDepObjectReturn<T>;
4
4
  dispose: Function;
@@ -2,3 +2,4 @@ export * from "./once.stream.ts";
2
2
  export * from "./race.stream.ts";
3
3
  export * from "./next.ts";
4
4
  export * from "./contracts.ts";
5
+ export * from "./reaction.ts";
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @deprecated
3
+ */
4
+ export declare function reaction<T>(fn: () => T): {
5
+ [Symbol.asyncIterator]: () => {
6
+ next: () => Promise<{
7
+ done: boolean;
8
+ value: T;
9
+ }>;
10
+ };
11
+ };
@@ -1,4 +1,4 @@
1
- export * from './dependency-stream/index.ts';
2
- export * from './dependency-stream/integrations/react/index.ts';
1
+ export * from './dependency/index.ts';
2
+ export * from './dependency/integrations/react/index.ts';
3
3
  export * from './delay.ts';
4
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.19",
3
+ "version": "1.0.23",
4
4
  "description": "Miscellaneous async utils",
5
5
  "homepage": "https://github.com/GlennMiller1991/async",
6
6
  "type": "module",
@@ -21,10 +21,10 @@
21
21
  "tsconfig.json"
22
22
  ],
23
23
  "scripts": {
24
- "clearDist": "rm dist -rf || true",
25
- "clearModules": "rm node_modules -rf || true",
24
+ "clearDist": "rm dist -r || true",
25
+ "clearModules": "rm node_modules -r || true",
26
26
  "clearAll": "npm run clearDist && npm run clearModules",
27
- "build": "cd src/dependency-stream/integrations/react && npm run build && cd ../../../../ && npm run clearAll && npm i && mkdir dist && tsc",
27
+ "build": "npm run clearAll && npm i && mkdir dist && tsc",
28
28
  "test": "node --expose-gc node_modules/.bin/jest --config=./__tests__/jest.config.cjs",
29
29
  "postVersionCommit": "git commit -m='post version commit' || true",
30
30
  "postVersionPush": "git push || true",
@@ -33,8 +33,12 @@
33
33
  },
34
34
  "author": "",
35
35
  "license": "ISC",
36
+ "dependencies": {
37
+ "react": "^19.x.x"
38
+ },
36
39
  "devDependencies": {
37
40
  "@types/jest": "^30.0.0",
41
+ "@types/react": "^19.x.x",
38
42
  "jest": "^30.0.4",
39
43
  "ts-jest": "^29.4.0",
40
44
  "typescript": "^5.x.x"
@@ -1,17 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./src/index.js"), exports);
@@ -1,22 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.isObjectsContentEqual = exports.RaceStreamController = void 0;
18
- __exportStar(require("./use-race.stream.js"), exports);
19
- var race_stream_controller_1 = require("./race-stream.controller");
20
- Object.defineProperty(exports, "RaceStreamController", { enumerable: true, get: function () { return race_stream_controller_1.RaceStreamController; } });
21
- var utils_1 = require("./utils");
22
- Object.defineProperty(exports, "isObjectsContentEqual", { enumerable: true, get: function () { return utils_1.isObjectsContentEqual; } });
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useRaceStream = useRaceStream;
4
- const react_1 = require("react");
5
- const race_stream_controller_1 = require("./race-stream.controller");
6
- function useRaceStream(deps) {
7
- const [obj] = (0, react_1.useState)(() => ({ controller: new race_stream_controller_1.RaceStreamController(deps) }));
8
- const [, setValue] = (0, react_1.useState)(false);
9
- obj.controller.rerenderTrigger = setValue;
10
- (0, react_1.useEffect)(() => {
11
- return () => {
12
- obj.controller.dispose();
13
- obj.controller = undefined;
14
- };
15
- }, []);
16
- return {
17
- value: obj.controller.value,
18
- dispose: obj.controller.dispose,
19
- };
20
- }
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isObjectsContentEqual = isObjectsContentEqual;
4
- function isObjectsContentEqual(obj1, obj2) {
5
- for (let key in obj1) {
6
- if (obj1[key] !== obj2[key])
7
- return false;
8
- }
9
- return true;
10
- }
@@ -1 +0,0 @@
1
- export * from './src/index.ts';
@@ -1,3 +0,0 @@
1
- export * from './use-race.stream.ts';
2
- export { RaceStreamController } from "./race-stream.controller";
3
- export { isObjectsContentEqual } from "./utils";