-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathcreateContext.ts
More file actions
62 lines (48 loc) · 1.87 KB
/
createContext.ts
File metadata and controls
62 lines (48 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use client';
import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';
import * as React from 'react';
import { unstable_NormalPriority as NormalPriority, unstable_runWithPriority as runWithPriority } from 'scheduler';
import type { Context, ContextValue } from './types';
const createProvider = <Value>(Original: React.Provider<ContextValue<Value>>) => {
const Provider: React.FC<React.ProviderProps<Value>> = props => {
// Holds an actual "props.value"
const valueRef = React.useRef(props.value);
// A stable object, is used to avoid context updates via mutation of its values.
const contextValue = React.useRef<ContextValue<Value>>(null);
if (!contextValue.current) {
contextValue.current = {
value: valueRef,
listeners: [],
};
}
useIsomorphicLayoutEffect(() => {
valueRef.current = props.value;
runWithPriority(NormalPriority, () => {
(contextValue.current as ContextValue<Value>).listeners.forEach(listener => {
listener(props.value);
});
});
}, [props.value]);
return React.createElement(Original, { value: contextValue.current }, props.children);
};
/* istanbul ignore else */
if (process.env.NODE_ENV !== 'production') {
Provider.displayName = 'ContextSelector.Provider';
}
return Provider as unknown as React.Provider<ContextValue<Value>>;
};
/**
* @internal
*/
export const createContext = <Value>(defaultValue: Value): Context<Value> => {
// eslint-disable-next-line @fluentui/no-context-default-value
const context = React.createContext<ContextValue<Value>>({
value: { current: defaultValue },
listeners: [],
isDefault: true,
});
context.Provider = createProvider<Value>(context.Provider);
// We don't support Consumer API
delete (context as unknown as Context<Value>).Consumer;
return context as unknown as Context<Value>;
};