@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,193 @@
1
+ import * as x from 'x-value';
2
+
3
+ import {assertDeclaring} from './@lifecycle.js';
4
+ import {DeviceEntry} from './device.js';
5
+ import {
6
+ getDeviceConstructor,
7
+ getProviderNamespaceDeviceConstructor,
8
+ hasProviderNamespace,
9
+ } from './registry.js';
10
+ import type {NamedObject, types} from './types.js';
11
+
12
+ export const ScopeName = x.string.nominal<'scope name'>();
13
+
14
+ export type ScopeName = x.TypeOf<typeof ScopeName>;
15
+
16
+ export const ScopePath = x.array(ScopeName);
17
+
18
+ export type ScopePath = x.TypeOf<typeof ScopePath>;
19
+
20
+ export class Scope implements NamedObject<ScopeName> {
21
+ declare [types]: {name: ScopeName};
22
+
23
+ private readonly scopeMap = new Map<string, ScopeWithDeviceConstructors>();
24
+
25
+ private readonly deviceMap = new Map<string, DeviceEntry>();
26
+
27
+ private readonly namespaceMap = new Map<string, object>();
28
+
29
+ readonly name: ScopeName;
30
+
31
+ constructor(
32
+ name: string,
33
+ readonly parent?: Scope,
34
+ ) {
35
+ this.name = name as ScopeName;
36
+ }
37
+
38
+ get path(): ScopePath {
39
+ const {name, parent} = this;
40
+
41
+ if (parent) {
42
+ return [...parent.path, name];
43
+ } else {
44
+ return [name];
45
+ }
46
+ }
47
+
48
+ get scopes(): IterableIterator<Scope> {
49
+ return this.scopeMap.values();
50
+ }
51
+
52
+ get devices(): IterableIterator<DeviceEntry> {
53
+ return this.deviceMap.values();
54
+ }
55
+
56
+ createDeviceEntry(name: string): DeviceEntry {
57
+ assertDeclaring();
58
+
59
+ const existingDeviceEntry = this.deviceMap.get(name);
60
+
61
+ if (existingDeviceEntry !== undefined) {
62
+ throw new TypeError(`Duplicate device: ${name}.`);
63
+ }
64
+
65
+ const deviceEntry = new DeviceEntry(name);
66
+ this.deviceMap.set(name, deviceEntry);
67
+
68
+ return deviceEntry;
69
+ }
70
+
71
+ getOrCreateNamespace(name: string, create: () => object): object {
72
+ let namespace = this.namespaceMap.get(name);
73
+
74
+ if (namespace === undefined) {
75
+ namespace = create();
76
+ this.namespaceMap.set(name, namespace);
77
+ }
78
+
79
+ return namespace;
80
+ }
81
+
82
+ $scope(name: string): ScopeWithDeviceConstructors {
83
+ assertDeclaring();
84
+
85
+ const existingScope = this.scopeMap.get(name);
86
+
87
+ if (existingScope !== undefined) {
88
+ throw new TypeError(`Duplicate scope: ${name}.`);
89
+ }
90
+
91
+ const scope = createScopeWithDeviceConstructors(new Scope(name, this));
92
+ this.scopeMap.set(name, scope);
93
+
94
+ return scope;
95
+ }
96
+ }
97
+
98
+ declare global {
99
+ namespace Home {
100
+ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
101
+ interface DeviceConstructors {}
102
+
103
+ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
104
+ interface ProviderNamespaces {}
105
+ }
106
+ }
107
+
108
+ type ScopeWithDeviceConstructors = Scope &
109
+ DeviceConstructors<Home.DeviceConstructors> & {
110
+ [TNamespace in keyof Home.ProviderNamespaces]: DeviceConstructors<
111
+ Home.ProviderNamespaces[TNamespace]
112
+ >;
113
+ };
114
+
115
+ type DeviceConstructors<TDeviceConstructors> = {
116
+ [TKey in Extract<keyof TDeviceConstructors, string> as `$${TKey}`]: (
117
+ name: string,
118
+ ) => TDeviceConstructors[TKey];
119
+ } & {
120
+ [TKey in Extract<keyof TDeviceConstructors, string> as `$$${TKey}`]: (
121
+ name: string,
122
+ ) => TDeviceConstructors[TKey][];
123
+ };
124
+
125
+ function createScopeWithDeviceConstructors(
126
+ scope: Scope,
127
+ ): ScopeWithDeviceConstructors;
128
+ function createScopeWithDeviceConstructors(scope: Scope): Scope {
129
+ return new Proxy(scope, {
130
+ get(target, property, receiver) {
131
+ if (typeof property === 'string' && !Reflect.has(target, property)) {
132
+ if (property.startsWith('$$')) {
133
+ // TODO: Resolve direct multi-device constructors.
134
+ } else if (property.startsWith('$')) {
135
+ const deviceType = property.slice(1);
136
+
137
+ return (name: string) => {
138
+ const Constructor = getDeviceConstructor(deviceType);
139
+
140
+ if (Constructor === undefined) {
141
+ throw new TypeError(`Unknown device constructor: ${deviceType}.`);
142
+ }
143
+
144
+ return scope.createDeviceEntry(name).createInstance(Constructor);
145
+ };
146
+ } else if (hasProviderNamespace(property)) {
147
+ return scope.getOrCreateNamespace(property, () =>
148
+ createProviderNamespaceWithDeviceConstructors(scope, property),
149
+ );
150
+ }
151
+ }
152
+
153
+ return Reflect.get(target, property, receiver);
154
+ },
155
+ });
156
+ }
157
+
158
+ function createProviderNamespaceWithDeviceConstructors(
159
+ scope: Scope,
160
+ providerNamespace: string,
161
+ ): object {
162
+ return new Proxy(
163
+ {},
164
+ {
165
+ get(_target, property) {
166
+ if (typeof property === 'string') {
167
+ if (property.startsWith('$$')) {
168
+ // TODO: Resolve multi-device constructors.
169
+ } else if (property.startsWith('$')) {
170
+ const deviceType = property.slice(1);
171
+
172
+ return (name: string) => {
173
+ const Constructor = getProviderNamespaceDeviceConstructor(
174
+ providerNamespace,
175
+ deviceType,
176
+ );
177
+
178
+ if (Constructor === undefined) {
179
+ throw new TypeError(
180
+ `Unknown device constructor: ${providerNamespace}.${deviceType}.`,
181
+ );
182
+ }
183
+
184
+ return scope.createDeviceEntry(name).createInstance(Constructor);
185
+ };
186
+ }
187
+ }
188
+
189
+ return undefined;
190
+ },
191
+ },
192
+ );
193
+ }
@@ -0,0 +1,8 @@
1
+ declare const console: {
2
+ error(error: unknown): void;
3
+ info(message: string): void;
4
+ };
5
+
6
+ declare function setTimeout(callback: () => void, delay: number): unknown;
7
+
8
+ declare function clearTimeout(timeout: unknown): void;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Ideally a symbol but:
3
+ * https://github.com/microsoft/TypeScript/issues/52644
4
+ *
5
+ * So always use this constant instead of string literal '__types__'.
6
+ */
7
+ export const types = '__types__';
8
+
9
+ export type types = typeof types;
10
+
11
+ export type NamedObject<TName extends string> = {
12
+ [types]: {
13
+ name: TName;
14
+ };
15
+ };
16
+
17
+ export type UnknownNamedObject = NamedObject<string>;
18
+
19
+ export type NamedTupleToRecord<TTuple extends readonly UnknownNamedObject[]> = {
20
+ // `TIndex in ... as ...` doesn't work here, adding `as` breaks tuple mapping
21
+ // behavior and results in something like {a: 'a' | 'b'} | {b: 'a' | 'b'}
22
+ [TName in TTuple[number][types]['name']]: Extract<
23
+ TTuple[number],
24
+ NamedObject<TName>
25
+ >;
26
+ };
@@ -0,0 +1,65 @@
1
+ export class ExponentialBackoff implements PromiseLike<void> {
2
+ private delay: number;
3
+
4
+ private waiting:
5
+ | {
6
+ readonly timeout: unknown;
7
+ readonly resolve: () => void;
8
+ }
9
+ | undefined;
10
+
11
+ constructor(
12
+ readonly initialDelay: number,
13
+ readonly maximumDelay: number,
14
+ ) {
15
+ if (
16
+ !Number.isFinite(initialDelay) ||
17
+ initialDelay <= 0 ||
18
+ !Number.isFinite(maximumDelay) ||
19
+ maximumDelay < initialDelay
20
+ ) {
21
+ throw new RangeError('Invalid exponential backoff delays.');
22
+ }
23
+
24
+ this.delay = initialDelay;
25
+ }
26
+
27
+ then<TResult1 = void, TResult2 = never>(
28
+ onfulfilled?: ((value: void) => TResult1 | PromiseLike<TResult1>) | null,
29
+ onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,
30
+ ): PromiseLike<TResult1 | TResult2> {
31
+ return this.wait().then(onfulfilled, onrejected);
32
+ }
33
+
34
+ reset(): void {
35
+ this.delay = this.initialDelay;
36
+
37
+ const waiting = this.waiting;
38
+
39
+ if (waiting === undefined) {
40
+ return;
41
+ }
42
+
43
+ this.waiting = undefined;
44
+ clearTimeout(waiting.timeout);
45
+ waiting.resolve();
46
+ }
47
+
48
+ private async wait(): Promise<void> {
49
+ if (this.waiting !== undefined) {
50
+ throw new Error('Exponential backoff is already waiting.');
51
+ }
52
+
53
+ const delay = this.delay;
54
+ this.delay = Math.min(delay * 2, this.maximumDelay);
55
+
56
+ await new Promise<void>(resolve => {
57
+ const timeout = setTimeout(() => {
58
+ this.waiting = undefined;
59
+ resolve();
60
+ }, delay);
61
+
62
+ this.waiting = {timeout, resolve};
63
+ });
64
+ }
65
+ }
@@ -0,0 +1,89 @@
1
+ import type {NamedObject, UnknownNamedObject} from '../types.js';
2
+
3
+ export function uniqueName(type: string): <T extends Named>(object: T) => void {
4
+ const nameSet = new Set<string>();
5
+
6
+ return object => {
7
+ const {name} = object;
8
+
9
+ if (nameSet.has(name)) {
10
+ throw new TypeError(`Duplicate ${type}: ${name}.`);
11
+ }
12
+
13
+ nameSet.add(name);
14
+ };
15
+ }
16
+
17
+ export function $constructor<
18
+ TConstructor extends abstract new (...args: never[]) => UnknownNamedObject,
19
+ >(
20
+ Constructor: TConstructor,
21
+ ): ConstructorParameters<TConstructor> extends [
22
+ name: string,
23
+ ...args: infer TRestArgs,
24
+ ]
25
+ ? ExtendedConstructorWithNameParameter<TRestArgs, InstanceType<TConstructor>>
26
+ : never;
27
+ export function $constructor<
28
+ TConstructor extends abstract new (...args: never[]) => object,
29
+ >(
30
+ Constructor: TConstructor,
31
+ ): ExtendedConstructor<
32
+ ConstructorParameters<TConstructor>,
33
+ InstanceType<TConstructor>
34
+ >;
35
+ export function $constructor(
36
+ Constructor: new (...args: unknown[]) => object,
37
+ ): (...args: unknown[]) => object {
38
+ return build([]);
39
+
40
+ function build(
41
+ builders: ((instance: object) => object | void)[],
42
+ ): (...args: unknown[]) => object {
43
+ const built = (...args: unknown[]): object => {
44
+ let instance = new Constructor(...args);
45
+
46
+ for (const builder of builders) {
47
+ instance = builder(instance) ?? instance;
48
+ }
49
+
50
+ return instance;
51
+ };
52
+
53
+ Reflect.setPrototypeOf(built, {
54
+ build(builder: (instance: object) => object | void) {
55
+ return build([...builders, builder]);
56
+ },
57
+ });
58
+
59
+ return built;
60
+ }
61
+ }
62
+
63
+ export type ExtendedConstructorWithNameParameter<
64
+ TRestArgs extends unknown[],
65
+ T extends object,
66
+ > = {
67
+ <TName extends string>(
68
+ name: TName,
69
+ ...args: TRestArgs
70
+ ): T & NamedObject<TName>;
71
+ build<TRefined extends object>(
72
+ builder: (instance: T) => TRefined,
73
+ ): ExtendedConstructorWithNameParameter<TRestArgs, TRefined>;
74
+ build(
75
+ builder: (instance: T) => void,
76
+ ): ExtendedConstructorWithNameParameter<TRestArgs, T>;
77
+ };
78
+
79
+ export type ExtendedConstructor<TArgs extends unknown[], T extends object> = {
80
+ (...args: TArgs): T;
81
+ build<TRefined extends object>(
82
+ builder: (instance: T) => TRefined,
83
+ ): ExtendedConstructor<TArgs, TRefined>;
84
+ build(builder: (instance: T) => void): ExtendedConstructor<TArgs, T>;
85
+ };
86
+
87
+ type Named = {
88
+ readonly name: string;
89
+ };
@@ -0,0 +1,2 @@
1
+ export * from './backoff.js';
2
+ export * from './constructor.js';
@@ -0,0 +1,16 @@
1
+ import * as x from 'x-value';
2
+
3
+ declare global {
4
+ namespace XValue {
5
+ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
6
+ interface Using extends UsingXData {}
7
+ }
8
+
9
+ type UsingXData = {
10
+ 'x-data': XDataTypes;
11
+ };
12
+
13
+ type XDataTypes = x.JSONTypes;
14
+ }
15
+
16
+ export const xData = x.extendedJSONValue.extend<UsingXData>();