Skip to content

Commit 7a4c69a

Browse files
Reduced bundle size and improved README
1 parent 420d979 commit 7a4c69a

File tree

22 files changed

+336
-176
lines changed

22 files changed

+336
-176
lines changed

README.md

Lines changed: 216 additions & 77 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "suspense-service",
3-
"version": "0.2.8",
3+
"version": "0.2.9",
44
"description": "Suspense integration library for React",
55
"repository": "github:patrickroberts/suspense-service",
66
"main": "dst/cjs/suspense-service.js",

src/IdContext/Consumer/index.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function createIdContextConsumer<T>(
1313
): IdContextConsumer<T> {
1414
const EnvironmentConsumer: IdContextConsumer<T> = ({ id, children }) => {
1515
const render = useCallback(
16-
(env: Environment<T>) => children(unwrap(env, id)),
16+
(env: Environment<T>) => children(unwrap(env, id!)),
1717
[id, children],
1818
);
1919

@@ -25,8 +25,5 @@ export function createIdContextConsumer<T>(
2525

2626
EnvironmentConsumer.defaultProps = defaultProps;
2727

28-
return memo(EnvironmentConsumer, (prev, next) => (
29-
Object.is(prev.id, next.id)
30-
&& Object.is(prev.children, next.children)
31-
));
28+
return memo(EnvironmentConsumer);
3229
}

src/IdContext/Environment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ export default interface Environment<T> {
88
}
99

1010
/** @ignore */
11-
export function wrap<T>(env: Environment<T>, value: T, id: Id = null): Environment<T> {
11+
export function wrap<T>(env: Environment<T>, value: T, id: Id): Environment<T> {
1212
return new Map(env).set(id, value).set(null, value);
1313
}
1414

1515
/** @ignore */
16-
export function unwrap<T>(env: Environment<T>, id: Id = null): T {
16+
export function unwrap<T>(env: Environment<T>, id: Id): T {
1717
if (!env.has(id)) {
1818
throw new Error(`Provider with id ${String(id)} is not in scope`);
1919
}

src/IdContext/Provider/index.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function createIdContextProvider<T>(
1515
const EnvironmentProvider: IdContextProvider<T> = ({ value, id, children }) => {
1616
const prev = useContext(EnvironmentContext);
1717
const next = useMemo(
18-
() => wrap(prev, value, id),
18+
() => wrap(prev, value, id!),
1919
[value, id, prev],
2020
);
2121

@@ -27,9 +27,5 @@ export function createIdContextProvider<T>(
2727

2828
EnvironmentProvider.defaultProps = defaultProps;
2929

30-
return memo(EnvironmentProvider, (prev, next) => (
31-
Object.is(prev.value, next.value)
32-
&& Object.is(prev.id, next.id)
33-
&& Object.is(prev.children, next.children)
34-
));
30+
return memo(EnvironmentProvider);
3531
}

src/IdContext/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export function createIdContext<T>(defaultValue: T): IdContext<T> {
4242
* @param context the {@link IdContext} to use
4343
* @param id the {@link IdContextProviderProps.id | IdContextProvider id} to use
4444
*/
45-
export function useIdContext<T>(context: IdContext<T>, id: Id = null): T {
45+
export function useIdContext<T>(context: IdContext<T>, id?: Id): T {
4646
const env = useContext(context[kEnvironment]);
4747

48-
return unwrap(env, id);
48+
return unwrap(env, id != null ? id : null);
4949
}

src/Service/Consumer/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,5 @@ export function createServiceConsumer<TRequest, TResponse>(
2727

2828
ResourceConsumer.defaultProps = defaultProps;
2929

30-
return memo(ResourceConsumer, (prev, next) => (
31-
Object.is(prev.id, next.id)
32-
&& Object.is(prev.children, next.children)
33-
));
30+
return memo(ResourceConsumer);
3431
}

src/Service/Handler.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import Id from '../IdContext/Id';
22
import useSync from '../State/useSync';
3-
import PromiseState from './PromiseState';
3+
import PromiseState, { PromiseStateProperty, StatusType } from './PromiseState';
44
import Resource from './Resource';
5-
import Status from './Status';
65

76
/**
87
* The type of asynchronous function for fetching data
@@ -15,27 +14,27 @@ export default Handler;
1514
export function createUseHandler<TRequest, TResponse>(
1615
handler: Handler<TRequest, TResponse>,
1716
) {
18-
return function useHandler(request: TRequest, id: Id = null): Resource<TResponse> {
17+
return function useHandler(request: TRequest, id: Id): Resource<TResponse> {
1918
return useSync(() => {
2019
let state: PromiseState<TResponse>;
2120
const promise = Promise.resolve(handler(request, id)).then(
2221
(value) => {
23-
state = { value, status: Status.Fulfilled };
22+
state = [StatusType.Fulfilled, value];
2423
return value;
2524
},
2625
(reason) => {
27-
state = { reason, status: Status.Rejected };
26+
state = [StatusType.Rejected, reason];
2827
throw reason;
2928
},
3029
);
3130

32-
state = { promise, status: Status.Pending };
31+
state = [StatusType.Pending, promise];
3332
return () => {
34-
switch (state.status) {
35-
case Status.Pending: throw state.promise;
36-
case Status.Fulfilled: return state.value;
37-
default: throw state.reason;
33+
if (state[PromiseStateProperty.Status] !== StatusType.Fulfilled) {
34+
throw state[PromiseStateProperty.Result];
3835
}
36+
37+
return state[PromiseStateProperty.Result];
3938
};
4039
}, [request, id]);
4140
};

src/Service/PromiseState.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1-
import Status from './Status';
1+
/** @ignore */
2+
const enum PromiseStateProperty {
3+
Status,
4+
Result,
5+
}
6+
7+
export { PromiseStateProperty };
8+
9+
/** @ignore */
10+
const enum StatusType {
11+
Pending,
12+
Fulfilled,
13+
Rejected,
14+
}
15+
16+
export { StatusType };
217

318
/** @ignore */
419
interface PromiseStatePending<TResponse> {
5-
promise: Promise<TResponse>;
6-
status: Status.Pending;
20+
[PromiseStateProperty.Status]: StatusType.Pending;
21+
[PromiseStateProperty.Result]: Promise<TResponse>;
722
}
823

924
/** @ignore */
1025
interface PromiseStateFulfilled<TResponse> {
11-
value: TResponse;
12-
status: Status.Fulfilled;
26+
[PromiseStateProperty.Status]: StatusType.Fulfilled;
27+
[PromiseStateProperty.Result]: TResponse;
1328
}
1429

1530
/** @ignore */
1631
interface PromiseStateRejected {
17-
reason: any;
18-
status: Status.Rejected;
32+
[PromiseStateProperty.Status]: StatusType.Rejected;
33+
[PromiseStateProperty.Result]: any;
1934
}
2035

2136
/** @ignore */

src/Service/Provider/index.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ export { ServiceProviderProps };
1313
/** @ignore */
1414
export function createServiceProvider<TRequest, TResponse>(
1515
{ Provider }: IdContext<[Resource<TResponse>, Dispatch<SetStateAction<TRequest>>]>,
16-
useHandler: (request: TRequest, id?: Id) => Resource<TResponse>,
16+
useHandler: (request: TRequest, id: Id) => Resource<TResponse>,
1717
): ServiceProvider<TRequest> {
1818
const ResourceProvider: ServiceProvider<TRequest> = ({
1919
request, id, children, fallback, reset,
2020
}) => {
21-
const stateAndSetState = useResetState(request, reset);
22-
const resource = useHandler(stateAndSetState[0], id);
23-
const setState = stateAndSetState[1];
21+
const { 0: state, 1: setState } = useResetState(request, reset);
22+
const resource = useHandler(state, id!);
2423
const element = useMemo(
2524
() => (fallback == null ? children : <Suspense fallback={fallback}>{children}</Suspense>),
2625
[children, fallback],
@@ -34,11 +33,5 @@ export function createServiceProvider<TRequest, TResponse>(
3433

3534
ResourceProvider.defaultProps = defaultProps;
3635

37-
return memo(ResourceProvider, (prev, next) => (
38-
Object.is(prev.request, next.request)
39-
&& Object.is(prev.id, next.id)
40-
&& Object.is(prev.children, next.children)
41-
&& Object.is(prev.fallback, next.fallback)
42-
&& Object.is(prev.reset, next.reset)
43-
));
36+
return memo(ResourceProvider);
4437
}

0 commit comments

Comments
 (0)