@seahax/elemental 0.4.1 → 0.4.2

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 CHANGED
@@ -14,8 +14,8 @@ Contains everything you need to build anything from a single component up to a f
14
14
  - No Dependencies
15
15
  - Tiny Bundle Size
16
16
 
17
- ![NPM Version](https://img.shields.io/npm/v/@seahax/elemental?color=red)
18
- [![bundlejs](https://deno.bundlejs.com/badge?q=@seahax/elemental@latest&treeshake=[*])](https://bundlejs.com/?q=%40seahax%2Felemental%40latest&treeshake=%5B*%5D)
17
+ [![NPM](https://img.shields.io/npm/v/%40seahax%2Felemental?style=for-the-badge&color=red)](https://www.npmjs.com/package/@seahax/elemental) [![BundleJS (GZIP)](https://img.shields.io/bundlejs/size/%40seahax/elemental?style=for-the-badge&label=bundlejs%20(gzip))
18
+ ](https://bundlejs.com/?q=%40seahax%2Felemental%40latest&treeshake=%5B*%5D)
19
19
 
20
20
 
21
21
  ## Define A Web Component
@@ -30,13 +30,20 @@ import {
30
30
  useLoading,
31
31
  useEffect,
32
32
  useChildEffect,
33
+ useDisconnectEffect,
33
34
  useHost,
34
35
  h,
35
36
  } from '@seahax/elemental';
36
37
 
37
38
  export const MyComponent = defineComponent((shadow) => {
38
- // This function is run once after the component is created, when it is
39
- // first connected to the document.
39
+ // This function is called every time the component is connected to the
40
+ // document.
41
+ //
42
+ // For the most part, a component's lifecycle should be handled as if it
43
+ // starts on connect and ends on disconnect, even though the component
44
+ // can be reconnected to the document. Restore internal state on
45
+ // connection from host attributes and properties. Effect hooks are
46
+ // designed to facilitate this pattern.
40
47
 
41
48
  // Create HTML elements and save references to them.
42
49
  const myInput = h('input');
@@ -91,8 +98,9 @@ export const MyComponent = defineComponent((shadow) => {
91
98
  // and when any of the dependencies change.
92
99
 
93
100
  return () => {
94
- // Cleanup before the next effect callback and after the component
95
- // is disconnected from the document.
101
+ // Cleanup after dependency refs are changed (before the next effect
102
+ // callback) and after the component is disconnected from the
103
+ // document.
96
104
  };
97
105
  });
98
106
 
@@ -108,6 +116,11 @@ export const MyComponent = defineComponent((shadow) => {
108
116
  };
109
117
  });
110
118
 
119
+ useDisconnectEffect(() => {
120
+ // Reactive code runs when the component is disconnected from the
121
+ // document.
122
+ });
123
+
111
124
  // Use the host element (generally only useful in reusable hooks).
112
125
  const host = useHost();
113
126
  });
@@ -1,3 +1,9 @@
1
1
  import type { ReadonlyRef, RefValues } from './ref.ts';
2
2
  /** React to observable (reference) changes. */
3
3
  export declare function useEffect<const TDeps extends readonly ReadonlyRef<any>[]>(deps: TDeps, callback: (...values: RefValues<TDeps>) => (() => void) | void): void;
4
+ /**
5
+ * React to document disconnection.
6
+ *
7
+ * Alias for: `useEffect([], () => callback)`
8
+ */
9
+ export declare function useDisconnectEffect(callback: () => void): void;
@@ -11,7 +11,10 @@ function n(n, r) {
11
11
  t && o.push(() => t());
12
12
  }), a.push(s);
13
13
  }
14
+ function r(e) {
15
+ n([], () => e);
16
+ }
14
17
  //#endregion
15
- export { n as useEffect };
18
+ export { r as useDisconnectEffect, n as useEffect };
16
19
 
17
20
  //# sourceMappingURL=effect.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"effect.js","names":[],"sources":["../../src/hooks/effect.ts"],"sourcesContent":["import { createCallbacks } from '../internal/callbacks.ts';\nimport { useContext } from './context.ts';\nimport type { ReadonlyRef, RefValues } from './ref.ts';\n\n/** React to observable (reference) changes. */\nexport function useEffect<const TDeps extends readonly ReadonlyRef<any>[]>(\n deps: TDeps,\n callback: (...values: RefValues<TDeps>) => (() => void) | void,\n): void {\n const { onNotify, onDisconnect } = useContext();\n const cleanupCallback = createCallbacks();\n const cleanup = (): void => cleanupCallback.runAndClear();\n let values: any[] | undefined;\n\n onNotify.push((): void => {\n const newValues = deps.map((dep) => dep.value);\n if (values?.length === newValues.length && values?.every((value, i) => value === newValues[i])) return;\n values = newValues;\n cleanup();\n const maybeCleanup = callback(...(values as any));\n if (maybeCleanup) cleanupCallback.push(() => maybeCleanup());\n });\n\n onDisconnect.push(cleanup);\n}\n"],"mappings":";;;AAKA,SAAgB,EACd,GACA,GACM;CACN,IAAM,EAAE,aAAU,oBAAiB,GAAY,EACzC,IAAkB,GAAiB,EACnC,UAAsB,EAAgB,aAAa,EACrD;AAWJ,CATA,EAAS,WAAiB;EACxB,IAAM,IAAY,EAAK,KAAK,MAAQ,EAAI,MAAM;AAC9C,MAAI,GAAQ,WAAW,EAAU,UAAU,GAAQ,OAAO,GAAO,MAAM,MAAU,EAAU,GAAG,CAAE;AAEhG,EADA,IAAS,GACT,GAAS;EACT,IAAM,IAAe,EAAS,GAAI,EAAe;AACjD,EAAI,KAAc,EAAgB,WAAW,GAAc,CAAC;GAC5D,EAEF,EAAa,KAAK,EAAQ"}
1
+ {"version":3,"file":"effect.js","names":[],"sources":["../../src/hooks/effect.ts"],"sourcesContent":["import { createCallbacks } from '../internal/callbacks.ts';\nimport { useContext } from './context.ts';\nimport type { ReadonlyRef, RefValues } from './ref.ts';\n\n/** React to observable (reference) changes. */\nexport function useEffect<const TDeps extends readonly ReadonlyRef<any>[]>(\n deps: TDeps,\n callback: (...values: RefValues<TDeps>) => (() => void) | void,\n): void {\n const { onNotify, onDisconnect } = useContext();\n const cleanupCallback = createCallbacks();\n const cleanup = (): void => cleanupCallback.runAndClear();\n let values: any[] | undefined;\n\n onNotify.push((): void => {\n const newValues = deps.map((dep) => dep.value);\n if (values?.length === newValues.length && values?.every((value, i) => value === newValues[i])) return;\n values = newValues;\n cleanup();\n const maybeCleanup = callback(...(values as any));\n if (maybeCleanup) cleanupCallback.push(() => maybeCleanup());\n });\n\n onDisconnect.push(cleanup);\n}\n\n/**\n * React to document disconnection.\n *\n * Alias for: `useEffect([], () => callback)`\n */\nexport function useDisconnectEffect(callback: () => void): void {\n useEffect([], () => callback);\n}\n"],"mappings":";;;AAKA,SAAgB,EACd,GACA,GACM;CACN,IAAM,EAAE,aAAU,oBAAiB,GAAY,EACzC,IAAkB,GAAiB,EACnC,UAAsB,EAAgB,aAAa,EACrD;AAWJ,CATA,EAAS,WAAiB;EACxB,IAAM,IAAY,EAAK,KAAK,MAAQ,EAAI,MAAM;AAC9C,MAAI,GAAQ,WAAW,EAAU,UAAU,GAAQ,OAAO,GAAO,MAAM,MAAU,EAAU,GAAG,CAAE;AAEhG,EADA,IAAS,GACT,GAAS;EACT,IAAM,IAAe,EAAS,GAAI,EAAe;AACjD,EAAI,KAAc,EAAgB,WAAW,GAAc,CAAC;GAC5D,EAEF,EAAa,KAAK,EAAQ;;AAQ5B,SAAgB,EAAoB,GAA4B;AAC9D,GAAU,EAAE,QAAQ,EAAS"}
package/dist/index.js CHANGED
@@ -1,13 +1,13 @@
1
1
  import { useRef as e } from "./hooks/ref.js";
2
2
  import { defineComponent as t } from "./component.js";
3
- import { useEffect as n } from "./hooks/effect.js";
4
- import { useHost as r } from "./hooks/host.js";
5
- import { useAttributes as i } from "./hooks/attributes.js";
6
- import { useChildEffect as a } from "./hooks/child-effect.js";
7
- import { useLoading as o } from "./hooks/loading.js";
8
- import { createStore as s } from "./store.js";
9
- import { getRouter as c } from "./router.js";
10
- import { useStore as l } from "./hooks/store.js";
11
- import { useRoute as u } from "./hooks/route.js";
12
- import { html as d } from "./html.js";
13
- export { s as createStore, t as defineComponent, c as getRouter, d as h, d as html, i as useAttributes, a as useChildEffect, n as useEffect, r as useHost, o as useLoading, e as useRef, u as useRoute, l as useStore };
3
+ import { useDisconnectEffect as n, useEffect as r } from "./hooks/effect.js";
4
+ import { useHost as i } from "./hooks/host.js";
5
+ import { useAttributes as a } from "./hooks/attributes.js";
6
+ import { useChildEffect as o } from "./hooks/child-effect.js";
7
+ import { useLoading as s } from "./hooks/loading.js";
8
+ import { createStore as c } from "./store.js";
9
+ import { getRouter as l } from "./router.js";
10
+ import { useStore as u } from "./hooks/store.js";
11
+ import { useRoute as d } from "./hooks/route.js";
12
+ import { html as f } from "./html.js";
13
+ export { c as createStore, t as defineComponent, l as getRouter, f as h, f as html, a as useAttributes, o as useChildEffect, n as useDisconnectEffect, r as useEffect, i as useHost, s as useLoading, e as useRef, d as useRoute, u as useStore };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seahax/elemental",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "Functional, reactive, web component base library.",
5
5
  "repository": {
6
6
  "type": "git",