-
Notifications
You must be signed in to change notification settings - Fork 31
Make undux React 18 compatible #160
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
base: master
Are you sure you want to change the base?
Changes from all 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 |
|---|---|---|
|
|
@@ -43,15 +43,16 @@ export function createConnectedStoreAs< | |
| let Context = React.createContext({ __MISSING_PROVIDER__: true } as any) | ||
|
|
||
| type ContainerState = { | ||
| storeDefinitions: { [K in keyof States]: StoreDefinition<States[K]> | null } | ||
| storeSnapshots: { [K in keyof States]: StoreSnapshot<States[K]> | null } | ||
| subscriptions: { [K in keyof States]: Subscription } | ||
| } | ||
|
|
||
| class Container extends React.Component< | ||
| ContainerPropsAs<States>, | ||
| ContainerState | ||
| > { | ||
| storeDefinitions: { [K in keyof States]: StoreDefinition<States[K]> } | ||
| subscriptions: { [K in keyof States]: Subscription } | null | ||
|
|
||
| constructor(props: ContainerPropsAs<States>) { | ||
| super(props) | ||
|
|
||
|
|
@@ -65,37 +66,36 @@ export function createConnectedStoreAs< | |
| fx(stores as any) // TODO | ||
| } | ||
|
|
||
| this.storeDefinitions = stores as any | ||
| this.subscriptions = this._createSubscriptions() | ||
| this.state = { | ||
| storeDefinitions: stores as any, // TODO | ||
| // TODO | ||
| storeSnapshots: mapValues(stores, _ => _.getCurrentSnapshot()) as any, | ||
| subscriptions: mapValues(stores, (_, k) => | ||
| _.onAll().subscribe(() => | ||
| this.setState(state => ({ | ||
| storeSnapshots: Object.assign({}, state.storeSnapshots, { | ||
| [k]: _.getCurrentSnapshot() | ||
| }) | ||
| })) | ||
| ) | ||
| storeSnapshots: mapValues(stores, _ => _.getCurrentSnapshot()) as any | ||
| } | ||
| } | ||
|
|
||
| _createSubscriptions = () => | ||
| mapValues(this.storeDefinitions, (_, k) => | ||
| _.onAll().subscribe(() => | ||
| this.setState(state => ({ | ||
| storeSnapshots: Object.assign({}, state.storeSnapshots, { | ||
| [k]: _.getCurrentSnapshot() | ||
| }) | ||
| })) | ||
| ) | ||
| ) as any | ||
|
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. Nit: Do you need this 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. I can double check, I feel like I had basically just moved these lines but could see if I can fix at least some of them. |
||
|
|
||
| componentDidMount(): void { | ||
| if (this.subscriptions == null) { | ||
| this.subscriptions = this._createSubscriptions() | ||
| } | ||
| } | ||
|
|
||
| componentWillUnmount() { | ||
| mapValues(this.state.subscriptions, _ => _.unsubscribe()) | ||
| // Let the state get GC'd. | ||
| // TODO: Find a more elegant way to do this. | ||
| if (this.state.storeSnapshots) { | ||
| if (this.subscriptions != null) { | ||
| mapValues(this.subscriptions, _ => _.unsubscribe()) | ||
| this.subscriptions = null | ||
| } | ||
| mapValues(this.state.storeSnapshots, _ => ((_ as any).state = null)) | ||
| mapValues( | ||
| this.state.storeSnapshots, | ||
| _ => ((_ as any).storeDefinition = null) | ||
|
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. Same here -- is there a way we could keep this GC-related code? 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. same as above, we need to re-establish the subscriptions. |
||
| ) | ||
| mapValues( | ||
| this.state.storeDefinitions, | ||
| _ => ((_ as any).storeSnapshot = null) | ||
| ) | ||
| } | ||
|
|
||
| render() { | ||
|
|
||
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.
Can we keep the GC lines? These were pretty important for long-running apps with complex state, IIRC.
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.
componentDidMountabove requires access to thestoreDefinitionto re-establish the subscription. Not sure there's a way around keeping the reference here?