Skip to content

Commit f1e610c

Browse files
Removed implicit use of iterators from internals
1 parent 348677c commit f1e610c

File tree

8 files changed

+38
-30
lines changed

8 files changed

+38
-30
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.6",
3+
"version": "0.2.7",
44
"description": "Suspense integration library for React",
55
"repository": "github:patrickroberts/suspense-service",
66
"main": "dst/cjs/suspense-service.js",

src/Service/Consumer/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ export function createServiceConsumer<TRequest, TResponse>(
1515
): ServiceConsumer<TRequest, TResponse> {
1616
const ResourceConsumer: ServiceConsumer<TRequest, TResponse> = ({ id, children }) => {
1717
const render = useCallback((
18-
[resource, setState]: [Resource<TResponse>, Dispatch<SetStateAction<TRequest>>],
18+
resourceAndSetState: [Resource<TResponse>, Dispatch<SetStateAction<TRequest>>],
1919
) => {
20+
const resource = resourceAndSetState[0];
21+
const setState = resourceAndSetState[1];
2022
const response = resource();
2123

2224
return children(response, setState);

src/Service/Provider/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { ComponentType, Dispatch, SetStateAction, Suspense, isValidElement, memo, useMemo } from 'react';
1+
import React, { ComponentType, Dispatch, SetStateAction, Suspense, memo, useMemo } from 'react';
22
import Id from '../../IdContext/Id';
33
import IdContext from '../../IdContext';
44
import useResetState from '../../State/useResetState';
@@ -18,10 +18,12 @@ export function createServiceProvider<TRequest, TResponse>(
1818
const ResourceProvider: ServiceProvider<TRequest> = ({
1919
request, id, children, fallback, reset,
2020
}) => {
21-
const [state, setState] = useResetState(request, reset);
21+
const stateAndSetState = useResetState(request, reset);
22+
const state = stateAndSetState[0];
23+
const setState = stateAndSetState[1];
2224
const resource = useHandler(state, id);
2325
const element = useMemo(() => (
24-
isValidElement(fallback)
26+
fallback != null
2527
? <Suspense fallback={fallback}>{children}</Suspense>
2628
: children
2729
), [children, fallback]);

src/Service/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ export function createService<TRequest, TResponse>(
5353
export function useServiceState<TRequest, TResponse>(
5454
service: Service<TRequest, TResponse>, id: Id = null,
5555
): [TResponse, Dispatch<SetStateAction<TRequest>>] {
56-
const [resource, setState] = useIdContext(service[kResource], id);
56+
const resourceAndSetState = useIdContext(service[kResource], id);
57+
const resource = resourceAndSetState[0];
58+
const setState = resourceAndSetState[1];
5759
const response = resource();
5860

5961
return [response, setState];
@@ -65,7 +67,5 @@ export function useServiceState<TRequest, TResponse>(
6567
* @param id the {@link ServiceProviderProps.id | ServiceProvider id} to use
6668
*/
6769
export function useService<TResponse>(service: Service<any, TResponse>, id: Id = null): TResponse {
68-
const [response] = useServiceState(service, id);
69-
70-
return response;
70+
return useServiceState(service, id)[0];
7171
}

src/State/is.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/State/useForceUpdate.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ import { DispatchWithoutAction, useReducer } from 'react';
22

33
/** @ignore */
44
export default function useForceUpdate(): DispatchWithoutAction {
5-
const [, dispatch] = useReducer(() => ({}), {});
6-
return dispatch;
5+
return useReducer(() => ({}), {})[1];
76
}

src/State/useSync.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
import { DependencyList, useRef } from 'react';
2-
import is from './is';
2+
3+
const intialValue: [any, DependencyList | undefined] = [undefined, undefined];
4+
5+
/** @ignore */
6+
function dependencyListIs(a: DependencyList | undefined, b: DependencyList | undefined): boolean {
7+
if (!a || !b || a.length !== b.length) return false;
8+
9+
for (let i = 0; i < a.length; ++i) {
10+
if (!Object.is(a[i], b[i])) return false;
11+
}
12+
13+
return true;
14+
}
315

416
/**
517
* @ignore
618
* An alternative to React useMemo that provides a semantic guarantee of referential stability.
19+
* This allows synchronous effects to be invoked safely during the render phase.
720
* @param factory The factory function to compute a referentially stable value
821
* @param deps The dependencies of the computation
922
*/
1023
export default function useSync<T>(factory: () => T, deps: DependencyList | undefined): T {
11-
const { current } = useRef<[T?, DependencyList?]>([]);
24+
const ref = useRef<[T, DependencyList | undefined]>(intialValue);
1225

13-
if (!is(current[1], deps)) {
14-
current[0] = factory();
15-
current[1] = deps;
26+
if (!dependencyListIs(ref.current[1], deps)) {
27+
ref.current = [factory(), deps];
1628
}
1729

18-
return current[0]!;
30+
return ref.current[0];
1931
}

src/StateContext/Consumer/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ export function createStateContextConsumer<T>(
1414
): StateContextConsumer<T> {
1515
const StateConsumer: StateContextConsumer<T> = ({ id, children }) => {
1616
const render = useCallback(
17-
([state, setState]: State<T>) => children(state, setState),
17+
(stateAndSetState: State<T>) => {
18+
const state = stateAndSetState[0];
19+
const setState = stateAndSetState[1];
20+
21+
return children(state, setState);
22+
},
1823
[children],
1924
);
2025

0 commit comments

Comments
 (0)