Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add infinite loop prevention #165

Open
wants to merge 7 commits into
base: next
Choose a base branch
from
Open

Add infinite loop prevention #165

wants to merge 7 commits into from

Conversation

ghengeveld
Copy link
Member

Description

This adds a mechanism that detects 'infinite' loops, and throws an error if that happens. This makes sure that if a user were to inadvertently create a loop, for example by defining promiseFn inline, the browser won't hang but the app will crash instead.

This fixes #34

Checklist

Make sure you check all the boxes. You can omit items that are not applicable.

  • Implementation for both <Async> and useAsync()
  • Added / updated the unit tests
  • Added / updated the documentation

@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 4, 2019

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 7592e1e:

Sandbox Source
clever-bash-1cmon Configuration

@codecov
Copy link

codecov bot commented Oct 17, 2019

Codecov Report

Merging #165 into next will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             next     #165   +/-   ##
=======================================
  Coverage   98.62%   98.62%           
=======================================
  Files           9        9           
  Lines         436      436           
  Branches      150      150           
=======================================
  Hits          430      430           
  Misses          6        6

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9f0bd4b...7592e1e. Read the comment docs.

Copy link
Member

@phryneas phryneas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides that, it looks good to me 👍

@@ -20,6 +21,9 @@ const useAsync = (arg1, arg2) => {
const lastOptions = useRef(undefined)
const lastPromise = useRef(neverSettle)
const abortController = useRef({ abort: noop })
const [preventLoop] = useState(loopPreventer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use useRefinstead of useState here and keep preventLoop in a ref, as it is not really application state. But that's more a thing of semantics and will not affect anything.

Maybe move it out in a custom hook like this:

function useLoopPreventer() {
    const preventLoop = useRef();
    if (!preventLoop.current) { 
        preventLoop.current = loopPreventer(); 
    }
    return preventLoop.current;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can't be a hook, because <Async> is backwards compatible to React 16.3 which doesn't support hooks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I used useState is for its lazy initial value. I give it a function that will only be invoked once, which is exactly what I want. It can be done with useRef but requires more code and doesn't take a resolver function for the initial value, so we'd be creating a new function each render, which we don't need.

@ghengeveld ghengeveld self-assigned this Dec 1, 2019
@ghengeveld ghengeveld removed their assignment Jun 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Infinite loop prevention
2 participants