rxfy-react 0.2.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 +36 -0
- package/dist/index.cjs +58 -0
- package/dist/index.d.cts +32 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +32 -0
- package/package.json +64 -0
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# rxfy-react
|
|
2
|
+
|
|
3
|
+
`rxfy-react` — official bindings of `rxfy` for `react`
|
|
4
|
+
|
|
5
|
+
# install
|
|
6
|
+
|
|
7
|
+
`pnpm install rxfy-react`
|
|
8
|
+
|
|
9
|
+
# api
|
|
10
|
+
|
|
11
|
+
- useEdge — use edge state
|
|
12
|
+
- <Edge /> — useEdge wrapped with render props pattern
|
|
13
|
+
|
|
14
|
+
# example
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import PQueue from "p-queue";
|
|
18
|
+
import { useMemo } from "react";
|
|
19
|
+
import { of } from "rxjs";
|
|
20
|
+
import { createAtom, createState, createStore } from "rxfy";
|
|
21
|
+
import { Edge } from "rxfy-react";
|
|
22
|
+
|
|
23
|
+
const queue = new PQueue({ concurrency: 5 });
|
|
24
|
+
const state = createAtom(createState({}));
|
|
25
|
+
const store = createStore(queue, state);
|
|
26
|
+
const userStore = store.factory("users", (id) => of({ id }));
|
|
27
|
+
|
|
28
|
+
export function User({ userId }: { userId: string }) {
|
|
29
|
+
const user = useMemo(() => userStore.get(userId), [userId]);
|
|
30
|
+
return (
|
|
31
|
+
<Edge edge={user} pending={<span>Loading..</span>} rejected={(err) => <span>{JSON.stringify(err)}</span>}>
|
|
32
|
+
{(user) => <div key={user.id}>{user.id}</div>}
|
|
33
|
+
</Edge>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.tsx
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
Edge: () => Edge,
|
|
24
|
+
useEdge: () => useEdge
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(src_exports);
|
|
27
|
+
var import_rxfy = require("rxfy");
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
function useEdge(edge) {
|
|
30
|
+
const [state, setState] = (0, import_react.useState)(edge.subject$.get());
|
|
31
|
+
(0, import_react.useEffect)(() => {
|
|
32
|
+
const sub = edge.subject$.subscribe((x) => setState(x));
|
|
33
|
+
return () => sub.unsubscribe();
|
|
34
|
+
}, [edge]);
|
|
35
|
+
return state;
|
|
36
|
+
}
|
|
37
|
+
function Edge({ edge, children, rejected = null, pending = null }) {
|
|
38
|
+
const state = useEdge(edge);
|
|
39
|
+
switch (state.type) {
|
|
40
|
+
case import_rxfy.StatusEnum.REJECTED:
|
|
41
|
+
return renderWithParams(rejected, state.error);
|
|
42
|
+
case import_rxfy.StatusEnum.FULFILLED:
|
|
43
|
+
return renderWithParams(children, state.value);
|
|
44
|
+
default:
|
|
45
|
+
return pending;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function renderWithParams(fn, data) {
|
|
49
|
+
if (typeof fn === "function") {
|
|
50
|
+
return fn(data);
|
|
51
|
+
}
|
|
52
|
+
return fn;
|
|
53
|
+
}
|
|
54
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
55
|
+
0 && (module.exports = {
|
|
56
|
+
Edge,
|
|
57
|
+
useEdge
|
|
58
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { IEdge, StatusEnum } from 'rxfy';
|
|
3
|
+
|
|
4
|
+
declare function useEdge<TData>(edge: IEdge<TData>): ({
|
|
5
|
+
type: StatusEnum.IDLE;
|
|
6
|
+
} & {
|
|
7
|
+
type: StatusEnum;
|
|
8
|
+
}) | ({
|
|
9
|
+
type: StatusEnum.PENDING;
|
|
10
|
+
} & {
|
|
11
|
+
type: StatusEnum;
|
|
12
|
+
}) | ({
|
|
13
|
+
type: StatusEnum.REJECTED;
|
|
14
|
+
error: unknown;
|
|
15
|
+
} & {
|
|
16
|
+
type: StatusEnum;
|
|
17
|
+
}) | ({
|
|
18
|
+
type: StatusEnum.FULFILLED;
|
|
19
|
+
value: TData;
|
|
20
|
+
} & {
|
|
21
|
+
type: StatusEnum;
|
|
22
|
+
});
|
|
23
|
+
type IEdgeProps<TData> = {
|
|
24
|
+
edge: IEdge<TData>;
|
|
25
|
+
children: IRenderFn<TData>;
|
|
26
|
+
rejected?: IRenderFn<unknown>;
|
|
27
|
+
pending?: React.ReactNode;
|
|
28
|
+
};
|
|
29
|
+
declare function Edge<TData>({ edge, children, rejected, pending }: IEdgeProps<TData>): react.ReactNode;
|
|
30
|
+
type IRenderFn<TData> = React.ReactNode | ((data: TData) => React.ReactNode);
|
|
31
|
+
|
|
32
|
+
export { Edge, type IRenderFn, useEdge };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { IEdge, StatusEnum } from 'rxfy';
|
|
3
|
+
|
|
4
|
+
declare function useEdge<TData>(edge: IEdge<TData>): ({
|
|
5
|
+
type: StatusEnum.IDLE;
|
|
6
|
+
} & {
|
|
7
|
+
type: StatusEnum;
|
|
8
|
+
}) | ({
|
|
9
|
+
type: StatusEnum.PENDING;
|
|
10
|
+
} & {
|
|
11
|
+
type: StatusEnum;
|
|
12
|
+
}) | ({
|
|
13
|
+
type: StatusEnum.REJECTED;
|
|
14
|
+
error: unknown;
|
|
15
|
+
} & {
|
|
16
|
+
type: StatusEnum;
|
|
17
|
+
}) | ({
|
|
18
|
+
type: StatusEnum.FULFILLED;
|
|
19
|
+
value: TData;
|
|
20
|
+
} & {
|
|
21
|
+
type: StatusEnum;
|
|
22
|
+
});
|
|
23
|
+
type IEdgeProps<TData> = {
|
|
24
|
+
edge: IEdge<TData>;
|
|
25
|
+
children: IRenderFn<TData>;
|
|
26
|
+
rejected?: IRenderFn<unknown>;
|
|
27
|
+
pending?: React.ReactNode;
|
|
28
|
+
};
|
|
29
|
+
declare function Edge<TData>({ edge, children, rejected, pending }: IEdgeProps<TData>): react.ReactNode;
|
|
30
|
+
type IRenderFn<TData> = React.ReactNode | ((data: TData) => React.ReactNode);
|
|
31
|
+
|
|
32
|
+
export { Edge, type IRenderFn, useEdge };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// src/index.tsx
|
|
2
|
+
import { StatusEnum } from "rxfy";
|
|
3
|
+
import { useEffect, useState } from "react";
|
|
4
|
+
function useEdge(edge) {
|
|
5
|
+
const [state, setState] = useState(edge.subject$.get());
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
const sub = edge.subject$.subscribe((x) => setState(x));
|
|
8
|
+
return () => sub.unsubscribe();
|
|
9
|
+
}, [edge]);
|
|
10
|
+
return state;
|
|
11
|
+
}
|
|
12
|
+
function Edge({ edge, children, rejected = null, pending = null }) {
|
|
13
|
+
const state = useEdge(edge);
|
|
14
|
+
switch (state.type) {
|
|
15
|
+
case StatusEnum.REJECTED:
|
|
16
|
+
return renderWithParams(rejected, state.error);
|
|
17
|
+
case StatusEnum.FULFILLED:
|
|
18
|
+
return renderWithParams(children, state.value);
|
|
19
|
+
default:
|
|
20
|
+
return pending;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function renderWithParams(fn, data) {
|
|
24
|
+
if (typeof fn === "function") {
|
|
25
|
+
return fn(data);
|
|
26
|
+
}
|
|
27
|
+
return fn;
|
|
28
|
+
}
|
|
29
|
+
export {
|
|
30
|
+
Edge,
|
|
31
|
+
useEdge
|
|
32
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "rxfy-react",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "rxfy bindings for react",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.cjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"module": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"package.json"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@vanya2h/utils": "0.1.2"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
26
|
+
"@testing-library/react": "^16.2.0",
|
|
27
|
+
"@types/lodash": "^4.17.16",
|
|
28
|
+
"@types/react": "^19.0.12",
|
|
29
|
+
"eslint": "^9.21.0",
|
|
30
|
+
"immutable": "^5.1.1",
|
|
31
|
+
"jiti": "^2.4.2",
|
|
32
|
+
"jsdom": "^26.0.0",
|
|
33
|
+
"lodash": "^4.17.21",
|
|
34
|
+
"p-queue": "^8.1.0",
|
|
35
|
+
"react": "^19.1.0",
|
|
36
|
+
"react-dom": "^19.1.0",
|
|
37
|
+
"rimraf": "^6.0.1",
|
|
38
|
+
"rxjs": "^7.8.2",
|
|
39
|
+
"tsup": "^8.3.6",
|
|
40
|
+
"tsx": "^4.19.2",
|
|
41
|
+
"vitest": "^3.0.5",
|
|
42
|
+
"zod": "^3.24.2",
|
|
43
|
+
"@vanya2h/typescript-config": "0.2.1",
|
|
44
|
+
"@vanya2h/eslint-config": "0.2.1",
|
|
45
|
+
"rxfy": "0.2.1"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@types/react": "^18.0.0 || ^19.0.0",
|
|
49
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
50
|
+
"rxfy": "0.2.1"
|
|
51
|
+
},
|
|
52
|
+
"publishConfig": {
|
|
53
|
+
"access": "public",
|
|
54
|
+
"registry": "https://registry.npmjs.org"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "tsup",
|
|
58
|
+
"check-types": "tsc --noEmit",
|
|
59
|
+
"clean": "rimraf ./dist",
|
|
60
|
+
"dev": "tsup --watch --silent",
|
|
61
|
+
"lint": "eslint .",
|
|
62
|
+
"test": "vitest run --passWithNoTests"
|
|
63
|
+
}
|
|
64
|
+
}
|