@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.
Files changed (57) hide show
  1. package/.github/workflows/browsers.macos.json +51 -0
  2. package/.github/workflows/browsers.ubuntu.json +37 -0
  3. package/.github/workflows/builds.yml +27 -0
  4. package/README.md +37 -12
  5. package/dist/events.d.ts +19 -10
  6. package/dist/events.js +8 -19
  7. package/dist/microtask.d.ts +4 -6
  8. package/dist/microtask.js +3 -8
  9. package/dist/mod.d.ts +0 -2
  10. package/dist/mod.js +0 -2
  11. package/dist/query_selector.d.ts +2 -5
  12. package/dist/query_selector.js +16 -14
  13. package/dist/subscription.d.ts +0 -1
  14. package/dist/subscription.js +6 -14
  15. package/dist/wc.d.ts +4 -4
  16. package/dist/wc.js +9 -9
  17. package/docs/events.md +16 -18
  18. package/docs/microtask.md +2 -5
  19. package/docs/query_selector.md +1 -3
  20. package/docs/wc.md +2 -4
  21. package/examples/counter/mod.js +13 -15
  22. package/examples/counter/mod.ts +20 -19
  23. package/examples/form_associated/mod.js +1 -3
  24. package/examples/form_associated/mod.ts +0 -1
  25. package/examples/form_associated/text_input.js +3 -6
  26. package/examples/form_associated/text_input.ts +7 -5
  27. package/examples/stopwatch/index.html +6 -3
  28. package/examples/stopwatch/mod.js +8 -3
  29. package/examples/stopwatch/mod.ts +6 -4
  30. package/examples/stopwatch/stopwatch.js +30 -27
  31. package/examples/stopwatch/stopwatch.ts +44 -35
  32. package/examples/tsconfig.json +1 -0
  33. package/jr.json +25 -0
  34. package/package.json +10 -6
  35. package/src/events.ts +35 -34
  36. package/src/microtask.ts +6 -17
  37. package/src/mod.ts +0 -2
  38. package/src/query_selector.ts +18 -27
  39. package/src/wc.ts +13 -12
  40. package/tests/dist/events.tests.js +60 -0
  41. package/tests/dist/microtask.tests.js +38 -0
  42. package/tests/dist/mod.js +10 -0
  43. package/tests/dist/query_selector.tests.js +43 -0
  44. package/tests/dist/wc.tests.js +41 -0
  45. package/tests/src/events.tests.ts +73 -0
  46. package/tests/src/microtask.tests.ts +54 -0
  47. package/tests/src/mod.ts +11 -0
  48. package/tests/src/query_selector.tests.ts +46 -0
  49. package/tests/src/wc.tests.ts +52 -0
  50. package/tests/tsconfig.json +8 -0
  51. package/.github/workflows/build_and_test.yml +0 -18
  52. package/dist/bind.d.ts +0 -7
  53. package/dist/bind.js +0 -14
  54. package/docs/bind.md +0 -26
  55. package/docs/subscription.md +0 -85
  56. package/src/bind.ts +0 -22
  57. package/src/subscription.ts +0 -47
@@ -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
- }
@@ -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
- }