Skip to content

Commit 02a8eb7

Browse files
committed
Mirgate to useMutableSource [first draft]
1 parent 2c97768 commit 02a8eb7

22 files changed

+1110
-1957
lines changed

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@
7777
"@rollup/plugin-replace": "^2.3.3",
7878
"@testing-library/jest-dom": "^5.11.5",
7979
"@testing-library/jest-native": "^3.4.3",
80-
"@testing-library/react": "^12.0.0",
81-
"@testing-library/react-hooks": "^3.4.2",
80+
"@testing-library/react": "file:///tmp/react.tgz",
81+
"@testing-library/react-hooks": "^7.0.1",
8282
"@testing-library/react-native": "^7.1.0",
8383
"@types/create-react-class": "^15.6.3",
8484
"@types/object-assign": "^4.0.30",
@@ -103,10 +103,10 @@
103103
"glob": "^7.1.6",
104104
"jest": "^26.6.1",
105105
"prettier": "^2.1.2",
106-
"react": "^16.14.0",
107-
"react-dom": "^16.14.0",
106+
"react": "18.0.0-alpha-b9934d6db-20210805",
107+
"react-dom": "18.0.0-alpha-b9934d6db-20210805",
108108
"react-native": "^0.64.1",
109-
"react-test-renderer": "^16.14.0",
109+
"react-test-renderer": "18.0.0-alpha-b9934d6db-20210805",
110110
"redux": "^4.0.5",
111111
"rimraf": "^3.0.2",
112112
"rollup": "^2.32.1",

src/alternate-renderers.ts

-9
This file was deleted.

src/components/Context.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import React from 'react'
1+
import React, { MutableSource } from 'react'
22
import { Action, AnyAction, Store } from 'redux'
33
import type { FixTypeLater } from '../types'
4-
import type { Subscription } from '../utils/Subscription'
54

65
export interface ReactReduxContextValue<
76
SS = FixTypeLater,
87
A extends Action = AnyAction
98
> {
9+
storeSource: MutableSource<Store<SS, A>>
1010
store: Store<SS, A>
11-
subscription: Subscription
1211
}
1312

1413
export const ReactReduxContext =

src/components/Provider.tsx

+17-27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import React, { Context, ReactNode, useMemo } from 'react'
1+
import React, {
2+
Context,
3+
ReactNode,
4+
unstable_createMutableSource as createMutableSource,
5+
useMemo,
6+
} from 'react'
27
import { ReactReduxContext, ReactReduxContextValue } from './Context'
3-
import { createSubscription } from '../utils/Subscription'
4-
import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect'
58
import type { FixTypeLater } from '../types'
69
import { Action, AnyAction, Store } from 'redux'
710

@@ -19,31 +22,18 @@ export interface ProviderProps<A extends Action = AnyAction> {
1922
children: ReactNode
2023
}
2124

22-
function Provider({ store, context, children }: ProviderProps) {
23-
const contextValue = useMemo(() => {
24-
const subscription = createSubscription(store)
25-
subscription.onStateChange = subscription.notifyNestedSubs
26-
return {
27-
store,
28-
subscription,
29-
}
30-
}, [store])
31-
32-
const previousState = useMemo(() => store.getState(), [store])
33-
34-
useIsomorphicLayoutEffect(() => {
35-
const { subscription } = contextValue
36-
subscription.trySubscribe()
37-
38-
if (previousState !== store.getState()) {
39-
subscription.notifyNestedSubs()
40-
}
41-
return () => {
42-
subscription.tryUnsubscribe()
43-
subscription.onStateChange = undefined
44-
}
45-
}, [contextValue, previousState])
25+
export function createReduxContext(store: Store) {
26+
return {
27+
storeSource: createMutableSource(store, () => store.getState()),
28+
store,
29+
}
30+
}
4631

32+
function Provider({ store, context, children }: ProviderProps) {
33+
const contextValue: ReactReduxContextValue = useMemo(
34+
() => createReduxContext(store),
35+
[store]
36+
)
4737
const Context = context || ReactReduxContext
4838

4939
return <Context.Provider value={contextValue}>{children}</Context.Provider>

0 commit comments

Comments
 (0)