File tree Expand file tree Collapse file tree 4 files changed +34
-38
lines changed Expand file tree Collapse file tree 4 files changed +34
-38
lines changed Original file line number Diff line number Diff line change 1- import { Dispatcher } from '../dispatcher'
1+ import { getCurrentIdentity , Dispatcher } from '../dispatcher'
2+
3+ describe ( 'getCurrentIdentity' , ( ) => {
4+ it ( 'throws when called outside of function components' , ( ) => {
5+ expect ( getCurrentIdentity ) . toThrow ( )
6+ } )
7+ } )
28
39describe ( 'useEffect' , ( ) => {
410 it ( 'is a noop' , ( ) => {
Original file line number Diff line number Diff line change 1- import {
2- getCurrentIdentity ,
3- setCurrentContextMap ,
4- readContextMap ,
5- maskContext
6- } from '../state'
7-
8- describe ( 'getCurrentIdentity' , ( ) => {
9- it ( 'throws when called outside of function components' , ( ) => {
10- expect ( getCurrentIdentity ) . toThrow ( )
11- } )
12- } )
1+ import { setCurrentContextMap , readContextMap , maskContext } from '../state'
132
143describe ( 'readContextMap' , ( ) => {
154 it ( 'returns values in a Map by key' , ( ) => {
Original file line number Diff line number Diff line change 22// Source: https://github.com/facebook/react/blob/c21c41e/packages/react-dom/src/server/ReactPartialRendererHooks.js
33
44import is from 'object-is'
5- import { getCurrentIdentity , readContextMap , type Identity } from './state'
5+ import { readContextMap } from './state'
66
77import type {
88 AbstractContext ,
@@ -13,6 +13,31 @@ import type {
1313 Hook
1414} from '../types'
1515
16+ export opaque type Identity = { }
17+
18+ let currentIdentity : Identity | null = null
19+
20+ export const makeIdentity = ( ) : Identity => ( { } )
21+
22+ export const setCurrentIdentity = ( id : Identity | null ) => {
23+ currentIdentity = id
24+ }
25+
26+ export const getCurrentIdentity = ( ) : Identity => {
27+ if ( currentIdentity === null ) {
28+ throw new Error (
29+ '[react-ssr-prepass] Hooks can only be called inside the body of a function component. ' +
30+ '(https://fb.me/react-invalid-hook-call)'
31+ )
32+ }
33+
34+ // NOTE: The warning that is used in ReactPartialRendererHooks is obsolete
35+ // in a prepass, since it'll be caught by a subsequent renderer anyway
36+ // https://github.com/facebook/react/blob/c21c41e/packages/react-dom/src/server/ReactPartialRendererHooks.js#L63-L71
37+
38+ return ( currentIdentity : Identity )
39+ }
40+
1641let firstWorkInProgressHook : Hook | null = null
1742let workInProgressHook : Hook | null = null
1843// Whether the work-in-progress hook is a re-rendered hook
Original file line number Diff line number Diff line change 22
33import type { AbstractContext , UserElement , ContextMap } from '../types'
44
5- export opaque type Identity = { }
6-
75const emptyMap : ContextMap = new Map ( )
8- let currentIdentity : Identity | null = null
96let currentContextMap : ContextMap = emptyMap
107
11- export const makeIdentity = ( ) : Identity => ( { } )
12-
13- export const setCurrentIdentity = ( id : Identity | null ) => {
14- currentIdentity = id
15- }
16-
17- export const getCurrentIdentity = ( ) : Identity => {
18- if ( currentIdentity === null ) {
19- throw new Error (
20- '[react-ssr-prepass] Hooks can only be called inside the body of a function component. ' +
21- '(https://fb.me/react-invalid-hook-call)'
22- )
23- }
24-
25- // NOTE: The warning that is used in ReactPartialRendererHooks is obsolete
26- // in a prepass, since it'll be caught by a subsequent renderer anyway
27- // https://github.com/facebook/react/blob/c21c41e/packages/react-dom/src/server/ReactPartialRendererHooks.js#L63-L71
28-
29- return ( currentIdentity : Identity )
30- }
31-
328export const clearCurrentContextMap = ( ) => {
339 currentContextMap = emptyMap
3410}
You can’t perform that action at this time.
0 commit comments