autosuspense 1.0.0 → 1.0.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/README.md
CHANGED
|
@@ -3,6 +3,7 @@ AutoSuspense is a lightweight React utility that automatically composes Suspense
|
|
|
3
3
|
|
|
4
4
|
It lets you define fallback UI at the component level, while a parent ```<AutoSuspense>``` boundary handles rendering everything correctly.
|
|
5
5
|
|
|
6
|
+
---
|
|
6
7
|
## Why AutoSuspense?:
|
|
7
8
|
|
|
8
9
|
React Suspense solves async rendering, but fallback composition quickly becomes messy:
|
|
@@ -38,6 +39,7 @@ function Page() {
|
|
|
38
39
|
|
|
39
40
|
export default Suspend(Page, <div>Loading page...</div>);
|
|
40
41
|
```
|
|
42
|
+
---
|
|
41
43
|
|
|
42
44
|
## Installation:
|
|
43
45
|
```npm install autosuspense```
|
|
@@ -67,31 +69,76 @@ const UserCard = () => {
|
|
|
67
69
|
export default Suspend(UserCard, <div>Loading user...</div>);
|
|
68
70
|
```
|
|
69
71
|
|
|
70
|
-
|
|
72
|
+
- Nested components automatically compose
|
|
71
73
|
```
|
|
72
74
|
const Parent = () => <Child />;
|
|
73
75
|
|
|
74
|
-
export default Suspend(Parent, <div>
|
|
76
|
+
export default Suspend(Parent, <div>Outer Loader...</div>);
|
|
75
77
|
const Child = () => {
|
|
76
78
|
const data = resource.read();
|
|
77
79
|
return <div>{data}</div>;
|
|
78
80
|
};
|
|
79
81
|
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
|
|
80
85
|
export default Suspend(Child, <div>Inner Loader...</div>);
|
|
81
86
|
```
|
|
82
87
|
👉 Resulting fallback:
|
|
83
88
|
```
|
|
84
|
-
|
|
89
|
+
Outer Loader...
|
|
85
90
|
Inner Loader...
|
|
86
91
|
```
|
|
87
|
-
|
|
92
|
+
|
|
93
|
+
## Using Prefabs (Reusable Fallbacks)
|
|
94
|
+
Instead of passing JSX everywhere, define reusable fallbacks once.
|
|
95
|
+
|
|
96
|
+
- Define prefabs at the root
|
|
97
|
+
```
|
|
98
|
+
import { AutoSuspense } from "autosuspense";
|
|
99
|
+
import PageSkeleton from "./skeletons/PageSkeleton";
|
|
100
|
+
import CardSkeleton from "./skeletons/CardSkeleton";
|
|
101
|
+
|
|
102
|
+
function App() {
|
|
103
|
+
return (
|
|
104
|
+
<AutoSuspense
|
|
105
|
+
prefab={{
|
|
106
|
+
page: PageSkeleton,
|
|
107
|
+
card: CardSkeleton,
|
|
108
|
+
}}
|
|
109
|
+
>
|
|
110
|
+
<Page />
|
|
111
|
+
</AutoSuspense>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
🎯 Use prefab keys in components
|
|
115
|
+
import { Suspend } from "autosuspense";
|
|
116
|
+
|
|
117
|
+
function Page() {
|
|
118
|
+
return <Feed />;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export default Suspend(Page, "page");
|
|
122
|
+
function Feed() {
|
|
123
|
+
const data = resource.read();
|
|
124
|
+
return <div>{data}</div>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export default Suspend(Feed, "card");
|
|
128
|
+
```
|
|
129
|
+
- Resulting fallback UI
|
|
130
|
+
```
|
|
131
|
+
PageSkeleton
|
|
132
|
+
CardSkeleton
|
|
133
|
+
```
|
|
88
134
|
|
|
89
135
|
## Core Idea:
|
|
90
|
-
- Wrap a subtree with
|
|
136
|
+
- Wrap a subtree with ```<AutoSuspense>```.
|
|
91
137
|
- Wrap components with Suspend().
|
|
92
138
|
- Each component declares its fallback.
|
|
93
139
|
- AutoSuspense automatically composes the fallback UI tree without explicit maintaince and wiring.
|
|
94
140
|
|
|
141
|
+
---
|
|
95
142
|
## Fallback Options:
|
|
96
143
|
|
|
97
144
|
You can provide fallbacks in multiple ways:
|
|
@@ -99,8 +146,6 @@ You can provide fallbacks in multiple ways:
|
|
|
99
146
|
1. JSX element:
|
|
100
147
|
```
|
|
101
148
|
Suspend(Component, <Skeleton />);
|
|
102
|
-
Component
|
|
103
|
-
Suspend(Component, SkeletonComponent);
|
|
104
149
|
```
|
|
105
150
|
|
|
106
151
|
2. Component:
|
|
@@ -113,4 +158,6 @@ Suspend(Component, SkeletonComponent);
|
|
|
113
158
|
Suspend(Component, SkeletonComponent);
|
|
114
159
|
```
|
|
115
160
|
|
|
116
|
-
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
AutoSuspense aims to make and enhance fallback creation and make it's maintainence easier.
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@ import { Block } from "./prebuilt/Block";
|
|
|
3
3
|
import { Card } from "./prebuilt/Card";
|
|
4
4
|
import { List } from "./prebuilt/List";
|
|
5
5
|
|
|
6
|
-
export const
|
|
6
|
+
export const defaultFallbacks: Record<string, React.ReactElement> = {
|
|
7
7
|
block: <Block />,
|
|
8
8
|
card: <Card />,
|
|
9
9
|
list: <List />,
|
|
@@ -5,18 +5,18 @@ import {
|
|
|
5
5
|
FallbackContext,
|
|
6
6
|
FallbackRegistry,
|
|
7
7
|
} from "../../types/FallbackRegistry";
|
|
8
|
-
import {
|
|
8
|
+
import { defaultFallbacks } from "../prefabs/defaultFallbacks";
|
|
9
9
|
|
|
10
10
|
export const AutoSuspense = ({
|
|
11
11
|
children,
|
|
12
|
-
|
|
12
|
+
fallbacks = defaultFallbacks,
|
|
13
13
|
}: {
|
|
14
14
|
children: React.ReactNode;
|
|
15
|
-
|
|
15
|
+
fallbacks?: Record<string, React.ReactElement | React.ComponentType<any>>;
|
|
16
16
|
}) => {
|
|
17
17
|
const registryRef = React.useRef<FallbackRegistry>({
|
|
18
18
|
nodes: new Map(),
|
|
19
|
-
prebuild: new Map(Object.entries(
|
|
19
|
+
prebuild: new Map(Object.entries(fallbacks)),
|
|
20
20
|
});
|
|
21
21
|
|
|
22
22
|
const fallback = <GeneratedFallback registry={registryRef.current} />;
|