@xyo-network/xl1-protocol-sdk 1.23.21 → 1.24.2

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 (49) hide show
  1. package/dist/neutral/CreatableProvider/AbstractCreatableProvider.d.ts +0 -364
  2. package/dist/neutral/CreatableProvider/AbstractCreatableProvider.d.ts.map +1 -1
  3. package/dist/neutral/CreatableProvider/CreatableProviderContext.zod.d.ts +508 -2
  4. package/dist/neutral/CreatableProvider/CreatableProviderContext.zod.d.ts.map +1 -1
  5. package/dist/neutral/actor/ActorV3.d.ts +2 -0
  6. package/dist/neutral/actor/ActorV3.d.ts.map +1 -1
  7. package/dist/neutral/actor/index.d.ts +0 -3
  8. package/dist/neutral/actor/index.d.ts.map +1 -1
  9. package/dist/neutral/config/Config.d.ts +200 -1160
  10. package/dist/neutral/config/Config.d.ts.map +1 -1
  11. package/dist/neutral/config/index.d.ts +1 -1
  12. package/dist/neutral/config/index.d.ts.map +1 -1
  13. package/dist/neutral/index.mjs +1667 -2260
  14. package/dist/neutral/index.mjs.map +1 -1
  15. package/dist/neutral/test/index.mjs +60 -370
  16. package/dist/neutral/test/index.mjs.map +1 -1
  17. package/package.json +4 -3
  18. package/src/CreatableProvider/CreatableProviderContext.zod.ts +18 -5
  19. package/src/actor/ActorV3.ts +2 -0
  20. package/src/actor/index.ts +0 -3
  21. package/src/config/Config.ts +9 -40
  22. package/src/config/index.ts +1 -1
  23. package/dist/neutral/actor/Actor.d.ts +0 -477
  24. package/dist/neutral/actor/Actor.d.ts.map +0 -1
  25. package/dist/neutral/actor/ActorContext.d.ts +0 -8
  26. package/dist/neutral/actor/ActorContext.d.ts.map +0 -1
  27. package/dist/neutral/actor/ActorV2.d.ts +0 -470
  28. package/dist/neutral/actor/ActorV2.d.ts.map +0 -1
  29. package/dist/neutral/config/actors/Api.d.ts +0 -76
  30. package/dist/neutral/config/actors/Api.d.ts.map +0 -1
  31. package/dist/neutral/config/actors/Bridge.d.ts +0 -124
  32. package/dist/neutral/config/actors/Bridge.d.ts.map +0 -1
  33. package/dist/neutral/config/actors/Mempool.d.ts +0 -76
  34. package/dist/neutral/config/actors/Mempool.d.ts.map +0 -1
  35. package/dist/neutral/config/actors/Producer.d.ts +0 -84
  36. package/dist/neutral/config/actors/Producer.d.ts.map +0 -1
  37. package/dist/neutral/config/actors/RewardRedemption.d.ts +0 -75
  38. package/dist/neutral/config/actors/RewardRedemption.d.ts.map +0 -1
  39. package/dist/neutral/config/actors/index.d.ts +0 -7
  40. package/dist/neutral/config/actors/index.d.ts.map +0 -1
  41. package/src/actor/Actor.ts +0 -216
  42. package/src/actor/ActorContext.ts +0 -9
  43. package/src/actor/ActorV2.ts +0 -171
  44. package/src/config/actors/Api.ts +0 -33
  45. package/src/config/actors/Bridge.ts +0 -136
  46. package/src/config/actors/Mempool.ts +0 -43
  47. package/src/config/actors/Producer.ts +0 -61
  48. package/src/config/actors/RewardRedemption.ts +0 -26
  49. package/src/config/actors/index.ts +0 -6
@@ -1,76 +0,0 @@
1
- import { z } from 'zod';
2
- export declare const MempoolConfigZod: z.ZodObject<{
3
- chain: z.ZodDefault<z.ZodObject<{
4
- id: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodTransform<import("@xylabs/hex").BrandedHex, string>>>;
5
- genesisRewardAddress: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodTransform<Lowercase<string> & {
6
- readonly __hex: true;
7
- } & {
8
- readonly __address: true;
9
- }, string>>>;
10
- }, z.core.$strip>>;
11
- evm: z.ZodDefault<z.ZodObject<{
12
- chainId: z.ZodOptional<z.ZodString>;
13
- infura: z.ZodOptional<z.ZodObject<{
14
- projectId: z.ZodOptional<z.ZodString>;
15
- projectSecret: z.ZodOptional<z.ZodString>;
16
- }, z.core.$strip>>;
17
- jsonRpc: z.ZodOptional<z.ZodObject<{
18
- url: z.ZodOptional<z.ZodURL>;
19
- }, z.core.$strip>>;
20
- }, z.core.$strip>>;
21
- log: z.ZodDefault<z.ZodObject<{
22
- logLevel: z.ZodDefault<z.ZodEnum<{
23
- error: "error";
24
- warn: "warn";
25
- info: "info";
26
- log: "log";
27
- debug: "debug";
28
- trace: "trace";
29
- }>>;
30
- silent: z.ZodDefault<z.ZodBoolean>;
31
- }, z.core.$strip>>;
32
- remote: z.ZodDefault<z.ZodObject<{
33
- rpc: z.ZodOptional<z.ZodObject<{
34
- url: z.ZodString;
35
- }, z.core.$strip>>;
36
- }, z.core.$strip>>;
37
- storage: z.ZodDefault<z.ZodObject<{
38
- mongo: z.ZodOptional<z.ZodObject<{
39
- connectionString: z.ZodOptional<z.ZodString>;
40
- database: z.ZodOptional<z.ZodString>;
41
- domain: z.ZodOptional<z.ZodString>;
42
- password: z.ZodOptional<z.ZodString>;
43
- username: z.ZodOptional<z.ZodString>;
44
- }, z.core.$strip>>;
45
- root: z.ZodOptional<z.ZodString>;
46
- }, z.core.$strip>>;
47
- telemetry: z.ZodDefault<z.ZodObject<{
48
- metrics: z.ZodOptional<z.ZodObject<{
49
- scrape: z.ZodObject<{
50
- path: z.ZodDefault<z.ZodString>;
51
- port: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
52
- }, z.core.$strip>;
53
- }, z.core.$strip>>;
54
- otel: z.ZodOptional<z.ZodObject<{
55
- otlpEndpoint: z.ZodOptional<z.ZodURL>;
56
- }, z.core.$strip>>;
57
- }, z.core.$strip>>;
58
- validation: z.ZodDefault<z.ZodObject<{
59
- allowedRewardRedeemers: z.ZodPipe<z.ZodTransform<unknown, unknown>, z.ZodOptional<z.ZodArray<z.ZodPipe<z.ZodString, z.ZodTransform<Lowercase<string> & {
60
- readonly __hex: true;
61
- } & {
62
- readonly __address: true;
63
- }, string>>>>>;
64
- allowedRewardEscrowAccountSigners: z.ZodPipe<z.ZodTransform<unknown, unknown>, z.ZodOptional<z.ZodArray<z.ZodPipe<z.ZodString, z.ZodTransform<Lowercase<string> & {
65
- readonly __hex: true;
66
- } & {
67
- readonly __address: true;
68
- }, string>>>>>;
69
- }, z.core.$strip>>;
70
- enabled: z.ZodPipe<z.ZodDefault<z.ZodUnion<readonly [z.ZodString, z.ZodBoolean]>>, z.ZodTransform<boolean, string | boolean>>;
71
- host: z.ZodDefault<z.ZodString>;
72
- mnemonic: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>>;
73
- port: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
74
- }, z.core.$strip>;
75
- export type MempoolConfig = z.infer<typeof MempoolConfigZod>;
76
- //# sourceMappingURL=Mempool.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Mempool.d.ts","sourceRoot":"","sources":["../../../../src/config/actors/Mempool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,CAAC,EAAE,MAAM,KAAK,CAAA;AAKvC,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmCpB,CAAA;AAET,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,gBAAgB,CAAC,CAAA"}
@@ -1,84 +0,0 @@
1
- import { z } from 'zod';
2
- export declare const ProducerConfigZod: z.ZodObject<{
3
- chain: z.ZodDefault<z.ZodObject<{
4
- id: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodTransform<import("@xylabs/sdk-js").BrandedHex, string>>>;
5
- genesisRewardAddress: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodTransform<Lowercase<string> & {
6
- readonly __hex: true;
7
- } & {
8
- readonly __address: true;
9
- }, string>>>;
10
- }, z.core.$strip>>;
11
- evm: z.ZodDefault<z.ZodObject<{
12
- chainId: z.ZodOptional<z.ZodString>;
13
- infura: z.ZodOptional<z.ZodObject<{
14
- projectId: z.ZodOptional<z.ZodString>;
15
- projectSecret: z.ZodOptional<z.ZodString>;
16
- }, z.core.$strip>>;
17
- jsonRpc: z.ZodOptional<z.ZodObject<{
18
- url: z.ZodOptional<z.ZodURL>;
19
- }, z.core.$strip>>;
20
- }, z.core.$strip>>;
21
- log: z.ZodDefault<z.ZodObject<{
22
- logLevel: z.ZodDefault<z.ZodEnum<{
23
- error: "error";
24
- warn: "warn";
25
- info: "info";
26
- log: "log";
27
- debug: "debug";
28
- trace: "trace";
29
- }>>;
30
- silent: z.ZodDefault<z.ZodBoolean>;
31
- }, z.core.$strip>>;
32
- remote: z.ZodDefault<z.ZodObject<{
33
- rpc: z.ZodOptional<z.ZodObject<{
34
- url: z.ZodString;
35
- }, z.core.$strip>>;
36
- }, z.core.$strip>>;
37
- storage: z.ZodDefault<z.ZodObject<{
38
- mongo: z.ZodOptional<z.ZodObject<{
39
- connectionString: z.ZodOptional<z.ZodString>;
40
- database: z.ZodOptional<z.ZodString>;
41
- domain: z.ZodOptional<z.ZodString>;
42
- password: z.ZodOptional<z.ZodString>;
43
- username: z.ZodOptional<z.ZodString>;
44
- }, z.core.$strip>>;
45
- root: z.ZodOptional<z.ZodString>;
46
- }, z.core.$strip>>;
47
- telemetry: z.ZodDefault<z.ZodObject<{
48
- metrics: z.ZodOptional<z.ZodObject<{
49
- scrape: z.ZodObject<{
50
- path: z.ZodDefault<z.ZodString>;
51
- port: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
52
- }, z.core.$strip>;
53
- }, z.core.$strip>>;
54
- otel: z.ZodOptional<z.ZodObject<{
55
- otlpEndpoint: z.ZodOptional<z.ZodURL>;
56
- }, z.core.$strip>>;
57
- }, z.core.$strip>>;
58
- validation: z.ZodDefault<z.ZodObject<{
59
- allowedRewardRedeemers: z.ZodPipe<z.ZodTransform<unknown, unknown>, z.ZodOptional<z.ZodArray<z.ZodPipe<z.ZodString, z.ZodTransform<Lowercase<string> & {
60
- readonly __hex: true;
61
- } & {
62
- readonly __address: true;
63
- }, string>>>>>;
64
- allowedRewardEscrowAccountSigners: z.ZodPipe<z.ZodTransform<unknown, unknown>, z.ZodOptional<z.ZodArray<z.ZodPipe<z.ZodString, z.ZodTransform<Lowercase<string> & {
65
- readonly __hex: true;
66
- } & {
67
- readonly __address: true;
68
- }, string>>>>>;
69
- }, z.core.$strip>>;
70
- allowlist: z.ZodPipe<z.ZodTransform<unknown, unknown>, z.ZodOptional<z.ZodArray<z.ZodPipe<z.ZodString, z.ZodTransform<Lowercase<string> & {
71
- readonly __hex: true;
72
- } & {
73
- readonly __address: true;
74
- }, string>>>>>;
75
- disableIntentRedeclaration: z.ZodOptional<z.ZodBoolean>;
76
- healthCheckPort: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
77
- heartbeatInterval: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
78
- minStake: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
79
- mnemonic: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>>;
80
- port: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
81
- rewardAddress: z.ZodOptional<z.ZodString>;
82
- }, z.core.$strip>;
83
- export type ProducerConfig = z.infer<typeof ProducerConfigZod>;
84
- //# sourceMappingURL=Producer.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Producer.d.ts","sourceRoot":"","sources":["../../../../src/config/actors/Producer.ts"],"names":[],"mappings":"AACA,OAAO,EAAkB,CAAC,EAAE,MAAM,KAAK,CAAA;AAKvC,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAoDrB,CAAA;AAET,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,iBAAiB,CAAC,CAAA"}
@@ -1,75 +0,0 @@
1
- import { z } from 'zod';
2
- export declare const RewardRedemptionConfigZod: z.ZodObject<{
3
- chain: z.ZodDefault<z.ZodObject<{
4
- id: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodTransform<import("@xylabs/hex").BrandedHex, string>>>;
5
- genesisRewardAddress: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodTransform<Lowercase<string> & {
6
- readonly __hex: true;
7
- } & {
8
- readonly __address: true;
9
- }, string>>>;
10
- }, z.core.$strip>>;
11
- evm: z.ZodDefault<z.ZodObject<{
12
- chainId: z.ZodOptional<z.ZodString>;
13
- infura: z.ZodOptional<z.ZodObject<{
14
- projectId: z.ZodOptional<z.ZodString>;
15
- projectSecret: z.ZodOptional<z.ZodString>;
16
- }, z.core.$strip>>;
17
- jsonRpc: z.ZodOptional<z.ZodObject<{
18
- url: z.ZodOptional<z.ZodURL>;
19
- }, z.core.$strip>>;
20
- }, z.core.$strip>>;
21
- log: z.ZodDefault<z.ZodObject<{
22
- logLevel: z.ZodDefault<z.ZodEnum<{
23
- error: "error";
24
- warn: "warn";
25
- info: "info";
26
- log: "log";
27
- debug: "debug";
28
- trace: "trace";
29
- }>>;
30
- silent: z.ZodDefault<z.ZodBoolean>;
31
- }, z.core.$strip>>;
32
- remote: z.ZodDefault<z.ZodObject<{
33
- rpc: z.ZodOptional<z.ZodObject<{
34
- url: z.ZodString;
35
- }, z.core.$strip>>;
36
- }, z.core.$strip>>;
37
- storage: z.ZodDefault<z.ZodObject<{
38
- mongo: z.ZodOptional<z.ZodObject<{
39
- connectionString: z.ZodOptional<z.ZodString>;
40
- database: z.ZodOptional<z.ZodString>;
41
- domain: z.ZodOptional<z.ZodString>;
42
- password: z.ZodOptional<z.ZodString>;
43
- username: z.ZodOptional<z.ZodString>;
44
- }, z.core.$strip>>;
45
- root: z.ZodOptional<z.ZodString>;
46
- }, z.core.$strip>>;
47
- telemetry: z.ZodDefault<z.ZodObject<{
48
- metrics: z.ZodOptional<z.ZodObject<{
49
- scrape: z.ZodObject<{
50
- path: z.ZodDefault<z.ZodString>;
51
- port: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
52
- }, z.core.$strip>;
53
- }, z.core.$strip>>;
54
- otel: z.ZodOptional<z.ZodObject<{
55
- otlpEndpoint: z.ZodOptional<z.ZodURL>;
56
- }, z.core.$strip>>;
57
- }, z.core.$strip>>;
58
- validation: z.ZodDefault<z.ZodObject<{
59
- allowedRewardRedeemers: z.ZodPipe<z.ZodTransform<unknown, unknown>, z.ZodOptional<z.ZodArray<z.ZodPipe<z.ZodString, z.ZodTransform<Lowercase<string> & {
60
- readonly __hex: true;
61
- } & {
62
- readonly __address: true;
63
- }, string>>>>>;
64
- allowedRewardEscrowAccountSigners: z.ZodPipe<z.ZodTransform<unknown, unknown>, z.ZodOptional<z.ZodArray<z.ZodPipe<z.ZodString, z.ZodTransform<Lowercase<string> & {
65
- readonly __hex: true;
66
- } & {
67
- readonly __address: true;
68
- }, string>>>>>;
69
- }, z.core.$strip>>;
70
- host: z.ZodDefault<z.ZodString>;
71
- mnemonic: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>>;
72
- port: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
73
- }, z.core.$strip>;
74
- export type RewardRedemptionConfig = z.infer<typeof RewardRedemptionConfigZod>;
75
- //# sourceMappingURL=RewardRedemption.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"RewardRedemption.d.ts","sourceRoot":"","sources":["../../../../src/config/actors/RewardRedemption.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,CAAC,EAAE,MAAM,KAAK,CAAA;AAKvC,eAAO,MAAM,yBAAyB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAkB7B,CAAA;AAET,MAAM,MAAM,sBAAsB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,yBAAyB,CAAC,CAAA"}
@@ -1,7 +0,0 @@
1
- export * from './Api.ts';
2
- export * from './Api.ts';
3
- export * from './Bridge.ts';
4
- export * from './Mempool.ts';
5
- export * from './Producer.ts';
6
- export * from './RewardRedemption.ts';
7
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/config/actors/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,UAAU,CAAA;AACxB,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,eAAe,CAAA;AAC7B,cAAc,uBAAuB,CAAA"}
@@ -1,216 +0,0 @@
1
- import type {
2
- CreatableInstance, CreatableParams, EmptyObject,
3
- Promisable,
4
- } from '@xylabs/sdk-js'
5
- import {
6
- AbstractCreatable,
7
- assertEx,
8
- Base, creatable, delay, IdLogger,
9
- } from '@xylabs/sdk-js'
10
- import { AccountInstance } from '@xyo-network/account-model'
11
- import { Semaphore } from 'async-mutex'
12
-
13
- import { Config, ConfigZod } from '../config/index.ts'
14
- import { CreatableProviderFactory, ProviderFactoryLocator } from '../CreatableProvider/index.ts'
15
- import { ActorContext } from './ActorContext.ts'
16
-
17
- /** @deprecated use ActorParamsV2 */
18
- export type ActorParams<T extends EmptyObject | void = void> = CreatableParams & {
19
- account: AccountInstance
20
- context?: ActorContext
21
- displayName?: string
22
- id: string
23
- } & (T extends void ? EmptyObject : T)
24
-
25
- /** @deprecated use ActorInstanceV2 */
26
- // eslint-disable-next-line @typescript-eslint/no-deprecated
27
- export type ActorInstance<T extends ActorParams = ActorParams> = CreatableInstance<T>
28
-
29
- /** @deprecated use Actor */
30
- @creatable()
31
- // eslint-disable-next-line @typescript-eslint/no-deprecated
32
- export class Actor<TParams extends ActorParams = ActorParams> extends AbstractCreatable<TParams> {
33
- protected readonly _intervals: Map<string, ReturnType<typeof setInterval>> = new Map()
34
- protected readonly _semaphores: Map<string, Semaphore> = new Map()
35
- protected readonly _timeouts: Map<string, ReturnType<typeof setTimeout>> = new Map()
36
- private _active = false
37
-
38
- get displayName() {
39
- return this.params.displayName ?? this.params.name ?? 'UnnamedActor'
40
- }
41
-
42
- get id() {
43
- return this.params.id
44
- }
45
-
46
- protected get account() {
47
- return this.params.account!
48
- }
49
-
50
- protected get config() {
51
- return this.context.config
52
- }
53
-
54
- protected get context() {
55
- return this.params.context!
56
- }
57
-
58
- protected get locator() {
59
- return this.context.locator
60
- }
61
-
62
- protected get logPrefix() {
63
- return `[${this.displayName} (${this.id})] `
64
- }
65
-
66
- static defaultFactories(): CreatableProviderFactory[] {
67
- return []
68
- }
69
-
70
- // eslint-disable-next-line @typescript-eslint/no-deprecated
71
- static override async paramsHandler<T extends ActorInstance>(params?: Partial<T['params']>) {
72
- const baseParams = await super.paramsHandler(params)
73
- const id = params?.id ?? baseParams.name ?? 'UnnamedActor'
74
- const displayName = params?.displayName ?? baseParams.name
75
- const account = assertEx(params?.account, () => `Account is required for actor ${id}.`)
76
- const context = await this.initContext({
77
- ...params, account, id, displayName,
78
- })
79
- const logger = context?.logger ?? new IdLogger(Base.defaultLogger ?? console, () => `[${displayName} (${id})] `)
80
- return {
81
- ...baseParams, account, context, displayName, id, logger,
82
- // eslint-disable-next-line @typescript-eslint/no-deprecated
83
- } satisfies ActorParams
84
- }
85
-
86
- // eslint-disable-next-line @typescript-eslint/no-deprecated
87
- protected static initContext<T extends ActorInstance>(
88
- params: T['params'],
89
- ): Promisable<ActorContext> {
90
- const logger = params?.context?.logger
91
-
92
- const config: Config = params?.context?.config ?? ConfigZod.parse({})
93
- const singletons = params?.context?.singletons ?? {}
94
-
95
- const locator = params.context?.locator ?? new ProviderFactoryLocator({
96
- ...params?.context,
97
- caches: params.context?.caches ?? {},
98
- config,
99
- logger,
100
- singletons,
101
- })
102
- locator.registerMany(this.defaultFactories())
103
- return locator.context
104
- }
105
-
106
- /**
107
- * The timer runs until the actor is deactivated (or you manually stop it).
108
- */
109
- registerTimer(timerName: string, callback: () => Promise<void>, dueTimeMs: number, periodMs: number) {
110
- if (!this._active) {
111
- this.logger?.warn(
112
- `Cannot register timer '${timerName}' because actor is not active.`,
113
- )
114
- return
115
- }
116
-
117
- let running = false
118
-
119
- this._semaphores.set(timerName, new Semaphore(1))
120
-
121
- const timeoutId = setTimeout(() => {
122
- const intervalId = setInterval(() => {
123
- const semaphore = this._semaphores.get(timerName)
124
- if (!this._active || !this._intervals.has(timerName) || !semaphore || running) return
125
- if (semaphore.isLocked()) {
126
- this.logger?.warn(
127
- `Skipping timer '${this.name}:${timerName}' execution because previous execution is still running.`,
128
- )
129
- return
130
- }
131
- semaphore.acquire().then(([, release]) => {
132
- const startTime = Date.now()
133
- running = true
134
- callback()
135
- .then(() => {
136
- const duration = Date.now() - startTime
137
- if (duration > periodMs) {
138
- this.logger?.warn(
139
- `Timer '${this.name}:${timerName}' execution took longer (${duration}ms) than the period (${periodMs}ms).`,
140
- )
141
- } else if (duration > 5000) {
142
- this.logger?.warn(
143
- `Timer '${this.name}:${timerName}' execution took longer (${duration}ms) than 5000ms.`,
144
- )
145
- }
146
- })
147
- .catch((error) => {
148
- this.logger?.error(`Error in timer '${this.name}:${timerName}': ${error}`)
149
- this.logger?.error(error.stack)
150
- })
151
- .finally(() => {
152
- release()
153
- running = false
154
- })
155
- }).catch((error) => {
156
- this.logger?.error(`Error acquiring semaphore for timer '${this.name}:${timerName}': ${error}`)
157
- })
158
- }, periodMs)
159
-
160
- // store interval so we can clear it on stop()
161
- this._intervals.set(timerName, intervalId)
162
- }, dueTimeMs)
163
-
164
- // store timeout so we can clear it on stop() if interval hasn't started yet
165
- this._timeouts.set(timerName, timeoutId)
166
-
167
- this.logger?.log(
168
- `Timer '${this.name}:${timerName}' registered: first call after ${dueTimeMs}ms, recurring every ${periodMs}ms.`,
169
- )
170
- }
171
-
172
- /**
173
- * Called by the Orchestrator when the actor is activated.
174
- */
175
- override async startHandler() {
176
- await super.startHandler()
177
- this._active = true
178
- this.logger?.log('Started.')
179
- }
180
-
181
- /**
182
- * Called by the Orchestrator when the actor is deactivated.
183
- * Stop all running timers.
184
- */
185
- override async stopHandler() {
186
- await super.stopHandler()
187
- this._active = false
188
- this.logger?.log('Stopping all timers...')
189
-
190
- // wait for all semaphores to be free and acquire them to prevent new tasks from starting
191
- await Promise.all(
192
- [...this._semaphores.values()].map(async (semaphore) => {
193
- // Wait for any running tasks to complete
194
- while (semaphore.isLocked()) {
195
- this.logger?.log('Waiting for running timer task to complete...')
196
- await delay(500)
197
- }
198
- await semaphore.acquire()
199
- }),
200
- )
201
-
202
- this._semaphores.clear()
203
-
204
- for (const [, timeoutRef] of this._timeouts.entries()) {
205
- clearTimeout(timeoutRef)
206
- }
207
- this._timeouts.clear()
208
-
209
- for (const [, intervalRef] of this._intervals.entries()) {
210
- clearInterval(intervalRef)
211
- }
212
- this._intervals.clear()
213
-
214
- this.logger?.log('Stopped.')
215
- }
216
- }
@@ -1,9 +0,0 @@
1
- import type { CachingContext } from '@xyo-network/xl1-protocol'
2
-
3
- import type { Config } from '../config/index.ts'
4
- import type { ProviderFactoryLocatorInstance } from '../CreatableProvider/index.ts'
5
-
6
- export interface ActorContext extends CachingContext {
7
- config: Config
8
- locator: ProviderFactoryLocatorInstance
9
- }
@@ -1,171 +0,0 @@
1
- import type {
2
- CreatableInstance, CreatableParams, EmptyObject,
3
- Logger,
4
- Promisable,
5
- } from '@xylabs/sdk-js'
6
- import {
7
- AbstractCreatable,
8
- assertEx,
9
- Base, delay, IdLogger,
10
- } from '@xylabs/sdk-js'
11
- import type { AccountInstance } from '@xyo-network/account-model'
12
- import { Semaphore } from 'async-mutex'
13
-
14
- import type { Config } from '../config/index.ts'
15
- import type { ProviderFactoryLocatorInstance } from '../CreatableProvider/index.ts'
16
- import type { ActorContext } from './ActorContext.ts'
17
-
18
- export type ActorParamsV2<T extends EmptyObject | void = void> = CreatableParams & {
19
- account: AccountInstance
20
- config: Config
21
- logger?: Logger
22
- } & (T extends void ? EmptyObject : T)
23
-
24
- export type ActorInstanceV2<T extends ActorParamsV2 = ActorParamsV2> = CreatableInstance<T>
25
-
26
- export abstract class ActorV2<TParams extends ActorParamsV2 = ActorParamsV2> extends AbstractCreatable<TParams> {
27
- protected readonly _intervals: Map<string, ReturnType<typeof setInterval>> = new Map()
28
- protected readonly _semaphores: Map<string, Semaphore> = new Map()
29
- protected readonly _timeouts: Map<string, ReturnType<typeof setTimeout>> = new Map()
30
- private _context!: ActorContext
31
-
32
- protected get account() {
33
- return this.params.account
34
- }
35
-
36
- protected get config() {
37
- return this.params.config
38
- }
39
-
40
- protected get context() {
41
- return this._context
42
- }
43
-
44
- protected get locator() {
45
- return this.context.locator
46
- }
47
-
48
- static override async paramsHandler<T extends ActorInstanceV2>(params: Partial<T['params']>) {
49
- const baseParams = await super.paramsHandler({ ...params, name: params.name ?? 'UnknownActor' })
50
- const account = assertEx(params.account, () => `Account is required for actor ${baseParams.name}.`)
51
- const config = assertEx(params.config, () => `Config is required for actor ${baseParams.name}.`)
52
- const inLogger = params.logger ?? Base.defaultLogger
53
- const logger = inLogger ? new IdLogger(inLogger, () => `[${baseParams.name}] `) : undefined
54
- return {
55
- ...baseParams, account, logger, config,
56
- } satisfies ActorParamsV2
57
- }
58
-
59
- override async createHandler() {
60
- await super.createHandler()
61
- this._context = await this.initContext()
62
- }
63
-
64
- /**
65
- * The timer runs until the actor is deactivated (or you manually stop it).
66
- */
67
- registerTimer(timerName: string, callback: () => Promise<void>, dueTimeMs: number, periodMs: number) {
68
- if (this.status !== 'starting') {
69
- this.logger?.warn(
70
- `Cannot register timer '${timerName}' because actor is not starting.`,
71
- )
72
- return
73
- }
74
-
75
- let running = false
76
-
77
- this._semaphores.set(timerName, new Semaphore(1))
78
-
79
- const timeoutId = setTimeout(() => {
80
- const intervalId = setInterval(() => {
81
- const semaphore = this._semaphores.get(timerName)
82
- if (this.status !== 'started' || !this._intervals.has(timerName) || !semaphore || running) return
83
- if (semaphore.isLocked()) {
84
- this.logger?.warn(
85
- `Skipping timer '${this.name}:${timerName}' execution because previous execution is still running.`,
86
- )
87
- return
88
- }
89
- semaphore.acquire().then(([, release]) => {
90
- const startTime = Date.now()
91
- running = true
92
- callback()
93
- .then(() => {
94
- const duration = Date.now() - startTime
95
- if (duration > periodMs) {
96
- this.logger?.warn(
97
- `Timer '${this.name}:${timerName}' execution took longer (${duration}ms) than the period (${periodMs}ms).`,
98
- )
99
- } else if (duration > 5000) {
100
- this.logger?.warn(
101
- `Timer '${this.name}:${timerName}' execution took longer (${duration}ms) than 5000ms.`,
102
- )
103
- }
104
- })
105
- .catch((error) => {
106
- this.logger?.error(`Error in timer '${this.name}:${timerName}': ${error}`)
107
- this.logger?.error(error.stack)
108
- })
109
- .finally(() => {
110
- release()
111
- running = false
112
- })
113
- }).catch((error) => {
114
- this.logger?.error(`Error acquiring semaphore for timer '${this.name}:${timerName}': ${error}`)
115
- })
116
- }, periodMs)
117
-
118
- // store interval so we can clear it on stop()
119
- this._intervals.set(timerName, intervalId)
120
- }, dueTimeMs)
121
-
122
- // store timeout so we can clear it on stop() if interval hasn't started yet
123
- this._timeouts.set(timerName, timeoutId)
124
-
125
- this.logger?.log(
126
- `Timer '${this.name}:${timerName}' registered: first call after ${dueTimeMs}ms, recurring every ${periodMs}ms.`,
127
- )
128
- }
129
-
130
- /**
131
- * Called by the Orchestrator when the actor is deactivated.
132
- * Stop all running timers.
133
- */
134
- override async stopHandler() {
135
- await super.stopHandler()
136
- this.logger?.log('Stopping all timers...')
137
-
138
- // wait for all semaphores to be free and acquire them to prevent new tasks from starting
139
- await Promise.all(
140
- [...this._semaphores.values()].map(async (semaphore) => {
141
- // Wait for any running tasks to complete
142
- while (semaphore.isLocked()) {
143
- this.logger?.log('Waiting for running timer task to complete...')
144
- await delay(500)
145
- }
146
- await semaphore.acquire()
147
- }),
148
- )
149
-
150
- this._semaphores.clear()
151
-
152
- for (const [, timeoutRef] of this._timeouts.entries()) {
153
- clearTimeout(timeoutRef)
154
- }
155
- this._timeouts.clear()
156
-
157
- for (const [, intervalRef] of this._intervals.entries()) {
158
- clearInterval(intervalRef)
159
- }
160
- this._intervals.clear()
161
-
162
- this.logger?.log('Stopped.')
163
- }
164
-
165
- protected async initContext(): Promise<ActorContext> {
166
- const locator = await this.initLocator()
167
- return locator.context
168
- }
169
-
170
- abstract initLocator(): Promisable<ProviderFactoryLocatorInstance>
171
- }