@thingmate/abilities 0.1.0 → 0.1.1
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/README.md +189 -1
- package/package.json +1 -1
- package/src/combined/command/command-abilities.d.ts +43 -0
- package/src/combined/command/command-abilities.js +43 -0
- package/src/combined/command/command-abilities.js.map +1 -1
- package/src/combined/command/having-command-abilities.d.ts +6 -0
- package/src/combined/property/having-property-abilities.d.ts +6 -0
- package/src/combined/property/property-abilities.d.ts +50 -0
- package/src/combined/property/property-abilities.js +50 -0
- package/src/combined/property/property-abilities.js.map +1 -1
- package/src/combined/property/readonly/having-readonly-property-abilities.d.ts +6 -0
- package/src/combined/property/readonly/readonly-property-abilities.d.ts +25 -0
- package/src/combined/property/readonly/readonly-property-abilities.js +25 -0
- package/src/combined/property/readonly/readonly-property-abilities.js.map +1 -1
- package/src/individual/observe/having-observe-ability.d.ts +8 -0
- package/src/individual/observe/observe-ability.d.ts +21 -0
- package/src/individual/observe/observe-ability.js +21 -0
- package/src/individual/observe/observe-ability.js.map +1 -1
- package/src/individual/read/having-read-ability.d.ts +6 -0
- package/src/individual/read/read-action.d.ts +7 -0
- package/src/individual/send/having-send-ability.d.ts +6 -0
- package/src/individual/send/send-action.d.ts +8 -0
- package/src/individual/write/having-write-ability.d.ts +6 -0
- package/src/individual/write/write-action.d.ts +7 -0
package/README.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|

|
|
3
3
|

|
|
4
4
|

|
|
5
|
+

|
|
5
6
|
|
|
6
7
|
<picture>
|
|
7
8
|
<source height="64" media="(prefers-color-scheme: dark)" srcset="https://github.com/thingmate/website/blob/main/assets/logo/png/logo-large-dark.png?raw=true">
|
|
@@ -23,4 +24,191 @@ npm install @thingmate/abilities --save
|
|
|
23
24
|
|
|
24
25
|
## 📜 Documentation
|
|
25
26
|
|
|
26
|
-
|
|
27
|
+
This library defines generic **abilities**: minimal and composable interfaces describing what a
|
|
28
|
+
_thing_ can do.
|
|
29
|
+
|
|
30
|
+
They are built on top of
|
|
31
|
+
[`@xstd/async-function`](https://www.npmjs.com/package/@xstd/async-function) and
|
|
32
|
+
[`@xstd/observable-async-function`](https://www.npmjs.com/package/@xstd/observable-async-function):
|
|
33
|
+
|
|
34
|
+
- [`SendAction`](#sendaction) - send (trigger) something with arguments, without expecting a result
|
|
35
|
+
- [`ReadAction`](#readaction) - read and return a value
|
|
36
|
+
- [`WriteAction`](#writeaction) - write a value
|
|
37
|
+
- [`ObserveAbility`](#observeability) - observe values over time
|
|
38
|
+
- [`CommandAbilities`](#commandabilities) - send a command and observe it
|
|
39
|
+
- [`ReadonlyPropertyAbilities`](#readonlypropertyabilities) - read a value and observe its changes
|
|
40
|
+
- [`PropertyAbilities`](#propertyabilities) - read, write and observe a value
|
|
41
|
+
|
|
42
|
+
Each ability also exists as a `Having*` structural interface (see [Abilities](#abilities)): any
|
|
43
|
+
object exposing the matching properties fulfills it, no base class is required.
|
|
44
|
+
|
|
45
|
+
### Actions
|
|
46
|
+
|
|
47
|
+
Actions are abortable async functions: every call takes a leading `AbortSignal`, followed by the
|
|
48
|
+
action's arguments, and returns a promise.
|
|
49
|
+
|
|
50
|
+
| Action | Equivalent to | Description |
|
|
51
|
+
| ------------------------ | --------------------------------- | ------------------------------------------------------- |
|
|
52
|
+
| `SendAction<GArguments>` | `AsyncFunction<GArguments, void>` | Sends (triggers) something with the provided arguments. |
|
|
53
|
+
| `ReadAction<GValue>` | `AsyncFunction<[], GValue>` | Reads and returns the current value. |
|
|
54
|
+
| `WriteAction<GValue>` | `AsyncFunction<[GValue], void>` | Writes the provided value. |
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { AsyncFunction } from '@xstd/async-function';
|
|
58
|
+
import type { ReadAction, WriteAction } from '@thingmate/abilities';
|
|
59
|
+
|
|
60
|
+
const readTemperature: ReadAction<number> = new AsyncFunction(async () => sensor.temperature);
|
|
61
|
+
|
|
62
|
+
const setTemperature: WriteAction<number> = new AsyncFunction(async (signal, value: number) => {
|
|
63
|
+
await sensor.setTemperature(value, { signal });
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const current: number = await readTemperature.call(signal);
|
|
67
|
+
await setTemperature.call(signal, 21.5);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Abilities
|
|
71
|
+
|
|
72
|
+
The `Having*` interfaces declare the abilities exposed by an object.
|
|
73
|
+
|
|
74
|
+
| Interface | Exposed property |
|
|
75
|
+
| ------------------------------------------ | ------------------------------------------------------ |
|
|
76
|
+
| `HavingSendAbility<GArguments>` | `send: SendAction<GArguments>` |
|
|
77
|
+
| `HavingReadAbility<GValue>` | `read: ReadAction<GValue>` |
|
|
78
|
+
| `HavingWriteAbility<GValue>` | `write: WriteAction<GValue>` |
|
|
79
|
+
| `HavingObserveAbility<GValue, GArguments>` | `observe: ObservableAsyncFunction<GValue, GArguments>` |
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import type { HavingReadAbility, HavingWriteAbility } from '@thingmate/abilities';
|
|
83
|
+
|
|
84
|
+
interface Thermostat extends HavingReadAbility<number>, HavingWriteAbility<number> {}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### ObserveAbility
|
|
88
|
+
|
|
89
|
+
The ability to observe values over time, wrapping an `ObservableAsyncFunction`: calling `observe`
|
|
90
|
+
establishes a live resource emitting the observed values.
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
import { ObserveAbility } from '@thingmate/abilities';
|
|
94
|
+
|
|
95
|
+
const temperature: ObserveAbility<number> = new ObserveAbility(temperatureChanges);
|
|
96
|
+
|
|
97
|
+
for await (const value of temperature.observe.values(signal)) {
|
|
98
|
+
console.log(value); // every emitted temperature
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### CommandAbilities
|
|
103
|
+
|
|
104
|
+
The abilities of a command: sending it (`send`) and observing it (`observe`).
|
|
105
|
+
|
|
106
|
+
The observed values are typed with the command's own arguments tuple: a command accepting
|
|
107
|
+
`[celsius: number]` observes values of type `[celsius: number]`.
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import { AsyncFunction } from '@xstd/async-function';
|
|
111
|
+
import { CommandAbilities } from '@thingmate/abilities';
|
|
112
|
+
|
|
113
|
+
const setCelsius: CommandAbilities<[celsius: number]> = new CommandAbilities({
|
|
114
|
+
send: new AsyncFunction(async (signal, celsius: number) => {
|
|
115
|
+
await thermostat.setTargetTemperature(celsius, { signal });
|
|
116
|
+
}),
|
|
117
|
+
observe: targetTemperatureChanges, // an `ObservableAsyncFunction<[celsius: number]>`
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
await setCelsius.send.call(signal, 21.5);
|
|
121
|
+
|
|
122
|
+
for await (const [celsius] of setCelsius.observe.values(signal)) {
|
|
123
|
+
console.log(celsius);
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
`map` retypes the command: the returned `send` accepts the new arguments and maps them back to the
|
|
128
|
+
original ones with `outIn`, while the observed values are mapped with `inOut`. Both are mapped
|
|
129
|
+
tuple-wise.
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
const setFahrenheit: CommandAbilities<[fahrenheit: number]> = setCelsius.map({
|
|
133
|
+
inOut: ([celsius]) => [celsius * 1.8 + 32],
|
|
134
|
+
outIn: ([fahrenheit]) => [(fahrenheit - 32) / 1.8],
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
await setFahrenheit.send.call(signal, 70); // sends the command with ~21.1°C
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Its structural counterpart is `HavingCommandAbilities<GArguments>`.
|
|
141
|
+
|
|
142
|
+
### ReadonlyPropertyAbilities
|
|
143
|
+
|
|
144
|
+
The abilities of a readonly property: reading its current value (`read`) and observing its changes
|
|
145
|
+
over time (`observe`).
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
import { AsyncFunction } from '@xstd/async-function';
|
|
149
|
+
import { ReadonlyPropertyAbilities } from '@thingmate/abilities';
|
|
150
|
+
|
|
151
|
+
const temperature: ReadonlyPropertyAbilities<number> = new ReadonlyPropertyAbilities({
|
|
152
|
+
read: new AsyncFunction(async () => sensor.temperature),
|
|
153
|
+
observe: temperatureChanges, // an `ObservableAsyncFunction<number>`
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const current: number = await temperature.read.call(signal);
|
|
157
|
+
|
|
158
|
+
for await (const value of temperature.observe.values(signal)) {
|
|
159
|
+
console.log(value); // every temperature change
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
`map` retypes the property with a single mapping function applied to the read and observed values.
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
const fahrenheit: ReadonlyPropertyAbilities<number> = temperature.map(
|
|
167
|
+
(celsius: number): number => celsius * 1.8 + 32,
|
|
168
|
+
);
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Its structural counterpart is `HavingReadonlyPropertyAbilities<GValue>`.
|
|
172
|
+
|
|
173
|
+
### PropertyAbilities
|
|
174
|
+
|
|
175
|
+
The abilities of a property: reading its current value (`read`), writing it (`write`), and
|
|
176
|
+
observing its changes over time (`observe`).
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
import { AsyncFunction } from '@xstd/async-function';
|
|
180
|
+
import { PropertyAbilities } from '@thingmate/abilities';
|
|
181
|
+
|
|
182
|
+
const temperature: PropertyAbilities<number> = new PropertyAbilities({
|
|
183
|
+
read: new AsyncFunction(async () => sensor.temperature),
|
|
184
|
+
write: new AsyncFunction(async (signal, value: number) => {
|
|
185
|
+
await sensor.setTemperature(value, { signal });
|
|
186
|
+
}),
|
|
187
|
+
observe: temperatureChanges, // an `ObservableAsyncFunction<number>`
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
const current: number = await temperature.read.call(signal);
|
|
191
|
+
await temperature.write.call(signal, 21.5);
|
|
192
|
+
|
|
193
|
+
for await (const value of temperature.observe.values(signal)) {
|
|
194
|
+
console.log(value); // every temperature change
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
`map` retypes the property with a bi-directional mapping: values read from, or observed through, the
|
|
199
|
+
returned property are mapped with `inOut`, while values written to it are mapped back with `outIn`.
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
const fahrenheit: PropertyAbilities<number> = temperature.map({
|
|
203
|
+
inOut: (celsius) => celsius * 1.8 + 32,
|
|
204
|
+
outIn: (fahrenheit) => (fahrenheit - 32) / 1.8,
|
|
205
|
+
});
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
`asReadonly` returns a readonly view of the property, without the `write` ability.
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
const readonlyTemperature: ReadonlyPropertyAbilities<number> = temperature.asReadonly();
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Its structural counterpart is `HavingPropertyAbilities<GValue>`.
|
package/package.json
CHANGED
|
@@ -2,10 +2,53 @@ import type { MapInOut } from '@xstd/map-function';
|
|
|
2
2
|
import type { ObservableAsyncFunction } from '@xstd/observable-async-function';
|
|
3
3
|
import type { SendAction } from '../../individual/send/send-action.ts';
|
|
4
4
|
import type { HavingCommandAbilities } from './having-command-abilities.ts';
|
|
5
|
+
/**
|
|
6
|
+
* The abilities of a command: sending it ({@link SendAction}) and observing it
|
|
7
|
+
* ({@link ObservableAsyncFunction}).
|
|
8
|
+
*
|
|
9
|
+
* The observed values are typed with the command's own arguments tuple: a command accepting
|
|
10
|
+
* `[volume: number]` observes values of type `[volume: number]`.
|
|
11
|
+
*
|
|
12
|
+
* @template GArguments - The tuple type of the arguments accepted by the command.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const setVolume: CommandAbilities<[volume: number]> = new CommandAbilities({
|
|
17
|
+
* send: new AsyncFunction(async (signal, volume: number): Promise<void> => {
|
|
18
|
+
* await device.setVolume(volume, { signal });
|
|
19
|
+
* }),
|
|
20
|
+
* observe: volumeChanges, // an `ObservableAsyncFunction<[volume: number]>`
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* await setVolume.send.call(signal, 50);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
5
26
|
export declare class CommandAbilities<GArguments extends readonly unknown[]> implements HavingCommandAbilities<GArguments> {
|
|
6
27
|
#private;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a new {@link CommandAbilities}.
|
|
30
|
+
*
|
|
31
|
+
* @param abilities - The abilities to wrap.
|
|
32
|
+
* @param abilities.send - The action invoked when the command is sent.
|
|
33
|
+
* @param abilities.observe - The observable async function emitting the command's events.
|
|
34
|
+
*/
|
|
7
35
|
constructor({ send, observe }: HavingCommandAbilities<GArguments>);
|
|
36
|
+
/** The action invoked when the command is sent. */
|
|
8
37
|
get send(): SendAction<GArguments>;
|
|
38
|
+
/** The observable async function emitting the command's events. */
|
|
9
39
|
get observe(): ObservableAsyncFunction<GArguments>;
|
|
40
|
+
/**
|
|
41
|
+
* Maps these command abilities to work with different arguments.
|
|
42
|
+
*
|
|
43
|
+
* The send action of the returned command abilities accepts `GNewArguments` and maps them back to
|
|
44
|
+
* this command's arguments with `outIn`, while the values emitted by its observation are mapped
|
|
45
|
+
* to the new type with `inOut`.
|
|
46
|
+
*
|
|
47
|
+
* @template GNewArguments - The tuple type of the arguments of the returned command abilities.
|
|
48
|
+
* @param mapping - The bi-directional mapping between this command's arguments and the new ones.
|
|
49
|
+
* @param mapping.inOut - Maps this command's arguments into the new ones.
|
|
50
|
+
* @param mapping.outIn - Maps the new arguments into this command's arguments.
|
|
51
|
+
* @returns New command abilities working with `GNewArguments`.
|
|
52
|
+
*/
|
|
10
53
|
map<GNewArguments extends readonly unknown[]>({ inOut, outIn, }: MapInOut<GArguments, GNewArguments>): CommandAbilities<GNewArguments>;
|
|
11
54
|
}
|
|
@@ -1,16 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The abilities of a command: sending it ({@link SendAction}) and observing it
|
|
3
|
+
* ({@link ObservableAsyncFunction}).
|
|
4
|
+
*
|
|
5
|
+
* The observed values are typed with the command's own arguments tuple: a command accepting
|
|
6
|
+
* `[volume: number]` observes values of type `[volume: number]`.
|
|
7
|
+
*
|
|
8
|
+
* @template GArguments - The tuple type of the arguments accepted by the command.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const setVolume: CommandAbilities<[volume: number]> = new CommandAbilities({
|
|
13
|
+
* send: new AsyncFunction(async (signal, volume: number): Promise<void> => {
|
|
14
|
+
* await device.setVolume(volume, { signal });
|
|
15
|
+
* }),
|
|
16
|
+
* observe: volumeChanges, // an `ObservableAsyncFunction<[volume: number]>`
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* await setVolume.send.call(signal, 50);
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
1
22
|
export class CommandAbilities {
|
|
2
23
|
#send;
|
|
3
24
|
#observe;
|
|
25
|
+
/**
|
|
26
|
+
* Creates a new {@link CommandAbilities}.
|
|
27
|
+
*
|
|
28
|
+
* @param abilities - The abilities to wrap.
|
|
29
|
+
* @param abilities.send - The action invoked when the command is sent.
|
|
30
|
+
* @param abilities.observe - The observable async function emitting the command's events.
|
|
31
|
+
*/
|
|
4
32
|
constructor({ send, observe }) {
|
|
5
33
|
this.#send = send;
|
|
6
34
|
this.#observe = observe;
|
|
7
35
|
}
|
|
36
|
+
/** The action invoked when the command is sent. */
|
|
8
37
|
get send() {
|
|
9
38
|
return this.#send;
|
|
10
39
|
}
|
|
40
|
+
/** The observable async function emitting the command's events. */
|
|
11
41
|
get observe() {
|
|
12
42
|
return this.#observe;
|
|
13
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Maps these command abilities to work with different arguments.
|
|
46
|
+
*
|
|
47
|
+
* The send action of the returned command abilities accepts `GNewArguments` and maps them back to
|
|
48
|
+
* this command's arguments with `outIn`, while the values emitted by its observation are mapped
|
|
49
|
+
* to the new type with `inOut`.
|
|
50
|
+
*
|
|
51
|
+
* @template GNewArguments - The tuple type of the arguments of the returned command abilities.
|
|
52
|
+
* @param mapping - The bi-directional mapping between this command's arguments and the new ones.
|
|
53
|
+
* @param mapping.inOut - Maps this command's arguments into the new ones.
|
|
54
|
+
* @param mapping.outIn - Maps the new arguments into this command's arguments.
|
|
55
|
+
* @returns New command abilities working with `GNewArguments`.
|
|
56
|
+
*/
|
|
14
57
|
map({ inOut, outIn, }) {
|
|
15
58
|
return new CommandAbilities({
|
|
16
59
|
send: this.#send.mapArguments((...args) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"command-abilities.js","sourceRoot":"","sources":["../../../../src/combined/command/command-abilities.ts"],"names":[],"mappings":"AAKA,MAAM,OAAO,gBAAgB;IAGlB,KAAK,CAAyB;IAC9B,QAAQ,CAAsC;IAEvD,YAAY,EAAE,IAAI,EAAE,OAAO,EAAsC;QAC/D,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED,GAAG,CAA2C,EAC5C,KAAK,EACL,KAAK,GAC+B;QACpC,OAAO,IAAI,gBAAgB,CAAgB;YACzC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,GAAG,IAAmB,EAAc,EAAE;gBACnE,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC;YACrB,CAAC,CAAC;YACF,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC;SAClC,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
1
|
+
{"version":3,"file":"command-abilities.js","sourceRoot":"","sources":["../../../../src/combined/command/command-abilities.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,OAAO,gBAAgB;IAGlB,KAAK,CAAyB;IAC9B,QAAQ,CAAsC;IAEvD;;;;;;OAMG;IACH,YAAY,EAAE,IAAI,EAAE,OAAO,EAAsC;QAC/D,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED,mDAAmD;IACnD,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,mEAAmE;IACnE,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,GAAG,CAA2C,EAC5C,KAAK,EACL,KAAK,GAC+B;QACpC,OAAO,IAAI,gBAAgB,CAAgB;YACzC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,GAAG,IAAmB,EAAc,EAAE;gBACnE,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC;YACrB,CAAC,CAAC;YACF,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC;SAClC,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import type { HavingObserveAbility } from '../../individual/observe/having-observe-ability.ts';
|
|
2
2
|
import type { HavingSendAbility } from '../../individual/send/having-send-ability.ts';
|
|
3
|
+
/**
|
|
4
|
+
* An object having the abilities of a command: sending it ({@link HavingSendAbility}) and observing
|
|
5
|
+
* it ({@link HavingObserveAbility}).
|
|
6
|
+
*
|
|
7
|
+
* @template GArguments - The tuple type of the arguments accepted by the command.
|
|
8
|
+
*/
|
|
3
9
|
export interface HavingCommandAbilities<GArguments extends readonly unknown[]> extends HavingSendAbility<GArguments>, HavingObserveAbility<GArguments> {
|
|
4
10
|
}
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import type { HavingWriteAbility } from '../../individual/write/having-write-ability.ts';
|
|
2
2
|
import type { HavingReadonlyPropertyAbilities } from './readonly/having-readonly-property-abilities.ts';
|
|
3
|
+
/**
|
|
4
|
+
* An object having the abilities of a property: reading and observing it
|
|
5
|
+
* ({@link HavingReadonlyPropertyAbilities}) and writing it ({@link HavingWriteAbility}).
|
|
6
|
+
*
|
|
7
|
+
* @template GValue - The type of the property's value.
|
|
8
|
+
*/
|
|
3
9
|
export interface HavingPropertyAbilities<GValue> extends HavingReadonlyPropertyAbilities<GValue>, HavingWriteAbility<GValue> {
|
|
4
10
|
}
|
|
@@ -4,12 +4,62 @@ import type { ReadAction } from '../../individual/read/read-action.ts';
|
|
|
4
4
|
import type { WriteAction } from '../../individual/write/write-action.ts';
|
|
5
5
|
import type { HavingPropertyAbilities } from './having-property-abilities.ts';
|
|
6
6
|
import { ReadonlyPropertyAbilities } from './readonly/readonly-property-abilities.ts';
|
|
7
|
+
/**
|
|
8
|
+
* The abilities of a property: reading its current value ({@link ReadAction}), writing it
|
|
9
|
+
* ({@link WriteAction}), and observing its changes over time ({@link ObservableAsyncFunction}).
|
|
10
|
+
*
|
|
11
|
+
* @template GValue - The type of the property's value.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const temperature: PropertyAbilities<number> = new PropertyAbilities({
|
|
16
|
+
* read: new AsyncFunction(async () => sensor.temperature),
|
|
17
|
+
* write: new AsyncFunction(async (signal, value: number): Promise<void> => {
|
|
18
|
+
* await sensor.setTemperature(value, { signal });
|
|
19
|
+
* }),
|
|
20
|
+
* observe: temperatureChanges, // an `ObservableAsyncFunction<number>`
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* const current: number = await temperature.read.call(signal);
|
|
24
|
+
* await temperature.write.call(signal, 21.5);
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
7
27
|
export declare class PropertyAbilities<GValue> implements HavingPropertyAbilities<GValue> {
|
|
8
28
|
#private;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a new {@link PropertyAbilities}.
|
|
31
|
+
*
|
|
32
|
+
* @param abilities - The abilities to wrap.
|
|
33
|
+
* @param abilities.read - The action reading the property's current value.
|
|
34
|
+
* @param abilities.write - The action writing the property's value.
|
|
35
|
+
* @param abilities.observe - The observable async function emitting the property's changes.
|
|
36
|
+
*/
|
|
9
37
|
constructor({ read, write, observe }: HavingPropertyAbilities<GValue>);
|
|
38
|
+
/** The action reading the property's current value. */
|
|
10
39
|
get read(): ReadAction<GValue>;
|
|
40
|
+
/** The action writing the property's value. */
|
|
11
41
|
get write(): WriteAction<GValue>;
|
|
42
|
+
/** The observable async function emitting the property's changes. */
|
|
12
43
|
get observe(): ObservableAsyncFunction<GValue>;
|
|
44
|
+
/**
|
|
45
|
+
* Maps these property abilities to work with a different value type.
|
|
46
|
+
*
|
|
47
|
+
* Values read from, or observed through, the returned property abilities are mapped with `inOut`,
|
|
48
|
+
* while values written to them are mapped back to this property's value type with `outIn`.
|
|
49
|
+
*
|
|
50
|
+
* @template GNewValue - The type of the value of the returned property abilities.
|
|
51
|
+
* @param mapping - The bi-directional mapping between this property's value and the new one.
|
|
52
|
+
* @param mapping.inOut - Maps this property's value into the new one.
|
|
53
|
+
* @param mapping.outIn - Maps the new value into this property's value.
|
|
54
|
+
* @returns New property abilities working with `GNewValue`.
|
|
55
|
+
*/
|
|
13
56
|
map<GNewValue>({ inOut, outIn }: MapInOut<GValue, GNewValue>): PropertyAbilities<GNewValue>;
|
|
57
|
+
/**
|
|
58
|
+
* Returns a readonly view of these property abilities.
|
|
59
|
+
*
|
|
60
|
+
* The returned instance exposes the same read and observe abilities, without the write ability.
|
|
61
|
+
*
|
|
62
|
+
* @returns New readonly property abilities reading from, and observing, this property.
|
|
63
|
+
*/
|
|
14
64
|
asReadonly(): ReadonlyPropertyAbilities<GValue>;
|
|
15
65
|
}
|
|
@@ -1,23 +1,66 @@
|
|
|
1
1
|
import { mapOneArgument } from '@xstd/map-function';
|
|
2
2
|
import { ReadonlyPropertyAbilities } from './readonly/readonly-property-abilities.js';
|
|
3
|
+
/**
|
|
4
|
+
* The abilities of a property: reading its current value ({@link ReadAction}), writing it
|
|
5
|
+
* ({@link WriteAction}), and observing its changes over time ({@link ObservableAsyncFunction}).
|
|
6
|
+
*
|
|
7
|
+
* @template GValue - The type of the property's value.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const temperature: PropertyAbilities<number> = new PropertyAbilities({
|
|
12
|
+
* read: new AsyncFunction(async () => sensor.temperature),
|
|
13
|
+
* write: new AsyncFunction(async (signal, value: number): Promise<void> => {
|
|
14
|
+
* await sensor.setTemperature(value, { signal });
|
|
15
|
+
* }),
|
|
16
|
+
* observe: temperatureChanges, // an `ObservableAsyncFunction<number>`
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* const current: number = await temperature.read.call(signal);
|
|
20
|
+
* await temperature.write.call(signal, 21.5);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
3
23
|
export class PropertyAbilities {
|
|
4
24
|
#read;
|
|
5
25
|
#write;
|
|
6
26
|
#observe;
|
|
27
|
+
/**
|
|
28
|
+
* Creates a new {@link PropertyAbilities}.
|
|
29
|
+
*
|
|
30
|
+
* @param abilities - The abilities to wrap.
|
|
31
|
+
* @param abilities.read - The action reading the property's current value.
|
|
32
|
+
* @param abilities.write - The action writing the property's value.
|
|
33
|
+
* @param abilities.observe - The observable async function emitting the property's changes.
|
|
34
|
+
*/
|
|
7
35
|
constructor({ read, write, observe }) {
|
|
8
36
|
this.#read = read;
|
|
9
37
|
this.#write = write;
|
|
10
38
|
this.#observe = observe;
|
|
11
39
|
}
|
|
40
|
+
/** The action reading the property's current value. */
|
|
12
41
|
get read() {
|
|
13
42
|
return this.#read;
|
|
14
43
|
}
|
|
44
|
+
/** The action writing the property's value. */
|
|
15
45
|
get write() {
|
|
16
46
|
return this.#write;
|
|
17
47
|
}
|
|
48
|
+
/** The observable async function emitting the property's changes. */
|
|
18
49
|
get observe() {
|
|
19
50
|
return this.#observe;
|
|
20
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Maps these property abilities to work with a different value type.
|
|
54
|
+
*
|
|
55
|
+
* Values read from, or observed through, the returned property abilities are mapped with `inOut`,
|
|
56
|
+
* while values written to them are mapped back to this property's value type with `outIn`.
|
|
57
|
+
*
|
|
58
|
+
* @template GNewValue - The type of the value of the returned property abilities.
|
|
59
|
+
* @param mapping - The bi-directional mapping between this property's value and the new one.
|
|
60
|
+
* @param mapping.inOut - Maps this property's value into the new one.
|
|
61
|
+
* @param mapping.outIn - Maps the new value into this property's value.
|
|
62
|
+
* @returns New property abilities working with `GNewValue`.
|
|
63
|
+
*/
|
|
21
64
|
map({ inOut, outIn }) {
|
|
22
65
|
return new PropertyAbilities({
|
|
23
66
|
read: this.#read.then(inOut),
|
|
@@ -25,6 +68,13 @@ export class PropertyAbilities {
|
|
|
25
68
|
observe: this.#observe.map(inOut),
|
|
26
69
|
});
|
|
27
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Returns a readonly view of these property abilities.
|
|
73
|
+
*
|
|
74
|
+
* The returned instance exposes the same read and observe abilities, without the write ability.
|
|
75
|
+
*
|
|
76
|
+
* @returns New readonly property abilities reading from, and observing, this property.
|
|
77
|
+
*/
|
|
28
78
|
asReadonly() {
|
|
29
79
|
return new ReadonlyPropertyAbilities({
|
|
30
80
|
read: this.#read,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"property-abilities.js","sourceRoot":"","sources":["../../../../src/combined/property/property-abilities.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAKnE,OAAO,EAAE,yBAAyB,EAAE,MAAM,2CAA2C,CAAC;AAEtF,MAAM,OAAO,iBAAiB;IACnB,KAAK,CAAqB;IAC1B,MAAM,CAAsB;IAC5B,QAAQ,CAAkC;IAEnD,YAAY,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAmC;QACnE,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED,GAAG,CAAY,EAAE,KAAK,EAAE,KAAK,EAA+B;QAC1D,OAAO,IAAI,iBAAiB,CAAY;YACtC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;YAC5B,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;YACtD,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC;SAClC,CAAC,CAAC;IACL,CAAC;IAED,UAAU;QACR,OAAO,IAAI,yBAAyB,CAAS;YAC3C,IAAI,EAAE,IAAI,CAAC,KAAK;YAChB,OAAO,EAAE,IAAI,CAAC,QAAQ;SACvB,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
1
|
+
{"version":3,"file":"property-abilities.js","sourceRoot":"","sources":["../../../../src/combined/property/property-abilities.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAKnE,OAAO,EAAE,yBAAyB,EAAE,MAAM,2CAA2C,CAAC;AAEtF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,iBAAiB;IACnB,KAAK,CAAqB;IAC1B,MAAM,CAAsB;IAC5B,QAAQ,CAAkC;IAEnD;;;;;;;OAOG;IACH,YAAY,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAmC;QACnE,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED,uDAAuD;IACvD,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,+CAA+C;IAC/C,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,qEAAqE;IACrE,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED;;;;;;;;;;;OAWG;IACH,GAAG,CAAY,EAAE,KAAK,EAAE,KAAK,EAA+B;QAC1D,OAAO,IAAI,iBAAiB,CAAY;YACtC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;YAC5B,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;YACtD,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC;SAClC,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACH,UAAU;QACR,OAAO,IAAI,yBAAyB,CAAS;YAC3C,IAAI,EAAE,IAAI,CAAC,KAAK;YAChB,OAAO,EAAE,IAAI,CAAC,QAAQ;SACvB,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import type { HavingObserveAbility } from '../../../individual/observe/having-observe-ability.ts';
|
|
2
2
|
import type { HavingReadAbility } from '../../../individual/read/having-read-ability.ts';
|
|
3
|
+
/**
|
|
4
|
+
* An object having the abilities of a readonly property: reading it ({@link HavingReadAbility}) and
|
|
5
|
+
* observing it ({@link HavingObserveAbility}).
|
|
6
|
+
*
|
|
7
|
+
* @template GValue - The type of the property's value.
|
|
8
|
+
*/
|
|
3
9
|
export interface HavingReadonlyPropertyAbilities<GValue> extends HavingReadAbility<GValue>, HavingObserveAbility<GValue> {
|
|
4
10
|
}
|
|
@@ -2,10 +2,35 @@ import { type MapFunction } from '@xstd/map-function';
|
|
|
2
2
|
import type { ObservableAsyncFunction } from '@xstd/observable-async-function';
|
|
3
3
|
import type { ReadAction } from '../../../individual/read/read-action.ts';
|
|
4
4
|
import type { HavingReadonlyPropertyAbilities } from './having-readonly-property-abilities.ts';
|
|
5
|
+
/**
|
|
6
|
+
* The abilities of a readonly property: reading its current value ({@link ReadAction}) and
|
|
7
|
+
* observing its changes over time ({@link ObservableAsyncFunction}).
|
|
8
|
+
*
|
|
9
|
+
* @template GValue - The type of the property's value.
|
|
10
|
+
*/
|
|
5
11
|
export declare class ReadonlyPropertyAbilities<GValue> implements HavingReadonlyPropertyAbilities<GValue> {
|
|
6
12
|
#private;
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new {@link ReadonlyPropertyAbilities}.
|
|
15
|
+
*
|
|
16
|
+
* @param abilities - The abilities to wrap.
|
|
17
|
+
* @param abilities.read - The action reading the property's current value.
|
|
18
|
+
* @param abilities.observe - The observable async function emitting the property's changes.
|
|
19
|
+
*/
|
|
7
20
|
constructor({ read, observe }: HavingReadonlyPropertyAbilities<GValue>);
|
|
21
|
+
/** The action reading the property's current value. */
|
|
8
22
|
get read(): ReadAction<GValue>;
|
|
23
|
+
/** The observable async function emitting the property's changes. */
|
|
9
24
|
get observe(): ObservableAsyncFunction<GValue>;
|
|
25
|
+
/**
|
|
26
|
+
* Maps these readonly property abilities to work with a different value type.
|
|
27
|
+
*
|
|
28
|
+
* Values read from, or observed through, the returned property abilities are mapped with the
|
|
29
|
+
* provided function.
|
|
30
|
+
*
|
|
31
|
+
* @template GNewValue - The type of the value of the returned property abilities.
|
|
32
|
+
* @param map - A function mapping this property's value into the new one.
|
|
33
|
+
* @returns New readonly property abilities working with `GNewValue`.
|
|
34
|
+
*/
|
|
10
35
|
map<GNewValue>(map: MapFunction<GValue, GNewValue>): ReadonlyPropertyAbilities<GNewValue>;
|
|
11
36
|
}
|
|
@@ -1,17 +1,42 @@
|
|
|
1
1
|
import {} from '@xstd/map-function';
|
|
2
|
+
/**
|
|
3
|
+
* The abilities of a readonly property: reading its current value ({@link ReadAction}) and
|
|
4
|
+
* observing its changes over time ({@link ObservableAsyncFunction}).
|
|
5
|
+
*
|
|
6
|
+
* @template GValue - The type of the property's value.
|
|
7
|
+
*/
|
|
2
8
|
export class ReadonlyPropertyAbilities {
|
|
3
9
|
#read;
|
|
4
10
|
#observe;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new {@link ReadonlyPropertyAbilities}.
|
|
13
|
+
*
|
|
14
|
+
* @param abilities - The abilities to wrap.
|
|
15
|
+
* @param abilities.read - The action reading the property's current value.
|
|
16
|
+
* @param abilities.observe - The observable async function emitting the property's changes.
|
|
17
|
+
*/
|
|
5
18
|
constructor({ read, observe }) {
|
|
6
19
|
this.#read = read;
|
|
7
20
|
this.#observe = observe;
|
|
8
21
|
}
|
|
22
|
+
/** The action reading the property's current value. */
|
|
9
23
|
get read() {
|
|
10
24
|
return this.#read;
|
|
11
25
|
}
|
|
26
|
+
/** The observable async function emitting the property's changes. */
|
|
12
27
|
get observe() {
|
|
13
28
|
return this.#observe;
|
|
14
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Maps these readonly property abilities to work with a different value type.
|
|
32
|
+
*
|
|
33
|
+
* Values read from, or observed through, the returned property abilities are mapped with the
|
|
34
|
+
* provided function.
|
|
35
|
+
*
|
|
36
|
+
* @template GNewValue - The type of the value of the returned property abilities.
|
|
37
|
+
* @param map - A function mapping this property's value into the new one.
|
|
38
|
+
* @returns New readonly property abilities working with `GNewValue`.
|
|
39
|
+
*/
|
|
15
40
|
map(map) {
|
|
16
41
|
return new ReadonlyPropertyAbilities({
|
|
17
42
|
read: this.#read.then(map),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readonly-property-abilities.js","sourceRoot":"","sources":["../../../../../src/combined/property/readonly/readonly-property-abilities.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,MAAM,oBAAoB,CAAC;AAKtD,MAAM,OAAO,yBAAyB;IAC3B,KAAK,CAAqB;IAC1B,QAAQ,CAAkC;IAEnD,YAAY,EAAE,IAAI,EAAE,OAAO,EAA2C;QACpE,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED,GAAG,CAAY,GAAmC;QAChD,OAAO,IAAI,yBAAyB,CAAY;YAC9C,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC;YAC1B,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC;SAChC,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
1
|
+
{"version":3,"file":"readonly-property-abilities.js","sourceRoot":"","sources":["../../../../../src/combined/property/readonly/readonly-property-abilities.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,MAAM,oBAAoB,CAAC;AAKtD;;;;;GAKG;AACH,MAAM,OAAO,yBAAyB;IAC3B,KAAK,CAAqB;IAC1B,QAAQ,CAAkC;IAEnD;;;;;;OAMG;IACH,YAAY,EAAE,IAAI,EAAE,OAAO,EAA2C;QACpE,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED,uDAAuD;IACvD,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,qEAAqE;IACrE,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED;;;;;;;;;OASG;IACH,GAAG,CAAY,GAAmC;QAChD,OAAO,IAAI,yBAAyB,CAAY;YAC9C,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC;YAC1B,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC;SAChC,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
import type { ObservableAsyncFunction } from '@xstd/observable-async-function';
|
|
2
|
+
/**
|
|
3
|
+
* An object having the ability to observe values over time: it exposes an
|
|
4
|
+
* {@link ObservableAsyncFunction}.
|
|
5
|
+
*
|
|
6
|
+
* @template GValue - The type of the observed values.
|
|
7
|
+
* @template GArguments - The tuple type of the extra arguments passed at observation time.
|
|
8
|
+
*/
|
|
2
9
|
export interface HavingObserveAbility<GValue, GArguments extends readonly unknown[] = []> {
|
|
10
|
+
/** Observes values over time: calling it establishes a live resource emitting the values. */
|
|
3
11
|
readonly observe: ObservableAsyncFunction<GValue, GArguments>;
|
|
4
12
|
}
|
|
@@ -1,7 +1,28 @@
|
|
|
1
1
|
import type { ObservableAsyncFunction } from '@xstd/observable-async-function';
|
|
2
2
|
import type { HavingObserveAbility } from './having-observe-ability.ts';
|
|
3
|
+
/**
|
|
4
|
+
* The ability to observe values over time, wrapping an {@link ObservableAsyncFunction}.
|
|
5
|
+
*
|
|
6
|
+
* @template GValue - The type of the observed values.
|
|
7
|
+
* @template GArguments - The tuple type of the extra arguments passed at observation time.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const temperature: ObserveAbility<number> = new ObserveAbility(temperatureChanges);
|
|
12
|
+
*
|
|
13
|
+
* for await (const value of temperature.observe.values(signal)) {
|
|
14
|
+
* console.log(value); // every emitted temperature
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
3
18
|
export declare class ObserveAbility<GValue, GArguments extends readonly unknown[] = []> implements HavingObserveAbility<GValue, GArguments> {
|
|
4
19
|
#private;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new {@link ObserveAbility}.
|
|
22
|
+
*
|
|
23
|
+
* @param observe - The observable async function emitting the observed values.
|
|
24
|
+
*/
|
|
5
25
|
constructor(observe: ObservableAsyncFunction<GValue, GArguments>);
|
|
26
|
+
/** The wrapped observable async function emitting the observed values over time. */
|
|
6
27
|
get observe(): ObservableAsyncFunction<GValue, GArguments>;
|
|
7
28
|
}
|
|
@@ -1,8 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ability to observe values over time, wrapping an {@link ObservableAsyncFunction}.
|
|
3
|
+
*
|
|
4
|
+
* @template GValue - The type of the observed values.
|
|
5
|
+
* @template GArguments - The tuple type of the extra arguments passed at observation time.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const temperature: ObserveAbility<number> = new ObserveAbility(temperatureChanges);
|
|
10
|
+
*
|
|
11
|
+
* for await (const value of temperature.observe.values(signal)) {
|
|
12
|
+
* console.log(value); // every emitted temperature
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
1
16
|
export class ObserveAbility {
|
|
2
17
|
#observe;
|
|
18
|
+
/**
|
|
19
|
+
* Creates a new {@link ObserveAbility}.
|
|
20
|
+
*
|
|
21
|
+
* @param observe - The observable async function emitting the observed values.
|
|
22
|
+
*/
|
|
3
23
|
constructor(observe) {
|
|
4
24
|
this.#observe = observe;
|
|
5
25
|
}
|
|
26
|
+
/** The wrapped observable async function emitting the observed values over time. */
|
|
6
27
|
get observe() {
|
|
7
28
|
return this.#observe;
|
|
8
29
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"observe-ability.js","sourceRoot":"","sources":["../../../../src/individual/observe/observe-ability.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,cAAc;IAIhB,QAAQ,CAA8C;IAE/D,YAAY,OAAoD;QAC9D,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;CACF"}
|
|
1
|
+
{"version":3,"file":"observe-ability.js","sourceRoot":"","sources":["../../../../src/individual/observe/observe-ability.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAO,cAAc;IAIhB,QAAQ,CAA8C;IAE/D;;;;OAIG;IACH,YAAY,OAAoD;QAC9D,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED,oFAAoF;IACpF,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;CACF"}
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import type { ReadAction } from './read-action.ts';
|
|
2
|
+
/**
|
|
3
|
+
* An object having the ability to read: it exposes a {@link ReadAction}.
|
|
4
|
+
*
|
|
5
|
+
* @template GValue - The type of the read value.
|
|
6
|
+
*/
|
|
2
7
|
export interface HavingReadAbility<GValue> {
|
|
8
|
+
/** Reads and returns the current value. */
|
|
3
9
|
readonly read: ReadAction<GValue>;
|
|
4
10
|
}
|
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
import type { AsyncFunction } from '@xstd/async-function';
|
|
2
|
+
/**
|
|
3
|
+
* An asynchronous, abortable action that reads and returns a value.
|
|
4
|
+
*
|
|
5
|
+
* Calling it resolves with the read value.
|
|
6
|
+
*
|
|
7
|
+
* @template GValue - The type of the read value.
|
|
8
|
+
*/
|
|
2
9
|
export type ReadAction<GValue> = AsyncFunction<[], GValue>;
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import type { SendAction } from './send-action.ts';
|
|
2
|
+
/**
|
|
3
|
+
* An object having the ability to send: it exposes a {@link SendAction}.
|
|
4
|
+
*
|
|
5
|
+
* @template GArguments - The tuple type of the arguments accepted by the send action.
|
|
6
|
+
*/
|
|
2
7
|
export interface HavingSendAbility<GArguments extends readonly unknown[]> {
|
|
8
|
+
/** Sends (triggers) something with the provided arguments, without expecting any result. */
|
|
3
9
|
readonly send: SendAction<GArguments>;
|
|
4
10
|
}
|
|
@@ -1,2 +1,10 @@
|
|
|
1
1
|
import type { AsyncFunction } from '@xstd/async-function';
|
|
2
|
+
/**
|
|
3
|
+
* An asynchronous, abortable action that sends (triggers) something with the provided arguments,
|
|
4
|
+
* without expecting any result.
|
|
5
|
+
*
|
|
6
|
+
* Calling it resolves with `void` once the action is completed.
|
|
7
|
+
*
|
|
8
|
+
* @template GArguments - The tuple type of the arguments passed to the action.
|
|
9
|
+
*/
|
|
2
10
|
export type SendAction<GArguments extends readonly unknown[]> = AsyncFunction<GArguments, void>;
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import type { WriteAction } from './write-action.ts';
|
|
2
|
+
/**
|
|
3
|
+
* An object having the ability to write: it exposes a {@link WriteAction}.
|
|
4
|
+
*
|
|
5
|
+
* @template GValue - The type of the writable value.
|
|
6
|
+
*/
|
|
2
7
|
export interface HavingWriteAbility<GValue> {
|
|
8
|
+
/** Writes the provided value. */
|
|
3
9
|
readonly write: WriteAction<GValue>;
|
|
4
10
|
}
|
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
import type { AsyncFunction } from '@xstd/async-function';
|
|
2
|
+
/**
|
|
3
|
+
* An asynchronous, abortable action that writes a value, without expecting any result.
|
|
4
|
+
*
|
|
5
|
+
* Calling it resolves with `void` once the value was written.
|
|
6
|
+
*
|
|
7
|
+
* @template GValue - The type of the written value.
|
|
8
|
+
*/
|
|
2
9
|
export type WriteAction<GValue> = AsyncFunction<[GValue], void>;
|