Skip to content

Commit 5b25b0a

Browse files
committed
Move identity state to dispatcher
1 parent 00f753a commit 5b25b0a

File tree

4 files changed

+34
-38
lines changed

4 files changed

+34
-38
lines changed

src/internals/__tests__/dispatcher.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
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

39
describe('useEffect', () => {
410
it('is a noop', () => {

src/internals/__tests__/state.test.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
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

143
describe('readContextMap', () => {
154
it('returns values in a Map by key', () => {

src/internals/dispatcher.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Source: https://github.com/facebook/react/blob/c21c41e/packages/react-dom/src/server/ReactPartialRendererHooks.js
33

44
import is from 'object-is'
5-
import { getCurrentIdentity, readContextMap, type Identity } from './state'
5+
import { readContextMap } from './state'
66

77
import 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+
1641
let firstWorkInProgressHook: Hook | null = null
1742
let workInProgressHook: Hook | null = null
1843
// Whether the work-in-progress hook is a re-rendered hook

src/internals/state.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,9 @@
22

33
import type { AbstractContext, UserElement, ContextMap } from '../types'
44

5-
export opaque type Identity = {}
6-
75
const emptyMap: ContextMap = new Map()
8-
let currentIdentity: Identity | null = null
96
let 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-
328
export const clearCurrentContextMap = () => {
339
currentContextMap = emptyMap
3410
}

0 commit comments

Comments
 (0)