Skip to content

Commit

Permalink
fix(executors): fall back into "loading" state if arguments change
Browse files Browse the repository at this point in the history
  • Loading branch information
pmelab committed Nov 7, 2024
1 parent 5f48d28 commit 2acc857
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/npm/@amazeelabs/executors/src/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ function DelayedOperation<T extends any>({
const [error, setError] = useState<unknown>();

useEffect(() => {
setState('loading');
operation()
.then((result) => {
setData(result);
Expand All @@ -101,7 +102,7 @@ function DelayedOperation<T extends any>({
setError(error);
setState('error');
});
}, [operation]);
}, [operation, setData, setError, setState]);

return <>{children({ state, error, data: data! })}</>;
}
Expand Down
30 changes: 24 additions & 6 deletions packages/npm/@amazeelabs/executors/test/src/add-client.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
'use client';
import { useEffect, useState } from 'react';

import { Operation, OperationExecutorsProvider } from '../../src/client.js';
import { TestComponent } from './add.js';
import { Calc, DelayedAdd, TestComponent } from './add.js';

function CountUp() {
const [count, setCount] = useState(0);
useEffect(() => {
const interval = setInterval(() => {
setCount(count + 1);
}, 3000);
return () => clearInterval(interval);
}, [count, setCount]);
return <Calc label={'Count'} a={count} b={0} Operation={Operation} />;
}

export const Add = () => (
<TestComponent
label={'Client'}
OperationExecutorsProvider={OperationExecutorsProvider}
Operation={Operation}
/>
<>
<TestComponent
label={'Client'}
OperationExecutorsProvider={OperationExecutorsProvider}
Operation={Operation}
/>
<OperationExecutorsProvider executors={[DelayedAdd]}>
<CountUp />
</OperationExecutorsProvider>
</>
);
6 changes: 2 additions & 4 deletions packages/npm/@amazeelabs/executors/test/src/add.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,13 @@ export function Calc({
return (
<Operation id={AddOperation} variables={{ a, b }}>
{(props) => {
if (props.state === 'loading') {
return <p data-testid={label}>Loading...</p>;
}
if (props.state === 'error') {
return <p data-testid={label}>Error: {`${props.error}`}</p>;
}
return (
<p data-testid={label}>
{label}: {a} + {b} = {props.data.result}
{label}: {a} + {b} ={' '}
{props.state === 'loading' ? '...' : props.data.result}
</p>
);
}}
Expand Down

0 comments on commit 2acc857

Please sign in to comment.