@projectqai/proto 0.0.27 → 0.0.29
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/device.ts +19 -2
- package/package.json +1 -1
package/device.ts
CHANGED
|
@@ -147,6 +147,7 @@ export interface AttachOptions<S extends SchemaProperties> {
|
|
|
147
147
|
device?: { category?: string };
|
|
148
148
|
icon?: string;
|
|
149
149
|
schema: S;
|
|
150
|
+
init?: (client: WorldClient, config: InferConfig<S>, signal: AbortSignal) => Promise<void>;
|
|
150
151
|
run: (client: WorldClient, config: InferConfig<S>, signal: AbortSignal) => Promise<void>;
|
|
151
152
|
health?: () => HealthResult | Promise<HealthResult>;
|
|
152
153
|
interval?: number;
|
|
@@ -202,10 +203,13 @@ export async function attach<S extends SchemaProperties>(opts: AttachOptions<S>)
|
|
|
202
203
|
|
|
203
204
|
let heartbeatId: ReturnType<typeof setInterval> | null = null;
|
|
204
205
|
|
|
206
|
+
let initialized = false;
|
|
207
|
+
|
|
205
208
|
const pushHeartbeat = (result?: Record<number, { label: string; value: number | bigint }>) => {
|
|
209
|
+
const state = initialized ? DeviceState.DeviceStateActive : DeviceState.DeviceStatePending;
|
|
206
210
|
const e = create(EntitySchema, {
|
|
207
211
|
id: entityID,
|
|
208
|
-
device: create(DeviceComponentSchema, { ...entity.device, state
|
|
212
|
+
device: create(DeviceComponentSchema, { ...entity.device, state }),
|
|
209
213
|
lifetime: create(LifetimeSchema, {
|
|
210
214
|
until: create(TimestampSchema, { seconds: BigInt(Math.floor((Date.now() + interval + 1_000) / 1000)) }),
|
|
211
215
|
}),
|
|
@@ -241,7 +245,7 @@ export async function attach<S extends SchemaProperties>(opts: AttachOptions<S>)
|
|
|
241
245
|
: create(ConfigurableComponentSchema);
|
|
242
246
|
cfg.state = state;
|
|
243
247
|
cfg.error = error;
|
|
244
|
-
if (entity.config && state === ConfigurableState.ConfigurableStateActive) {
|
|
248
|
+
if (entity.config && (state === ConfigurableState.ConfigurableStateStarting || state === ConfigurableState.ConfigurableStateActive)) {
|
|
245
249
|
cfg.appliedVersion = entity.config.version;
|
|
246
250
|
}
|
|
247
251
|
await push(client, create(EntitySchema, { id: entityID, configurable: cfg })).catch(() => { });
|
|
@@ -257,6 +261,7 @@ export async function attach<S extends SchemaProperties>(opts: AttachOptions<S>)
|
|
|
257
261
|
runningAbort.abort();
|
|
258
262
|
runningAbort = null;
|
|
259
263
|
currentEntity = null;
|
|
264
|
+
initialized = false;
|
|
260
265
|
console.log(`stopped entity=${entityID}`);
|
|
261
266
|
if (e) pushState(e, ConfigurableState.ConfigurableStateInactive);
|
|
262
267
|
}
|
|
@@ -270,6 +275,18 @@ export async function attach<S extends SchemaProperties>(opts: AttachOptions<S>)
|
|
|
270
275
|
(async () => {
|
|
271
276
|
while (!childSignal.aborted) {
|
|
272
277
|
await pushState(e, ConfigurableState.ConfigurableStateStarting);
|
|
278
|
+
|
|
279
|
+
try {
|
|
280
|
+
if (opts.init) await opts.init(client, extractConfig(e, opts.schema), childSignal);
|
|
281
|
+
} catch (err) {
|
|
282
|
+
if (childSignal.aborted || isCanceled(err)) return;
|
|
283
|
+
console.error(`init failed entity=${entityID}`, err);
|
|
284
|
+
await pushState(e, ConfigurableState.ConfigurableStateFailed, String(err));
|
|
285
|
+
await sleep(5_000, childSignal);
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
initialized = true;
|
|
273
290
|
await pushState(e, ConfigurableState.ConfigurableStateActive);
|
|
274
291
|
|
|
275
292
|
try {
|