@amazon/vinyl-observable 1.1.0 → 1.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 +113 -0
- package/package.json +5 -5
- package/docs/OBSERVABLE.md +0 -95
package/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# @amazon/vinyl-observable
|
|
2
|
+
|
|
3
|
+
[](https://amazonmusic.github.io/vinyl)
|
|
4
|
+
[](https://www.npmjs.com/package/@amazon/vinyl-observable)
|
|
5
|
+
[](https://bundlejs.com/?q=@amazon/vinyl-observable)
|
|
6
|
+
|
|
7
|
+
A small, typed observable value primitive for state tracking without a
|
|
8
|
+
third-party reactivity system. Under 1 KiB minified + gzipped.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```shell
|
|
13
|
+
npm install @amazon/vinyl-observable
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## ObservableValue\<T>
|
|
17
|
+
|
|
18
|
+
An `ObservableValue<T>` represents a reactive, read-only data container that can
|
|
19
|
+
be observed for changes over time.
|
|
20
|
+
|
|
21
|
+
### Key Features
|
|
22
|
+
|
|
23
|
+
- Reactive: subscribers are notified on value changes.
|
|
24
|
+
- Composable: derive new observable values using `map` and `pick`.
|
|
25
|
+
- Typed: strong type safety for both value and callback arguments.
|
|
26
|
+
- Lightweight: minified and gzipped bundle size is under 1 KiB.
|
|
27
|
+
|
|
28
|
+
### Example
|
|
29
|
+
|
|
30
|
+
Use `data` to create a `MutableValue` for a data type.
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { data } from '@amazon/vinyl-observable'
|
|
34
|
+
|
|
35
|
+
const user$ = data({ name: 'Alice', age: 30 })
|
|
36
|
+
|
|
37
|
+
const name$ = user$.pick('name')
|
|
38
|
+
name$.onData((name) => {
|
|
39
|
+
console.log('Name changed:', name)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
user$.value = { name: 'Bob', age: 30 }
|
|
43
|
+
// Logs: "Name changed: Bob"
|
|
44
|
+
|
|
45
|
+
const age$ = user$.pick('age')
|
|
46
|
+
age$.value = 31
|
|
47
|
+
|
|
48
|
+
user$.value.age // 31
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Utilities
|
|
52
|
+
|
|
53
|
+
### combineData
|
|
54
|
+
|
|
55
|
+
Combines multiple data providers into a single record, notifying observers when
|
|
56
|
+
any have changed.
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { combineData, data } from '@amazon/vinyl-observable'
|
|
60
|
+
|
|
61
|
+
const a = data(1)
|
|
62
|
+
const b = data('x')
|
|
63
|
+
const combined = combineData({ a, b })
|
|
64
|
+
|
|
65
|
+
combined.onValue((v) => {
|
|
66
|
+
console.log('changed: ', v.a, v.b)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
a.value = 2
|
|
70
|
+
// Logs: "changed: 2 x"
|
|
71
|
+
b.value = 'y'
|
|
72
|
+
// Logs: "changed: 2 y"
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The `combined` `onValue` callback is invoked synchronously; every change from
|
|
76
|
+
every sub-provider is delivered. See `throttle` in
|
|
77
|
+
[`@amazon/vinyl-util`](../vinyl-util) to handle frequent changes.
|
|
78
|
+
|
|
79
|
+
### externalData
|
|
80
|
+
|
|
81
|
+
Creates an `ObservableValue` that integrates with an external data source. Data
|
|
82
|
+
is only actively fetched or listened to when a subscriber calls `onData`,
|
|
83
|
+
allowing lazy subscription to external event systems such as WebSocket, polling,
|
|
84
|
+
or other push-based updates.
|
|
85
|
+
|
|
86
|
+
Returns an `ObservableValue<T>` that:
|
|
87
|
+
|
|
88
|
+
- Emits `initialValue` immediately on subscription.
|
|
89
|
+
- Receives future values from `onDataRequested`.
|
|
90
|
+
- Automatically unsubscribes from the external source when no listeners remain.
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { externalData } from '@amazon/vinyl-observable'
|
|
94
|
+
|
|
95
|
+
const timeData = externalData<Date>(new Date(), (setData) => {
|
|
96
|
+
const interval = setInterval(() => {
|
|
97
|
+
setData(new Date())
|
|
98
|
+
}, 1000)
|
|
99
|
+
|
|
100
|
+
return () => clearInterval(interval)
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
const unsubscribe = timeData.onData((now) => {
|
|
104
|
+
console.log('Current time:', now)
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
// Later, to stop receiving updates
|
|
108
|
+
unsubscribe()
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
Apache-2.0
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amazon/vinyl-observable",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "A simple active model",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -69,14 +69,14 @@
|
|
|
69
69
|
"watch:release": "tsx buildSrc/build.ts --watch --release"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
|
-
"@amazon/vinyl-build-utils": "^1.1.
|
|
73
|
-
"@amazon/vinyl-jasmine-wrapper": "^1.1.
|
|
72
|
+
"@amazon/vinyl-build-utils": "^1.1.1",
|
|
73
|
+
"@amazon/vinyl-jasmine-wrapper": "^1.1.1"
|
|
74
74
|
},
|
|
75
75
|
"dependencies": {
|
|
76
|
-
"@amazon/vinyl-util": "^1.1.
|
|
76
|
+
"@amazon/vinyl-util": "^1.1.1"
|
|
77
77
|
},
|
|
78
78
|
"browserslist": [
|
|
79
79
|
"defaults, chrome 52, firefox 54, edge 15, safari 8"
|
|
80
80
|
],
|
|
81
|
-
"gitHead": "
|
|
81
|
+
"gitHead": "e5a3429d802564132b16808a5b8a8e15a023a548"
|
|
82
82
|
}
|
package/docs/OBSERVABLE.md
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
# Observable
|
|
2
|
-
|
|
3
|
-
# ObservableValue\<T>
|
|
4
|
-
|
|
5
|
-
An `ObservableValue<T>` represents a reactive, read-only data container that can
|
|
6
|
-
be observed for changes over time. It is designed for simple and efficient state
|
|
7
|
-
tracking without introducing third-party reactivity systems.
|
|
8
|
-
|
|
9
|
-
## Key Features
|
|
10
|
-
|
|
11
|
-
- Reactive: Subscribers are notified on value changes
|
|
12
|
-
- Composable: You can derive new observable values using `map` and `pick`
|
|
13
|
-
- Typed: Strong type safety for both value and callback arguments
|
|
14
|
-
- Lightweight: Minified and GZipped bundle size is under 1KiB.
|
|
15
|
-
|
|
16
|
-
## Example
|
|
17
|
-
|
|
18
|
-
Use `data` to create a MutableValue for a data type.
|
|
19
|
-
|
|
20
|
-
```typescript
|
|
21
|
-
const user$ = data({ name: 'Alice', age: 30 })
|
|
22
|
-
|
|
23
|
-
const name$ = user$.pick('name')
|
|
24
|
-
name$.onData((name) => {
|
|
25
|
-
console.log('Name changed:', name)
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
user$.value = { name: 'Bob', age: 30 }
|
|
29
|
-
// Logs: "Name changed: Bob"
|
|
30
|
-
|
|
31
|
-
age$ = user$.pick('age')
|
|
32
|
-
age$.value = 31
|
|
33
|
-
|
|
34
|
-
user$.value.age // 31
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
## Utilities
|
|
38
|
-
|
|
39
|
-
### combineData
|
|
40
|
-
|
|
41
|
-
Combines multiple data providers into a single record, notifying observers when
|
|
42
|
-
any have changed.
|
|
43
|
-
|
|
44
|
-
```typescript
|
|
45
|
-
const a = data(1)
|
|
46
|
-
const b = data('x')
|
|
47
|
-
const combined = combineData({ a, b })
|
|
48
|
-
|
|
49
|
-
combined.onValue((v) => {
|
|
50
|
-
console.log('changed: ', v.a, v.b)
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
a.value = 2
|
|
54
|
-
// Logs: "changed: 2 x"
|
|
55
|
-
b.value = 'y'
|
|
56
|
-
// Logs: "changed: 2 y"
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
Note - combined onValue callback is invoked synchronously; every change from
|
|
60
|
-
every sub-provider will be notified to observers. See `throttle` in vinyl-util
|
|
61
|
-
to handle frequent changes.
|
|
62
|
-
|
|
63
|
-
## externalData
|
|
64
|
-
|
|
65
|
-
Creates an `ObservableValue` that integrates with an external data source. The
|
|
66
|
-
data is only actively fetched or listened to when a subscriber calls `onData`,
|
|
67
|
-
allowing lazy subscription to external event systems such as WebSocket, polling,
|
|
68
|
-
or other push-based updates.
|
|
69
|
-
|
|
70
|
-
Returns An ObservableValue<T> that:
|
|
71
|
-
|
|
72
|
-
- Emits initialValue immediately on subscription.
|
|
73
|
-
- Receives future values from onDataRequested.
|
|
74
|
-
- Automatically unsubscribes from the external source when no listeners remain.
|
|
75
|
-
|
|
76
|
-
Example:
|
|
77
|
-
|
|
78
|
-
```typescript
|
|
79
|
-
import { externalData } from './data'
|
|
80
|
-
|
|
81
|
-
const timeData = externalData<Date>(new Date(), (setData) => {
|
|
82
|
-
const interval = setInterval(() => {
|
|
83
|
-
setData(new Date())
|
|
84
|
-
}, 1000)
|
|
85
|
-
|
|
86
|
-
return () => clearInterval(interval)
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
const unsubscribe = timeData.onData((now) => {
|
|
90
|
-
console.log('Current time:', now)
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
// Later, to stop receiving updates
|
|
94
|
-
unsubscribe()
|
|
95
|
-
```
|