@quantform/core 0.7.0-beta.16 → 0.7.0-beta.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.
Files changed (46) hide show
  1. package/dist/cli/dev.d.ts.map +1 -1
  2. package/dist/cli/dev.js +3 -36
  3. package/dist/cli/internal/script.d.ts +8 -0
  4. package/dist/cli/internal/script.d.ts.map +1 -0
  5. package/dist/cli/internal/script.js +55 -0
  6. package/dist/cli/pull.d.ts.map +1 -1
  7. package/dist/cli/pull.js +3 -5
  8. package/dist/index.d.ts +2 -5
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +2 -5
  11. package/dist/use-memo.d.ts +0 -10
  12. package/dist/use-memo.d.ts.map +1 -1
  13. package/dist/use-memo.js +0 -10
  14. package/dist/{use-socket.d.ts → when-socket.d.ts} +2 -2
  15. package/dist/when-socket.d.ts.map +1 -0
  16. package/dist/{use-socket.js → when-socket.js} +3 -3
  17. package/dist/{use-request.d.ts → with-request.d.ts} +2 -2
  18. package/dist/with-request.d.ts.map +1 -0
  19. package/dist/with-request.js +26 -0
  20. package/package.json +1 -1
  21. package/src/cli/dev.ts +4 -20
  22. package/src/cli/internal/script.ts +23 -0
  23. package/src/cli/pull.ts +3 -6
  24. package/src/index.ts +2 -5
  25. package/src/use-memo.ts +0 -10
  26. package/src/{use-socket.ts → when-socket.ts} +1 -1
  27. package/src/{use-request.ts → with-request.ts} +6 -14
  28. package/dist/strat.d.ts +0 -7
  29. package/dist/strat.d.ts.map +0 -1
  30. package/dist/strat.js +0 -7
  31. package/dist/use-lock.d.ts +0 -9
  32. package/dist/use-lock.d.ts.map +0 -1
  33. package/dist/use-lock.js +0 -40
  34. package/dist/use-request.d.ts.map +0 -1
  35. package/dist/use-request.js +0 -33
  36. package/dist/use-socket.d.ts.map +0 -1
  37. package/dist/use-state.d.ts +0 -4
  38. package/dist/use-state.d.ts.map +0 -1
  39. package/dist/use-state.js +0 -24
  40. package/dist/use-state.spec.d.ts +0 -2
  41. package/dist/use-state.spec.d.ts.map +0 -1
  42. package/dist/use-state.spec.js +0 -36
  43. package/src/strat.ts +0 -7
  44. package/src/use-lock.ts +0 -52
  45. package/src/use-state.spec.ts +0 -31
  46. package/src/use-state.ts +0 -30
@@ -1 +1 @@
1
- {"version":3,"file":"dev.d.ts","sourceRoot":"","sources":["../../src/cli/dev.ts"],"names":[],"mappings":"AAUA,yBAA+B,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,iBAoBxD"}
1
+ {"version":3,"file":"dev.d.ts","sourceRoot":"","sources":["../../src/cli/dev.ts"],"names":[],"mappings":"AAKA,yBAA+B,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,iBASxD"}
package/dist/cli/dev.js CHANGED
@@ -1,27 +1,4 @@
1
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -35,26 +12,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
35
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
13
  };
37
14
  Object.defineProperty(exports, "__esModule", { value: true });
38
- const path_1 = require("path");
39
- const rxjs_1 = require("rxjs");
40
15
  const build_1 = __importDefault(require("../cli/build"));
41
- const workspace_1 = require("../cli/internal/workspace");
42
- const core_1 = require("../core");
43
- const module_1 = require("../module");
44
16
  const use_execution_mode_1 = require("../use-execution-mode");
17
+ const script_1 = require("./internal/script");
45
18
  function default_1(name, options) {
46
19
  return __awaiter(this, void 0, void 0, function* () {
47
20
  if (yield (0, build_1.default)()) {
48
21
  return;
49
22
  }
50
- const script = (yield Promise.resolve(`${(0, path_1.join)((0, workspace_1.buildDirectory)(), name)}`).then(s => __importStar(require(s)))).default;
51
- const module = new module_1.Module([
52
- ...(0, core_1.core)(),
53
- ...script.dependencies,
54
- (0, use_execution_mode_1.paperExecutionMode)({ recording: false })
55
- ]);
56
- const { act } = yield module.awake();
57
- const output = yield act(() => (0, rxjs_1.lastValueFrom)(script.fn()));
23
+ const script = new script_1.Script(name, [(0, use_execution_mode_1.paperExecutionMode)({ recording: false })]);
24
+ const output = yield script.runWhileFinished();
58
25
  console.log(output);
59
26
  });
60
27
  }
@@ -0,0 +1,8 @@
1
+ import { Dependency } from '../../module';
2
+ export declare class Script {
3
+ private readonly name;
4
+ private readonly dependencies;
5
+ constructor(name: string, dependencies: Dependency[]);
6
+ runWhileFinished(): Promise<unknown>;
7
+ }
8
+ //# sourceMappingURL=script.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script.d.ts","sourceRoot":"","sources":["../../../src/cli/internal/script.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,UAAU,EAAU,MAAM,aAAa,CAAC;AAIjD,qBAAa,MAAM;IAEf,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,YAAY;gBADZ,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,UAAU,EAAE;IAGvC,gBAAgB;CAQvB"}
@@ -0,0 +1,55 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.Script = void 0;
36
+ const path_1 = require("path");
37
+ const rxjs_1 = require("rxjs");
38
+ const core_1 = require("../../core");
39
+ const module_1 = require("../../module");
40
+ const workspace_1 = require("./workspace");
41
+ class Script {
42
+ constructor(name, dependencies) {
43
+ this.name = name;
44
+ this.dependencies = dependencies;
45
+ }
46
+ runWhileFinished() {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ const script = yield Promise.resolve(`${(0, path_1.join)((0, workspace_1.buildDirectory)(), this.name)}`).then(s => __importStar(require(s)));
49
+ const module = new module_1.Module([...(0, core_1.core)(), ...script.onInstall(), ...this.dependencies]);
50
+ const { act } = yield module.awake();
51
+ return yield act(() => (0, rxjs_1.lastValueFrom)(script.onAwake()));
52
+ });
53
+ }
54
+ }
55
+ exports.Script = Script;
@@ -1 +1 @@
1
- {"version":3,"file":"pull.d.ts","sourceRoot":"","sources":["../../src/cli/pull.ts"],"names":[],"mappings":"AAUA,yBAA+B,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,iBAgE5E"}
1
+ {"version":3,"file":"pull.d.ts","sourceRoot":"","sources":["../../src/cli/pull.ts"],"names":[],"mappings":"AAQA,yBAA+B,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,iBA+D5E"}
package/dist/cli/pull.js CHANGED
@@ -41,22 +41,20 @@ const workspace_1 = require("../cli/internal/workspace");
41
41
  const core_1 = require("../core");
42
42
  const module_1 = require("../module");
43
43
  const use_execution_mode_1 = require("../use-execution-mode");
44
- const use_memo_1 = require("../use-memo");
45
44
  function default_1(name, instrument, options) {
46
45
  return __awaiter(this, void 0, void 0, function* () {
47
46
  if (yield (0, build_1.default)()) {
48
47
  return;
49
48
  }
50
49
  yield Promise.resolve(`${(0, path_1.join)((0, workspace_1.buildDirectory)(), 'index')}`).then(s => __importStar(require(s)));
51
- const script = (yield Promise.resolve(`${(0, path_1.join)((0, workspace_1.buildDirectory)(), name)}`).then(s => __importStar(require(s))));
50
+ const script = yield Promise.resolve(`${(0, path_1.join)((0, workspace_1.buildDirectory)(), name)}`).then(s => __importStar(require(s)));
52
51
  const module = new module_1.Module([
53
52
  ...(0, core_1.core)(),
54
- ...script.dependencies,
53
+ ...script.module,
55
54
  (0, use_execution_mode_1.paperExecutionMode)({ recording: false })
56
55
  ]);
57
56
  const { act } = yield module.awake();
58
- const o = yield act(() => script.fn());
59
- console.log(module.get(use_memo_1.token));
57
+ const output = yield act(() => script.default());
60
58
  /*const builder = new SessionBuilder().useSessionId(
61
59
  options.id ? Number(options.id) : now()
62
60
  );
package/dist/index.d.ts CHANGED
@@ -7,7 +7,6 @@ export * from './use-timestamp';
7
7
  export * from './simulator';
8
8
  export * from './make-test-module';
9
9
  export * from './core';
10
- export * from './use-state';
11
10
  export * from './replay/use-replay-coordinator';
12
11
  export * from './use-execution-mode';
13
12
  export * from './storage/use-storage';
@@ -15,14 +14,12 @@ export * from './storage/use-cache';
15
14
  export * from './use-logger';
16
15
  export * from './replay';
17
16
  export * from './replay/use-replay-coordinator';
18
- export * from './use-socket';
19
- export * from './use-request';
17
+ export * from './when-socket';
18
+ export * from './with-request';
20
19
  export * from './defined';
21
20
  export * from './as-readonly';
22
21
  export * from './with-memo';
23
22
  export * from './session';
24
23
  export * from './exclude';
25
- export * from './use-lock';
26
- export * from './strat';
27
24
  export * from './errored';
28
25
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,oCAAoC,CAAC;AACnD,cAAc,yBAAyB,CAAC;AACxC,cAAc,0BAA0B,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,oCAAoC,CAAC;AACnD,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,WAAW,CAAC;AAC1B,cAAc,oCAAoC,CAAC;AACnD,cAAc,yBAAyB,CAAC;AACxC,cAAc,0BAA0B,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,oCAAoC,CAAC;AACnD,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC"}
package/dist/index.js CHANGED
@@ -23,7 +23,6 @@ __exportStar(require("./use-timestamp"), exports);
23
23
  __exportStar(require("./simulator"), exports);
24
24
  __exportStar(require("./make-test-module"), exports);
25
25
  __exportStar(require("./core"), exports);
26
- __exportStar(require("./use-state"), exports);
27
26
  __exportStar(require("./replay/use-replay-coordinator"), exports);
28
27
  __exportStar(require("./use-execution-mode"), exports);
29
28
  __exportStar(require("./storage/use-storage"), exports);
@@ -31,13 +30,11 @@ __exportStar(require("./storage/use-cache"), exports);
31
30
  __exportStar(require("./use-logger"), exports);
32
31
  __exportStar(require("./replay"), exports);
33
32
  __exportStar(require("./replay/use-replay-coordinator"), exports);
34
- __exportStar(require("./use-socket"), exports);
35
- __exportStar(require("./use-request"), exports);
33
+ __exportStar(require("./when-socket"), exports);
34
+ __exportStar(require("./with-request"), exports);
36
35
  __exportStar(require("./defined"), exports);
37
36
  __exportStar(require("./as-readonly"), exports);
38
37
  __exportStar(require("./with-memo"), exports);
39
38
  __exportStar(require("./session"), exports);
40
39
  __exportStar(require("./exclude"), exports);
41
- __exportStar(require("./use-lock"), exports);
42
- __exportStar(require("./strat"), exports);
43
40
  __exportStar(require("./errored"), exports);
@@ -7,15 +7,5 @@ export declare function memo(): {
7
7
  provide: symbol;
8
8
  useValue: {};
9
9
  };
10
- /**
11
- * @name useMemo
12
- * @description
13
- * A hook that caches the result of an expensive calculation, based on a set of dependencies.
14
- *
15
- * @template T The type of the value that is memoized.
16
- * @param {() => T} calculateValue A function that returns the value to memoize.
17
- * @param {dependency[]} dependencies An array of dependencies that determine when the value should be recalculated.
18
- * @returns {T} The memoized value.
19
- */
20
10
  export declare function useMemo<T>(calculateValue: () => T, dependencies: dependency[]): T;
21
11
  //# sourceMappingURL=use-memo.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-memo.d.ts","sourceRoot":"","sources":["../src/use-memo.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAW,MAAM,eAAe,CAAC;AAEpD,eAAO,MAAM,KAAK,eAAuB,CAAC;AAE1C;;GAEG;AACH,wBAAgB,IAAI;;;EAKnB;AAED;;;;;;;;;GASG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,cAAc,EAAE,MAAM,CAAC,EAAE,YAAY,EAAE,UAAU,EAAE,KAS7E"}
1
+ {"version":3,"file":"use-memo.d.ts","sourceRoot":"","sources":["../src/use-memo.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAW,MAAM,eAAe,CAAC;AAEpD,eAAO,MAAM,KAAK,eAAuB,CAAC;AAE1C;;GAEG;AACH,wBAAgB,IAAI;;;EAKnB;AAED,wBAAgB,OAAO,CAAC,CAAC,EAAE,cAAc,EAAE,MAAM,CAAC,EAAE,YAAY,EAAE,UAAU,EAAE,KAS7E"}
package/dist/use-memo.js CHANGED
@@ -14,16 +14,6 @@ function memo() {
14
14
  };
15
15
  }
16
16
  exports.memo = memo;
17
- /**
18
- * @name useMemo
19
- * @description
20
- * A hook that caches the result of an expensive calculation, based on a set of dependencies.
21
- *
22
- * @template T The type of the value that is memoized.
23
- * @param {() => T} calculateValue A function that returns the value to memoize.
24
- * @param {dependency[]} dependencies An array of dependencies that determine when the value should be recalculated.
25
- * @returns {T} The memoized value.
26
- */
27
17
  function useMemo(calculateValue, dependencies) {
28
18
  const memory = (0, module_1.useContext)(exports.token);
29
19
  const hash = (0, use_hash_1.useHash)(dependencies);
@@ -1,6 +1,6 @@
1
1
  import { Observable } from 'rxjs';
2
- export declare function useSocket(url: string): [Observable<{
2
+ export declare function whenSocket(url: string): [Observable<{
3
3
  timestamp: number;
4
4
  payload: unknown;
5
5
  }>, (message: unknown) => void];
6
- //# sourceMappingURL=use-socket.d.ts.map
6
+ //# sourceMappingURL=when-socket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"when-socket.d.ts","sourceRoot":"","sources":["../src/when-socket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAMlC,wBAAgB,UAAU,CACxB,GAAG,EAAE,MAAM,GACV,CAAC,UAAU,CAAC;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC,CAmDnF"}
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useSocket = void 0;
3
+ exports.whenSocket = void 0;
4
4
  const rxjs_1 = require("rxjs");
5
5
  const ws_1 = require("ws");
6
6
  const use_logger_1 = require("./use-logger");
7
7
  const use_timestamp_1 = require("./use-timestamp");
8
- function useSocket(url) {
8
+ function whenSocket(url) {
9
9
  const { debug } = (0, use_logger_1.useLogger)('useSocket');
10
10
  const message = new rxjs_1.Observable(stream => {
11
11
  const socket = new ws_1.WebSocket(url);
@@ -50,4 +50,4 @@ function useSocket(url) {
50
50
  });
51
51
  return [message, (message) => JSON.stringify(message)];
52
52
  }
53
- exports.useSocket = useSocket;
53
+ exports.whenSocket = whenSocket;
@@ -4,7 +4,7 @@ export declare class RequestNetworkError extends Error {
4
4
  readonly json: () => Promise<string>;
5
5
  constructor(statusCode: number, json: () => Promise<string>);
6
6
  }
7
- export declare function useRequest(args: {
7
+ export declare function withRequest(args: {
8
8
  method: RequestMethod;
9
9
  url: string;
10
10
  headers?: Record<string, any>;
@@ -13,4 +13,4 @@ export declare function useRequest(args: {
13
13
  timestamp: number;
14
14
  payload: any;
15
15
  }>;
16
- //# sourceMappingURL=use-request.d.ts.map
16
+ //# sourceMappingURL=with-request.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"with-request.d.ts","sourceRoot":"","sources":["../src/with-request.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,aAAa,GACrB,KAAK,GACL,MAAM,GACN,MAAM,GACN,KAAK,GACL,QAAQ,GACR,SAAS,GACT,SAAS,GACT,OAAO,GACP,OAAO,CAAC;AAEZ,qBAAa,mBAAoB,SAAQ,KAAK;IAChC,QAAQ,CAAC,UAAU,EAAE,MAAM;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC;gBAAxD,UAAU,EAAE,MAAM,EAAW,IAAI,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC;CAG9E;AAED,wBAAgB,WAAW,CAAC,IAAI,EAAE;IAChC,MAAM,EAAE,aAAa,CAAC;IACtB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC9B,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;;;GAsBA"}
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withRequest = exports.RequestNetworkError = void 0;
4
+ const rxjs_1 = require("rxjs");
5
+ const undici_1 = require("undici");
6
+ const use_logger_1 = require("./use-logger");
7
+ const use_timestamp_1 = require("./use-timestamp");
8
+ class RequestNetworkError extends Error {
9
+ constructor(statusCode, json) {
10
+ super(`Request network error, received status code: ${statusCode}`);
11
+ this.statusCode = statusCode;
12
+ this.json = json;
13
+ }
14
+ }
15
+ exports.RequestNetworkError = RequestNetworkError;
16
+ function withRequest(args) {
17
+ const { error } = (0, use_logger_1.useLogger)(withRequest.name);
18
+ return (0, rxjs_1.defer)(() => (0, rxjs_1.from)((0, undici_1.request)(args.url, args)).pipe((0, rxjs_1.switchMap)(it => {
19
+ if (it.statusCode !== 200) {
20
+ error(`errored`, Object.assign(Object.assign({}, args), { statusCode: it.statusCode }));
21
+ return (0, rxjs_1.throwError)(() => new RequestNetworkError(it.statusCode, () => it.body.json()));
22
+ }
23
+ return (0, rxjs_1.from)(it.body.json());
24
+ }), (0, rxjs_1.map)(payload => ({ timestamp: (0, use_timestamp_1.useTimestamp)(), payload }))));
25
+ }
26
+ exports.withRequest = withRequest;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quantform/core",
3
- "version": "0.7.0-beta.16",
3
+ "version": "0.7.0-beta.17",
4
4
  "license": "MIT",
5
5
  "author": "Mateusz Majchrzak",
6
6
  "description": "Node.js library for building systematic trading strategies in reactive way.",
package/src/cli/dev.ts CHANGED
@@ -1,31 +1,15 @@
1
- import { join } from 'path';
2
- import { lastValueFrom } from 'rxjs';
3
-
4
1
  import build from '@lib/cli/build';
5
- import { buildDirectory } from '@lib/cli/internal/workspace';
6
- import { core } from '@lib/core';
7
- import { Module } from '@lib/module';
8
- import { strat } from '@lib/strat';
9
2
  import { paperExecutionMode } from '@lib/use-execution-mode';
10
3
 
4
+ import { Script } from './internal/script';
5
+
11
6
  export default async function (name: string, options: any) {
12
7
  if (await build()) {
13
8
  return;
14
9
  }
15
10
 
16
- const script = (await import(join(buildDirectory(), name))).default as ReturnType<
17
- typeof strat
18
- >;
19
-
20
- const module = new Module([
21
- ...core(),
22
- ...script.dependencies,
23
- paperExecutionMode({ recording: false })
24
- ]);
25
-
26
- const { act } = await module.awake();
27
-
28
- const output = await act(() => lastValueFrom(script.fn()));
11
+ const script = new Script(name, [paperExecutionMode({ recording: false })]);
12
+ const output = await script.runWhileFinished();
29
13
 
30
14
  console.log(output);
31
15
  }
@@ -0,0 +1,23 @@
1
+ import { join } from 'path';
2
+ import { lastValueFrom } from 'rxjs';
3
+
4
+ import { core } from '@lib/core';
5
+ import { Dependency, Module } from '@lib/module';
6
+
7
+ import { buildDirectory } from './workspace';
8
+
9
+ export class Script {
10
+ constructor(
11
+ private readonly name: string,
12
+ private readonly dependencies: Dependency[]
13
+ ) {}
14
+
15
+ async runWhileFinished() {
16
+ const script = await import(join(buildDirectory(), this.name));
17
+ const module = new Module([...core(), ...script.onInstall(), ...this.dependencies]);
18
+
19
+ const { act } = await module.awake();
20
+
21
+ return await act(() => lastValueFrom(script.onAwake()));
22
+ }
23
+ }
package/src/cli/pull.ts CHANGED
@@ -4,9 +4,7 @@ import build from '@lib/cli/build';
4
4
  import { buildDirectory } from '@lib/cli/internal/workspace';
5
5
  import { core } from '@lib/core';
6
6
  import { Module } from '@lib/module';
7
- import { strat } from '@lib/strat';
8
7
  import { paperExecutionMode } from '@lib/use-execution-mode';
9
- import { token } from '@lib/use-memo';
10
8
 
11
9
  export default async function (name: string, instrument: string, options: any) {
12
10
  if (await build()) {
@@ -14,18 +12,17 @@ export default async function (name: string, instrument: string, options: any) {
14
12
  }
15
13
  await import(join(buildDirectory(), 'index'));
16
14
 
17
- const script = (await import(join(buildDirectory(), name))) as ReturnType<typeof strat>;
15
+ const script = await import(join(buildDirectory(), name));
18
16
 
19
17
  const module = new Module([
20
18
  ...core(),
21
- ...script.dependencies,
19
+ ...script.module,
22
20
  paperExecutionMode({ recording: false })
23
21
  ]);
24
22
 
25
23
  const { act } = await module.awake();
26
24
 
27
- const o = await act(() => script.fn());
28
- console.log(module.get<any>(token));
25
+ const output = await act(() => script.default());
29
26
 
30
27
  /*const builder = new SessionBuilder().useSessionId(
31
28
  options.id ? Number(options.id) : now()
package/src/index.ts CHANGED
@@ -7,7 +7,6 @@ export * from '@lib/use-timestamp';
7
7
  export * from '@lib/simulator';
8
8
  export * from '@lib/make-test-module';
9
9
  export * from '@lib/core';
10
- export * from '@lib/use-state';
11
10
  export * from '@lib/replay/use-replay-coordinator';
12
11
  export * from '@lib/use-execution-mode';
13
12
  export * from '@lib/storage/use-storage';
@@ -15,13 +14,11 @@ export * from '@lib/storage/use-cache';
15
14
  export * from '@lib/use-logger';
16
15
  export * from '@lib/replay';
17
16
  export * from '@lib/replay/use-replay-coordinator';
18
- export * from '@lib/use-socket';
19
- export * from '@lib/use-request';
17
+ export * from '@lib/when-socket';
18
+ export * from '@lib/with-request';
20
19
  export * from '@lib/defined';
21
20
  export * from '@lib/as-readonly';
22
21
  export * from '@lib/with-memo';
23
22
  export * from '@lib/session';
24
23
  export * from '@lib/exclude';
25
- export * from '@lib/use-lock';
26
- export * from '@lib/strat';
27
24
  export * from '@lib/errored';
package/src/use-memo.ts CHANGED
@@ -13,16 +13,6 @@ export function memo() {
13
13
  };
14
14
  }
15
15
 
16
- /**
17
- * @name useMemo
18
- * @description
19
- * A hook that caches the result of an expensive calculation, based on a set of dependencies.
20
- *
21
- * @template T The type of the value that is memoized.
22
- * @param {() => T} calculateValue A function that returns the value to memoize.
23
- * @param {dependency[]} dependencies An array of dependencies that determine when the value should be recalculated.
24
- * @returns {T} The memoized value.
25
- */
26
16
  export function useMemo<T>(calculateValue: () => T, dependencies: dependency[]) {
27
17
  const memory = useContext<Record<string, any>>(token);
28
18
  const hash = useHash(dependencies);
@@ -4,7 +4,7 @@ import { WebSocket } from 'ws';
4
4
  import { useLogger } from './use-logger';
5
5
  import { useTimestamp } from './use-timestamp';
6
6
 
7
- export function useSocket(
7
+ export function whenSocket(
8
8
  url: string
9
9
  ): [Observable<{ timestamp: number; payload: unknown }>, (message: unknown) => void] {
10
10
  const { debug } = useLogger('useSocket');
@@ -21,24 +21,16 @@ export class RequestNetworkError extends Error {
21
21
  }
22
22
  }
23
23
 
24
- export function useRequest(args: {
24
+ export function withRequest(args: {
25
25
  method: RequestMethod;
26
26
  url: string;
27
27
  headers?: Record<string, any>;
28
28
  body?: string;
29
29
  }) {
30
- const { info, error } = useLogger(useRequest.name);
30
+ const { error } = useLogger(withRequest.name);
31
31
 
32
- return defer(() => {
33
- info('requested', args);
34
-
35
- return from(
36
- request(args.url, {
37
- method: args.method,
38
- headers: args.headers,
39
- body: args.body
40
- })
41
- ).pipe(
32
+ return defer(() =>
33
+ from(request(args.url, args)).pipe(
42
34
  switchMap(it => {
43
35
  if (it.statusCode !== 200) {
44
36
  error(`errored`, {
@@ -54,6 +46,6 @@ export function useRequest(args: {
54
46
  return from(it.body.json());
55
47
  }),
56
48
  map(payload => ({ timestamp: useTimestamp(), payload }))
57
- );
58
- });
49
+ )
50
+ );
59
51
  }
package/dist/strat.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import { Observable } from 'rxjs';
2
- import { Dependency } from './module';
3
- export declare function strat(fn: () => Observable<unknown>, dependencies: Dependency[]): {
4
- fn: () => Observable<unknown>;
5
- dependencies: Dependency[];
6
- };
7
- //# sourceMappingURL=strat.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"strat.d.ts","sourceRoot":"","sources":["../src/strat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAElC,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAEtC,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,UAAU,CAAC,OAAO,CAAC,EAAE,YAAY,EAAE,UAAU,EAAE;cAA/C,WAAW,OAAO,CAAC;;EAElD"}
package/dist/strat.js DELETED
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.strat = void 0;
4
- function strat(fn, dependencies) {
5
- return { fn, dependencies };
6
- }
7
- exports.strat = strat;
@@ -1,9 +0,0 @@
1
- import { Observable } from 'rxjs';
2
- import { dependency } from './use-hash';
3
- export declare const useExclusiveLock: () => {
4
- acquire: (dependencies: dependency[]) => void;
5
- release: (dependencies: dependency[]) => void;
6
- alreadyAcquired: (dependencies: dependency[]) => boolean;
7
- };
8
- export declare function exclusive<T>(dependencies: dependency[]): (input: Observable<T>) => Observable<T>;
9
- //# sourceMappingURL=use-lock.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-lock.d.ts","sourceRoot":"","sources":["../src/use-lock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,UAAU,EAAE,MAAM,MAAM,CAAC;AAGpD,OAAO,EAAE,UAAU,EAAW,MAAM,eAAe,CAAC;AAEpD,eAAO,MAAM,gBAAgB;4BAEI,UAAU,EAAE;4BAUZ,UAAU,EAAE;oCAUJ,UAAU,EAAE;CAWnD,CAAC;AAEH,wBAAgB,SAAS,CAAC,CAAC,EAAE,YAAY,EAAE,UAAU,EAAE,WACtC,WAAW,CAAC,CAAC,mBAU7B"}
package/dist/use-lock.js DELETED
@@ -1,40 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.exclusive = exports.useExclusiveLock = void 0;
4
- const rxjs_1 = require("rxjs");
5
- const with_memo_1 = require("./with-memo");
6
- const use_hash_1 = require("./use-hash");
7
- exports.useExclusiveLock = (0, with_memo_1.withMemo)(() => {
8
- const locking = {};
9
- const acquire = (dependencies) => {
10
- const hash = (0, use_hash_1.useHash)(dependencies);
11
- if (locking[hash]) {
12
- throw Error('nested locks not allowed');
13
- }
14
- locking[hash] = true;
15
- };
16
- const release = (dependencies) => {
17
- const hash = (0, use_hash_1.useHash)(dependencies);
18
- if (!locking[hash]) {
19
- throw Error('nested locks not allowed');
20
- }
21
- locking[hash] = false;
22
- };
23
- const alreadyAcquired = (dependencies) => {
24
- const hash = (0, use_hash_1.useHash)(dependencies);
25
- return locking[hash] == true;
26
- };
27
- return {
28
- acquire,
29
- release,
30
- alreadyAcquired
31
- };
32
- });
33
- function exclusive(dependencies) {
34
- return (input) => {
35
- const { acquire, release, alreadyAcquired } = (0, exports.useExclusiveLock)();
36
- acquire(dependencies);
37
- return input.pipe((0, rxjs_1.filter)(() => alreadyAcquired(dependencies)), (0, rxjs_1.finalize)(() => release(dependencies)));
38
- };
39
- }
40
- exports.exclusive = exclusive;
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-request.d.ts","sourceRoot":"","sources":["../src/use-request.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,aAAa,GACrB,KAAK,GACL,MAAM,GACN,MAAM,GACN,KAAK,GACL,QAAQ,GACR,SAAS,GACT,SAAS,GACT,OAAO,GACP,OAAO,CAAC;AAEZ,qBAAa,mBAAoB,SAAQ,KAAK;IAChC,QAAQ,CAAC,UAAU,EAAE,MAAM;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC;gBAAxD,UAAU,EAAE,MAAM,EAAW,IAAI,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC;CAG9E;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE;IAC/B,MAAM,EAAE,aAAa,CAAC;IACtB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC9B,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;;;GA8BA"}
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useRequest = exports.RequestNetworkError = void 0;
4
- const rxjs_1 = require("rxjs");
5
- const undici_1 = require("undici");
6
- const use_logger_1 = require("./use-logger");
7
- const use_timestamp_1 = require("./use-timestamp");
8
- class RequestNetworkError extends Error {
9
- constructor(statusCode, json) {
10
- super(`Request network error, received status code: ${statusCode}`);
11
- this.statusCode = statusCode;
12
- this.json = json;
13
- }
14
- }
15
- exports.RequestNetworkError = RequestNetworkError;
16
- function useRequest(args) {
17
- const { info, error } = (0, use_logger_1.useLogger)(useRequest.name);
18
- return (0, rxjs_1.defer)(() => {
19
- info('requested', args);
20
- return (0, rxjs_1.from)((0, undici_1.request)(args.url, {
21
- method: args.method,
22
- headers: args.headers,
23
- body: args.body
24
- })).pipe((0, rxjs_1.switchMap)(it => {
25
- if (it.statusCode !== 200) {
26
- error(`errored`, Object.assign(Object.assign({}, args), { statusCode: it.statusCode }));
27
- return (0, rxjs_1.throwError)(() => new RequestNetworkError(it.statusCode, () => it.body.json()));
28
- }
29
- return (0, rxjs_1.from)(it.body.json());
30
- }), (0, rxjs_1.map)(payload => ({ timestamp: (0, use_timestamp_1.useTimestamp)(), payload })));
31
- });
32
- }
33
- exports.useRequest = useRequest;
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-socket.d.ts","sourceRoot":"","sources":["../src/use-socket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAMlC,wBAAgB,SAAS,CACvB,GAAG,EAAE,MAAM,GACV,CAAC,UAAU,CAAC;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC,CAmDnF"}
@@ -1,4 +0,0 @@
1
- import { Observable } from 'rxjs';
2
- import { dependency } from './use-hash';
3
- export declare function useState<T>(initialValue: T, dependencies: dependency[]): [Observable<Readonly<T>>, (value: T | ((p: T) => T)) => Readonly<T>];
4
- //# sourceMappingURL=use-state.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-state.d.ts","sourceRoot":"","sources":["../src/use-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,UAAU,EAAE,MAAM,MAAM,CAAC;AAInD,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAExC,wBAAgB,QAAQ,CAAC,CAAC,EACxB,YAAY,EAAE,CAAC,EACf,YAAY,EAAE,UAAU,EAAE,GACzB,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAoBtE"}
package/dist/use-state.js DELETED
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useState = void 0;
4
- const rxjs_1 = require("rxjs");
5
- const use_memo_1 = require("./use-memo");
6
- function useState(initialValue, dependencies) {
7
- return (0, use_memo_1.useMemo)(() => {
8
- const state = new rxjs_1.BehaviorSubject(initialValue);
9
- const setState = (newState) => {
10
- if (newState instanceof Function) {
11
- const value = newState(state.value);
12
- if (value) {
13
- state.next(value);
14
- }
15
- }
16
- else {
17
- state.next(newState);
18
- }
19
- return state.value;
20
- };
21
- return [state.asObservable(), setState];
22
- }, dependencies);
23
- }
24
- exports.useState = useState;
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=use-state.spec.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-state.spec.d.ts","sourceRoot":"","sources":["../src/use-state.spec.ts"],"names":[],"mappings":""}
@@ -1,36 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const make_test_module_1 = require("./make-test-module");
13
- const use_state_1 = require("./use-state");
14
- describe(use_state_1.useState.name, () => {
15
- let fixtures;
16
- beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
17
- fixtures = yield getFixtures();
18
- }));
19
- test('keep same state between multiple requests for the same dependency', () => __awaiter(void 0, void 0, void 0, function* () {
20
- const state1 = yield fixtures.givenState({ text: 'Hello my state' }, ['my-state']);
21
- const state2 = yield fixtures.givenState({ text: 'Hello my override state' }, [
22
- 'my-state'
23
- ]);
24
- expect(Object.is(state1, state2)).toBeTruthy();
25
- }));
26
- });
27
- function getFixtures() {
28
- return __awaiter(this, void 0, void 0, function* () {
29
- const { act } = yield (0, make_test_module_1.makeTestModule)([]);
30
- return {
31
- givenState(value, dependencies) {
32
- return act(() => (0, use_state_1.useState)(value, dependencies));
33
- }
34
- };
35
- });
36
- }
package/src/strat.ts DELETED
@@ -1,7 +0,0 @@
1
- import { Observable } from 'rxjs';
2
-
3
- import { Dependency } from './module';
4
-
5
- export function strat(fn: () => Observable<unknown>, dependencies: Dependency[]) {
6
- return { fn, dependencies };
7
- }
package/src/use-lock.ts DELETED
@@ -1,52 +0,0 @@
1
- import { filter, finalize, Observable } from 'rxjs';
2
-
3
- import { withMemo } from '@lib/with-memo';
4
- import { dependency, useHash } from '@lib/use-hash';
5
-
6
- export const useExclusiveLock = withMemo(() => {
7
- const locking = {} as Record<string, boolean>;
8
- const acquire = (dependencies: dependency[]) => {
9
- const hash = useHash(dependencies);
10
-
11
- if (locking[hash]) {
12
- throw Error('nested locks not allowed');
13
- }
14
-
15
- locking[hash] = true;
16
- };
17
-
18
- const release = (dependencies: dependency[]) => {
19
- const hash = useHash(dependencies);
20
-
21
- if (!locking[hash]) {
22
- throw Error('nested locks not allowed');
23
- }
24
-
25
- locking[hash] = false;
26
- };
27
-
28
- const alreadyAcquired = (dependencies: dependency[]) => {
29
- const hash = useHash(dependencies);
30
-
31
- return locking[hash] == true;
32
- };
33
-
34
- return {
35
- acquire,
36
- release,
37
- alreadyAcquired
38
- };
39
- });
40
-
41
- export function exclusive<T>(dependencies: dependency[]) {
42
- return (input: Observable<T>) => {
43
- const { acquire, release, alreadyAcquired } = useExclusiveLock();
44
-
45
- acquire(dependencies);
46
-
47
- return input.pipe(
48
- filter(() => alreadyAcquired(dependencies)),
49
- finalize(() => release(dependencies))
50
- );
51
- };
52
- }
@@ -1,31 +0,0 @@
1
- import { makeTestModule } from '@lib/make-test-module';
2
- import { useState } from '@lib/use-state';
3
-
4
- import { dependency } from './use-hash';
5
-
6
- describe(useState.name, () => {
7
- let fixtures: Awaited<ReturnType<typeof getFixtures>>;
8
-
9
- beforeEach(async () => {
10
- fixtures = await getFixtures();
11
- });
12
-
13
- test('keep same state between multiple requests for the same dependency', async () => {
14
- const state1 = await fixtures.givenState({ text: 'Hello my state' }, ['my-state']);
15
- const state2 = await fixtures.givenState({ text: 'Hello my override state' }, [
16
- 'my-state'
17
- ]);
18
-
19
- expect(Object.is(state1, state2)).toBeTruthy();
20
- });
21
- });
22
-
23
- async function getFixtures() {
24
- const { act } = await makeTestModule([]);
25
-
26
- return {
27
- givenState<T>(value: T, dependencies: dependency[]) {
28
- return act(() => useState(value, dependencies));
29
- }
30
- };
31
- }
package/src/use-state.ts DELETED
@@ -1,30 +0,0 @@
1
- import { BehaviorSubject, Observable } from 'rxjs';
2
-
3
- import { useMemo } from '@lib/use-memo';
4
-
5
- import { dependency } from './use-hash';
6
-
7
- export function useState<T>(
8
- initialValue: T,
9
- dependencies: dependency[]
10
- ): [Observable<Readonly<T>>, (value: T | ((p: T) => T)) => Readonly<T>] {
11
- return useMemo(() => {
12
- const state = new BehaviorSubject<T>(initialValue);
13
-
14
- const setState = (newState: T | ((prevState: T) => T | undefined)) => {
15
- if (newState instanceof Function) {
16
- const value = newState(state.value);
17
-
18
- if (value) {
19
- state.next(value);
20
- }
21
- } else {
22
- state.next(newState);
23
- }
24
-
25
- return state.value;
26
- };
27
-
28
- return [state.asObservable(), setState];
29
- }, dependencies);
30
- }