Skip to content

Commit 3a06a4f

Browse files
authored
chore(deps): inline mini-create-react-context dependency (#9382)
1 parent 5e2b040 commit 3a06a4f

File tree

5 files changed

+186
-12
lines changed

5 files changed

+186
-12
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// MIT License
2+
// Copyright (c) 2019-present StringEpsilon <[email protected]>
3+
// Copyright (c) 2017-2019 James Kyle <[email protected]>
4+
// https://github.com/StringEpsilon/mini-create-react-context
5+
import React from "react";
6+
import createReactContext from "./miniCreateReactContext";
7+
8+
export default React.createContext || createReactContext;

packages/react-router/modules/createNamedContext.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// TODO: Replace with React.createContext once we can assume React 16+
2-
import createContext from "mini-create-react-context";
2+
import createContext from "./createContext";
33

44
const createNamedContext = name => {
55
const context = createContext();
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
// MIT License
2+
// Copyright (c) 2019-present StringEpsilon <[email protected]>
3+
// Copyright (c) 2017-2019 James Kyle <[email protected]>
4+
// https://github.com/StringEpsilon/mini-create-react-context
5+
import React from "react";
6+
import PropTypes from "prop-types";
7+
import warning from "tiny-warning";
8+
9+
const MAX_SIGNED_31_BIT_INT = 1073741823;
10+
11+
const commonjsGlobal =
12+
typeof globalThis !== "undefined" // 'global proper'
13+
? // eslint-disable-next-line no-undef
14+
globalThis
15+
: typeof window !== "undefined"
16+
? window // Browser
17+
: typeof global !== "undefined"
18+
? global // node.js
19+
: {};
20+
21+
function getUniqueId() {
22+
let key = "__global_unique_id__";
23+
return (commonjsGlobal[key] = (commonjsGlobal[key] || 0) + 1);
24+
}
25+
26+
// Inlined Object.is polyfill.
27+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
28+
function objectIs(x, y) {
29+
if (x === y) {
30+
return x !== 0 || 1 / x === 1 / y;
31+
} else {
32+
// eslint-disable-next-line no-self-compare
33+
return x !== x && y !== y;
34+
}
35+
}
36+
37+
function createEventEmitter(value) {
38+
let handlers = [];
39+
return {
40+
on(handler) {
41+
handlers.push(handler);
42+
},
43+
44+
off(handler) {
45+
handlers = handlers.filter(h => h !== handler);
46+
},
47+
48+
get() {
49+
return value;
50+
},
51+
52+
set(newValue, changedBits) {
53+
value = newValue;
54+
handlers.forEach(handler => handler(value, changedBits));
55+
}
56+
};
57+
}
58+
59+
function onlyChild(children) {
60+
return Array.isArray(children) ? children[0] : children;
61+
}
62+
63+
export default function createReactContext(defaultValue, calculateChangedBits) {
64+
const contextProp = "__create-react-context-" + getUniqueId() + "__";
65+
66+
class Provider extends React.Component {
67+
emitter = createEventEmitter(this.props.value);
68+
69+
static childContextTypes = {
70+
[contextProp]: PropTypes.object.isRequired
71+
};
72+
73+
getChildContext() {
74+
return {
75+
[contextProp]: this.emitter
76+
};
77+
}
78+
79+
componentWillReceiveProps(nextProps) {
80+
if (this.props.value !== nextProps.value) {
81+
let oldValue = this.props.value;
82+
let newValue = nextProps.value;
83+
let changedBits;
84+
85+
if (objectIs(oldValue, newValue)) {
86+
changedBits = 0; // No change
87+
} else {
88+
changedBits =
89+
typeof calculateChangedBits === "function"
90+
? calculateChangedBits(oldValue, newValue)
91+
: MAX_SIGNED_31_BIT_INT;
92+
if (process.env.NODE_ENV !== "production") {
93+
warning(
94+
(changedBits & MAX_SIGNED_31_BIT_INT) === changedBits,
95+
"calculateChangedBits: Expected the return value to be a " +
96+
"31-bit integer. Instead received: " +
97+
changedBits
98+
);
99+
}
100+
101+
changedBits |= 0;
102+
103+
if (changedBits !== 0) {
104+
this.emitter.set(nextProps.value, changedBits);
105+
}
106+
}
107+
}
108+
}
109+
110+
render() {
111+
return this.props.children;
112+
}
113+
}
114+
115+
class Consumer extends React.Component {
116+
static contextTypes = {
117+
[contextProp]: PropTypes.object
118+
};
119+
120+
observedBits;
121+
122+
state = {
123+
value: this.getValue()
124+
};
125+
126+
componentWillReceiveProps(nextProps) {
127+
let { observedBits } = nextProps;
128+
this.observedBits =
129+
observedBits === undefined || observedBits === null
130+
? MAX_SIGNED_31_BIT_INT // Subscribe to all changes by default
131+
: observedBits;
132+
}
133+
134+
componentDidMount() {
135+
if (this.context[contextProp]) {
136+
this.context[contextProp].on(this.onUpdate);
137+
}
138+
let { observedBits } = this.props;
139+
this.observedBits =
140+
observedBits === undefined || observedBits === null
141+
? MAX_SIGNED_31_BIT_INT // Subscribe to all changes by default
142+
: observedBits;
143+
}
144+
145+
componentWillUnmount() {
146+
if (this.context[contextProp]) {
147+
this.context[contextProp].off(this.onUpdate);
148+
}
149+
}
150+
151+
getValue() {
152+
if (this.context[contextProp]) {
153+
return this.context[contextProp].get();
154+
} else {
155+
return defaultValue;
156+
}
157+
}
158+
159+
onUpdate = (newValue, changedBits) => {
160+
const observedBits = this.observedBits | 0;
161+
if ((observedBits & changedBits) !== 0) {
162+
this.setState({ value: this.getValue() });
163+
}
164+
};
165+
166+
render() {
167+
return onlyChild(this.props.children)(this.state.value);
168+
}
169+
}
170+
171+
return {
172+
Provider,
173+
Consumer
174+
};
175+
}

packages/react-router/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"history": "^4.9.0",
4848
"hoist-non-react-statics": "^3.1.0",
4949
"loose-envify": "^1.3.1",
50-
"mini-create-react-context": "^0.4.0",
5150
"path-to-regexp": "^1.7.0",
5251
"prop-types": "^15.6.2",
5352
"react-is": "^16.6.0",

yarn.lock

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@
902902
pirates "^4.0.0"
903903
source-map-support "^0.5.16"
904904

905-
"@babel/runtime@^7.0.0", "@babel/runtime@^7.1.2", "@babel/runtime@^7.12.13", "@babel/runtime@^7.4.5", "@babel/runtime@^7.5.5", "@babel/runtime@^7.8.4":
905+
"@babel/runtime@^7.0.0", "@babel/runtime@^7.1.2", "@babel/runtime@^7.12.13", "@babel/runtime@^7.4.5", "@babel/runtime@^7.8.4":
906906
version "7.12.13"
907907
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.12.13.tgz#0a21452352b02542db0ffb928ac2d3ca7cb6d66d"
908908
integrity sha512-8+3UMPBrjFa/6TtKi/7sehPKqfAm4g6K+YQjyyFOLUTxzOngcRZTlAVY8sc2CORJYqdHQY8gRPHmn+qo15rCBw==
@@ -7101,14 +7101,6 @@ min-indent@^1.0.0:
71017101
resolved "https://registry.yarnpkg.com/min-indent/-/min-indent-1.0.1.tgz#a63f681673b30571fbe8bc25686ae746eefa9869"
71027102
integrity sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==
71037103

7104-
mini-create-react-context@^0.4.0:
7105-
version "0.4.0"
7106-
resolved "https://registry.yarnpkg.com/mini-create-react-context/-/mini-create-react-context-0.4.0.tgz#df60501c83151db69e28eac0ef08b4002efab040"
7107-
integrity sha512-b0TytUgFSbgFJGzJqXPKCFCBWigAjpjo+Fl7Vf7ZbKRDptszpppKxXH6DRXEABZ/gcEQczeb0iZ7JvL8e8jjCA==
7108-
dependencies:
7109-
"@babel/runtime" "^7.5.5"
7110-
tiny-warning "^1.0.3"
7111-
71127104
minimatch@^3.0.4:
71137105
version "3.0.4"
71147106
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
@@ -9963,7 +9955,7 @@ tiny-invariant@^1.0.2:
99639955
resolved "https://registry.yarnpkg.com/tiny-invariant/-/tiny-invariant-1.0.6.tgz#b3f9b38835e36a41c843a3b0907a5a7b3755de73"
99649956
integrity sha512-FOyLWWVjG+aC0UqG76V53yAWdXfH8bO6FNmyZOuUrzDzK8DI3/JRY25UD7+g49JWM1LXwymsKERB+DzI0dTEQA==
99659957

9966-
tiny-warning@^1.0.0, tiny-warning@^1.0.3:
9958+
tiny-warning@^1.0.0:
99679959
version "1.0.3"
99689960
resolved "https://registry.yarnpkg.com/tiny-warning/-/tiny-warning-1.0.3.tgz#94a30db453df4c643d0fd566060d60a875d84754"
99699961
integrity sha512-lBN9zLN/oAf68o3zNXYrdCt1kP8WsiGW8Oo2ka41b2IM5JL/S1CTyX1rW0mb/zSuJun0ZUrDxx4sqvYS2FWzPA==

0 commit comments

Comments
 (0)