@@ -2,64 +2,58 @@ import { enqueueRender } from './component';
22
33export let i = 0 ;
44
5- export function createContext ( defaultValue , contextId ) {
6- contextId = '__cC' + i ++ ;
7-
8- const context = {
9- _id : contextId ,
10- _defaultValue : defaultValue ,
11- /** @type {import('./internal').FunctionComponent } */
12- Consumer ( props , contextValue ) {
13- // return props.children(
14- // context[contextId] ? context[contextId].props.value : defaultValue
15- // );
16- return props . children ( contextValue ) ;
17- } ,
18- /** @type {import('./internal').FunctionComponent } */
19- Provider ( props ) {
20- if ( ! this . getChildContext ) {
21- /** @type {Set<import('./internal').Component> | null } */
22- let subs = new Set ( ) ;
23- let ctx = { } ;
24- ctx [ contextId ] = this ;
25-
26- this . getChildContext = ( ) => ctx ;
27-
28- this . componentWillUnmount = ( ) => {
29- subs = null ;
30- } ;
31-
32- this . shouldComponentUpdate = function ( _props ) {
33- if ( this . props . value !== _props . value ) {
34- subs . forEach ( c => {
35- c . _force = true ;
36- enqueueRender ( c ) ;
37- } ) ;
5+ export function createContext ( defaultValue ) {
6+ function Context ( props ) {
7+ if ( ! this . getChildContext ) {
8+ /** @type {Set<import('./internal').Component> | null } */
9+ let subs = new Set ( ) ;
10+ let ctx = { } ;
11+ ctx [ Context . _id ] = this ;
12+
13+ this . getChildContext = ( ) => ctx ;
14+
15+ this . componentWillUnmount = ( ) => {
16+ subs = null ;
17+ } ;
18+
19+ this . shouldComponentUpdate = function ( _props ) {
20+ // @ts -expect-error even
21+ if ( this . props . value !== _props . value ) {
22+ subs . forEach ( c => {
23+ c . _force = true ;
24+ enqueueRender ( c ) ;
25+ } ) ;
26+ }
27+ } ;
28+
29+ this . sub = c => {
30+ subs . add ( c ) ;
31+ let old = c . componentWillUnmount ;
32+ c . componentWillUnmount = ( ) => {
33+ if ( subs ) {
34+ subs . delete ( c ) ;
3835 }
36+ if ( old ) old . call ( c ) ;
3937 } ;
38+ } ;
39+ }
4040
41- this . sub = c => {
42- subs . add ( c ) ;
43- let old = c . componentWillUnmount ;
44- c . componentWillUnmount = ( ) => {
45- if ( subs ) {
46- subs . delete ( c ) ;
47- }
48- if ( old ) old . call ( c ) ;
49- } ;
50- } ;
51- }
41+ return props . children ;
42+ }
5243
53- return props . children ;
54- }
44+ Context . _id = '__cC' + i ++ ;
45+ Context . _defaultValue = defaultValue ;
46+
47+ /** @type {import('./internal').FunctionComponent } */
48+ Context . Consumer = ( props , contextValue ) => {
49+ return props . children ( contextValue ) ;
5550 } ;
5651
57- // Devtools needs access to the context object when it
58- // encounters a Provider. This is necessary to support
59- // setting `displayName` on the context object instead
60- // of on the component itself. See:
61- // https://reactjs.org/docs/context.html#contextdisplayname
52+ // we could also get rid of _contextRef entirely
53+ Context . Provider =
54+ Context . _contextRef =
55+ Context . Consumer . contextType =
56+ Context ;
6257
63- return ( context . Provider . _contextRef = context . Consumer . contextType =
64- context ) ;
58+ return Context ;
6559}
0 commit comments