Skip to content

Commit 51a1937

Browse files
gaearongregberge
authored andcommitted
Stop using WeakMap
Fixes #50
1 parent 60fd045 commit 51a1937

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

packages/react-proxy/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
"babel-plugin-transform-object-rest-spread": "^6.3.13",
5757
"babel-plugin-transform-react-display-name": "^6.4.0",
5858
"babel-plugin-transform-react-jsx": "^6.4.0",
59+
"babel-preset-es2015": "^6.6.0",
60+
"babel-preset-react": "^6.5.0",
5961
"expect": "^1.9.0",
6062
"mocha": "^2.2.4",
6163
"react": "^0.14.0",
@@ -64,9 +66,6 @@
6466
"webpack": "1.4.8"
6567
},
6668
"dependencies": {
67-
"babel-preset-es2015": "^6.6.0",
68-
"babel-preset-react": "^6.5.0",
69-
"core-js": "^2.1.3",
70-
"lodash": "^3.7.0"
69+
"lodash": "^4.6.1"
7170
}
7271
}

packages/react-proxy/src/createClassProxy.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import WeakMap from 'core-js/library/es6/weak-map';
1+
import find from 'lodash/find';
22
import createPrototypeProxy from './createPrototypeProxy';
33
import bindAutoBindMethods from './bindAutoBindMethods';
44
import deleteUnknownAutoBindMethods from './deleteUnknownAutoBindMethods';
@@ -28,13 +28,23 @@ function isEqualDescriptor(a, b) {
2828
return true;
2929
}
3030

31-
let allProxies = new WeakMap();
31+
// This was originally a WeakMap but we had issues with React Native:
32+
// https://github.com/gaearon/react-proxy/issues/50#issuecomment-192928066
33+
let allProxies = [];
34+
function findProxy(Component) {
35+
const pair = allProxies.find(([key]) => key === Component);
36+
return pair ? pair[1] : null;
37+
}
38+
function addProxy(Component, proxy) {
39+
allProxies.push([Component, proxy]);
40+
}
3241

3342
function proxyClass(InitialComponent) {
3443
// Prevent double wrapping.
3544
// Given a proxy class, return the existing proxy managing it.
36-
if (allProxies.has(InitialComponent)) {
37-
return allProxies.get(InitialComponent);
45+
var existingProxy = findProxy(InitialComponent);
46+
if (existingProxy) {
47+
return existingProxy;
3848
}
3949

4050
let CurrentComponent;
@@ -97,8 +107,9 @@ function proxyClass(InitialComponent) {
97107
}
98108

99109
// Prevent proxy cycles
100-
if (allProxies.has(NextComponent)) {
101-
return update(allProxies.get(NextComponent).__getCurrent());
110+
var existingProxy = findProxy(NextComponent);
111+
if (existingProxy) {
112+
return update(existingProxy.__getCurrent());
102113
}
103114

104115
// Save the next constructor so we call it
@@ -176,7 +187,7 @@ function proxyClass(InitialComponent) {
176187
update(InitialComponent);
177188

178189
const proxy = { get, update };
179-
allProxies.set(ProxyComponent, proxy);
190+
addProxy(ProxyComponent, proxy);
180191

181192
Object.defineProperty(proxy, '__getCurrent', {
182193
configurable: false,

packages/react-proxy/src/createPrototypeProxy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import assign from 'lodash/object/assign';
2-
import difference from 'lodash/array/difference';
1+
import assign from 'lodash/assign';
2+
import difference from 'lodash/difference';
33

44
export default function createPrototypeProxy() {
55
let proxy = {};

0 commit comments

Comments
 (0)