Skip to content

Commit 420d979

Browse files
useResetReducer bugfix and format for minification
1 parent f1e610c commit 420d979

File tree

13 files changed

+55
-69
lines changed

13 files changed

+55
-69
lines changed

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.7",
3+
"version": "0.2.8",
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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ export function createIdContextConsumer<T>(
1212
{ Consumer }: Context<Environment<T>>,
1313
): IdContextConsumer<T> {
1414
const EnvironmentConsumer: IdContextConsumer<T> = ({ id, children }) => {
15-
const render = useCallback((env: Environment<T>) => {
16-
const value = unwrap(env, id);
15+
const render = useCallback(
16+
(env: Environment<T>) => children(unwrap(env, id)),
17+
[id, children],
18+
);
1719

18-
return children(value);
19-
}, [id, children]);
20-
21-
return useMemo(() => (
22-
<Consumer>{render}</Consumer>
23-
), [render]);
20+
return useMemo(
21+
() => <Consumer>{render}</Consumer>,
22+
[render],
23+
);
2424
};
2525

2626
EnvironmentConsumer.defaultProps = defaultProps;

src/IdContext/Environment.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,18 @@ import Id from './Id';
22

33
/** @internal */
44
export default interface Environment<T> {
5-
has (key: Id): boolean;
6-
get (key: Id): T | undefined;
7-
[Symbol.iterator] (): IterableIterator<[Id, T]>;
5+
has(key: Id): boolean;
6+
get(key: Id): T | undefined;
7+
[Symbol.iterator](): IterableIterator<[Id, T]>;
88
}
99

1010
/** @ignore */
11-
export function wrap<T>(
12-
env: Environment<T>,
13-
value: T,
14-
id: Id = null,
15-
): Environment<T> {
11+
export function wrap<T>(env: Environment<T>, value: T, id: Id = null): Environment<T> {
1612
return new Map(env).set(id, value).set(null, value);
1713
}
1814

1915
/** @ignore */
20-
export function unwrap<T>(
21-
env: Environment<T>,
22-
id: Id = null,
23-
): T {
16+
export function unwrap<T>(env: Environment<T>, id: Id = null): T {
2417
if (!env.has(id)) {
2518
throw new Error(`Provider with id ${String(id)} is not in scope`);
2619
}

src/IdContext/Provider/index.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ export function createIdContextProvider<T>(
1414
const { Provider } = EnvironmentContext;
1515
const EnvironmentProvider: IdContextProvider<T> = ({ value, id, children }) => {
1616
const prev = useContext(EnvironmentContext);
17-
const next = useMemo(() => (
18-
wrap(prev, value, id)
19-
), [value, id, prev]);
17+
const next = useMemo(
18+
() => wrap(prev, value, id),
19+
[value, id, prev],
20+
);
2021

21-
return useMemo(() => (
22-
<Provider value={next}>{children}</Provider>
23-
), [children, next]);
22+
return useMemo(
23+
() => <Provider value={next}>{children}</Provider>,
24+
[children, next],
25+
);
2426
};
2527

2628
EnvironmentProvider.defaultProps = defaultProps;

src/Service/Consumer/index.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@ export function createServiceConsumer<TRequest, TResponse>(
1616
const ResourceConsumer: ServiceConsumer<TRequest, TResponse> = ({ id, children }) => {
1717
const render = useCallback((
1818
resourceAndSetState: [Resource<TResponse>, Dispatch<SetStateAction<TRequest>>],
19-
) => {
20-
const resource = resourceAndSetState[0];
21-
const setState = resourceAndSetState[1];
22-
const response = resource();
19+
) => children(resourceAndSetState[0](), resourceAndSetState[1]),
20+
[children]);
2321

24-
return children(response, setState);
25-
}, [children]);
26-
27-
return useMemo(() => (
28-
<Consumer id={id}>{render}</Consumer>
29-
), [id, render]);
22+
return useMemo(
23+
() => <Consumer id={id}>{render}</Consumer>,
24+
[id, render],
25+
);
3026
};
3127

3228
ResourceConsumer.defaultProps = defaultProps;

src/Service/Handler.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@ export function createUseHandler<TRequest, TResponse>(
3131

3232
state = { promise, status: Status.Pending };
3333
return () => {
34-
const { status } = state;
35-
3634
switch (state.status) {
3735
case Status.Pending: throw state.promise;
3836
case Status.Fulfilled: return state.value;
39-
case Status.Rejected: throw state.reason;
40-
default: throw new Error(`Unexpected status ${status}`);
37+
default: throw state.reason;
4138
}
4239
};
4340
}, [request, id]);

src/Service/Provider/index.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,17 @@ export function createServiceProvider<TRequest, TResponse>(
1919
request, id, children, fallback, reset,
2020
}) => {
2121
const stateAndSetState = useResetState(request, reset);
22-
const state = stateAndSetState[0];
22+
const resource = useHandler(stateAndSetState[0], id);
2323
const setState = stateAndSetState[1];
24-
const resource = useHandler(state, id);
25-
const element = useMemo(() => (
26-
fallback != null
27-
? <Suspense fallback={fallback}>{children}</Suspense>
28-
: children
29-
), [children, fallback]);
24+
const element = useMemo(
25+
() => (fallback == null ? children : <Suspense fallback={fallback}>{children}</Suspense>),
26+
[children, fallback],
27+
);
3028

31-
return useMemo(() => (
32-
<Provider value={[resource, setState]} id={id}>{element}</Provider>
33-
), [resource, setState, id, element]);
29+
return useMemo(
30+
() => <Provider value={[resource, setState]} id={id}>{element}</Provider>,
31+
[resource, setState, id, element],
32+
);
3433
};
3534

3635
ResourceProvider.defaultProps = defaultProps;

src/Service/Status.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const enum Status {
33
Pending,
44
Fulfilled,
5-
Rejected
5+
Rejected,
66
}
77

88
export default Status;

src/Service/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function createService<TRequest, TResponse>(
3535
handler: Handler<TRequest, TResponse>,
3636
): Service<TRequest, TResponse> {
3737
const ResourceContext = createIdContext<[
38-
Resource<TResponse>, Dispatch<SetStateAction<TRequest>>
38+
Resource<TResponse>, Dispatch<SetStateAction<TRequest>>,
3939
]>(defaultValue);
4040

4141
return {
@@ -54,11 +54,8 @@ export function useServiceState<TRequest, TResponse>(
5454
service: Service<TRequest, TResponse>, id: Id = null,
5555
): [TResponse, Dispatch<SetStateAction<TRequest>>] {
5656
const resourceAndSetState = useIdContext(service[kResource], id);
57-
const resource = resourceAndSetState[0];
58-
const setState = resourceAndSetState[1];
59-
const response = resource();
6057

61-
return [response, setState];
58+
return [resourceAndSetState[0](), resourceAndSetState[1]];
6259
}
6360

6461
/**

src/State/useResetReducer.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ export default function useResetReducer<R extends Reducer<any, any>>(
2020
initialState,
2121
state: initialState,
2222
dispatch: (action: ReducerAction<R>) => {
23-
current.state = current.reducer(current.state, action);
24-
forceUpdate();
23+
const nextState = current.reducer(current.state, action);
24+
25+
if (!Object.is(current.state, nextState)) {
26+
current.state = nextState;
27+
forceUpdate();
28+
}
2529
},
2630
});
2731

0 commit comments

Comments
 (0)