@homelib/core 0.0.0-wip.ba4eafa051eb

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 (132) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +7 -0
  3. package/bld/exports/mobx.d.ts +1 -0
  4. package/bld/exports/mobx.js +2 -0
  5. package/bld/exports/mobx.js.map +1 -0
  6. package/bld/library/@lifecycle.d.ts +4 -0
  7. package/bld/library/@lifecycle.js +23 -0
  8. package/bld/library/@lifecycle.js.map +1 -0
  9. package/bld/library/atomics/index.d.ts +1 -0
  10. package/bld/library/atomics/index.js +2 -0
  11. package/bld/library/atomics/index.js.map +1 -0
  12. package/bld/library/atomics/temperature.d.ts +13 -0
  13. package/bld/library/atomics/temperature.js +53 -0
  14. package/bld/library/atomics/temperature.js.map +1 -0
  15. package/bld/library/command.d.ts +12 -0
  16. package/bld/library/command.js +21 -0
  17. package/bld/library/command.js.map +1 -0
  18. package/bld/library/device/index.d.ts +1 -0
  19. package/bld/library/device/index.js +2 -0
  20. package/bld/library/device/index.js.map +1 -0
  21. package/bld/library/device/sensor.d.ts +8 -0
  22. package/bld/library/device/sensor.js +2 -0
  23. package/bld/library/device/sensor.js.map +1 -0
  24. package/bld/library/device-namespace.d.ts +11 -0
  25. package/bld/library/device-namespace.js +9 -0
  26. package/bld/library/device-namespace.js.map +1 -0
  27. package/bld/library/device.d.ts +23 -0
  28. package/bld/library/device.js +92 -0
  29. package/bld/library/device.js.map +1 -0
  30. package/bld/library/devices/air-conditioner.d.ts +72 -0
  31. package/bld/library/devices/air-conditioner.js +246 -0
  32. package/bld/library/devices/air-conditioner.js.map +1 -0
  33. package/bld/library/devices/dehumidifier.d.ts +62 -0
  34. package/bld/library/devices/dehumidifier.js +211 -0
  35. package/bld/library/devices/dehumidifier.js.map +1 -0
  36. package/bld/library/devices/fan.d.ts +70 -0
  37. package/bld/library/devices/fan.js +229 -0
  38. package/bld/library/devices/fan.js.map +1 -0
  39. package/bld/library/devices/index.d.ts +4 -0
  40. package/bld/library/devices/index.js +5 -0
  41. package/bld/library/devices/index.js.map +1 -0
  42. package/bld/library/devices/light.d.ts +59 -0
  43. package/bld/library/devices/light.js +197 -0
  44. package/bld/library/devices/light.js.map +1 -0
  45. package/bld/library/endpoint.d.ts +100 -0
  46. package/bld/library/endpoint.js +431 -0
  47. package/bld/library/endpoint.js.map +1 -0
  48. package/bld/library/home.d.ts +4 -0
  49. package/bld/library/home.js +11 -0
  50. package/bld/library/home.js.map +1 -0
  51. package/bld/library/index.d.ts +16 -0
  52. package/bld/library/index.js +17 -0
  53. package/bld/library/index.js.map +1 -0
  54. package/bld/library/log.d.ts +43 -0
  55. package/bld/library/log.js +78 -0
  56. package/bld/library/log.js.map +1 -0
  57. package/bld/library/provider.d.ts +26 -0
  58. package/bld/library/provider.js +18 -0
  59. package/bld/library/provider.js.map +1 -0
  60. package/bld/library/registry.d.ts +19 -0
  61. package/bld/library/registry.js +65 -0
  62. package/bld/library/registry.js.map +1 -0
  63. package/bld/library/runtime/@binding-creation.d.ts +4 -0
  64. package/bld/library/runtime/@binding-creation.js +19 -0
  65. package/bld/library/runtime/@binding-creation.js.map +1 -0
  66. package/bld/library/runtime/binding.d.ts +50 -0
  67. package/bld/library/runtime/binding.js +108 -0
  68. package/bld/library/runtime/binding.js.map +1 -0
  69. package/bld/library/runtime/bootstrap-frontend.d.ts +48 -0
  70. package/bld/library/runtime/bootstrap-frontend.js +96 -0
  71. package/bld/library/runtime/bootstrap-frontend.js.map +1 -0
  72. package/bld/library/runtime/bootstrap.d.ts +9 -0
  73. package/bld/library/runtime/bootstrap.js +165 -0
  74. package/bld/library/runtime/bootstrap.js.map +1 -0
  75. package/bld/library/runtime/environment.d.ts +1 -0
  76. package/bld/library/runtime/environment.js +7 -0
  77. package/bld/library/runtime/environment.js.map +1 -0
  78. package/bld/library/runtime/index.d.ts +4 -0
  79. package/bld/library/runtime/index.js +5 -0
  80. package/bld/library/runtime/index.js.map +1 -0
  81. package/bld/library/scope.d.ts +41 -0
  82. package/bld/library/scope.js +132 -0
  83. package/bld/library/scope.js.map +1 -0
  84. package/bld/library/types.d.ts +17 -0
  85. package/bld/library/types.js +8 -0
  86. package/bld/library/types.js.map +1 -0
  87. package/bld/library/utils/backoff.d.ts +10 -0
  88. package/bld/library/utils/backoff.js +63 -0
  89. package/bld/library/utils/backoff.js.map +1 -0
  90. package/bld/library/utils/constructor.d.ts +21 -0
  91. package/bld/library/utils/constructor.js +30 -0
  92. package/bld/library/utils/constructor.js.map +1 -0
  93. package/bld/library/utils/index.d.ts +2 -0
  94. package/bld/library/utils/index.js +3 -0
  95. package/bld/library/utils/index.js.map +1 -0
  96. package/bld/library/x-data-medium.d.ts +12 -0
  97. package/bld/library/x-data-medium.js +3 -0
  98. package/bld/library/x-data-medium.js.map +1 -0
  99. package/package.json +27 -0
  100. package/src/exports/mobx.ts +1 -0
  101. package/src/library/@lifecycle.ts +30 -0
  102. package/src/library/atomics/index.ts +1 -0
  103. package/src/library/atomics/temperature.ts +60 -0
  104. package/src/library/command.ts +25 -0
  105. package/src/library/device/index.ts +1 -0
  106. package/src/library/device/sensor.ts +10 -0
  107. package/src/library/device-namespace.ts +21 -0
  108. package/src/library/device.ts +91 -0
  109. package/src/library/devices/air-conditioner.ts +234 -0
  110. package/src/library/devices/dehumidifier.ts +200 -0
  111. package/src/library/devices/fan.ts +204 -0
  112. package/src/library/devices/index.ts +4 -0
  113. package/src/library/devices/light.ts +177 -0
  114. package/src/library/endpoint.ts +610 -0
  115. package/src/library/home.ts +11 -0
  116. package/src/library/index.ts +16 -0
  117. package/src/library/log.ts +163 -0
  118. package/src/library/provider.ts +66 -0
  119. package/src/library/registry.ts +129 -0
  120. package/src/library/runtime/@binding-creation.ts +48 -0
  121. package/src/library/runtime/binding.ts +165 -0
  122. package/src/library/runtime/bootstrap-frontend.ts +243 -0
  123. package/src/library/runtime/bootstrap.ts +286 -0
  124. package/src/library/runtime/environment.ts +6 -0
  125. package/src/library/runtime/index.ts +4 -0
  126. package/src/library/scope.ts +193 -0
  127. package/src/library/types/global.d.ts +8 -0
  128. package/src/library/types.ts +26 -0
  129. package/src/library/utils/backoff.ts +65 -0
  130. package/src/library/utils/constructor.ts +89 -0
  131. package/src/library/utils/index.ts +2 -0
  132. package/src/library/x-data-medium.ts +16 -0
@@ -0,0 +1,610 @@
1
+ import {action, comparer, computed, observable, reaction} from 'mobx';
2
+
3
+ import {type Command, CommandError, StatefulCommand} from './command.js';
4
+ import {
5
+ hasEndpointLogTarget,
6
+ logEndpointCommand,
7
+ logEndpointError,
8
+ logEndpointState,
9
+ } from './log.js';
10
+ import {ExponentialBackoff} from './utils/index.js';
11
+
12
+ export abstract class Endpoint<
13
+ TCommand extends Command,
14
+ TConnection extends EndpointConnection<TCommand> =
15
+ EndpointConnection<TCommand>,
16
+ > {
17
+ @observable.ref private accessor connection_: TConnection | undefined;
18
+
19
+ private pendingCommands: TCommand[] = [];
20
+
21
+ private processingCommands = false;
22
+
23
+ private readonly connectionErrorBackoff = new ExponentialBackoff(100, 10_000);
24
+
25
+ private connectionReactionDisposer: (() => void) | undefined;
26
+
27
+ private logStateReactionDisposer: (() => void) | undefined;
28
+
29
+ private acknowledgedCommands: AcknowledgedCommand[] = [];
30
+
31
+ private commandsWithUncertainEffects: UncertainCommandEffect[] = [];
32
+
33
+ private connectionStateGeneration = 0;
34
+
35
+ constructor(readonly name = '') {}
36
+
37
+ @computed
38
+ get ready(): boolean {
39
+ return this.connection?.ready ?? false;
40
+ }
41
+
42
+ protected get connection(): TConnection | undefined {
43
+ return this.connection_;
44
+ }
45
+
46
+ protected get logState(): EndpointLogState {
47
+ return {ready: this.ready};
48
+ }
49
+
50
+ @action
51
+ bindConnection(connection: TConnection | undefined): void {
52
+ if (this.connection_ === connection) {
53
+ return;
54
+ }
55
+
56
+ this.connectionReactionDisposer?.();
57
+ this.logStateReactionDisposer?.();
58
+ this.clearCommandEffectState();
59
+
60
+ this.connection_ = connection;
61
+ this.connectionStateGeneration++;
62
+ this.connectionErrorBackoff.reset();
63
+
64
+ this.logStateReactionDisposer =
65
+ connection !== undefined && hasEndpointLogTarget(this)
66
+ ? reaction(
67
+ () => this.logState,
68
+ (state, previousState) => {
69
+ logEndpointState(this, connection, state, previousState);
70
+ },
71
+ {equals: comparer.structural, fireImmediately: true},
72
+ )
73
+ : undefined;
74
+
75
+ this.connectionReactionDisposer = connection
76
+ ? reaction(
77
+ () => ({
78
+ ready: connection.ready,
79
+ stateRevision: connection.stateRevision,
80
+ }),
81
+ (state, previousState) => {
82
+ const {ready} = state;
83
+
84
+ if (ready) {
85
+ if (
86
+ previousState !== undefined &&
87
+ state.stateRevision !== previousState.stateRevision
88
+ ) {
89
+ this.reconcileCommandEffects();
90
+ }
91
+
92
+ void this.processPendingCommands().catch(logEndpointError);
93
+ } else {
94
+ this.connectionStateGeneration++;
95
+ this.clearCommandEffectState();
96
+ }
97
+ },
98
+ {equals: comparer.structural, fireImmediately: true},
99
+ )
100
+ : undefined;
101
+ }
102
+
103
+ @action
104
+ unbindConnection(connection: TConnection): void {
105
+ if (this.connection_ === connection) {
106
+ this.bindConnection(undefined);
107
+ }
108
+ }
109
+
110
+ enqueueCommand(command: TCommand): this {
111
+ this.pendingCommands = this.pendingCommands.filter(
112
+ pendingCommand => !command.supersedes(pendingCommand),
113
+ );
114
+
115
+ this.pendingCommands.push(command);
116
+
117
+ void this.processPendingCommands().catch(logEndpointError);
118
+
119
+ return this;
120
+ }
121
+
122
+ private async processPendingCommands(): Promise<void> {
123
+ if (this.processingCommands) {
124
+ return;
125
+ }
126
+
127
+ this.processingCommands = true;
128
+
129
+ try {
130
+ while (
131
+ this.connection &&
132
+ this.connection.ready &&
133
+ this.pendingCommands.length > 0
134
+ ) {
135
+ const command = this.pendingCommands[0];
136
+ const connection = this.connection;
137
+ let executionStarted = false;
138
+ let executionEffect: CommandEffect | undefined;
139
+ let executionEffectObservationRevision: number | undefined;
140
+ let executionStateGeneration: number | undefined;
141
+ try {
142
+ const execution = connection.prepareCommand(command);
143
+ executionEffect = execution.effect;
144
+
145
+ if (this.isStatefulCommandSatisfied(command, execution.effect)) {
146
+ logEndpointCommand(this, connection, command, 'skip', execution);
147
+ consumeCommand(this.pendingCommands, command);
148
+ this.connectionErrorBackoff.reset();
149
+ continue;
150
+ }
151
+
152
+ executionEffectObservationRevision =
153
+ execution.effect?.observationRevision;
154
+ executionStateGeneration = this.connectionStateGeneration;
155
+
156
+ logEndpointCommand(this, connection, command, 'execute', execution);
157
+ executionStarted = true;
158
+ await execution.execute();
159
+
160
+ if (command instanceof StatefulCommand) {
161
+ this.removeAcknowledgedCommandsSupersededBy(command);
162
+ this.removeUncertainEffectsSupersededBy(command);
163
+ }
164
+
165
+ if (
166
+ command instanceof StatefulCommand &&
167
+ execution.effect !== undefined &&
168
+ this.connection === connection &&
169
+ connection.ready
170
+ ) {
171
+ this.acknowledgeCommand(
172
+ command,
173
+ execution.effect,
174
+ connection,
175
+ executionEffectObservationRevision,
176
+ executionStateGeneration,
177
+ );
178
+ } else if (
179
+ command instanceof StatefulCommand &&
180
+ execution.effect === undefined
181
+ ) {
182
+ this.markCommandEffectUncertain(
183
+ command,
184
+ undefined,
185
+ connection,
186
+ undefined,
187
+ executionStateGeneration,
188
+ );
189
+ }
190
+ } catch (error) {
191
+ if (error instanceof EndpointConnectionError) {
192
+ await this.connectionErrorBackoff;
193
+ continue;
194
+ }
195
+
196
+ if (
197
+ executionStarted &&
198
+ command instanceof StatefulCommand &&
199
+ !(error instanceof CommandError)
200
+ ) {
201
+ this.removeAcknowledgedCommandsSupersededBy(command);
202
+ this.removeUncertainEffectsSupersededBy(command);
203
+ this.markCommandEffectUncertain(
204
+ command,
205
+ executionEffect,
206
+ connection,
207
+ executionEffectObservationRevision,
208
+ executionStateGeneration,
209
+ );
210
+ }
211
+
212
+ logEndpointError(error);
213
+ }
214
+
215
+ consumeCommand(this.pendingCommands, command);
216
+
217
+ this.connectionErrorBackoff.reset();
218
+ }
219
+ } finally {
220
+ this.processingCommands = false;
221
+ }
222
+
223
+ function consumeCommand(
224
+ pendingCommands: TCommand[],
225
+ command: TCommand,
226
+ ): void {
227
+ if (pendingCommands.length > 0 && pendingCommands[0] === command) {
228
+ pendingCommands.shift();
229
+ }
230
+ }
231
+ }
232
+
233
+ private isStatefulCommandSatisfied(
234
+ command: TCommand,
235
+ preparedEffect: CommandEffect | undefined,
236
+ ): boolean {
237
+ if (!(command instanceof StatefulCommand) || preparedEffect === undefined) {
238
+ return false;
239
+ }
240
+
241
+ if (
242
+ this.commandsWithUncertainEffects.some(uncertainEffect =>
243
+ command.supersedes(uncertainEffect.command),
244
+ )
245
+ ) {
246
+ return false;
247
+ }
248
+
249
+ let supersededAcknowledgedCommand = false;
250
+
251
+ for (const acknowledgedCommand of this.acknowledgedCommands) {
252
+ if (command.supersedes(acknowledgedCommand.command)) {
253
+ supersededAcknowledgedCommand = true;
254
+
255
+ if (acknowledgedCommand.effect.equals(preparedEffect)) {
256
+ return true;
257
+ }
258
+ }
259
+ }
260
+
261
+ if (supersededAcknowledgedCommand) {
262
+ return false;
263
+ }
264
+
265
+ return this.commandEffectMatches(preparedEffect);
266
+ }
267
+
268
+ private acknowledgeCommand(
269
+ command: Command,
270
+ effect: CommandEffect,
271
+ connection: TConnection,
272
+ observationRevision: number | undefined,
273
+ connectionStateGeneration: number | undefined,
274
+ ): void {
275
+ if (
276
+ observationRevision === undefined ||
277
+ connectionStateGeneration === undefined ||
278
+ !this.isCurrentConnectionState(connection, connectionStateGeneration)
279
+ ) {
280
+ return;
281
+ }
282
+
283
+ const currentObservationRevision = effect.observationRevision;
284
+
285
+ if (currentObservationRevision !== observationRevision) {
286
+ if (this.commandEffectMatches(effect)) {
287
+ this.acknowledgedCommands.push({
288
+ command,
289
+ effect,
290
+ observationRevision: currentObservationRevision,
291
+ });
292
+ } else {
293
+ this.commandsWithUncertainEffects.push({
294
+ command,
295
+ effect,
296
+ observationRevision: currentObservationRevision,
297
+ });
298
+ }
299
+
300
+ return;
301
+ }
302
+
303
+ this.acknowledgedCommands.push({
304
+ command,
305
+ effect,
306
+ observationRevision: currentObservationRevision,
307
+ });
308
+ }
309
+
310
+ private removeAcknowledgedCommandsSupersededBy(command: Command): void {
311
+ for (const acknowledgedCommand of [...this.acknowledgedCommands]) {
312
+ if (command.supersedes(acknowledgedCommand.command)) {
313
+ this.removeAcknowledgedCommand(acknowledgedCommand);
314
+ }
315
+ }
316
+ }
317
+
318
+ private removeUncertainEffectsSupersededBy(command: Command): void {
319
+ this.commandsWithUncertainEffects =
320
+ this.commandsWithUncertainEffects.filter(
321
+ uncertainEffect => !command.supersedes(uncertainEffect.command),
322
+ );
323
+ }
324
+
325
+ private markCommandEffectUncertain(
326
+ command: Command,
327
+ effect: CommandEffect | undefined,
328
+ connection: TConnection,
329
+ observationRevision: number | undefined,
330
+ connectionStateGeneration: number | undefined,
331
+ ): void {
332
+ if (
333
+ connectionStateGeneration === undefined ||
334
+ !this.isCurrentConnectionState(connection, connectionStateGeneration)
335
+ ) {
336
+ return;
337
+ }
338
+
339
+ if (effect === undefined) {
340
+ this.commandsWithUncertainEffects.push({command, effect});
341
+ return;
342
+ }
343
+
344
+ if (observationRevision === undefined) {
345
+ return;
346
+ }
347
+
348
+ const currentObservationRevision = effect.observationRevision;
349
+
350
+ if (currentObservationRevision !== observationRevision) {
351
+ if (this.commandEffectMatches(effect)) {
352
+ this.acknowledgedCommands.push({
353
+ command,
354
+ effect,
355
+ observationRevision: currentObservationRevision,
356
+ });
357
+ } else {
358
+ this.commandsWithUncertainEffects.push({
359
+ command,
360
+ effect,
361
+ observationRevision: currentObservationRevision,
362
+ });
363
+ }
364
+
365
+ return;
366
+ }
367
+
368
+ this.commandsWithUncertainEffects.push({
369
+ command,
370
+ effect,
371
+ observationRevision,
372
+ });
373
+ }
374
+
375
+ private removeAcknowledgedCommand(
376
+ acknowledgedCommand: AcknowledgedCommand,
377
+ ): void {
378
+ const index = this.acknowledgedCommands.indexOf(acknowledgedCommand);
379
+
380
+ if (index === -1) {
381
+ return;
382
+ }
383
+
384
+ this.acknowledgedCommands.splice(index, 1);
385
+ }
386
+
387
+ private reconcileCommandEffects(): void {
388
+ for (const acknowledgedCommand of [...this.acknowledgedCommands]) {
389
+ const observationRevision =
390
+ acknowledgedCommand.effect.observationRevision;
391
+
392
+ if (observationRevision === acknowledgedCommand.observationRevision) {
393
+ continue;
394
+ }
395
+
396
+ if (this.commandEffectMatches(acknowledgedCommand.effect)) {
397
+ acknowledgedCommand.observationRevision = observationRevision;
398
+ } else {
399
+ this.removeAcknowledgedCommand(acknowledgedCommand);
400
+ }
401
+ }
402
+
403
+ for (const uncertainEffect of [...this.commandsWithUncertainEffects]) {
404
+ if (
405
+ uncertainEffect.effect === undefined ||
406
+ uncertainEffect.observationRevision === undefined
407
+ ) {
408
+ continue;
409
+ }
410
+
411
+ const observationRevision = uncertainEffect.effect.observationRevision;
412
+
413
+ if (observationRevision === uncertainEffect.observationRevision) {
414
+ continue;
415
+ }
416
+
417
+ this.removeUncertainCommandEffect(uncertainEffect);
418
+
419
+ if (this.commandEffectMatches(uncertainEffect.effect)) {
420
+ this.acknowledgedCommands.push({
421
+ command: uncertainEffect.command,
422
+ effect: uncertainEffect.effect,
423
+ observationRevision,
424
+ });
425
+ }
426
+ }
427
+ }
428
+
429
+ private removeUncertainCommandEffect(
430
+ uncertainEffect: UncertainCommandEffect,
431
+ ): void {
432
+ const index = this.commandsWithUncertainEffects.indexOf(uncertainEffect);
433
+
434
+ if (index !== -1) {
435
+ this.commandsWithUncertainEffects.splice(index, 1);
436
+ }
437
+ }
438
+
439
+ private commandEffectMatches(effect: CommandEffect): boolean {
440
+ try {
441
+ return effect.matches(this);
442
+ } catch (error) {
443
+ logEndpointError(error);
444
+ return false;
445
+ }
446
+ }
447
+
448
+ private isCurrentConnectionState(
449
+ connection: TConnection,
450
+ connectionStateGeneration: number,
451
+ ): boolean {
452
+ return (
453
+ this.connection === connection &&
454
+ connection.ready &&
455
+ this.connectionStateGeneration === connectionStateGeneration
456
+ );
457
+ }
458
+
459
+ private clearCommandEffectState(): void {
460
+ this.acknowledgedCommands = [];
461
+ this.commandsWithUncertainEffects = [];
462
+ }
463
+ }
464
+
465
+ export type EndpointConnectionMetadata = {};
466
+
467
+ export type EndpointLogState = Readonly<
468
+ Record<string, string | number | boolean | undefined>
469
+ >;
470
+
471
+ export type EndpointReference = {
472
+ readonly name: string;
473
+ readonly ready: boolean;
474
+ };
475
+
476
+ export type CommandEffect = {
477
+ /**
478
+ * Revision of the observations relevant to the result of {@link matches}.
479
+ *
480
+ * It must increase only after every value relevant to {@link matches} has
481
+ * been observed since its previous revision, including observations equal to
482
+ * the previous values. It must remain unchanged for partial or unrelated
483
+ * updates. Every change must accompany a change to the owning connection's
484
+ * `stateRevision`; equality deliberately ignores this lifecycle value.
485
+ */
486
+ readonly observationRevision: number;
487
+ /**
488
+ * Whether this and `effect` describe the same desired state.
489
+ *
490
+ * Core only compares effects for stateful commands in the same effect slot,
491
+ * as determined by `StatefulCommand.supersedes`, and from the same bound
492
+ * endpoint connection lifecycle.
493
+ */
494
+ equals(effect: CommandEffect): boolean;
495
+ /**
496
+ * Whether the endpoint's latest complete state already reflects this effect.
497
+ *
498
+ * Core only calls this with the endpoint that owns the effect while its
499
+ * current connection is ready. Implementations need not repeat connection
500
+ * readiness or endpoint ownership checks.
501
+ */
502
+ matches(endpoint: EndpointReference): boolean;
503
+ };
504
+
505
+ export type CommandExecution = {
506
+ /** The desired state effect, available before execution for noop detection. */
507
+ readonly effect?: CommandEffect;
508
+ /** Starts the externally observable command execution. */
509
+ readonly execute: () => PromiseLike<void>;
510
+ /**
511
+ * Describes the prepared execution, reflecting any normalization the
512
+ * connection applied. Core prefers it over the command description when
513
+ * logging command actions.
514
+ */
515
+ readonly toLogString?: () => string;
516
+ };
517
+
518
+ export type EndpointConnection<in TCommand extends Command> = {
519
+ readonly ready: boolean;
520
+ /**
521
+ * Monotonically increases after every complete or incremental state update,
522
+ * including updates whose values equal the previously observed state.
523
+ */
524
+ readonly stateRevision: number;
525
+ /**
526
+ * Validates and prepares an inert execution plan. External execution must not
527
+ * begin until the returned `execute()` function is called.
528
+ */
529
+ readonly prepareCommand: (command: TCommand) => CommandExecution;
530
+ readonly toLogString?: () => string;
531
+ };
532
+
533
+ type AcknowledgedCommand = {
534
+ readonly command: Command;
535
+ readonly effect: CommandEffect;
536
+ observationRevision: number;
537
+ };
538
+
539
+ type UncertainCommandEffect = {
540
+ readonly command: Command;
541
+ readonly effect: CommandEffect | undefined;
542
+ readonly observationRevision?: number;
543
+ };
544
+
545
+ export type EndpointConnectionBinding = {
546
+ bind(): void;
547
+ dispose(): PromiseLike<void>;
548
+ };
549
+
550
+ export function createEndpointConnectionBinding<
551
+ TCommand extends Command,
552
+ TConnection extends EndpointConnection<TCommand>,
553
+ >(
554
+ endpoint: Endpoint<TCommand, TConnection>,
555
+ connection: TConnection,
556
+ disposeConnection: () => void | PromiseLike<void> = () => undefined,
557
+ ): EndpointConnectionBinding {
558
+ let bound = false;
559
+ let disposePromise: Promise<void> | undefined;
560
+
561
+ return {
562
+ bind() {
563
+ if (disposePromise !== undefined) {
564
+ throw new Error('Cannot bind a disposed endpoint connection.');
565
+ }
566
+
567
+ bound = true;
568
+ endpoint.bindConnection(connection);
569
+ },
570
+ dispose() {
571
+ disposePromise ??= (async () => {
572
+ const errors: unknown[] = [];
573
+
574
+ if (bound) {
575
+ bound = false;
576
+
577
+ try {
578
+ endpoint.unbindConnection(connection);
579
+ } catch (error) {
580
+ errors.push(error);
581
+ }
582
+ }
583
+
584
+ try {
585
+ await disposeConnection();
586
+ } catch (error) {
587
+ errors.push(error);
588
+ }
589
+
590
+ if (errors.length === 1) {
591
+ throw errors[0];
592
+ } else if (errors.length > 1) {
593
+ throw new AggregateError(
594
+ errors,
595
+ 'Failed to dispose endpoint connection binding.',
596
+ );
597
+ }
598
+ })();
599
+
600
+ return disposePromise;
601
+ },
602
+ };
603
+ }
604
+
605
+ export class EndpointConnectionError extends Error {
606
+ constructor(message: string) {
607
+ super(message);
608
+ this.name = new.target.name;
609
+ }
610
+ }
@@ -0,0 +1,11 @@
1
+ import {registerRootScope} from './registry.js';
2
+ import {Scope} from './scope.js';
3
+ import {$constructor, uniqueName} from './utils/index.js';
4
+
5
+ export class Home extends Scope {}
6
+
7
+ export const $home = $constructor(Home)
8
+ .build(uniqueName('home'))
9
+ .build(home => {
10
+ registerRootScope(home);
11
+ });
@@ -0,0 +1,16 @@
1
+ export * from './atomics/index.js';
2
+ export * from './command.js';
3
+ export * from './device.js';
4
+ export * from './device/index.js';
5
+ export * from './device-namespace.js';
6
+ export * from './devices/index.js';
7
+ export * from './endpoint.js';
8
+ export * from './home.js';
9
+ export * from './log.js';
10
+ export * from './provider.js';
11
+ export * from './registry.js';
12
+ export * from './runtime/index.js';
13
+ export * from './scope.js';
14
+ export * from './types.js';
15
+ export * from './utils/index.js';
16
+ export * from './x-data-medium.js';