Revert "Add ReactDebugInstanceMap"
This reverts commit 575fb79162efa0f913c4fbd79960d4fc335e92cb.
Dan Abramov committed
Apr 19, 2016 at 16:19 UTC
b6928936400dd23cba9fcff89b403b47eadd0e32
2 files changed
-297
src/isomorphic/ReactDebugInstanceMap.js
deleted
-124
@@ -1,124 +0,0 @@
1
-/**
2
- * Copyright 2016-present, Facebook, Inc.
3
- * All rights reserved.
4
- *
5
- * This source code is licensed under the BSD-style license found in the
6
- * LICENSE file in the root directory of this source tree. An additional grant
7
- * of patent rights can be found in the PATENTS file in the same directory.
8
- *
9
- * @providesModule ReactDebugInstanceMap
10
- */
11
-
12
-'use strict';
13
-
14
-var warning = require('warning');
15
-
16
-function checkValidInstance(internalInstance) {
17
- if (!internalInstance) {
18
- warning(
19
- false,
20
- 'There is an internal error in the React developer tools integration. ' +
21
- 'Instead of an internal instance, received %s. ' +
22
- 'Please report this as a bug in React.',
23
- internalInstance
24
- );
25
- return false;
26
- }
27
- var isValid = typeof internalInstance.mountComponent === 'function';
28
- warning(
29
- isValid,
30
- 'There is an internal error in the React developer tools integration. ' +
31
- 'Instead of an internal instance, received an object with the following ' +
32
- 'keys: %s. Please report this as a bug in React.',
33
- Object.keys(internalInstance).join(', ')
34
- );
35
- return isValid;
36
-}
37
-
38
-var idCounter = 1;
39
-var instancesByIDs = {};
40
-var instancesToIDs;
41
-
42
-function getIDForInstance(internalInstance) {
43
- if (!instancesToIDs) {
44
- instancesToIDs = new WeakMap();
45
- }
46
- if (instancesToIDs.has(internalInstance)) {
47
- return instancesToIDs.get(internalInstance);
48
- } else {
49
- var instanceID = (idCounter++).toString();
50
- instancesToIDs.set(internalInstance, instanceID);
51
- return instanceID;
52
- }
53
-}
54
-
55
-function getInstanceByID(instanceID) {
56
- return instancesByIDs[instanceID] || null;
57
-}
58
-
59
-function isRegisteredInstance(internalInstance) {
60
- var instanceID = getIDForInstance(internalInstance);
61
- if (instanceID) {
62
- return instancesByIDs.hasOwnProperty(instanceID);
63
- } else {
64
- return false;
65
- }
66
-}
67
-
68
-function registerInstance(internalInstance) {
69
- var instanceID = getIDForInstance(internalInstance);
70
- if (instanceID) {
71
- instancesByIDs[instanceID] = internalInstance;
72
- }
73
-}
74
-
75
-function unregisterInstance(internalInstance) {
76
- var instanceID = getIDForInstance(internalInstance);
77
- if (instanceID) {
78
- delete instancesByIDs[instanceID];
79
- }
80
-}
81
-
82
-var ReactDebugInstanceMap = {
83
- getIDForInstance(internalInstance) {
84
- if (!checkValidInstance(internalInstance)) {
85
- return null;
86
- }
87
- return getIDForInstance(internalInstance);
88
- },
89
- getInstanceByID(instanceID) {
90
- return getInstanceByID(instanceID);
91
- },
92
- isRegisteredInstance(internalInstance) {
93
- if (!checkValidInstance(internalInstance)) {
94
- return false;
95
- }
96
- return isRegisteredInstance(internalInstance);
97
- },
98
- registerInstance(internalInstance) {
99
- if (!checkValidInstance(internalInstance)) {
100
- return;
101
- }
102
- warning(
103
- !isRegisteredInstance(internalInstance),
104
- 'There is an internal error in the React developer tools integration. ' +
105
- 'A registered instance should not be registered again. ' +
106
- 'Please report this as a bug in React.'
107
- );
108
- registerInstance(internalInstance);
109
- },
110
- unregisterInstance(internalInstance) {
111
- if (!checkValidInstance(internalInstance)) {
112
- return;
113
- }
114
- warning(
115
- isRegisteredInstance(internalInstance),
116
- 'There is an internal error in the React developer tools integration. ' +
117
- 'An unregistered instance should not be unregistered again. ' +
118
- 'Please report this as a bug in React.'
119
- );
120
- unregisterInstance(internalInstance);
121
- },
122
-};
123
-
124
-module.exports = ReactDebugInstanceMap;
src/isomorphic/__tests__/ReactDebugInstanceMap-test.js
deleted
-173
@@ -1,173 +0,0 @@
1
-/**
2
- * Copyright 2016-present, Facebook, Inc.
3
- * All rights reserved.
4
- *
5
- * This source code is licensed under the BSD-style license found in the
6
- * LICENSE file in the root directory of this source tree. An additional grant
7
- * of patent rights can be found in the PATENTS file in the same directory.
8
- *
9
- * @emails react-core
10
- */
11
-
12
-'use strict';
13
-
14
-describe('ReactDebugInstanceMap', function() {
15
- var React;
16
- var ReactDebugInstanceMap;
17
- var ReactDOM;
18
-
19
- beforeEach(function() {
20
- jest.resetModuleRegistry();
21
- React = require('React');
22
- ReactDebugInstanceMap = require('ReactDebugInstanceMap');
23
- ReactDOM = require('ReactDOM');
24
- });
25
-
26
- function createStubInstance() {
27
- return { mountComponent: () => {} };
28
- }
29
-
30
- it('should register and unregister instances', function() {
31
- var inst1 = createStubInstance();
32
- var inst2 = createStubInstance();
33
-
34
- expect(ReactDebugInstanceMap.isRegisteredInstance(inst1)).toBe(false);
35
- expect(ReactDebugInstanceMap.isRegisteredInstance(inst2)).toBe(false);
36
-
37
- ReactDebugInstanceMap.registerInstance(inst1);
38
- expect(ReactDebugInstanceMap.isRegisteredInstance(inst1)).toBe(true);
39
- expect(ReactDebugInstanceMap.isRegisteredInstance(inst2)).toBe(false);
40
-
41
- ReactDebugInstanceMap.registerInstance(inst2);
42
- expect(ReactDebugInstanceMap.isRegisteredInstance(inst1)).toBe(true);
43
- expect(ReactDebugInstanceMap.isRegisteredInstance(inst2)).toBe(true);
44
-
45
- ReactDebugInstanceMap.unregisterInstance(inst2);
46
- expect(ReactDebugInstanceMap.isRegisteredInstance(inst1)).toBe(true);
47
- expect(ReactDebugInstanceMap.isRegisteredInstance(inst2)).toBe(false);
48
-
49
- ReactDebugInstanceMap.unregisterInstance(inst1);
50
- expect(ReactDebugInstanceMap.isRegisteredInstance(inst1)).toBe(false);
51
- expect(ReactDebugInstanceMap.isRegisteredInstance(inst2)).toBe(false);
52
- });
53
-
54
- it('should assign stable IDs', function() {
55
- var inst1 = createStubInstance();
56
- var inst2 = createStubInstance();
57
-
58
- var inst1ID = ReactDebugInstanceMap.getIDForInstance(inst1);
59
- var inst2ID = ReactDebugInstanceMap.getIDForInstance(inst2);
60
- expect(typeof inst1ID).toBe('string');
61
- expect(typeof inst2ID).toBe('string');
62
- expect(inst1ID).not.toBe(inst2ID);
63
-
64
- ReactDebugInstanceMap.registerInstance(inst1);
65
- ReactDebugInstanceMap.registerInstance(inst2);
66
- expect(ReactDebugInstanceMap.getIDForInstance(inst1)).toBe(inst1ID);
67
- expect(ReactDebugInstanceMap.getIDForInstance(inst2)).toBe(inst2ID);
68
-
69
- ReactDebugInstanceMap.unregisterInstance(inst1);
70
- ReactDebugInstanceMap.unregisterInstance(inst2);
71
- expect(ReactDebugInstanceMap.getIDForInstance(inst1)).toBe(inst1ID);
72
- expect(ReactDebugInstanceMap.getIDForInstance(inst2)).toBe(inst2ID);
73
- });
74
-
75
- it('should retrieve registered instance by its ID', function() {
76
- var inst1 = createStubInstance();
77
- var inst2 = createStubInstance();
78
-
79
- var inst1ID = ReactDebugInstanceMap.getIDForInstance(inst1);
80
- var inst2ID = ReactDebugInstanceMap.getIDForInstance(inst2);
81
- expect(ReactDebugInstanceMap.getInstanceByID(inst1ID)).toBe(null);
82
- expect(ReactDebugInstanceMap.getInstanceByID(inst2ID)).toBe(null);
83
-
84
- ReactDebugInstanceMap.registerInstance(inst1);
85
- ReactDebugInstanceMap.registerInstance(inst2);
86
- expect(ReactDebugInstanceMap.getInstanceByID(inst1ID)).toBe(inst1);
87
- expect(ReactDebugInstanceMap.getInstanceByID(inst2ID)).toBe(inst2);
88
-
89
- ReactDebugInstanceMap.unregisterInstance(inst1);
90
- ReactDebugInstanceMap.unregisterInstance(inst2);
91
- expect(ReactDebugInstanceMap.getInstanceByID(inst1ID)).toBe(null);
92
- expect(ReactDebugInstanceMap.getInstanceByID(inst2ID)).toBe(null);
93
- });
94
-
95
- it('should warn when registering an instance twice', function() {
96
- spyOn(console, 'error');
97
-
98
- var inst = createStubInstance();
99
- ReactDebugInstanceMap.registerInstance(inst);
100
- expect(console.error.argsForCall.length).toBe(0);
101
-
102
- ReactDebugInstanceMap.registerInstance(inst);
103
- expect(console.error.argsForCall.length).toBe(1);
104
- expect(console.error.argsForCall[0][0]).toContain(
105
- 'There is an internal error in the React developer tools integration. ' +
106
- 'A registered instance should not be registered again. ' +
107
- 'Please report this as a bug in React.'
108
- );
109
-
110
- ReactDebugInstanceMap.unregisterInstance(inst);
111
- ReactDebugInstanceMap.registerInstance(inst);
112
- expect(console.error.argsForCall.length).toBe(1);
113
- });
114
-
115
- it('should warn when unregistering an instance twice', function() {
116
- spyOn(console, 'error');
117
- var inst = createStubInstance();
118
-
119
- ReactDebugInstanceMap.unregisterInstance(inst);
120
- expect(console.error.argsForCall.length).toBe(1);
121
- expect(console.error.argsForCall[0][0]).toContain(
122
- 'There is an internal error in the React developer tools integration. ' +
123
- 'An unregistered instance should not be unregistered again. ' +
124
- 'Please report this as a bug in React.'
125
- );
126
-
127
- ReactDebugInstanceMap.registerInstance(inst);
128
- ReactDebugInstanceMap.unregisterInstance(inst);
129
- expect(console.error.argsForCall.length).toBe(1);
130
-
131
- ReactDebugInstanceMap.unregisterInstance(inst);
132
- expect(console.error.argsForCall.length).toBe(2);
133
- expect(console.error.argsForCall[1][0]).toContain(
134
- 'There is an internal error in the React developer tools integration. ' +
135
- 'An unregistered instance should not be unregistered again. ' +
136
- 'Please report this as a bug in React.'
137
- );
138
- });
139
-
140
- it('should warn about anything than is not an internal instance', function() {
141
- class Foo extends React.Component {
142
- render() {
143
- return <div />;
144
- }
145
- }
146
-
147
- spyOn(console, 'error');
148
- var warningCount = 0;
149
- var div = document.createElement('div');
150
- var publicInst = ReactDOM.render(<Foo />, div);
151
-
152
- [false, null, undefined, {}, div, publicInst].forEach(falsyValue => {
153
- ReactDebugInstanceMap.registerInstance(falsyValue);
154
- warningCount++;
155
- expect(ReactDebugInstanceMap.getIDForInstance(falsyValue)).toBe(null);
156
- warningCount++;
157
- expect(ReactDebugInstanceMap.isRegisteredInstance(falsyValue)).toBe(false);
158
- warningCount++;
159
- ReactDebugInstanceMap.unregisterInstance(falsyValue);
160
- warningCount++;
161
- });
162
-
163
- expect(console.error.argsForCall.length).toBe(warningCount);
164
- for (var i = 0; i < warningCount.length; i++) {
165
- // Ideally we could check for the more detailed error message here
166
- // but it depends on the input type and is meant for internal bugs
167
- // anyway so I don't think it's worth complicating the test with it.
168
- expect(console.error.argsForCall[i][0]).toContain(
169
- 'There is an internal error in the React developer tools integration.'
170
- );
171
- }
172
- });
173
-});