-
Notifications
You must be signed in to change notification settings - Fork 8
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
Fix provider global clenup on error #89
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,42 @@ | ||
import React, { useContext, useMemo, forwardRef, useEffect } from 'react'; | ||
import React, { forwardRef, Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { diRegistry } from './constants'; | ||
import { Context } from './context'; | ||
import { addInjectableToMap, getDisplayName, findInjectable } from './utils'; | ||
import { globalDi } from './global'; | ||
|
||
export const DiProvider = ({ children, use, target, global }) => { | ||
const { getDependencies } = useContext(Context); | ||
export class DiProvider extends Component { | ||
static contextType = Context; | ||
|
||
static propTypes = { | ||
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]), | ||
global: PropTypes.bool, | ||
target: PropTypes.oneOfType([ | ||
PropTypes.func, | ||
PropTypes.arrayOf(PropTypes.func), | ||
]), | ||
use: PropTypes.arrayOf( | ||
PropTypes.oneOfType([PropTypes.func, PropTypes.object]) | ||
).isRequired, | ||
}; | ||
|
||
componentDidCatch(err) { | ||
globalDi._remove(this.props.use); | ||
throw err; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to double check - :notsureif: unmount not happens during catch. I just cannot imagine that some allocated resources (any) can be "not freed" in case of emergency. That would like a disaster! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not, just tested: neither There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something we need to clarify for ourselves |
||
} | ||
|
||
componentWillUnmount() { | ||
globalDi._remove(this.props.use); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these are not the same There were a few bugs due to mismatches of this sort, I would better remeber a "cleanup callback" somewhere in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How come? I would expect them to be consistent 🤔 Unless someone decides to conditionally add/remove injectables (which we call out as not supported) it should contain the same "stuff" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You know, stuff happens. Ideally just expose another (secret) method to check if global DI is clear. Then we can call it before test to verify that everything is as it should be. Later we can remove this method. Well, it should be fine, but we all know that our assumptions are not always true |
||
} | ||
|
||
value = undefined; | ||
getValue() { | ||
if (this.value) return this.value; | ||
|
||
const { use, target, global } = this.props; | ||
const { getDependencies } = this.context; | ||
|
||
// memo provider value so gets computed only once | ||
const value = useMemo(() => { | ||
// create a map of dependency real -> replacements for fast lookup | ||
const replacementMap = use.reduce((acc, inj) => { | ||
addInjectableToMap(acc, inj); | ||
|
@@ -21,7 +47,7 @@ export const DiProvider = ({ children, use, target, global }) => { | |
// support single or multiple targets | ||
const targets = target && (Array.isArray(target) ? target : [target]); | ||
|
||
return { | ||
this.value = { | ||
getDependencies(realDeps, targetChild) { | ||
// First we collect dependencies from parent provider(s) (if any) | ||
const dependencies = getDependencies(realDeps, targetChild); | ||
|
@@ -44,25 +70,17 @@ export const DiProvider = ({ children, use, target, global }) => { | |
return dependencies; | ||
}, | ||
}; | ||
}, [getDependencies]); // ignore use & target props | ||
|
||
// on unmount | ||
useEffect(() => () => globalDi._remove(use), []); // ignore use prop | ||
return this.value; | ||
} | ||
|
||
return <Context.Provider value={value}>{children}</Context.Provider>; | ||
}; | ||
|
||
DiProvider.propTypes = { | ||
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]), | ||
global: PropTypes.bool, | ||
target: PropTypes.oneOfType([ | ||
PropTypes.func, | ||
PropTypes.arrayOf(PropTypes.func), | ||
]), | ||
use: PropTypes.arrayOf( | ||
PropTypes.oneOfType([PropTypes.func, PropTypes.object]) | ||
).isRequired, | ||
}; | ||
render() { | ||
return ( | ||
<Context.Provider value={this.getValue()}> | ||
{this.props.children} | ||
</Context.Provider> | ||
); | ||
} | ||
} | ||
|
||
export function withDi(Comp, deps, target = null) { | ||
const WrappedComponent = forwardRef((props, ref) => ( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an interesting test, look like there is a story behind it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is because context provider memo can be fiddly 😅