@resonatehq/sdk 0.5.3 → 0.5.5

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 (71) hide show
  1. package/dist/async.d.ts +23 -5
  2. package/dist/async.d.ts.map +1 -1
  3. package/dist/async.js +94 -22
  4. package/dist/async.js.map +1 -1
  5. package/dist/core/calls.d.ts +23 -0
  6. package/dist/core/calls.d.ts.map +1 -0
  7. package/dist/core/calls.js +13 -0
  8. package/dist/core/calls.js.map +1 -0
  9. package/dist/core/encoders/json.d.ts.map +1 -1
  10. package/dist/core/encoders/json.js +12 -0
  11. package/dist/core/encoders/json.js.map +1 -1
  12. package/dist/core/execution.d.ts +0 -10
  13. package/dist/core/execution.d.ts.map +1 -1
  14. package/dist/core/execution.js +10 -105
  15. package/dist/core/execution.js.map +1 -1
  16. package/dist/core/invocation.d.ts +4 -7
  17. package/dist/core/invocation.d.ts.map +1 -1
  18. package/dist/core/invocation.js +6 -28
  19. package/dist/core/invocation.js.map +1 -1
  20. package/dist/core/options.d.ts +10 -8
  21. package/dist/core/options.d.ts.map +1 -1
  22. package/dist/core/options.js.map +1 -1
  23. package/dist/core/promises/promises.d.ts +2 -2
  24. package/dist/core/promises/promises.d.ts.map +1 -1
  25. package/dist/core/promises/promises.js +2 -1
  26. package/dist/core/promises/promises.js.map +1 -1
  27. package/dist/core/promises/types.d.ts +10 -83
  28. package/dist/core/promises/types.d.ts.map +1 -1
  29. package/dist/core/promises/types.js +10 -10
  30. package/dist/core/promises/types.js.map +1 -1
  31. package/dist/core/retry.d.ts +35 -26
  32. package/dist/core/retry.d.ts.map +1 -1
  33. package/dist/core/retry.js +111 -14
  34. package/dist/core/retry.js.map +1 -1
  35. package/dist/core/storages/memory.d.ts.map +1 -1
  36. package/dist/core/storages/memory.js +2 -0
  37. package/dist/core/storages/memory.js.map +1 -1
  38. package/dist/core/storages/withTimeout.d.ts +6 -6
  39. package/dist/core/storages/withTimeout.d.ts.map +1 -1
  40. package/dist/core/storages/withTimeout.js +2 -13
  41. package/dist/core/storages/withTimeout.js.map +1 -1
  42. package/dist/core/store.d.ts +7 -7
  43. package/dist/core/store.d.ts.map +1 -1
  44. package/dist/core/stores/local.d.ts +9 -9
  45. package/dist/core/stores/local.d.ts.map +1 -1
  46. package/dist/core/stores/local.js.map +1 -1
  47. package/dist/core/stores/remote.d.ts +7 -7
  48. package/dist/core/stores/remote.d.ts.map +1 -1
  49. package/dist/core/stores/remote.js +15 -6
  50. package/dist/core/stores/remote.js.map +1 -1
  51. package/dist/core/utils.d.ts +27 -0
  52. package/dist/core/utils.d.ts.map +1 -1
  53. package/dist/core/utils.js +37 -1
  54. package/dist/core/utils.js.map +1 -1
  55. package/dist/index.d.ts +1 -4
  56. package/dist/index.d.ts.map +1 -1
  57. package/dist/index.js +4 -7
  58. package/dist/index.js.map +1 -1
  59. package/dist/resonate.d.ts +10 -15
  60. package/dist/resonate.d.ts.map +1 -1
  61. package/dist/resonate.js +71 -44
  62. package/dist/resonate.js.map +1 -1
  63. package/package.json +1 -1
  64. package/dist/core/retries/retry.d.ts +0 -20
  65. package/dist/core/retries/retry.d.ts.map +0 -1
  66. package/dist/core/retries/retry.js +0 -41
  67. package/dist/core/retries/retry.js.map +0 -1
  68. package/dist/generator.d.ts +0 -208
  69. package/dist/generator.d.ts.map +0 -1
  70. package/dist/generator.js +0 -427
  71. package/dist/generator.js.map +0 -1
@@ -1,41 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Retry = void 0;
4
- const retry_1 = require("../retry");
5
- class Retry extends retry_1.IterableRetry {
6
- initialDelay;
7
- backoffFactor;
8
- maxAttempts;
9
- maxDelay;
10
- constructor(initialDelay, backoffFactor, maxAttempts, maxDelay) {
11
- super();
12
- this.initialDelay = initialDelay;
13
- this.backoffFactor = backoffFactor;
14
- this.maxAttempts = maxAttempts;
15
- this.maxDelay = maxDelay;
16
- }
17
- static exponential(initialDelay = 100, backoffFactor = 2, maxAttempts = Infinity, maxDelay = 60000) {
18
- return new Retry(initialDelay, backoffFactor, maxAttempts, maxDelay);
19
- }
20
- static linear(delay = 1000, // 1 second
21
- maxAttempts = Infinity) {
22
- return new Retry(delay, 1, maxAttempts, delay);
23
- }
24
- static never() {
25
- return new Retry(0, 0, 1, 0);
26
- }
27
- next(ctx) {
28
- // attempt 0: 0ms delay
29
- // attampt n: {initial * factor^(attempt-1)}ms delay (or max delay)
30
- const delay = Math.min(Math.min(ctx.attempt, 1) * this.initialDelay * Math.pow(this.backoffFactor, ctx.attempt - 1), this.maxDelay);
31
- if (Date.now() + delay >= ctx.timeout || ctx.attempt >= this.maxAttempts) {
32
- return { done: true };
33
- }
34
- return {
35
- done: false,
36
- delay: delay,
37
- };
38
- }
39
- }
40
- exports.Retry = Retry;
41
- //# sourceMappingURL=retry.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"retry.js","sourceRoot":"","sources":["../../../lib/core/retries/retry.ts"],"names":[],"mappings":";;;AAAA,oCAAiD;AAEjD,MAAa,KAAM,SAAQ,qBAAa;IAE5B;IACA;IACA;IACA;IAJV,YACU,YAAoB,EACpB,aAAqB,EACrB,WAAmB,EACnB,QAAgB;QAExB,KAAK,EAAE,CAAC;QALA,iBAAY,GAAZ,YAAY,CAAQ;QACpB,kBAAa,GAAb,aAAa,CAAQ;QACrB,gBAAW,GAAX,WAAW,CAAQ;QACnB,aAAQ,GAAR,QAAQ,CAAQ;IAG1B,CAAC;IAED,MAAM,CAAC,WAAW,CAChB,eAAuB,GAAG,EAC1B,gBAAwB,CAAC,EACzB,cAAsB,QAAQ,EAC9B,WAAmB,KAAK;QAExB,OAAO,IAAI,KAAK,CAAC,YAAY,EAAE,aAAa,EAAE,WAAW,EAAE,QAAQ,CAAC,CAAC;IACvE,CAAC;IAED,MAAM,CAAC,MAAM,CACX,QAAgB,IAAI,EAAE,WAAW;IACjC,cAAsB,QAAQ;QAE9B,OAAO,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC,EAAE,WAAW,EAAE,KAAK,CAAC,CAAC;IACjD,CAAC;IAED,MAAM,CAAC,KAAK;QACV,OAAO,IAAI,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IAC/B,CAAC;IAED,IAAI,CAAiD,GAAM;QACzD,uBAAuB;QACvB,mEAAmE;QACnE,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CACpB,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,aAAa,EAAE,GAAG,CAAC,OAAO,GAAG,CAAC,CAAC,EAC5F,IAAI,CAAC,QAAQ,CACd,CAAC;QAEF,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,IAAI,GAAG,CAAC,OAAO,IAAI,GAAG,CAAC,OAAO,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YACzE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;QACxB,CAAC;QAED,OAAO;YACL,IAAI,EAAE,KAAK;YACX,KAAK,EAAE,KAAK;SACb,CAAC;IACJ,CAAC;CACF;AA/CD,sBA+CC"}
@@ -1,208 +0,0 @@
1
- import { Future, ResonatePromise } from "./core/future";
2
- import { Invocation } from "./core/invocation";
3
- import { ResonateOptions, Options, PartialOptions } from "./core/options";
4
- import { DurablePromise } from "./core/promises/promises";
5
- import * as schedules from "./core/schedules/schedules";
6
- import { ResonateBase } from "./resonate";
7
- export type GFunc = (ctx: Context, ...args: any[]) => Generator<Yieldable>;
8
- export type IFunc = (info: Info, ...args: any[]) => any;
9
- export type Params<F> = F extends (ctx: any, ...args: infer P) => any ? P : never;
10
- export type Return<F> = F extends (...args: any[]) => Generator<any, infer T> ? T : never;
11
- export type Yieldable = Call | Future<any>;
12
- export type Call = {
13
- kind: "call";
14
- value: ResonateFunction | OrdinaryFunction | DeferredFunction;
15
- yieldFuture: boolean;
16
- };
17
- type ResonateFunction = {
18
- kind: "resonate";
19
- func: GFunc;
20
- args: any[];
21
- opts: Options;
22
- };
23
- type OrdinaryFunction = {
24
- kind: "ordinary";
25
- func: IFunc;
26
- args: any[];
27
- opts: Options;
28
- };
29
- type DeferredFunction = {
30
- kind: "deferred";
31
- func: string;
32
- args: any;
33
- opts: Options;
34
- };
35
- export declare class Resonate extends ResonateBase {
36
- private scheduler;
37
- /**
38
- * Creates a Resonate instance. This is the starting point for using Resonate.
39
- *
40
- * @constructor
41
- * @param opts - A partial {@link ResonateOptions} object.
42
- */
43
- constructor(opts?: Partial<ResonateOptions>);
44
- protected execute<F extends GFunc>(name: string, id: string, func: F, args: Params<F>, opts: Options, defaults: Options, durablePromise?: DurablePromise<any>): ResonatePromise<Return<F>>;
45
- /**
46
- * Register a function with Resonate. Registered functions can be invoked by calling {@link run}, or by the returned function.
47
- *
48
- * @template F The type of the generator function.
49
- * @param name A unique name to identify the function.
50
- * @param func The generator function to register with Resonate.
51
- * @param opts Resonate options, can be constructed by calling {@link options}.
52
- * @returns Resonate function
53
- */
54
- register<F extends GFunc>(name: string, func: F, opts?: Partial<Options>): (id: string, ...args: Params<F>) => ResonatePromise<Return<F>>;
55
- /**
56
- * Register a module with Resonate. Registered functions can be invoked by calling {@link run}.
57
- *
58
- * @template F The type of the generator function.
59
- * @param module The module to register with Resonate.
60
- * @param opts Resonate options, can be constructed by calling {@link options}.
61
- * @returns Resonate function
62
- */
63
- registerModule<F extends GFunc>(module: Record<string, F>, opts?: Partial<Options>): void;
64
- /**
65
- * Schedule a resonate function.
66
- *
67
- * @param name The schedule name.
68
- * @param cron The schedule cron expression.
69
- * @param func The function to schedule.
70
- * @param args The function arguments.
71
- * @returns The schedule object.
72
- */
73
- schedule<F extends GFunc>(name: string, cron: string, func: F, ...args: [...Params<F>, PartialOptions?]): Promise<schedules.Schedule>;
74
- /**
75
- * Schedule a resonate function that is already registered.
76
- *
77
- * @param name The schedule name.
78
- * @param cron The schedule cron expression.
79
- * @param func The registered function name.
80
- * @param args The function arguments.
81
- * @returns The schedule object.
82
- */
83
- schedule(name: string, cron: string, func: string, ...args: [...any, PartialOptions?]): Promise<schedules.Schedule>;
84
- }
85
- export declare class Info {
86
- private invocation;
87
- constructor(invocation: Invocation<any>);
88
- /**
89
- * The running count of function execution attempts.
90
- */
91
- get attempt(): number;
92
- /**
93
- * Uniquely identifies the function invocation.
94
- */
95
- get id(): string;
96
- /**
97
- * Deduplicates function invocations with the same id.
98
- */
99
- get idempotencyKey(): string;
100
- /**
101
- * All configured options for this info.
102
- */
103
- get opts(): Options;
104
- /**
105
- * The timestamp in ms, once this time elapses the function invocation will timeout.
106
- */
107
- get timeout(): number;
108
- /**
109
- * The resonate function version.
110
- */
111
- get version(): number;
112
- }
113
- export declare class Context {
114
- private invocation;
115
- constructor(invocation: Invocation<any>);
116
- /**
117
- * The running count of child function invocations.
118
- */
119
- get counter(): number;
120
- /**
121
- * The time the invocation was created. Will use the durable promise creation time if available.
122
- */
123
- get createdOn(): number;
124
- /**
125
- * Uniquely identifies the function invocation.
126
- */
127
- get id(): string;
128
- /**
129
- * Deduplicates function invocations with the same id.
130
- */
131
- get idempotencyKey(): string;
132
- /**
133
- * All configured options for this context.
134
- */
135
- get opts(): Options;
136
- /**
137
- * The timestamp in ms, once this time elapses the function invocation will timeout.
138
- */
139
- get timeout(): number;
140
- /**
141
- * The resonate function version.
142
- */
143
- get version(): number;
144
- /**
145
- * Invoke a generator function.
146
- *
147
- * @template F The type of the generator function.
148
- * @param func The function to invoke.
149
- * @param args The function arguments, optionally followed by {@link options}.
150
- * @returns A {@link Call} that can be yielded for a value.
151
- */
152
- run<F extends GFunc>(func: F, ...args: [...Params<F>, PartialOptions?]): Call;
153
- /**
154
- * Invoke a function.
155
- *
156
- * @template F The type of the function.
157
- * @param func The function to invoke.
158
- * @param args The function arguments, optionally followed by {@link options}.
159
- * @returns A {@link Call} that can be yielded for a value.
160
- */
161
- run<F extends IFunc>(func: F, ...args: [...Params<F>, PartialOptions?]): Call;
162
- /**
163
- * Invoke a remote function.
164
- *
165
- * @param func The id of the remote function.
166
- * @param args The arguments to pass to the remote function.
167
- * @param opts Optional {@link options}.
168
- * @returns A {@link Call} that can be yielded for a value.
169
- */
170
- run(func: string, args?: any, opts?: PartialOptions): Call;
171
- /**
172
- * Invoke a generator function.
173
- *
174
- * @template F The type of the generator function.
175
- * @param func The function to invoke.
176
- * @param args The function arguments, optionally followed by {@link options}.
177
- * @returns A {@link Call} that can be yielded for a {@link Future}.
178
- */
179
- call<F extends GFunc>(func: F, ...args: [...Params<F>, PartialOptions?]): Call;
180
- /**
181
- * Invoke a function.
182
- *
183
- * @template F The type of the function.
184
- * @param func The function to invoke.
185
- * @param args The function arguments, optionally followed by {@link options}.
186
- * @returns A {@link Call} that can be yielded for a {@link Future}.
187
- */
188
- call<F extends IFunc>(func: F, ...args: [...Params<F>, PartialOptions?]): Call;
189
- /**
190
- * Invoke a remote function.
191
- *
192
- * @param func The id of the remote function.
193
- * @param args The arguments to pass to the remote function.
194
- * @param opts Optional {@link options}.
195
- * @returns A {@link Call} that can be yielded for a {@link Future}.
196
- */
197
- call(func: string, args?: any, opts?: PartialOptions): Call;
198
- private _call;
199
- /**
200
- * Construct options.
201
- *
202
- * @param opts A partial {@link Options} object.
203
- * @returns Options with the __resonate flag set.
204
- */
205
- options(opts?: Partial<Options>): PartialOptions;
206
- }
207
- export {};
208
- //# sourceMappingURL=generator.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../lib/generator.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,eAAe,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAC1E,OAAO,EAAE,cAAc,EAAE,M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AC,CAAC,GAAG,IAAI;IAE7E;;;;;;;OAOG;IACH,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,cAAc,GAAG,IAAI;IAK1D;;;;;;;OAOG;IACH,IAAI,CAAC,CAAC,SAAS,KAAK,EAAE,IAAI,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,EAAE,cAAc,CAAC,CAAC,GAAG,IAAI;IAE9E;;;;;;;OAOG;IACH,IAAI,CAAC,CAAC,SAAS,KAAK,EAAE,IAAI,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,EAAE,cAAc,CAAC,CAAC,GAAG,IAAI;IAE9E;;;;;;;OAOG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,cAAc,GAAG,IAAI;IAK3D,OAAO,CAAC,KAAK;IAYb;;;;;OAKG;IACH,OAAO,CAAC,IAAI,GAAE,OAAO,CAAC,OAAO,CAAM,GAAG,cAAc;CAGrD"}
package/dist/generator.js DELETED
@@ -1,427 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Context = exports.Info = exports.Resonate = void 0;
4
- const execution_1 = require("./core/execution");
5
- const future_1 = require("./core/future");
6
- const invocation_1 = require("./core/invocation");
7
- const resonate_1 = require("./resonate");
8
- /////////////////////////////////////////////////////////////////////
9
- // Resonate
10
- /////////////////////////////////////////////////////////////////////
11
- class Resonate extends resonate_1.ResonateBase {
12
- scheduler;
13
- /**
14
- * Creates a Resonate instance. This is the starting point for using Resonate.
15
- *
16
- * @constructor
17
- * @param opts - A partial {@link ResonateOptions} object.
18
- */
19
- constructor(opts = {}) {
20
- super(opts);
21
- this.scheduler = new Scheduler(this);
22
- }
23
- execute(name, id, func, args, opts, defaults, durablePromise) {
24
- return this.scheduler.add(name, id, func, args, opts, defaults, durablePromise);
25
- }
26
- /**
27
- * Register a function with Resonate. Registered functions can be invoked by calling {@link run}, or by the returned function.
28
- *
29
- * @template F The type of the generator function.
30
- * @param name A unique name to identify the function.
31
- * @param func The generator function to register with Resonate.
32
- * @param opts Resonate options, can be constructed by calling {@link options}.
33
- * @returns Resonate function
34
- */
35
- register(name, func, opts) {
36
- return super.register(name, func, opts);
37
- }
38
- /**
39
- * Register a module with Resonate. Registered functions can be invoked by calling {@link run}.
40
- *
41
- * @template F The type of the generator function.
42
- * @param module The module to register with Resonate.
43
- * @param opts Resonate options, can be constructed by calling {@link options}.
44
- * @returns Resonate function
45
- */
46
- registerModule(module, opts = {}) {
47
- super.registerModule(module, opts);
48
- }
49
- schedule(name, cron, func, ...args) {
50
- return super.schedule(name, cron, func, ...args);
51
- }
52
- }
53
- exports.Resonate = Resonate;
54
- /////////////////////////////////////////////////////////////////////
55
- // Context
56
- /////////////////////////////////////////////////////////////////////
57
- class Info {
58
- invocation;
59
- constructor(invocation) {
60
- this.invocation = invocation;
61
- }
62
- /**
63
- * The running count of function execution attempts.
64
- */
65
- get attempt() {
66
- return this.invocation.attempt;
67
- }
68
- /**
69
- * Uniquely identifies the function invocation.
70
- */
71
- get id() {
72
- return this.invocation.id;
73
- }
74
- /**
75
- * Deduplicates function invocations with the same id.
76
- */
77
- get idempotencyKey() {
78
- return this.invocation.idempotencyKey;
79
- }
80
- /**
81
- * All configured options for this info.
82
- */
83
- get opts() {
84
- return this.invocation.opts;
85
- }
86
- /**
87
- * The timestamp in ms, once this time elapses the function invocation will timeout.
88
- */
89
- get timeout() {
90
- return this.invocation.timeout;
91
- }
92
- /**
93
- * The resonate function version.
94
- */
95
- get version() {
96
- return this.invocation.root.opts.version;
97
- }
98
- }
99
- exports.Info = Info;
100
- class Context {
101
- invocation;
102
- constructor(invocation) {
103
- this.invocation = invocation;
104
- }
105
- /**
106
- * The running count of child function invocations.
107
- */
108
- get counter() {
109
- return this.invocation.counter;
110
- }
111
- /**
112
- * The time the invocation was created. Will use the durable promise creation time if available.
113
- */
114
- get createdOn() {
115
- return this.invocation.createdOn;
116
- }
117
- /**
118
- * Uniquely identifies the function invocation.
119
- */
120
- get id() {
121
- return this.invocation.id;
122
- }
123
- /**
124
- * Deduplicates function invocations with the same id.
125
- */
126
- get idempotencyKey() {
127
- return this.invocation.idempotencyKey;
128
- }
129
- /**
130
- * All configured options for this context.
131
- */
132
- get opts() {
133
- return this.invocation.opts;
134
- }
135
- /**
136
- * The timestamp in ms, once this time elapses the function invocation will timeout.
137
- */
138
- get timeout() {
139
- return this.invocation.timeout;
140
- }
141
- /**
142
- * The resonate function version.
143
- */
144
- get version() {
145
- return this.invocation.root.opts.version;
146
- }
147
- run(func, ...args) {
148
- return this._call(func, args, false);
149
- }
150
- call(func, ...args) {
151
- return this._call(func, args, true);
152
- }
153
- _call(func, argsWithOpts, yieldFuture) {
154
- const { args, opts } = this.invocation.split(argsWithOpts);
155
- if (typeof func === "string") {
156
- return { kind: "call", value: { kind: "deferred", func, args, opts }, yieldFuture };
157
- }
158
- else if (func.constructor.name === "GeneratorFunction") {
159
- return { kind: "call", value: { kind: "resonate", func, args, opts }, yieldFuture };
160
- }
161
- else {
162
- return { kind: "call", value: { kind: "ordinary", func, args, opts }, yieldFuture };
163
- }
164
- }
165
- /**
166
- * Construct options.
167
- *
168
- * @param opts A partial {@link Options} object.
169
- * @returns Options with the __resonate flag set.
170
- */
171
- options(opts = {}) {
172
- return { ...opts, __resonate: true };
173
- }
174
- }
175
- exports.Context = Context;
176
- /////////////////////////////////////////////////////////////////////
177
- // Scheduler
178
- /////////////////////////////////////////////////////////////////////
179
- class Scheduler {
180
- resonate;
181
- // tick is mutually exclusive
182
- running = false;
183
- // all top level executions
184
- cache = {};
185
- // executions ready to be run
186
- runnable = [];
187
- // executions that are awaiting another execution
188
- awaiting = [];
189
- // all executions
190
- executions = [];
191
- // executions that have been killed
192
- killed = [];
193
- constructor(resonate) {
194
- this.resonate = resonate;
195
- }
196
- add(name, id, func, args, opts, defaults, durablePromise) {
197
- // if the execution is already running, and not killed,
198
- // return the promise
199
- if (opts.durable && this.cache[id] && !this.cache[id].killed) {
200
- return this.cache[id].execute();
201
- }
202
- // params, used for recovery
203
- const param = {
204
- func: name,
205
- version: opts.version,
206
- args,
207
- };
208
- // create a new invocation
209
- const invocation = new invocation_1.Invocation(name, id, undefined, param, opts, defaults);
210
- // create a new execution
211
- const generator = func(new Context(invocation), ...args);
212
- const execution = new execution_1.GeneratorExecution(this.resonate, invocation, generator, durablePromise);
213
- // once the durable promise has been created,
214
- // add the execution to runnable if the future is pending
215
- execution.create().then(() => {
216
- if (invocation.future.pending) {
217
- this.runnable.push({ execution, next: { kind: "init" } });
218
- this.tick();
219
- }
220
- });
221
- // store the invocation and execution,
222
- // will be used if run is called again with the same id
223
- this.cache[id] = execution;
224
- this.executions.push(execution);
225
- return execution.execute();
226
- }
227
- async tick() {
228
- // need to ensure that tick is mutually exclusive,
229
- // if tick is already running all continuations will be picked up in the while loop
230
- if (this.running)
231
- return;
232
- this.running = true;
233
- while (this.runnable.length > 0) {
234
- // grab the next continuation
235
- const continuation = this.runnable.shift();
236
- // step through the generator if in debug mode
237
- if (this.resonate.logger.level === "debug" && (await this.keypress()) === "\u001b") {
238
- // kill when escape key is pressed
239
- continuation?.execution.kill("manual kill");
240
- }
241
- if (continuation && !continuation.execution.killed) {
242
- try {
243
- // apply the next value to the generator
244
- const result = this.next(continuation);
245
- // if done, we can resolve the execution
246
- if (result.done) {
247
- // need to handle the special case where a generator returns a future
248
- if (result.value instanceof future_1.Future) {
249
- result.value.promise.then((v) => continuation.execution.resolve(v), (e) => continuation.execution.reject(e));
250
- }
251
- else {
252
- // resolve the durable promise
253
- await continuation.execution.resolve(result.value);
254
- }
255
- }
256
- else {
257
- // apply the yielded value to the generator
258
- await this.apply(continuation, result.value);
259
- }
260
- }
261
- catch (error) {
262
- // if anything goes wrong, reject the durable promise
263
- await continuation.execution.reject(error);
264
- }
265
- // housekeeping
266
- if (continuation.execution.killed) {
267
- this.kill(continuation.execution);
268
- }
269
- }
270
- // print all invocations if in debug mode
271
- this.print();
272
- }
273
- // TODO: suspend
274
- // set running back to false
275
- this.running = false;
276
- }
277
- next({ execution, next }) {
278
- // apply the next value to the generator
279
- switch (next.kind) {
280
- case "init":
281
- return execution.generator.next();
282
- case "value":
283
- return execution.generator.next(next.value);
284
- case "error":
285
- return execution.generator.throw(next.error);
286
- default:
287
- this.yeet(`permitted continuation values are (init, value, error), received ${next}`);
288
- }
289
- }
290
- async apply(continuation, yielded) {
291
- // apply the yielded value to the generator
292
- switch (yielded.kind) {
293
- case "call":
294
- await this.applyCall(continuation, yielded);
295
- break;
296
- case "future":
297
- this.applyFuture(continuation, yielded);
298
- break;
299
- default:
300
- this.yeet(`permitted yielded values are (call, future), received ${yielded}`);
301
- }
302
- }
303
- async applyCall(continuation, { value, yieldFuture }) {
304
- // the parent is the current invocation
305
- const parent = continuation.execution.invocation;
306
- // the id is either:
307
- // 1. a provided string in the case of a deferred execution
308
- // 2. a generated string in the case of an ordinary execution
309
- const id = value.kind === "deferred" ? value.func : `${parent.id}.${parent.counter}`;
310
- // human readable name of the function
311
- const name = value.kind === "deferred" ? value.func : value.func.name;
312
- // default opts never change
313
- const defaults = parent.defaults;
314
- // param is only required for deferred executions
315
- const param = value.kind === "deferred" ? value.args[0] : undefined;
316
- // create a new invocation
317
- const invocation = new invocation_1.Invocation(name, id, undefined, param, value.opts, defaults, parent);
318
- // add child and increment counter
319
- parent.addChild(invocation);
320
- parent.counter++;
321
- let execution;
322
- if (value.kind === "resonate") {
323
- // create a generator execution
324
- const ctx = new Context(invocation);
325
- execution = new execution_1.GeneratorExecution(this.resonate, invocation, value.func(ctx, ...value.args));
326
- await execution.create();
327
- // if the future is pending, add to runnable
328
- if (invocation.future.pending) {
329
- this.runnable.push({ execution, next: { kind: "init" } });
330
- }
331
- }
332
- else if (value.kind === "ordinary") {
333
- // create an ordinary execution
334
- const info = new Info(invocation);
335
- execution = new execution_1.OrdinaryExecution(this.resonate, invocation, () => value.func(info, ...value.args));
336
- execution.execute().catch(() => { });
337
- }
338
- else if (value.kind === "deferred") {
339
- // create a deferred execution
340
- execution = new execution_1.DeferredExecution(this.resonate, invocation);
341
- execution.execute().catch(() => { });
342
- }
343
- else {
344
- this.yeet(`permitted call values are (resonate, ordinary, deferred), received ${value}`);
345
- }
346
- // add to all executions
347
- this.executions.push(execution);
348
- if (yieldFuture) {
349
- // if the call is expected to yield a future, add the future to runnable
350
- this.runnable.push({
351
- execution: continuation.execution,
352
- next: { kind: "value", value: invocation.future },
353
- });
354
- }
355
- else {
356
- // otherwise, skip ahead to apply future
357
- this.applyFuture(continuation, invocation.future);
358
- }
359
- }
360
- applyFuture({ execution }, future) {
361
- if (execution.invocation.future.root !== future.root) {
362
- this.yeet(`yielded future originates from ${future.root.id}, but this execution originates from ${execution.invocation.future.root.id}`);
363
- }
364
- // add to awaiting
365
- this.awaiting.push(execution);
366
- execution.invocation.await(future);
367
- execution.invocation.block(future);
368
- const apply = (next) => {
369
- // unblock
370
- execution.invocation.unblock();
371
- // remove from awaiting
372
- this.awaiting = this.awaiting.filter((e) => e !== execution);
373
- // add to runnable
374
- this.runnable.push({ execution, next });
375
- // tick again
376
- this.tick();
377
- };
378
- // add the next value to runnable when the future fulfills
379
- future.promise.then((value) => apply({ kind: "value", value }), (error) => apply({ kind: "error", error }));
380
- }
381
- kill(execution) {
382
- // add to killed
383
- const killed = this.executions.filter((e) => e.invocation.root === execution.invocation.root);
384
- this.killed = this.killed.concat(killed);
385
- // remove from awaiting and runnable
386
- this.runnable = this.runnable.filter((c) => c.execution.invocation.root !== execution.invocation.root);
387
- this.awaiting = this.awaiting.filter((e) => e.invocation.root !== execution.invocation.root);
388
- }
389
- async keypress() {
390
- this.resonate.logger.debug("Press any key to continue...");
391
- return new Promise((resolve) => {
392
- const onData = (data) => {
393
- process.stdin.removeListener("data", onData);
394
- process.stdin.setRawMode(false);
395
- process.stdin.pause();
396
- const c = data.toString();
397
- if (c === "\u0003") {
398
- this.resonate.logger.debug("^C");
399
- process.exit(1);
400
- }
401
- resolve(c);
402
- };
403
- process.stdin.resume();
404
- process.stdin.setRawMode(true);
405
- process.stdin.once("data", onData);
406
- });
407
- }
408
- print() {
409
- this.resonate.logger.debug("Executions");
410
- this.resonate.logger.table(this.executions.map((e) => ({
411
- name: e.invocation.name,
412
- id: e.invocation.id,
413
- idempotencyKey: e.invocation.idempotencyKey,
414
- parent: e.invocation.parent ? e.invocation.parent.id : undefined,
415
- killed: e.killed,
416
- pending: e.invocation.future.value.kind === "pending",
417
- awaited: e.invocation.awaited.map((f) => f.id).join(","),
418
- blocked: e.invocation.blocked?.id,
419
- })));
420
- }
421
- yeet(msg) {
422
- // an unrecoverable error has occurred, log and exit
423
- this.resonate.logger.error(msg);
424
- process.exit(1);
425
- }
426
- }
427
- //# sourceMappingURL=generator.js.map