@w-lfpup/wctk 0.1.0 → 0.2.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/.github/workflows/browsers.macos.json +51 -0
- package/.github/workflows/browsers.ubuntu.json +37 -0
- package/.github/workflows/builds.yml +27 -0
- package/README.md +37 -12
- package/dist/events.d.ts +19 -10
- package/dist/events.js +8 -19
- package/dist/microtask.d.ts +4 -6
- package/dist/microtask.js +3 -8
- package/dist/mod.d.ts +0 -2
- package/dist/mod.js +0 -2
- package/dist/query_selector.d.ts +2 -5
- package/dist/query_selector.js +16 -14
- package/dist/subscription.d.ts +0 -1
- package/dist/subscription.js +6 -14
- package/dist/wc.d.ts +4 -4
- package/dist/wc.js +9 -9
- package/docs/events.md +16 -18
- package/docs/microtask.md +2 -5
- package/docs/query_selector.md +1 -3
- package/docs/wc.md +2 -4
- package/examples/counter/mod.js +13 -15
- package/examples/counter/mod.ts +20 -19
- package/examples/form_associated/mod.js +1 -3
- package/examples/form_associated/mod.ts +0 -1
- package/examples/form_associated/text_input.js +3 -6
- package/examples/form_associated/text_input.ts +7 -5
- package/examples/stopwatch/index.html +6 -3
- package/examples/stopwatch/mod.js +8 -3
- package/examples/stopwatch/mod.ts +6 -4
- package/examples/stopwatch/stopwatch.js +30 -27
- package/examples/stopwatch/stopwatch.ts +44 -35
- package/examples/tsconfig.json +1 -0
- package/jr.json +25 -0
- package/package.json +10 -6
- package/src/events.ts +35 -34
- package/src/microtask.ts +6 -17
- package/src/mod.ts +0 -2
- package/src/query_selector.ts +18 -27
- package/src/wc.ts +13 -12
- package/tests/dist/events.tests.js +60 -0
- package/tests/dist/microtask.tests.js +38 -0
- package/tests/dist/mod.js +10 -0
- package/tests/dist/query_selector.tests.js +43 -0
- package/tests/dist/wc.tests.js +41 -0
- package/tests/src/events.tests.ts +73 -0
- package/tests/src/microtask.tests.ts +54 -0
- package/tests/src/mod.ts +11 -0
- package/tests/src/query_selector.tests.ts +46 -0
- package/tests/src/wc.tests.ts +52 -0
- package/tests/tsconfig.json +8 -0
- package/.github/workflows/build_and_test.yml +0 -18
- package/dist/bind.d.ts +0 -7
- package/dist/bind.js +0 -14
- package/docs/bind.md +0 -26
- package/docs/subscription.md +0 -85
- package/src/bind.ts +0 -22
- package/src/subscription.ts +0 -47
package/docs/subscription.md
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
# Subscribe Controller
|
|
2
|
-
|
|
3
|
-
Subscribe web components to external state.
|
|
4
|
-
|
|
5
|
-
## How to use
|
|
6
|
-
|
|
7
|
-
### Callbacks
|
|
8
|
-
|
|
9
|
-
Create functions that subscribe and unsubscribe an element from a data store.
|
|
10
|
-
|
|
11
|
-
The result of the subscribe function is passed as the to the unsubscribe function.
|
|
12
|
-
|
|
13
|
-
```ts
|
|
14
|
-
import { Datastore } from "./some-datastore.js";
|
|
15
|
-
|
|
16
|
-
let store = new Datastore();
|
|
17
|
-
|
|
18
|
-
function subscribe(callback): number {
|
|
19
|
-
return store.subscribe(callback);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function unsubscribe(results: number): void {
|
|
23
|
-
store.unsubscribe(results);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export { store, subscribe, unsubscribe };
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
### Controller
|
|
30
|
-
|
|
31
|
-
Add a `Subscription` controller to a web component. Pass subscribe, unsubscribe, and a callback function on instantiation.
|
|
32
|
-
|
|
33
|
-
```ts
|
|
34
|
-
import { Subscription } from "wctk";
|
|
35
|
-
import { getState, subscribe, unsubscribe } from "./datastore.js";
|
|
36
|
-
|
|
37
|
-
class MyElement extends HTMLElement {
|
|
38
|
-
#sc = new Subscription({
|
|
39
|
-
host: this,
|
|
40
|
-
callback: this.#update,
|
|
41
|
-
subscribe,
|
|
42
|
-
unsubscribe,
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
#update() {
|
|
46
|
-
let state = getState();
|
|
47
|
-
// do something with state
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// lifecycle method
|
|
51
|
-
connectedCallback() {
|
|
52
|
-
this.#sc.connect();
|
|
53
|
-
this.#update();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// lifecycle method
|
|
57
|
-
disconnectedCallback() {
|
|
58
|
-
this.#sc.disconnect();
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
### Shortcut life cycle methods
|
|
64
|
-
|
|
65
|
-
In the example below, the `connected` property is set to true and the component is immediately subscribed on instantiation.
|
|
66
|
-
|
|
67
|
-
```ts
|
|
68
|
-
import { Subscription } from "wctk";
|
|
69
|
-
import { getState, subscribe, unsubscribe } from "./datastore.js";
|
|
70
|
-
|
|
71
|
-
class MyElement extends HTMLElement {
|
|
72
|
-
#sc = new Subscription({
|
|
73
|
-
host: this,
|
|
74
|
-
callback: this.#update,
|
|
75
|
-
connected: true,
|
|
76
|
-
subscribe,
|
|
77
|
-
unsubscribe,
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
#update() {
|
|
81
|
-
let state = getState();
|
|
82
|
-
// do something with state
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
```
|
package/src/bind.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export interface BindParamsInterface {
|
|
2
|
-
host: Object;
|
|
3
|
-
callbacks: Function[];
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export class Bind {
|
|
7
|
-
constructor(params: BindParamsInterface) {
|
|
8
|
-
let { host, callbacks } = params;
|
|
9
|
-
|
|
10
|
-
for (let callback of callbacks) {
|
|
11
|
-
// do not bind and replace already bound functions
|
|
12
|
-
if (
|
|
13
|
-
callback instanceof Function &&
|
|
14
|
-
!callback.hasOwnProperty("prototype")
|
|
15
|
-
) {
|
|
16
|
-
let { name } = callback;
|
|
17
|
-
if (!name.startsWith("#"))
|
|
18
|
-
host[name as keyof typeof host] = callback.bind(host);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
package/src/subscription.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
export type Subscribe<E, A> = (cb: E) => A;
|
|
2
|
-
export type Unsubscribe<A> = (affect: A) => void;
|
|
3
|
-
|
|
4
|
-
export interface SubscriptionInterface {
|
|
5
|
-
connect(): void;
|
|
6
|
-
disconnect(): void;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface SubscriptionParamsInterface<E, A> {
|
|
10
|
-
host: Object;
|
|
11
|
-
callback: E;
|
|
12
|
-
connected?: boolean;
|
|
13
|
-
subscribe: Subscribe<E, A>;
|
|
14
|
-
unsubscribe: Unsubscribe<A>;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class Subscription<E, A> implements SubscriptionInterface {
|
|
18
|
-
#callback: E;
|
|
19
|
-
#affect?: A;
|
|
20
|
-
#subscribe: Subscribe<E, A>;
|
|
21
|
-
#unsubscribe: Unsubscribe<A>;
|
|
22
|
-
|
|
23
|
-
constructor(params: SubscriptionParamsInterface<E, A>) {
|
|
24
|
-
let { host, callback, connected, subscribe, unsubscribe } = params;
|
|
25
|
-
|
|
26
|
-
this.#subscribe = subscribe;
|
|
27
|
-
this.#unsubscribe = unsubscribe;
|
|
28
|
-
|
|
29
|
-
this.#callback = callback;
|
|
30
|
-
if (
|
|
31
|
-
callback instanceof Function &&
|
|
32
|
-
!callback.hasOwnProperty("prototype")
|
|
33
|
-
) {
|
|
34
|
-
this.#callback = callback.bind(host);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (connected) this.connect();
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
connect() {
|
|
41
|
-
if (!this.#affect) this.#affect = this.#subscribe(this.#callback);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
disconnect() {
|
|
45
|
-
if (this.#affect) this.#unsubscribe(this.#affect);
|
|
46
|
-
}
|
|
47
|
-
}
|