awai-react 0.0.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/README.md ADDED
@@ -0,0 +1,16 @@
1
+ <div align="center">
2
+ <h1>Awai-React</h1>
3
+
4
+ <p>React hooks for <a href="https://awai.vercel.app">Awai</a> integration</p>
5
+
6
+ <div>
7
+ <img src="https://img.shields.io/badge/stability-experimental-blue.svg" />
8
+ <img src="https://img.shields.io/badge/License-MIT-blue.svg" />
9
+ </div>
10
+
11
+ <br />
12
+
13
+ <p>
14
+ | <a href="https://github.com/yuriyyakym/awai">Awai</a> | <a href="https://awai.vercel.app/">Documentation</a> |
15
+ </p>
16
+ </div>
@@ -0,0 +1,11 @@
1
+ import { WritableState, WritableAsyncState, State, AsyncState, Setter, AsyncSetter, ReadableState, ReadableAsyncState } from 'awai';
2
+
3
+ declare const useSetState: <Q extends WritableState<T> | WritableAsyncState<T>, T = any>(writable: Q) => Q["set"];
4
+
5
+ type Return<T> = T extends State<infer U> ? [U, Setter<U>] : T extends AsyncState<infer V> ? [V, AsyncSetter<V>] : never;
6
+ declare function useState<T>(state: State<T>): Return<State<T>>;
7
+ declare function useState<T>(state: AsyncState<T>): Return<AsyncState<T>>;
8
+
9
+ declare const useStateValue: <T>(readable: ReadableState<T> | ReadableAsyncState<T>) => T;
10
+
11
+ export { useSetState, useState, useStateValue };
package/dist/index.js ADDED
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ var awai = require('awai');
4
+ var react = require('react');
5
+
6
+ const useSetState = (writable) => {
7
+ return writable.set;
8
+ };
9
+
10
+ const useStateValue = (readable) => {
11
+ const [state, setState] = react.useState(readable.get);
12
+ if (awai.isReadableAsyncState(readable) && state === void 0) {
13
+ throw new Promise((resolve) => readable.events.changed.then(resolve));
14
+ }
15
+ react.useEffect(() => {
16
+ let mounted = true;
17
+ (async () => {
18
+ while (mounted) {
19
+ await readable.events.changed;
20
+ if (mounted) {
21
+ setState(readable.get());
22
+ }
23
+ }
24
+ })();
25
+ return () => {
26
+ mounted = false;
27
+ };
28
+ }, [readable]);
29
+ return state;
30
+ };
31
+
32
+ function useState(state) {
33
+ const value = useStateValue(state);
34
+ const setValue = useSetState(state);
35
+ return [value, setValue];
36
+ }
37
+
38
+ exports.useSetState = useSetState;
39
+ exports.useState = useState;
40
+ exports.useStateValue = useStateValue;
41
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/useSetState.ts","../src/useStateValue.ts","../src/useState.ts"],"sourcesContent":["import type { WritableAsyncState, WritableState } from 'awai';\n\nconst useSetState = <Q extends WritableState<T> | WritableAsyncState<T>, T = any>(\n writable: Q,\n): Q['set'] => {\n return writable.set;\n};\n\nexport default useSetState;\n","import { ReadableAsyncState, ReadableState, isReadableAsyncState } from 'awai';\nimport { useEffect, useState } from 'react';\n\nconst useStateValue = <T>(readable: ReadableState<T> | ReadableAsyncState<T>): T => {\n const [state, setState] = useState<T | undefined>(readable.get);\n\n if (isReadableAsyncState(readable) && state === undefined) {\n throw new Promise((resolve) => readable.events.changed.then(resolve));\n }\n\n useEffect(() => {\n let mounted = true;\n\n (async () => {\n while (mounted) {\n /**\n * @todo Cleanup on unmount\n * @url https://github.com/yuriyyakym/awai/issues/1\n */\n await readable.events.changed;\n if (mounted) {\n setState(readable.get());\n }\n }\n })();\n\n return () => {\n mounted = false;\n };\n }, [readable]);\n\n return state as T;\n};\n\nexport default useStateValue;\n","import type { AsyncSetter, AsyncState, Setter, State } from 'awai';\n\nimport useSetState from './useSetState';\nimport useStateValue from './useStateValue';\n\ntype Return<T> = T extends State<infer U>\n ? [U, Setter<U>]\n : T extends AsyncState<infer V>\n ? [V, AsyncSetter<V>]\n : never;\n\nfunction useState<T>(state: State<T>): Return<State<T>>;\nfunction useState<T>(state: AsyncState<T>): Return<AsyncState<T>>;\nfunction useState<T>(state: State<T> | AsyncState<T>) {\n const value = useStateValue(state);\n const setValue = useSetState(state);\n\n return [value, setValue];\n}\n\nexport default useState;\n"],"names":["useState","isReadableAsyncState","useEffect"],"mappings":";;;;;AAEM,MAAA,WAAA,GAAc,CAClB,QACa,KAAA;AACb,EAAA,OAAO,QAAS,CAAA,GAAA,CAAA;AAClB;;ACHM,MAAA,aAAA,GAAgB,CAAI,QAA0D,KAAA;AAClF,EAAA,MAAM,CAAC,KAAO,EAAA,QAAQ,CAAI,GAAAA,cAAA,CAAwB,SAAS,GAAG,CAAA,CAAA;AAE9D,EAAA,IAAIC,yBAAqB,CAAA,QAAQ,CAAK,IAAA,KAAA,KAAU,KAAW,CAAA,EAAA;AACzD,IAAM,MAAA,IAAI,QAAQ,CAAC,OAAA,KAAY,SAAS,MAAO,CAAA,OAAA,CAAQ,IAAK,CAAA,OAAO,CAAC,CAAA,CAAA;AAAA,GACtE;AAEA,EAAAC,eAAA,CAAU,MAAM;AACd,IAAA,IAAI,OAAU,GAAA,IAAA,CAAA;AAEd,IAAA,CAAC,YAAY;AACX,MAAA,OAAO,OAAS,EAAA;AAKd,QAAA,MAAM,SAAS,MAAO,CAAA,OAAA,CAAA;AACtB,QAAA,IAAI,OAAS,EAAA;AACX,UAAS,QAAA,CAAA,QAAA,CAAS,KAAK,CAAA,CAAA;AAAA,SACzB;AAAA,OACF;AAAA,KACC,GAAA,CAAA;AAEH,IAAA,OAAO,MAAM;AACX,MAAU,OAAA,GAAA,KAAA,CAAA;AAAA,KACZ,CAAA;AAAA,GACF,EAAG,CAAC,QAAQ,CAAC,CAAA,CAAA;AAEb,EAAO,OAAA,KAAA,CAAA;AACT;;ACnBA,SAAS,SAAY,KAAiC,EAAA;AACpD,EAAM,MAAA,KAAA,GAAQ,cAAc,KAAK,CAAA,CAAA;AACjC,EAAM,MAAA,QAAA,GAAW,YAAY,KAAK,CAAA,CAAA;AAElC,EAAO,OAAA,CAAC,OAAO,QAAQ,CAAA,CAAA;AACzB;;;;;;"}
package/dist/index.mjs ADDED
@@ -0,0 +1,37 @@
1
+ import { isReadableAsyncState } from 'awai';
2
+ import { useState as useState$1, useEffect } from 'react';
3
+
4
+ const useSetState = (writable) => {
5
+ return writable.set;
6
+ };
7
+
8
+ const useStateValue = (readable) => {
9
+ const [state, setState] = useState$1(readable.get);
10
+ if (isReadableAsyncState(readable) && state === void 0) {
11
+ throw new Promise((resolve) => readable.events.changed.then(resolve));
12
+ }
13
+ useEffect(() => {
14
+ let mounted = true;
15
+ (async () => {
16
+ while (mounted) {
17
+ await readable.events.changed;
18
+ if (mounted) {
19
+ setState(readable.get());
20
+ }
21
+ }
22
+ })();
23
+ return () => {
24
+ mounted = false;
25
+ };
26
+ }, [readable]);
27
+ return state;
28
+ };
29
+
30
+ function useState(state) {
31
+ const value = useStateValue(state);
32
+ const setValue = useSetState(state);
33
+ return [value, setValue];
34
+ }
35
+
36
+ export { useSetState, useState, useStateValue };
37
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":["../src/useSetState.ts","../src/useStateValue.ts","../src/useState.ts"],"sourcesContent":["import type { WritableAsyncState, WritableState } from 'awai';\n\nconst useSetState = <Q extends WritableState<T> | WritableAsyncState<T>, T = any>(\n writable: Q,\n): Q['set'] => {\n return writable.set;\n};\n\nexport default useSetState;\n","import { ReadableAsyncState, ReadableState, isReadableAsyncState } from 'awai';\nimport { useEffect, useState } from 'react';\n\nconst useStateValue = <T>(readable: ReadableState<T> | ReadableAsyncState<T>): T => {\n const [state, setState] = useState<T | undefined>(readable.get);\n\n if (isReadableAsyncState(readable) && state === undefined) {\n throw new Promise((resolve) => readable.events.changed.then(resolve));\n }\n\n useEffect(() => {\n let mounted = true;\n\n (async () => {\n while (mounted) {\n /**\n * @todo Cleanup on unmount\n * @url https://github.com/yuriyyakym/awai/issues/1\n */\n await readable.events.changed;\n if (mounted) {\n setState(readable.get());\n }\n }\n })();\n\n return () => {\n mounted = false;\n };\n }, [readable]);\n\n return state as T;\n};\n\nexport default useStateValue;\n","import type { AsyncSetter, AsyncState, Setter, State } from 'awai';\n\nimport useSetState from './useSetState';\nimport useStateValue from './useStateValue';\n\ntype Return<T> = T extends State<infer U>\n ? [U, Setter<U>]\n : T extends AsyncState<infer V>\n ? [V, AsyncSetter<V>]\n : never;\n\nfunction useState<T>(state: State<T>): Return<State<T>>;\nfunction useState<T>(state: AsyncState<T>): Return<AsyncState<T>>;\nfunction useState<T>(state: State<T> | AsyncState<T>) {\n const value = useStateValue(state);\n const setValue = useSetState(state);\n\n return [value, setValue];\n}\n\nexport default useState;\n"],"names":["useState"],"mappings":";;;AAEM,MAAA,WAAA,GAAc,CAClB,QACa,KAAA;AACb,EAAA,OAAO,QAAS,CAAA,GAAA,CAAA;AAClB;;ACHM,MAAA,aAAA,GAAgB,CAAI,QAA0D,KAAA;AAClF,EAAA,MAAM,CAAC,KAAO,EAAA,QAAQ,CAAI,GAAAA,UAAA,CAAwB,SAAS,GAAG,CAAA,CAAA;AAE9D,EAAA,IAAI,oBAAqB,CAAA,QAAQ,CAAK,IAAA,KAAA,KAAU,KAAW,CAAA,EAAA;AACzD,IAAM,MAAA,IAAI,QAAQ,CAAC,OAAA,KAAY,SAAS,MAAO,CAAA,OAAA,CAAQ,IAAK,CAAA,OAAO,CAAC,CAAA,CAAA;AAAA,GACtE;AAEA,EAAA,SAAA,CAAU,MAAM;AACd,IAAA,IAAI,OAAU,GAAA,IAAA,CAAA;AAEd,IAAA,CAAC,YAAY;AACX,MAAA,OAAO,OAAS,EAAA;AAKd,QAAA,MAAM,SAAS,MAAO,CAAA,OAAA,CAAA;AACtB,QAAA,IAAI,OAAS,EAAA;AACX,UAAS,QAAA,CAAA,QAAA,CAAS,KAAK,CAAA,CAAA;AAAA,SACzB;AAAA,OACF;AAAA,KACC,GAAA,CAAA;AAEH,IAAA,OAAO,MAAM;AACX,MAAU,OAAA,GAAA,KAAA,CAAA;AAAA,KACZ,CAAA;AAAA,GACF,EAAG,CAAC,QAAQ,CAAC,CAAA,CAAA;AAEb,EAAO,OAAA,KAAA,CAAA;AACT;;ACnBA,SAAS,SAAY,KAAiC,EAAA;AACpD,EAAM,MAAA,KAAA,GAAQ,cAAc,KAAK,CAAA,CAAA;AACjC,EAAM,MAAA,QAAA,GAAW,YAAY,KAAK,CAAA,CAAA;AAElC,EAAO,OAAA,CAAC,OAAO,QAAQ,CAAA,CAAA;AACzB;;;;"}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "awai-react",
3
+ "version": "0.0.0",
4
+ "author": "Yuriy Yakym",
5
+ "description": "React hooks for Awai integration",
6
+ "license": "MIT",
7
+ "main": "dist/index.js",
8
+ "module": "dist/index.mjs",
9
+ "typings": "dist/index.d.ts",
10
+ "type": "module",
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "scripts": {
15
+ "build": "rm -rf dist && rollup -c rollup.config.js"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/yuriyyakym/awai-react.git"
20
+ },
21
+ "bugs": {
22
+ "url": "https://github.com/yuriyyakym/awai-react/issues"
23
+ },
24
+ "keywords": [
25
+ "react",
26
+ "state",
27
+ "library",
28
+ "typescript",
29
+ "javascript",
30
+ "promise",
31
+ "async"
32
+ ],
33
+ "devDependencies": {
34
+ "@types/react": "^18.2.16",
35
+ "awai": "^0.0.1-alpha-3",
36
+ "react": "^18.2.0",
37
+ "rollup": "^3.26.3",
38
+ "rollup-plugin-dts": "^5.3.0",
39
+ "rollup-plugin-esbuild": "^5.0.0",
40
+ "typescript": "^5.1.6"
41
+ },
42
+ "peerDependencies": {
43
+ "awai": "*",
44
+ "react": "*"
45
+ }
46
+ }