@xyo-network/diviner-abstract 3.15.6 → 3.15.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/neutral/index.mjs +55 -35
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/types/AbstractDiviner.d.ts.map +1 -1
- package/package.json +21 -17
- package/src/AbstractDiviner.ts +65 -37
package/dist/neutral/index.mjs
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
// src/AbstractDiviner.ts
|
|
2
2
|
import { assertEx } from "@xylabs/assert";
|
|
3
3
|
import { globallyUnique } from "@xylabs/base";
|
|
4
|
+
import { delay } from "@xylabs/delay";
|
|
5
|
+
import { forget } from "@xylabs/forget";
|
|
4
6
|
import { retry } from "@xylabs/retry";
|
|
5
7
|
import { spanAsync } from "@xylabs/telemetry";
|
|
8
|
+
import { isDefined, isNull } from "@xylabs/typeof";
|
|
6
9
|
import { isArchivistInstance } from "@xyo-network/archivist-model";
|
|
7
10
|
import { QueryBoundWitnessWrapper } from "@xyo-network/boundwitness-wrapper";
|
|
8
11
|
import {
|
|
@@ -13,6 +16,22 @@ import {
|
|
|
13
16
|
import { AbstractModuleInstance } from "@xyo-network/module-abstract";
|
|
14
17
|
import { PayloadBuilder } from "@xyo-network/payload-builder";
|
|
15
18
|
import { isWitnessInstance } from "@xyo-network/witness-model";
|
|
19
|
+
var delayedResolve = async (parent, id, closure, timeout = 3e4, logger) => {
|
|
20
|
+
const start = Date.now();
|
|
21
|
+
let result;
|
|
22
|
+
while (result) {
|
|
23
|
+
result = await parent.resolve(id);
|
|
24
|
+
if (isDefined(result)) {
|
|
25
|
+
closure(result);
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
if (Date.now() - start > timeout) {
|
|
29
|
+
logger?.error(`Timed out waiting for ${id} to resolve`);
|
|
30
|
+
closure(null);
|
|
31
|
+
}
|
|
32
|
+
await delay(500);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
16
35
|
var AbstractDiviner = class _AbstractDiviner extends AbstractModuleInstance {
|
|
17
36
|
static configSchemas = [...super.configSchemas, DivinerConfigSchema];
|
|
18
37
|
static defaultConfigSchema = DivinerConfigSchema;
|
|
@@ -79,43 +98,44 @@ var AbstractDiviner = class _AbstractDiviner extends AbstractModuleInstance {
|
|
|
79
98
|
sourceModule,
|
|
80
99
|
targetModuleFunction
|
|
81
100
|
} = subscription;
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
101
|
+
if (targetModuleFunction === "divine") {
|
|
102
|
+
forget(delayedResolve(this, sourceModule, (sourceModuleInstance) => {
|
|
103
|
+
if (isNull(sourceModuleInstance)) {
|
|
104
|
+
this.logger?.error(`Failed to resolve ${sourceModule} for ${this.modName}`);
|
|
105
|
+
} else {
|
|
106
|
+
if (isArchivistInstance(sourceModuleInstance)) {
|
|
107
|
+
if (sourceEvent === "inserted") {
|
|
108
|
+
this._eventUnsubscribeFunctions.push(
|
|
109
|
+
sourceModuleInstance.on(sourceEvent, async ({ outPayloads, payloads }) => {
|
|
110
|
+
await this.divine(outPayloads ?? payloads);
|
|
111
|
+
})
|
|
112
|
+
);
|
|
113
|
+
} else {
|
|
114
|
+
this.logger?.warn(`Unsupported sourceEvent ${sourceEvent} for ${sourceModuleInstance.modName}`);
|
|
115
|
+
}
|
|
116
|
+
} else if (isDivinerInstance(sourceModuleInstance)) {
|
|
117
|
+
if (sourceEvent === "divineEnd") {
|
|
118
|
+
this._eventUnsubscribeFunctions.push(
|
|
119
|
+
sourceModuleInstance.on(sourceEvent, async ({ outPayloads }) => {
|
|
120
|
+
await this.divine(outPayloads);
|
|
121
|
+
})
|
|
122
|
+
);
|
|
123
|
+
} else {
|
|
124
|
+
this.logger?.warn(`Unsupported sourceEvent ${sourceEvent} for ${sourceModuleInstance.modName}`);
|
|
125
|
+
}
|
|
126
|
+
} else if (isWitnessInstance(sourceModuleInstance)) {
|
|
127
|
+
if (sourceEvent === "observeEnd") {
|
|
128
|
+
this._eventUnsubscribeFunctions.push(
|
|
129
|
+
sourceModuleInstance.on(sourceEvent, async ({ outPayloads }) => {
|
|
130
|
+
await this.divine(outPayloads);
|
|
131
|
+
})
|
|
132
|
+
);
|
|
133
|
+
} else {
|
|
134
|
+
this.logger?.warn(`Unsupported sourceEvent ${sourceEvent} for ${sourceModuleInstance.modName}`);
|
|
135
|
+
}
|
|
114
136
|
}
|
|
115
137
|
}
|
|
116
|
-
}
|
|
117
|
-
this.logger?.warn(`Unsupported targetModuleFunction ${targetModuleFunction} for ${this.modName}`);
|
|
118
|
-
}
|
|
138
|
+
}));
|
|
119
139
|
}
|
|
120
140
|
}
|
|
121
141
|
return await super.startHandler(timeout);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/AbstractDiviner.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { globallyUnique } from '@xylabs/base'\nimport type { Promisable } from '@xylabs/promise'\nimport type { RetryConfig, RetryConfigWithComplete } from '@xylabs/retry'\nimport { retry } from '@xylabs/retry'\nimport { spanAsync } from '@xylabs/telemetry'\nimport type { AccountInstance } from '@xyo-network/account-model'\nimport { isArchivistInstance } from '@xyo-network/archivist-model'\nimport type { QueryBoundWitness } from '@xyo-network/boundwitness-model'\nimport { QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-wrapper'\nimport type {\n AttachableDivinerInstance,\n DivinerDivineQuery,\n DivinerDivineResult,\n DivinerInstance,\n DivinerModuleEventData,\n DivinerParams,\n DivinerQueries,\n} from '@xyo-network/diviner-model'\nimport {\n DivinerConfigSchema,\n DivinerDivineQuerySchema,\n isDivinerInstance,\n} from '@xyo-network/diviner-model'\nimport { AbstractModuleInstance } from '@xyo-network/module-abstract'\nimport type { EventUnsubscribeFunction } from '@xyo-network/module-events'\nimport type {\n ModuleConfig, ModuleQueryHandlerResult, ModuleQueryResult,\n} from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type {\n Payload, Schema, WithoutPrivateStorageMeta,\n} from '@xyo-network/payload-model'\nimport { isWitnessInstance } from '@xyo-network/witness-model'\n\nexport abstract class AbstractDiviner<\n TParams extends DivinerParams = DivinerParams,\n TIn extends Payload = Payload,\n TOut extends Payload = Payload,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n>\n extends AbstractModuleInstance<TParams, TEventData>\n implements AttachableDivinerInstance<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, DivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = DivinerConfigSchema\n static readonly targetSchema: string\n static override readonly uniqueName = globallyUnique('AbstractDiviner', AbstractDiviner, 'xyo')\n\n private _eventUnsubscribeFunctions: EventUnsubscribeFunction[] = []\n\n override get queries(): string[] {\n return [DivinerDivineQuerySchema, ...super.queries]\n }\n\n /** @function divine The main entry point for a diviner. Do not override this function. Implement/override divineHandler for custom functionality */\n async divine(payloads: TIn[] = [], retryConfigIn?: RetryConfigWithComplete): Promise<DivinerDivineResult<TOut>[]> {\n this._noOverride('divine')\n return await spanAsync('divine', async () => {\n if (this.reentrancy?.scope === 'global' && this.reentrancy.action === 'skip' && this.globalReentrancyMutex?.isLocked()) {\n return []\n }\n try {\n await this.globalReentrancyMutex?.acquire()\n return await this.busy(async () => {\n const retryConfig = retryConfigIn ?? this.config.retry\n await this.started('throw')\n await this.emit('divineStart', { inPayloads: payloads, mod: this })\n const resultPayloads\n = (retryConfig ? await retry(() => this.divineHandler(payloads), retryConfig) : await this.divineHandler(payloads)) ?? []\n await this.emit('divineEnd', {\n errors: [], inPayloads: payloads, mod: this, outPayloads: resultPayloads,\n })\n return PayloadBuilder.omitPrivateStorageMeta(resultPayloads)\n })\n } finally {\n this.globalReentrancyMutex?.release()\n }\n }, this.tracer)\n }\n\n async divineQuery(payloads?: TIn[], account?: AccountInstance, _retry?: RetryConfig): Promise<ModuleQueryResult<TOut>> {\n const queryPayload: DivinerDivineQuery = { schema: DivinerDivineQuerySchema }\n return await this.sendQueryRaw(queryPayload, payloads, account)\n }\n\n /** @function queryHandler Calls divine for a divine query. Override to support additional queries. */\n protected override async queryHandler<T extends QueryBoundWitness = QueryBoundWitness, TConfig extends ModuleConfig = ModuleConfig>(\n query: T,\n payloads?: Payload[],\n queryConfig?: TConfig,\n ): Promise<ModuleQueryHandlerResult> {\n const wrapper = QueryBoundWitnessWrapper.parseQuery<DivinerQueries>(query, payloads)\n // remove the query payload\n const cleanPayloads = await PayloadBuilder.filterExclude(payloads, query.query)\n const queryPayload = await wrapper.getQuery()\n assertEx(await this.queryable(query, payloads, queryConfig))\n const resultPayloads: WithoutPrivateStorageMeta<Payload>[] = []\n switch (queryPayload.schema) {\n case DivinerDivineQuerySchema: {\n resultPayloads.push(...(await this.divine(cleanPayloads as TIn[])))\n break\n }\n default: {\n return super.queryHandler(query, payloads)\n }\n }\n return PayloadBuilder.omitPrivateStorageMeta(resultPayloads)\n }\n\n protected override async startHandler(timeout?: number) {\n const { eventSubscriptions = [] } = this.config\n\n for (const subscription of eventSubscriptions) {\n const {\n sourceEvent, sourceModule, targetModuleFunction,\n } = subscription\n const sourceModuleInstance = await this.resolve(sourceModule)\n if (sourceModuleInstance) {\n if (targetModuleFunction === 'divine') {\n if (isArchivistInstance(sourceModuleInstance)) {\n if (sourceEvent === 'inserted') {\n this._eventUnsubscribeFunctions.push(\n sourceModuleInstance.on(sourceEvent, async ({ outPayloads, payloads }) => {\n await this.divine((outPayloads ?? payloads) as Payload[] as TIn[])\n }),\n )\n } else {\n this.logger?.warn(`Unsupported sourceEvent ${sourceEvent} for ${sourceModuleInstance.modName}`)\n }\n } else if (isDivinerInstance(sourceModuleInstance)) {\n if (sourceEvent === 'divineEnd') {\n this._eventUnsubscribeFunctions.push(\n sourceModuleInstance.on(sourceEvent, async ({ outPayloads }) => {\n await this.divine(outPayloads as Payload[] as TIn[])\n }),\n )\n } else {\n this.logger?.warn(`Unsupported sourceEvent ${sourceEvent} for ${sourceModuleInstance.modName}`)\n }\n } else if (isWitnessInstance(sourceModuleInstance)) {\n if (sourceEvent === 'observeEnd') {\n this._eventUnsubscribeFunctions.push(\n sourceModuleInstance.on(sourceEvent, async ({ outPayloads }) => {\n await this.divine(outPayloads as Payload[] as TIn[])\n }),\n )\n } else {\n this.logger?.warn(`Unsupported sourceEvent ${sourceEvent} for ${sourceModuleInstance.modName}`)\n }\n }\n } else {\n this.logger?.warn(`Unsupported targetModuleFunction ${targetModuleFunction} for ${this.modName}`)\n }\n }\n }\n\n return await super.startHandler(timeout)\n }\n\n protected override async stopHandler(timeout?: number) {\n for (const unsubscribe of this._eventUnsubscribeFunctions) {\n unsubscribe()\n }\n this._eventUnsubscribeFunctions = []\n this._eventUnsubscribeFunctions = []\n return await super.stopHandler(timeout)\n }\n\n /** @function divineHandler Implement or override to add custom functionality to a diviner */\n protected abstract divineHandler(payloads?: TIn[]): Promisable<TOut[]>\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,sBAAsB;AAG/B,SAAS,aAAa;AACtB,SAAS,iBAAiB;AAE1B,SAAS,2BAA2B;AAEpC,SAAS,gCAAgC;AAUzC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,8BAA8B;AAKvC,SAAS,sBAAsB;AAI/B,SAAS,yBAA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oBAAoB,GAAG;AAClD,gBAAI,gBAAgB,aAAa;AAC/B,mBAAK,2BAA2B;AAAA,gBAC9B,qBAAqB,GAAG,aAAa,OAAO,EAAE,YAAY,MAAM;AAC9D,wBAAM,KAAK,OAAO,WAAiC;AAAA,gBACrD,CAAC;AAAA,cACH;AAAA,YACF,OAAO;AACL,mBAAK,QAAQ,KAAK,2BAA2B,WAAW,QAAQ,qBAAqB,OAAO,EAAE;AAAA,YAChG;AAAA,UACF,WAAW,kBAAkB,oBAAoB,GAAG;AAClD,gBAAI,gBAAgB,cAAc;AAChC,mBAAK,2BAA2B;AAAA,gBAC9B,qBAAqB,GAAG,aAAa,OAAO,EAAE,YAAY,MAAM;AAC9D,wBAAM,KAAK,OAAO,WAAiC;AAAA,gBACrD,CAAC;AAAA,cACH;AAAA,YACF,OAAO;AACL,mBAAK,QAAQ,KAAK,2BAA2B,WAAW,QAAQ,qBAAqB,OAAO,EAAE;AAAA,YAChG;AAAA,UACF;AAAA,QACF,OAAO;AACL,eAAK,QAAQ,KAAK,oCAAoC,oBAAoB,QAAQ,KAAK,OAAO,EAAE;AAAA,QAClG;AAAA,MACF;AAAA,IACF;AAEA,WAAO,MAAM,MAAM,aAAa,OAAO;AAAA,EACzC;AAAA,EAEA,MAAyB,YAAY,SAAkB;AACrD,eAAW,eAAe,KAAK,4BAA4B;AACzD,kBAAY;AAAA,IACd;AACA,SAAK,6BAA6B,CAAC;AACnC,SAAK,6BAA6B,CAAC;AACnC,WAAO,MAAM,MAAM,YAAY,OAAO;AAAA,EACxC;AAIF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/AbstractDiviner.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { globallyUnique } from '@xylabs/base'\nimport { delay } from '@xylabs/delay'\nimport type { EventUnsubscribeFunction } from '@xylabs/events'\nimport { forget } from '@xylabs/forget'\nimport type { Logger } from '@xylabs/logger'\nimport type { Promisable } from '@xylabs/promise'\nimport type { RetryConfig, RetryConfigWithComplete } from '@xylabs/retry'\nimport { retry } from '@xylabs/retry'\nimport { spanAsync } from '@xylabs/telemetry'\nimport { isDefined, isNull } from '@xylabs/typeof'\nimport type { AccountInstance } from '@xyo-network/account-model'\nimport { isArchivistInstance } from '@xyo-network/archivist-model'\nimport type { QueryBoundWitness } from '@xyo-network/boundwitness-model'\nimport { QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-wrapper'\nimport type {\n AttachableDivinerInstance,\n DivinerDivineQuery,\n DivinerDivineResult,\n DivinerInstance,\n DivinerModuleEventData,\n DivinerParams,\n DivinerQueries,\n} from '@xyo-network/diviner-model'\nimport {\n DivinerConfigSchema,\n DivinerDivineQuerySchema,\n isDivinerInstance,\n} from '@xyo-network/diviner-model'\nimport { AbstractModuleInstance } from '@xyo-network/module-abstract'\nimport type {\n ModuleConfig, ModuleIdentifier, ModuleInstance, ModuleQueryHandlerResult, ModuleQueryResult,\n} from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type {\n Payload, Schema, WithoutPrivateStorageMeta,\n} from '@xyo-network/payload-model'\nimport { isWitnessInstance } from '@xyo-network/witness-model'\n\nconst delayedResolve = async (\n parent: ModuleInstance,\n id: ModuleIdentifier,\n closure: (mod: ModuleInstance | null) => void,\n timeout = 30_000,\n logger?: Logger,\n) => {\n const start = Date.now()\n let result: ModuleInstance | undefined\n while (result) {\n result = await parent.resolve(id)\n if (isDefined(result)) {\n closure(result)\n break\n }\n if (Date.now() - start > timeout) {\n logger?.error(`Timed out waiting for ${id} to resolve`)\n closure(null)\n }\n await delay(500)\n }\n}\n\nexport abstract class AbstractDiviner<\n TParams extends DivinerParams = DivinerParams,\n TIn extends Payload = Payload,\n TOut extends Payload = Payload,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n>\n extends AbstractModuleInstance<TParams, TEventData>\n implements AttachableDivinerInstance<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, DivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = DivinerConfigSchema\n static readonly targetSchema: string\n static override readonly uniqueName = globallyUnique('AbstractDiviner', AbstractDiviner, 'xyo')\n\n private _eventUnsubscribeFunctions: EventUnsubscribeFunction[] = []\n\n override get queries(): string[] {\n return [DivinerDivineQuerySchema, ...super.queries]\n }\n\n /** @function divine The main entry point for a diviner. Do not override this function. Implement/override divineHandler for custom functionality */\n async divine(payloads: TIn[] = [], retryConfigIn?: RetryConfigWithComplete): Promise<DivinerDivineResult<TOut>[]> {\n this._noOverride('divine')\n return await spanAsync('divine', async () => {\n if (this.reentrancy?.scope === 'global' && this.reentrancy.action === 'skip' && this.globalReentrancyMutex?.isLocked()) {\n return []\n }\n try {\n await this.globalReentrancyMutex?.acquire()\n return await this.busy(async () => {\n const retryConfig = retryConfigIn ?? this.config.retry\n await this.started('throw')\n await this.emit('divineStart', { inPayloads: payloads, mod: this })\n const resultPayloads\n = (retryConfig ? await retry(() => this.divineHandler(payloads), retryConfig) : await this.divineHandler(payloads)) ?? []\n await this.emit('divineEnd', {\n errors: [], inPayloads: payloads, mod: this, outPayloads: resultPayloads,\n })\n return PayloadBuilder.omitPrivateStorageMeta(resultPayloads)\n })\n } finally {\n this.globalReentrancyMutex?.release()\n }\n }, this.tracer)\n }\n\n async divineQuery(payloads?: TIn[], account?: AccountInstance, _retry?: RetryConfig): Promise<ModuleQueryResult<TOut>> {\n const queryPayload: DivinerDivineQuery = { schema: DivinerDivineQuerySchema }\n return await this.sendQueryRaw(queryPayload, payloads, account)\n }\n\n /** @function queryHandler Calls divine for a divine query. Override to support additional queries. */\n protected override async queryHandler<T extends QueryBoundWitness = QueryBoundWitness, TConfig extends ModuleConfig = ModuleConfig>(\n query: T,\n payloads?: Payload[],\n queryConfig?: TConfig,\n ): Promise<ModuleQueryHandlerResult> {\n const wrapper = QueryBoundWitnessWrapper.parseQuery<DivinerQueries>(query, payloads)\n // remove the query payload\n const cleanPayloads = await PayloadBuilder.filterExclude(payloads, query.query)\n const queryPayload = await wrapper.getQuery()\n assertEx(await this.queryable(query, payloads, queryConfig))\n const resultPayloads: WithoutPrivateStorageMeta<Payload>[] = []\n switch (queryPayload.schema) {\n case DivinerDivineQuerySchema: {\n resultPayloads.push(...(await this.divine(cleanPayloads as TIn[])))\n break\n }\n default: {\n return super.queryHandler(query, payloads)\n }\n }\n return PayloadBuilder.omitPrivateStorageMeta(resultPayloads)\n }\n\n protected override async startHandler(timeout?: number) {\n const { eventSubscriptions = [] } = this.config\n\n for (const subscription of eventSubscriptions) {\n const {\n sourceEvent, sourceModule, targetModuleFunction,\n } = subscription\n if (targetModuleFunction === 'divine') {\n forget(delayedResolve(this, sourceModule, (sourceModuleInstance: ModuleInstance | null) => {\n if (isNull(sourceModuleInstance)) {\n this.logger?.error(`Failed to resolve ${sourceModule} for ${this.modName}`)\n } else {\n if (isArchivistInstance(sourceModuleInstance)) {\n if (sourceEvent === 'inserted') {\n this._eventUnsubscribeFunctions.push(\n sourceModuleInstance.on(sourceEvent, async ({ outPayloads, payloads }) => {\n await this.divine((outPayloads ?? payloads) as Payload[] as TIn[])\n }),\n )\n } else {\n this.logger?.warn(`Unsupported sourceEvent ${sourceEvent} for ${sourceModuleInstance.modName}`)\n }\n } else if (isDivinerInstance(sourceModuleInstance)) {\n if (sourceEvent === 'divineEnd') {\n this._eventUnsubscribeFunctions.push(\n sourceModuleInstance.on(sourceEvent, async ({ outPayloads }) => {\n await this.divine(outPayloads as Payload[] as TIn[])\n }),\n )\n } else {\n this.logger?.warn(`Unsupported sourceEvent ${sourceEvent} for ${sourceModuleInstance.modName}`)\n }\n } else if (isWitnessInstance(sourceModuleInstance)) {\n if (sourceEvent === 'observeEnd') {\n this._eventUnsubscribeFunctions.push(\n sourceModuleInstance.on(sourceEvent, async ({ outPayloads }) => {\n await this.divine(outPayloads as Payload[] as TIn[])\n }),\n )\n } else {\n this.logger?.warn(`Unsupported sourceEvent ${sourceEvent} for ${sourceModuleInstance.modName}`)\n }\n }\n }\n }))\n }\n }\n\n return await super.startHandler(timeout)\n }\n\n protected override async stopHandler(timeout?: number) {\n for (const unsubscribe of this._eventUnsubscribeFunctions) {\n unsubscribe()\n }\n this._eventUnsubscribeFunctions = []\n this._eventUnsubscribeFunctions = []\n return await super.stopHandler(timeout)\n }\n\n /** @function divineHandler Implement or override to add custom functionality to a diviner */\n protected abstract divineHandler(payloads?: TIn[]): Promisable<TOut[]>\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,sBAAsB;AAC/B,SAAS,aAAa;AAEtB,SAAS,cAAc;AAIvB,SAAS,aAAa;AACtB,SAAS,iBAAiB;AAC1B,SAAS,WAAW,cAAc;AAElC,SAAS,2BAA2B;AAEpC,SAAS,gCAAgC;AAUzC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,8BAA8B;AAIvC,SAAS,sBAAsB;AAI/B,SAAS,yBAAyB;AAElC,IAAM,iBAAiB,OACrB,QACA,IACA,SACA,UAAU,KACV,WACG;AACH,QAAM,QAAQ,KAAK,IAAI;AACvB,MAAI;AACJ,SAAO,QAAQ;AACb,aAAS,MAAM,OAAO,QAAQ,EAAE;AAChC,QAAI,UAAU,MAAM,GAAG;AACrB,cAAQ,MAAM;AACd;AAAA,IACF;AACA,QAAI,KAAK,IAAI,IAAI,QAAQ,SAAS;AAChC,cAAQ,MAAM,yBAAyB,EAAE,aAAa;AACtD,cAAQ,IAAI;AAAA,IACd;AACA,UAAM,MAAM,GAAG;AAAA,EACjB;AACF;AAEO,IAAe,kBAAf,MAAe,yBAUZ,uBAC6D;AAAA,EACrE,OAAyB,gBAA0B,CAAC,GAAG,MAAM,eAAe,mBAAmB;AAAA,EAC/F,OAAyB,sBAA8B;AAAA,EACvD,OAAgB;AAAA,EAChB,OAAyB,aAAa,eAAe,mBAAmB,kBAAiB,KAAK;AAAA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MAAM;AAC9D,0BAAM,KAAK,OAAO,WAAiC;AAAA,kBACrD,CAAC;AAAA,gBACH;AAAA,cACF,OAAO;AACL,qBAAK,QAAQ,KAAK,2BAA2B,WAAW,QAAQ,qBAAqB,OAAO,EAAE;AAAA,cAChG;AAAA,YACF,WAAW,kBAAkB,oBAAoB,GAAG;AAClD,kBAAI,gBAAgB,cAAc;AAChC,qBAAK,2BAA2B;AAAA,kBAC9B,qBAAqB,GAAG,aAAa,OAAO,EAAE,YAAY,MAAM;AAC9D,0BAAM,KAAK,OAAO,WAAiC;AAAA,kBACrD,CAAC;AAAA,gBACH;AAAA,cACF,OAAO;AACL,qBAAK,QAAQ,KAAK,2BAA2B,WAAW,QAAQ,qBAAqB,OAAO,EAAE;AAAA,cAChG;AAAA,YACF;AAAA,UACF;AAAA,QACF,CAAC,CAAC;AAAA,MACJ;AAAA,IACF;AAEA,WAAO,MAAM,MAAM,aAAa,OAAO;AAAA,EACzC;AAAA,EAEA,MAAyB,YAAY,SAAkB;AACrD,eAAW,eAAe,KAAK,4BAA4B;AACzD,kBAAY;AAAA,IACd;AACA,SAAK,6BAA6B,CAAC;AACnC,SAAK,6BAA6B,CAAC;AACnC,WAAO,MAAM,MAAM,YAAY,OAAO;AAAA,EACxC;AAIF;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbstractDiviner.d.ts","sourceRoot":"","sources":["../../src/AbstractDiviner.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AbstractDiviner.d.ts","sourceRoot":"","sources":["../../src/AbstractDiviner.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,KAAK,EAAE,WAAW,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAA;AAIzE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAA;AAEjE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iCAA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G,EAAE,EAAE,OAAO,CAAC,EAAE,eAAe,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC;IAKtH,uGAAuG;cAC9E,YAAY,CAAC,CAAC,SAAS,iBAAiB,GAAG,iBAAiB,EAAE,OAAO,SAAS,YAAY,GAAG,YAAY,EAChI,KAAK,EAAE,CAAC,EACR,QAAQ,CAAC,EAAE,OAAO,EAAE,EACpB,WAAW,CAAC,EAAE,OAAO,GACpB,OAAO,CAAC,wBAAwB,CAAC;cAmBX,YAAY,CAAC,OAAO,CAAC,EAAE,MAAM;cAmD7B,WAAW,CAAC,OAAO,CAAC,EAAE,MAAM;IASrD,6FAA6F;IAC7F,SAAS,CAAC,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE,GAAG,UAAU,CAAC,IAAI,EAAE,CAAC;CACvE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xyo-network/diviner-abstract",
|
|
3
|
-
"version": "3.15.
|
|
3
|
+
"version": "3.15.7",
|
|
4
4
|
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
5
5
|
"homepage": "https://xyo.network",
|
|
6
6
|
"bugs": {
|
|
@@ -29,26 +29,30 @@
|
|
|
29
29
|
"module": "dist/neutral/index.mjs",
|
|
30
30
|
"types": "dist/types/index.d.ts",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@xylabs/assert": "^4.9.
|
|
33
|
-
"@xylabs/base": "^4.9.
|
|
34
|
-
"@xylabs/
|
|
35
|
-
"@xylabs/
|
|
36
|
-
"@xylabs/
|
|
37
|
-
"@
|
|
38
|
-
"@
|
|
39
|
-
"@
|
|
40
|
-
"@xyo-network/
|
|
41
|
-
"@xyo-network/
|
|
42
|
-
"@xyo-network/
|
|
43
|
-
"@xyo-network/
|
|
44
|
-
"@xyo-network/
|
|
45
|
-
"@xyo-network/
|
|
46
|
-
"@xyo-network/
|
|
32
|
+
"@xylabs/assert": "^4.9.17",
|
|
33
|
+
"@xylabs/base": "^4.9.17",
|
|
34
|
+
"@xylabs/delay": "^4.9.17",
|
|
35
|
+
"@xylabs/logger": "^4.9.17",
|
|
36
|
+
"@xylabs/promise": "^4.9.17",
|
|
37
|
+
"@xylabs/retry": "^4.9.17",
|
|
38
|
+
"@xylabs/telemetry": "^4.9.17",
|
|
39
|
+
"@xylabs/typeof": "^4.9.17",
|
|
40
|
+
"@xyo-network/account-model": "^3.15.7",
|
|
41
|
+
"@xyo-network/archivist-model": "^3.15.7",
|
|
42
|
+
"@xyo-network/boundwitness-model": "^3.15.7",
|
|
43
|
+
"@xyo-network/boundwitness-wrapper": "^3.15.7",
|
|
44
|
+
"@xyo-network/diviner-model": "^3.15.7",
|
|
45
|
+
"@xyo-network/module-abstract": "^3.15.7",
|
|
46
|
+
"@xyo-network/module-model": "^3.15.7",
|
|
47
|
+
"@xyo-network/payload-builder": "^3.15.7",
|
|
48
|
+
"@xyo-network/payload-model": "^3.15.7",
|
|
49
|
+
"@xyo-network/witness-model": "^3.15.7"
|
|
47
50
|
},
|
|
48
51
|
"devDependencies": {
|
|
52
|
+
"@xylabs/events": "^4.9.17",
|
|
49
53
|
"@xylabs/ts-scripts-yarn3": "^6.5.5",
|
|
50
54
|
"@xylabs/tsconfig": "^6.5.5",
|
|
51
|
-
"@xyo-network/module-events": "^3.15.
|
|
55
|
+
"@xyo-network/module-events": "^3.15.7",
|
|
52
56
|
"typescript": "^5.8.3"
|
|
53
57
|
},
|
|
54
58
|
"publishConfig": {
|
package/src/AbstractDiviner.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { assertEx } from '@xylabs/assert'
|
|
2
2
|
import { globallyUnique } from '@xylabs/base'
|
|
3
|
+
import { delay } from '@xylabs/delay'
|
|
4
|
+
import type { EventUnsubscribeFunction } from '@xylabs/events'
|
|
5
|
+
import { forget } from '@xylabs/forget'
|
|
6
|
+
import type { Logger } from '@xylabs/logger'
|
|
3
7
|
import type { Promisable } from '@xylabs/promise'
|
|
4
8
|
import type { RetryConfig, RetryConfigWithComplete } from '@xylabs/retry'
|
|
5
9
|
import { retry } from '@xylabs/retry'
|
|
6
10
|
import { spanAsync } from '@xylabs/telemetry'
|
|
11
|
+
import { isDefined, isNull } from '@xylabs/typeof'
|
|
7
12
|
import type { AccountInstance } from '@xyo-network/account-model'
|
|
8
13
|
import { isArchivistInstance } from '@xyo-network/archivist-model'
|
|
9
14
|
import type { QueryBoundWitness } from '@xyo-network/boundwitness-model'
|
|
@@ -23,9 +28,8 @@ import {
|
|
|
23
28
|
isDivinerInstance,
|
|
24
29
|
} from '@xyo-network/diviner-model'
|
|
25
30
|
import { AbstractModuleInstance } from '@xyo-network/module-abstract'
|
|
26
|
-
import type { EventUnsubscribeFunction } from '@xyo-network/module-events'
|
|
27
31
|
import type {
|
|
28
|
-
ModuleConfig, ModuleQueryHandlerResult, ModuleQueryResult,
|
|
32
|
+
ModuleConfig, ModuleIdentifier, ModuleInstance, ModuleQueryHandlerResult, ModuleQueryResult,
|
|
29
33
|
} from '@xyo-network/module-model'
|
|
30
34
|
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
31
35
|
import type {
|
|
@@ -33,6 +37,29 @@ import type {
|
|
|
33
37
|
} from '@xyo-network/payload-model'
|
|
34
38
|
import { isWitnessInstance } from '@xyo-network/witness-model'
|
|
35
39
|
|
|
40
|
+
const delayedResolve = async (
|
|
41
|
+
parent: ModuleInstance,
|
|
42
|
+
id: ModuleIdentifier,
|
|
43
|
+
closure: (mod: ModuleInstance | null) => void,
|
|
44
|
+
timeout = 30_000,
|
|
45
|
+
logger?: Logger,
|
|
46
|
+
) => {
|
|
47
|
+
const start = Date.now()
|
|
48
|
+
let result: ModuleInstance | undefined
|
|
49
|
+
while (result) {
|
|
50
|
+
result = await parent.resolve(id)
|
|
51
|
+
if (isDefined(result)) {
|
|
52
|
+
closure(result)
|
|
53
|
+
break
|
|
54
|
+
}
|
|
55
|
+
if (Date.now() - start > timeout) {
|
|
56
|
+
logger?.error(`Timed out waiting for ${id} to resolve`)
|
|
57
|
+
closure(null)
|
|
58
|
+
}
|
|
59
|
+
await delay(500)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
36
63
|
export abstract class AbstractDiviner<
|
|
37
64
|
TParams extends DivinerParams = DivinerParams,
|
|
38
65
|
TIn extends Payload = Payload,
|
|
@@ -118,43 +145,44 @@ export abstract class AbstractDiviner<
|
|
|
118
145
|
const {
|
|
119
146
|
sourceEvent, sourceModule, targetModuleFunction,
|
|
120
147
|
} = subscription
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
148
|
+
if (targetModuleFunction === 'divine') {
|
|
149
|
+
forget(delayedResolve(this, sourceModule, (sourceModuleInstance: ModuleInstance | null) => {
|
|
150
|
+
if (isNull(sourceModuleInstance)) {
|
|
151
|
+
this.logger?.error(`Failed to resolve ${sourceModule} for ${this.modName}`)
|
|
152
|
+
} else {
|
|
153
|
+
if (isArchivistInstance(sourceModuleInstance)) {
|
|
154
|
+
if (sourceEvent === 'inserted') {
|
|
155
|
+
this._eventUnsubscribeFunctions.push(
|
|
156
|
+
sourceModuleInstance.on(sourceEvent, async ({ outPayloads, payloads }) => {
|
|
157
|
+
await this.divine((outPayloads ?? payloads) as Payload[] as TIn[])
|
|
158
|
+
}),
|
|
159
|
+
)
|
|
160
|
+
} else {
|
|
161
|
+
this.logger?.warn(`Unsupported sourceEvent ${sourceEvent} for ${sourceModuleInstance.modName}`)
|
|
162
|
+
}
|
|
163
|
+
} else if (isDivinerInstance(sourceModuleInstance)) {
|
|
164
|
+
if (sourceEvent === 'divineEnd') {
|
|
165
|
+
this._eventUnsubscribeFunctions.push(
|
|
166
|
+
sourceModuleInstance.on(sourceEvent, async ({ outPayloads }) => {
|
|
167
|
+
await this.divine(outPayloads as Payload[] as TIn[])
|
|
168
|
+
}),
|
|
169
|
+
)
|
|
170
|
+
} else {
|
|
171
|
+
this.logger?.warn(`Unsupported sourceEvent ${sourceEvent} for ${sourceModuleInstance.modName}`)
|
|
172
|
+
}
|
|
173
|
+
} else if (isWitnessInstance(sourceModuleInstance)) {
|
|
174
|
+
if (sourceEvent === 'observeEnd') {
|
|
175
|
+
this._eventUnsubscribeFunctions.push(
|
|
176
|
+
sourceModuleInstance.on(sourceEvent, async ({ outPayloads }) => {
|
|
177
|
+
await this.divine(outPayloads as Payload[] as TIn[])
|
|
178
|
+
}),
|
|
179
|
+
)
|
|
180
|
+
} else {
|
|
181
|
+
this.logger?.warn(`Unsupported sourceEvent ${sourceEvent} for ${sourceModuleInstance.modName}`)
|
|
182
|
+
}
|
|
153
183
|
}
|
|
154
184
|
}
|
|
155
|
-
}
|
|
156
|
-
this.logger?.warn(`Unsupported targetModuleFunction ${targetModuleFunction} for ${this.modName}`)
|
|
157
|
-
}
|
|
185
|
+
}))
|
|
158
186
|
}
|
|
159
187
|
}
|
|
160
188
|
|