Skip to content

Commit

Permalink
feat: effect observable performance (#158)
Browse files Browse the repository at this point in the history
feat: effect observable performance
  • Loading branch information
foreleven authored Apr 14, 2020
1 parent 999e730 commit c569dc5
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stated-bean",
"version": "0.9.0",
"version": "0.9.1",
"description": "A light but scalable state management library with react hooks",
"repository": "[email protected]:mjolnirjs/stated-bean.git",
"license": "MIT",
Expand Down
10 changes: 3 additions & 7 deletions src/decorator/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,16 @@ export function Effect(): MethodDecorator {
if (isPromise(result)) {
return result
.then(data => {
emitEffectAction({ data: data as T });
emitEffectAction({ loading: false, error: null, data: data as T });
return data;
})
.catch((e: unknown) => {
emitEffectAction({ loading: false, error: e });
emitEffectAction({ loading: false, error: e, data: undefined });
throw e;
})
.finally(() => {
emitEffectAction({ loading: false });
});
} else {
emitEffectAction({ loading: false, data: result });
throw new Error('Effect must decorated for a Promise function');
}
return result;
}

return originalMethod.apply(this, args);
Expand Down
4 changes: 1 addition & 3 deletions src/hooks/useObserveEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ export function useObserveEffect<T>(bean: T, effect: FunctionPropertyNames<T> |
loading: false,
error: null,
data: null,
effect: effect as string | symbol,
};
});

const listener = useCallback(
(action: EffectAction) => {
console.log(action.effectTarget === effect);
if (action.effect === effect || action.effectTarget === effect) {
setEffectState(action);
setEffectState({ loading: action.loading, error: action.error, data: action.data });
}
},
[effect]
Expand Down
2 changes: 1 addition & 1 deletion src/types/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface EffectAction<T = unknown> {
loading: boolean;
data: T;
error: unknown;
effect: string | symbol;
effect?: string | symbol;
effectTarget?: Function;
}

Expand Down
13 changes: 5 additions & 8 deletions test/effect-action.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,17 @@ describe('effect action test', () => {
<StatedBeanProvider providers={[PostProvidedSample]}>{children}</StatedBeanProvider>
);

const { result, unmount } = renderHook(
const { unmount } = renderHook(
() => {
const bean = useInject(PostProvidedSample);
const action = useObserveEffect(bean, 'add2');
expect(() => {
const bean = useInject(PostProvidedSample);

return { bean, action };
useObserveEffect(bean, 'add2');
}).toThrow();
},
{ wrapper }
);

expect(result.current.action.loading).toBe(false);
act(() => result.current.bean.add2());
expect(result.current.action.loading).toBe(false);

unmount();
});

Expand Down

0 comments on commit c569dc5

Please sign in to comment.