@wc-bindable/rxjs 0.7.0 → 0.7.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 +121 -117
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,117 +1,121 @@
|
|
|
1
|
-
# @wc-bindable/rxjs
|
|
2
|
-
|
|
3
|
-
[RxJS](https://rxjs.dev/) adapter for the **wc-bindable** protocol.
|
|
4
|
-
|
|
5
|
-
Bridges the protocol's `bind()` into `BehaviorSubject`, so each declared
|
|
6
|
-
property of a Web Component is exposed as an observable you can `subscribe` to,
|
|
7
|
-
combine, transform, or feed into any RxJS pipeline.
|
|
8
|
-
|
|
9
|
-
## Install
|
|
10
|
-
|
|
11
|
-
```bash
|
|
12
|
-
npm install @wc-bindable/rxjs rxjs
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
## API
|
|
16
|
-
|
|
17
|
-
### `wcBindable(el, onUpdate): unbind`
|
|
18
|
-
|
|
19
|
-
Low-level helper. Call it with the DOM node and a callback that receives every
|
|
20
|
-
property update. The adapter does no reactivity wiring — you decide what to do
|
|
21
|
-
with each update (typically calling `.next()` on a `Subject` you control).
|
|
22
|
-
|
|
23
|
-
| Parameter | Type | Description |
|
|
24
|
-
|---|---|---|
|
|
25
|
-
| `el` | `Element` | The Web Component DOM node |
|
|
26
|
-
| `onUpdate` | `(name: string, value: unknown) => void` | Called once per declared property on initial sync, and again on every change event |
|
|
27
|
-
|
|
28
|
-
Returns an `unbind` function. If `el` does not implement the wc-bindable
|
|
29
|
-
protocol, the helper is a no-op and the returned function is safe to call.
|
|
30
|
-
|
|
31
|
-
### `createWcBindable<V>(initialValues?): WcBindableBinder<V>`
|
|
32
|
-
|
|
33
|
-
Stateful helper. Pre-creates one `BehaviorSubject` per key in `initialValues`
|
|
34
|
-
and keeps each subject's value in sync with the component's matching declared
|
|
35
|
-
property.
|
|
36
|
-
|
|
37
|
-
Returns `{ subjects, bind, unbind }`. Call `bind(el)` to attach to the
|
|
38
|
-
element (safe to call before or after it is connected to the DOM — the
|
|
39
|
-
initial-value read is deferred via `syncOn: "connect"`); call `unbind()`
|
|
40
|
-
when you're tearing the view down. Subscribe to changes with
|
|
41
|
-
`subjects.<name>.subscribe(...)`; read the current value synchronously with
|
|
42
|
-
`subjects.<name>.getValue()`.
|
|
43
|
-
|
|
44
|
-
```ts
|
|
45
|
-
import { createWcBindable } from "@wc-bindable/rxjs";
|
|
46
|
-
import { combineLatest, map } from "rxjs";
|
|
47
|
-
|
|
48
|
-
const binder = createWcBindable<{ value: string; checked: boolean }>({
|
|
49
|
-
value: "",
|
|
50
|
-
checked: false,
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
const el = document.querySelector("my-input")!;
|
|
54
|
-
binder.bind(el);
|
|
55
|
-
|
|
56
|
-
const derived$ = combineLatest([binder.subjects.value, binder.subjects.checked]).pipe(
|
|
57
|
-
map(([value, checked]) => `value=${value} checked=${checked}`),
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
derived$.subscribe(console.log);
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
Properties emitted by the component that are not in `initialValues` are
|
|
64
|
-
created lazily on first event, accessible via `binder.subjects[name]`. Because
|
|
65
|
-
they did not exist before the first event, they have no replayed initial value
|
|
66
|
-
for subscribers attached earlier — subscribe after `bind()` if you need to
|
|
67
|
-
catch lazy properties.
|
|
68
|
-
|
|
69
|
-
`unbind()` stops listening to DOM events but does **not** complete the
|
|
70
|
-
subjects, so you can rebind to a different element or keep them around for
|
|
71
|
-
downstream subscribers. If you want to terminate them, call `.complete()` on
|
|
72
|
-
each subject yourself or compose with `takeUntil`.
|
|
73
|
-
|
|
74
|
-
## Usage
|
|
75
|
-
|
|
76
|
-
```ts
|
|
77
|
-
import { createWcBindable } from "@wc-bindable/rxjs";
|
|
78
|
-
import "./my-counter.js";
|
|
79
|
-
|
|
80
|
-
const binder = createWcBindable<{ count: number }>({ count: 0 });
|
|
81
|
-
|
|
82
|
-
const el = document.createElement("my-counter");
|
|
83
|
-
binder.bind(el); // initial-sync is deferred until the element is connected
|
|
84
|
-
document.body.appendChild(el);
|
|
85
|
-
|
|
86
|
-
const out = document.createElement("p");
|
|
87
|
-
document.body.appendChild(out);
|
|
88
|
-
|
|
89
|
-
binder.subjects.count.subscribe((count) => {
|
|
90
|
-
out.textContent = `count: ${count}`;
|
|
91
|
-
});
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
### Low-level usage
|
|
95
|
-
|
|
96
|
-
When you want to manage your own `Subject`s (e.g. to merge multiple bindable
|
|
97
|
-
elements into one stream), use the low-level helper:
|
|
98
|
-
|
|
99
|
-
```ts
|
|
100
|
-
import { Subject } from "rxjs";
|
|
101
|
-
import { wcBindable } from "@wc-bindable/rxjs";
|
|
102
|
-
|
|
103
|
-
const value$ = new Subject<string>();
|
|
104
|
-
|
|
105
|
-
const el = document.createElement("my-input");
|
|
106
|
-
document.body.appendChild(el);
|
|
107
|
-
const unbind = wcBindable(el, (name, v) => {
|
|
108
|
-
if (name === "value") value$.next(v as string);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
// later
|
|
112
|
-
unbind();
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
##
|
|
116
|
-
|
|
117
|
-
|
|
1
|
+
# @wc-bindable/rxjs
|
|
2
|
+
|
|
3
|
+
[RxJS](https://rxjs.dev/) adapter for the **wc-bindable** protocol.
|
|
4
|
+
|
|
5
|
+
Bridges the protocol's `bind()` into `BehaviorSubject`, so each declared
|
|
6
|
+
property of a Web Component is exposed as an observable you can `subscribe` to,
|
|
7
|
+
combine, transform, or feed into any RxJS pipeline.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @wc-bindable/rxjs rxjs
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## API
|
|
16
|
+
|
|
17
|
+
### `wcBindable(el, onUpdate): unbind`
|
|
18
|
+
|
|
19
|
+
Low-level helper. Call it with the DOM node and a callback that receives every
|
|
20
|
+
property update. The adapter does no reactivity wiring — you decide what to do
|
|
21
|
+
with each update (typically calling `.next()` on a `Subject` you control).
|
|
22
|
+
|
|
23
|
+
| Parameter | Type | Description |
|
|
24
|
+
|---|---|---|
|
|
25
|
+
| `el` | `Element` | The Web Component DOM node |
|
|
26
|
+
| `onUpdate` | `(name: string, value: unknown) => void` | Called once per declared property on initial sync, and again on every change event |
|
|
27
|
+
|
|
28
|
+
Returns an `unbind` function. If `el` does not implement the wc-bindable
|
|
29
|
+
protocol, the helper is a no-op and the returned function is safe to call.
|
|
30
|
+
|
|
31
|
+
### `createWcBindable<V>(initialValues?): WcBindableBinder<V>`
|
|
32
|
+
|
|
33
|
+
Stateful helper. Pre-creates one `BehaviorSubject` per key in `initialValues`
|
|
34
|
+
and keeps each subject's value in sync with the component's matching declared
|
|
35
|
+
property.
|
|
36
|
+
|
|
37
|
+
Returns `{ subjects, bind, unbind }`. Call `bind(el)` to attach to the
|
|
38
|
+
element (safe to call before or after it is connected to the DOM — the
|
|
39
|
+
initial-value read is deferred via `syncOn: "connect"`); call `unbind()`
|
|
40
|
+
when you're tearing the view down. Subscribe to changes with
|
|
41
|
+
`subjects.<name>.subscribe(...)`; read the current value synchronously with
|
|
42
|
+
`subjects.<name>.getValue()`.
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { createWcBindable } from "@wc-bindable/rxjs";
|
|
46
|
+
import { combineLatest, map } from "rxjs";
|
|
47
|
+
|
|
48
|
+
const binder = createWcBindable<{ value: string; checked: boolean }>({
|
|
49
|
+
value: "",
|
|
50
|
+
checked: false,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const el = document.querySelector("my-input")!;
|
|
54
|
+
binder.bind(el);
|
|
55
|
+
|
|
56
|
+
const derived$ = combineLatest([binder.subjects.value, binder.subjects.checked]).pipe(
|
|
57
|
+
map(([value, checked]) => `value=${value} checked=${checked}`),
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
derived$.subscribe(console.log);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Properties emitted by the component that are not in `initialValues` are
|
|
64
|
+
created lazily on first event, accessible via `binder.subjects[name]`. Because
|
|
65
|
+
they did not exist before the first event, they have no replayed initial value
|
|
66
|
+
for subscribers attached earlier — subscribe after `bind()` if you need to
|
|
67
|
+
catch lazy properties.
|
|
68
|
+
|
|
69
|
+
`unbind()` stops listening to DOM events but does **not** complete the
|
|
70
|
+
subjects, so you can rebind to a different element or keep them around for
|
|
71
|
+
downstream subscribers. If you want to terminate them, call `.complete()` on
|
|
72
|
+
each subject yourself or compose with `takeUntil`.
|
|
73
|
+
|
|
74
|
+
## Usage
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import { createWcBindable } from "@wc-bindable/rxjs";
|
|
78
|
+
import "./my-counter.js";
|
|
79
|
+
|
|
80
|
+
const binder = createWcBindable<{ count: number }>({ count: 0 });
|
|
81
|
+
|
|
82
|
+
const el = document.createElement("my-counter");
|
|
83
|
+
binder.bind(el); // initial-sync is deferred until the element is connected
|
|
84
|
+
document.body.appendChild(el);
|
|
85
|
+
|
|
86
|
+
const out = document.createElement("p");
|
|
87
|
+
document.body.appendChild(out);
|
|
88
|
+
|
|
89
|
+
binder.subjects.count.subscribe((count) => {
|
|
90
|
+
out.textContent = `count: ${count}`;
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Low-level usage
|
|
95
|
+
|
|
96
|
+
When you want to manage your own `Subject`s (e.g. to merge multiple bindable
|
|
97
|
+
elements into one stream), use the low-level helper:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import { Subject } from "rxjs";
|
|
101
|
+
import { wcBindable } from "@wc-bindable/rxjs";
|
|
102
|
+
|
|
103
|
+
const value$ = new Subject<string>();
|
|
104
|
+
|
|
105
|
+
const el = document.createElement("my-input");
|
|
106
|
+
document.body.appendChild(el);
|
|
107
|
+
const unbind = wcBindable(el, (name, v) => {
|
|
108
|
+
if (name === "value") value$.next(v as string);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// later
|
|
112
|
+
unbind();
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Specification
|
|
116
|
+
|
|
117
|
+
The protocol contract this adapter implements lives in [SPEC.md](../../SPEC.md); the optional input/command invocation surface and the remote wire format live in [SPEC-extensions.md](../../SPEC-extensions.md). Runnable conformance vectors are in [CONFORMANCE.md](../../CONFORMANCE.md).
|
|
118
|
+
|
|
119
|
+
## License
|
|
120
|
+
|
|
121
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wc-bindable/rxjs",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "RxJS adapter for wc-bindable protocol",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"test:watch": "vitest tests"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@wc-bindable/core": "^0.
|
|
26
|
+
"@wc-bindable/core": "^0.7.1"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"rxjs": ">=7"
|