Skip to content

Commit

Permalink
update snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Oct 29, 2017
1 parent ac667e5 commit a5503ba
Show file tree
Hide file tree
Showing 5 changed files with 525 additions and 252 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@
"dependencies": {
"global": "^4.3.0",
"react-deep-force-update": "^2.1.1",
"react-stand-in": "^1.0.0-alpha-2",
"react-stand-in": "^1.0.2",
"redbox-react": "^1.3.6",
"source-map": "^0.6.1",
"stacktrace-js": "2.0.0"
"source-map": "^0.6.1"
},
"devDependencies": {
"babel-cli": "^6.7.5",
Expand Down
10 changes: 10 additions & 0 deletions src/babel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ module.exports = function plugin(args) {

var evalTemplate = template('this[key]=eval(code);')

var rewireSuperTemplate = template('ID=superClass;');

// We're making the IIFE we insert at the end of the file an unused variable
// because it otherwise breaks the output of the babel-node REPL (#359).
const buildTagger = template(`
Expand Down Expand Up @@ -153,13 +155,21 @@ module.exports = function plugin(args) {
},
Class(classPath) {
const classBody = classPath.get('body')

var newMethod = t.classMethod(
'method',
t.identifier('__facade__regenerateByEval'),
[t.identifier('key'), t.identifier('code')],
t.blockStatement([evalTemplate()])
)
classBody.pushContainer('body',newMethod)

var newMethod = t.classMethod(
'method',
t.identifier('__facade__rewireSuper'),
[t.identifier('superClass')],
t.blockStatement([rewireSuperTemplate({ ID: t.identifier(classPath.node.id.name) })]));
classBody.pushContainer('body', newMethod);
},
},
}
Expand Down
43 changes: 27 additions & 16 deletions src/patch.dev.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const React = require('react')
const createProxy = require('react-stand-in').default
const global = require('global')
const stack = require('stacktrace-js');

class ComponentMap {
constructor(useWeakMap) {
Expand Down Expand Up @@ -46,7 +45,7 @@ class ComponentMap {
return
}
}
slot.push({key: type, value})
slot.push({ key: type, value })
}
}

Expand All @@ -69,11 +68,18 @@ let proxiesByID
let didWarnAboutID
let hasCreatedElementsByType
let idsByType
let firstInstances
let knownSignatures
let didUpdateProxy

function replaceFirstInstance (id, type) {
try {
firstInstances[id].prototype.__facade__rewireSuper(type)
} catch (e) {}
}

const hooks = {
register(type, uniqueLocalName, fileName) {
register(type, uniqueLocalName, fileName, replaceCallback) {
if (typeof type !== 'function') {
return
}
Expand Down Expand Up @@ -106,8 +112,10 @@ const hooks = {
// the same way as the original classes but are updatable with
// new versions without destroying original instances.
if (!proxiesByID[id]) {
firstInstances[id] = type;
proxiesByID[id] = createProxy(type)
} else {
replaceFirstInstance(id, type)
proxiesByID[id].update(type)
didUpdateProxy = true
}
Expand All @@ -118,6 +126,7 @@ const hooks = {
didWarnAboutID = {}
hasCreatedElementsByType = new ComponentMap(useWeakMap)
idsByType = new ComponentMap(useWeakMap)
firstInstances = {}
knownSignatures = {}
didUpdateProxy = false
},
Expand All @@ -144,30 +153,32 @@ function resolveType(type) {
return type
}

// When available, give proxy class to React instead of the real class.
let id = idsByType.get(type)
const wasKnownBefore = hasCreatedElementsByType.get(type)
hasCreatedElementsByType.set(type, true)

// When available, give proxy class to React instead of the real class.
const id = idsByType.get(type)
if (!id) {
const trace = stack.getSync();
const fingerPrint = [type.name,type.displayName,trace[2].functionName,trace[2].source].join(';');
const typeSignature = type.toString();

hooks.register(type, typeSignature, 'source');
hooks.register(type, fingerPrint, 'trace');
id = idsByType.get(type)
if (!wasKnownBefore) {
const signature = type.toString()
if (knownSignatures[signature]) {
warnAboutUnnacceptedClass(type)
} else {
knownSignatures[signature] = type
}
}
return type
}

hasCreatedElementsByType.set(type, true)

const proxy = proxiesByID[id];
const proxy = proxiesByID[id]
if (!proxy) {
return type
}

return proxy.get()
}

const {createElement} = React
const { createElement } = React
function patchedCreateElement(type, ...args) {
// Trick React into rendering a proxy so that
// its state is preserved when the class changes.
Expand Down
Loading

0 comments on commit a5503ba

Please sign in to comment.