@wc-bindable/rxjs 0.6.0
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/LICENSE +21 -0
- package/README.md +115 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 wc-bindable-protocol
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
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)` once the element is in
|
|
38
|
+
the DOM, `unbind()` when you're tearing the view down. Subscribe to changes
|
|
39
|
+
with `subjects.<name>.subscribe(...)`; read the current value synchronously
|
|
40
|
+
with `subjects.<name>.getValue()`.
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { createWcBindable } from "@wc-bindable/rxjs";
|
|
44
|
+
import { combineLatest, map } from "rxjs";
|
|
45
|
+
|
|
46
|
+
const binder = createWcBindable<{ value: string; checked: boolean }>({
|
|
47
|
+
value: "",
|
|
48
|
+
checked: false,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const el = document.querySelector("my-input")!;
|
|
52
|
+
binder.bind(el);
|
|
53
|
+
|
|
54
|
+
const derived$ = combineLatest([binder.subjects.value, binder.subjects.checked]).pipe(
|
|
55
|
+
map(([value, checked]) => `value=${value} checked=${checked}`),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
derived$.subscribe(console.log);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Properties emitted by the component that are not in `initialValues` are
|
|
62
|
+
created lazily on first event, accessible via `binder.subjects[name]`. Because
|
|
63
|
+
they did not exist before the first event, they have no replayed initial value
|
|
64
|
+
for subscribers attached earlier — subscribe after `bind()` if you need to
|
|
65
|
+
catch lazy properties.
|
|
66
|
+
|
|
67
|
+
`unbind()` stops listening to DOM events but does **not** complete the
|
|
68
|
+
subjects, so you can rebind to a different element or keep them around for
|
|
69
|
+
downstream subscribers. If you want to terminate them, call `.complete()` on
|
|
70
|
+
each subject yourself or compose with `takeUntil`.
|
|
71
|
+
|
|
72
|
+
## Usage
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { createWcBindable } from "@wc-bindable/rxjs";
|
|
76
|
+
import "./my-counter.js";
|
|
77
|
+
|
|
78
|
+
const binder = createWcBindable<{ count: number }>({ count: 0 });
|
|
79
|
+
|
|
80
|
+
const el = document.createElement("my-counter");
|
|
81
|
+
document.body.appendChild(el);
|
|
82
|
+
binder.bind(el); // bind AFTER append so initial-sync sees post-connect values
|
|
83
|
+
|
|
84
|
+
const out = document.createElement("p");
|
|
85
|
+
document.body.appendChild(out);
|
|
86
|
+
|
|
87
|
+
binder.subjects.count.subscribe((count) => {
|
|
88
|
+
out.textContent = `count: ${count}`;
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Low-level usage
|
|
93
|
+
|
|
94
|
+
When you want to manage your own `Subject`s (e.g. to merge multiple bindable
|
|
95
|
+
elements into one stream), use the low-level helper:
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import { Subject } from "rxjs";
|
|
99
|
+
import { wcBindable } from "@wc-bindable/rxjs";
|
|
100
|
+
|
|
101
|
+
const value$ = new Subject<string>();
|
|
102
|
+
|
|
103
|
+
const el = document.createElement("my-input");
|
|
104
|
+
document.body.appendChild(el);
|
|
105
|
+
const unbind = wcBindable(el, (name, v) => {
|
|
106
|
+
if (name === "value") value$.next(v as string);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// later
|
|
110
|
+
unbind();
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BehaviorSubject } from "rxjs";
|
|
2
|
+
export type UnbindFn = () => void;
|
|
3
|
+
export type OnUpdate = (name: string, value: unknown) => void;
|
|
4
|
+
export declare function wcBindable(el: Element, onUpdate: OnUpdate): UnbindFn;
|
|
5
|
+
export type WcBindableSubjects<V extends object> = {
|
|
6
|
+
[K in keyof V]: BehaviorSubject<V[K]>;
|
|
7
|
+
};
|
|
8
|
+
export interface WcBindableBinder<V extends object> {
|
|
9
|
+
readonly subjects: WcBindableSubjects<V>;
|
|
10
|
+
bind(el: Element): void;
|
|
11
|
+
unbind(): void;
|
|
12
|
+
}
|
|
13
|
+
export declare function createWcBindable<V extends object = Record<string, unknown>>(initialValues?: Partial<V>): WcBindableBinder<V>;
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAGvC,MAAM,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC;AAClC,MAAM,MAAM,QAAQ,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;AAE9D,wBAAgB,UAAU,CAAC,EAAE,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,GAAG,QAAQ,CAGpE;AAED,MAAM,MAAM,kBAAkB,CAAC,CAAC,SAAS,MAAM,IAAI;KAChD,CAAC,IAAI,MAAM,CAAC,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CACtC,CAAC;AAEF,MAAM,WAAW,gBAAgB,CAAC,CAAC,SAAS,MAAM;IAChD,QAAQ,CAAC,QAAQ,EAAE,kBAAkB,CAAC,CAAC,CAAC,CAAC;IACzC,IAAI,CAAC,EAAE,EAAE,OAAO,GAAG,IAAI,CAAC;IACxB,MAAM,IAAI,IAAI,CAAC;CAChB;AAED,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACzE,aAAa,GAAE,OAAO,CAAC,CAAC,CAAM,GAC7B,gBAAgB,CAAC,CAAC,CAAC,CAoCrB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { BehaviorSubject } from "rxjs";
|
|
2
|
+
import { bind, isWcBindable } from "@wc-bindable/core";
|
|
3
|
+
export function wcBindable(el, onUpdate) {
|
|
4
|
+
if (!isWcBindable(el))
|
|
5
|
+
return () => { };
|
|
6
|
+
return bind(el, onUpdate);
|
|
7
|
+
}
|
|
8
|
+
export function createWcBindable(initialValues = {}) {
|
|
9
|
+
const subjects = {};
|
|
10
|
+
for (const key of Object.keys(initialValues)) {
|
|
11
|
+
subjects[key] = new BehaviorSubject(initialValues[key]);
|
|
12
|
+
}
|
|
13
|
+
let unbindFn = null;
|
|
14
|
+
return {
|
|
15
|
+
get subjects() {
|
|
16
|
+
return subjects;
|
|
17
|
+
},
|
|
18
|
+
bind(el) {
|
|
19
|
+
if (unbindFn) {
|
|
20
|
+
unbindFn();
|
|
21
|
+
unbindFn = null;
|
|
22
|
+
}
|
|
23
|
+
if (!isWcBindable(el))
|
|
24
|
+
return;
|
|
25
|
+
unbindFn = bind(el, (name, value) => {
|
|
26
|
+
const existing = subjects[name];
|
|
27
|
+
if (existing) {
|
|
28
|
+
existing.next(value);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
subjects[name] = new BehaviorSubject(value);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
},
|
|
35
|
+
unbind() {
|
|
36
|
+
if (unbindFn) {
|
|
37
|
+
unbindFn();
|
|
38
|
+
unbindFn = null;
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAKvD,MAAM,UAAU,UAAU,CAAC,EAAW,EAAE,QAAkB;IACxD,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC;QAAE,OAAO,GAAG,EAAE,GAAE,CAAC,CAAC;IACvC,OAAO,IAAI,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAC;AAC5B,CAAC;AAYD,MAAM,UAAU,gBAAgB,CAC9B,gBAA4B,EAAE;IAE9B,MAAM,QAAQ,GAAG,EAA8C,CAAC;IAChE,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,aAAa,CAAgB,EAAE,CAAC;QAC5D,QAAQ,CAAC,GAAa,CAAC,GAAG,IAAI,eAAe,CAC1C,aAAyC,CAAC,GAAa,CAAC,CAC1D,CAAC;IACJ,CAAC;IAED,IAAI,QAAQ,GAAoB,IAAI,CAAC;IAErC,OAAO;QACL,IAAI,QAAQ;YACV,OAAO,QAAiC,CAAC;QAC3C,CAAC;QACD,IAAI,CAAC,EAAE;YACL,IAAI,QAAQ,EAAE,CAAC;gBACb,QAAQ,EAAE,CAAC;gBACX,QAAQ,GAAG,IAAI,CAAC;YAClB,CAAC;YACD,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC;gBAAE,OAAO;YAC9B,QAAQ,GAAG,IAAI,CAAC,EAAE,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE;gBAClC,MAAM,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;gBAChC,IAAI,QAAQ,EAAE,CAAC;oBACb,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;gBACvB,CAAC;qBAAM,CAAC;oBACN,QAAQ,CAAC,IAAI,CAAC,GAAG,IAAI,eAAe,CAAU,KAAK,CAAC,CAAC;gBACvD,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC;QACD,MAAM;YACJ,IAAI,QAAQ,EAAE,CAAC;gBACb,QAAQ,EAAE,CAAC;gBACX,QAAQ,GAAG,IAAI,CAAC;YAClB,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wc-bindable/rxjs",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "RxJS adapter for wc-bindable protocol",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"LICENSE",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc",
|
|
21
|
+
"dev": "tsc --watch",
|
|
22
|
+
"test": "vitest run tests",
|
|
23
|
+
"test:watch": "vitest tests"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@wc-bindable/core": "^0.6.0"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"rxjs": ">=7"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/wc-bindable-protocol/wc-bindable-protocol.git",
|
|
34
|
+
"directory": "packages/rxjs"
|
|
35
|
+
},
|
|
36
|
+
"license": "MIT"
|
|
37
|
+
}
|