Skip to content

Commit

Permalink
Merge pull request #40 from a-ignatov-parc/fix-sigabrt-crash
Browse files Browse the repository at this point in the history
Fix app crashes with SIGABRT and EXC_BAD_ACCESS
  • Loading branch information
a-ignatov-parc authored Oct 14, 2018
2 parents 44e96bc + faad194 commit 8cd58ee
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
}],
"no-underscore-dangle": ["error", {
"allow": [
"_uid",
"_sink",
"_internalRoot",
"_targetTextNode",
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"homepage": "https://github.com/a-ignatov-parc/tvdml#readme",
"dependencies": {
"react-reconciler": "^0.11.0"
"react-reconciler": "^0.15.0"
},
"devDependencies": {
"babel-core": "^6.26.3",
Expand All @@ -53,12 +53,12 @@
"eslint-plugin-react": "^7.8.2",
"mocha": "^4.0.1",
"mocha-junit-reporter": "^1.15.0",
"react": "^16.4.0",
"react": "^16.5.2",
"uglifyjs-webpack-plugin": "^1.1.4",
"webpack": "^3.10.0"
},
"peerDependencies": {
"react": "^16.4.0"
"react": "^16.5.2"
},
"files": [
"dist"
Expand Down
43 changes: 34 additions & 9 deletions src/react-tvml/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ const supportedEventMapping = {
onHoldselect: 'holdselect',
};

let uid = 0;

function getNodeId(node) {
if (node) {
if (node._uid == null) {
/**
* `console.info` is a weird workaround that prevents nodes
* from the aggressive GC in tvOS 12.
*
* This fixes:
* - ITMLKit (9): EXC_BAD_ACCESS
* - ITMLKit (8): signal SIGABRT
*/
console.info('Preventing node from GC', node);
uid += 1;
node._uid = uid;
}
return node._uid;
}
return null;
}

function isTextNode(node) {
return node && node.nodeType === TEXT_NODE;
}
Expand Down Expand Up @@ -348,13 +370,13 @@ const TVMLRenderer = ReactFiberReconciler({
if (isTextNode(target) && isTextNode(child)) {
if (!target._handledTextNodes) {
target._handledTextNodes = [{
node: target,
id: getNodeId(target),
value: target.nodeValue,
}];
}

target._handledTextNodes.push({
node: child,
id: getNodeId(child),
value: child.nodeValue,
});

Expand Down Expand Up @@ -446,7 +468,8 @@ const TVMLRenderer = ReactFiberReconciler({

if (target) {
const nodes = target._handledTextNodes;
const reference = nodes.find(({ node }) => node === textInstance);
const nodeId = getNodeId(textInstance);
const reference = nodes.find(({ id }) => id === nodeId);

reference.value = newText;
updateNodeValue(target);
Expand All @@ -465,13 +488,13 @@ const TVMLRenderer = ReactFiberReconciler({
if (isTextNode(target) && isTextNode(child)) {
if (!target._handledTextNodes) {
target._handledTextNodes = [{
node: target,
id: getNodeId(target),
value: target.nodeValue,
}];
}

target._handledTextNodes.push({
node: child,
id: getNodeId(child),
value: child.nodeValue,
});

Expand Down Expand Up @@ -499,13 +522,13 @@ const TVMLRenderer = ReactFiberReconciler({
if (isTextNode(beforeChild) && isTextNode(child)) {
if (!beforeChild._handledTextNodes) {
beforeChild._handledTextNodes = [{
node: beforeChild,
id: getNodeId(beforeChild),
value: beforeChild.nodeValue,
}];
}

beforeChild._handledTextNodes.unshift({
node: child,
id: getNodeId(child),
value: child.nodeValue,
});

Expand Down Expand Up @@ -535,14 +558,16 @@ const TVMLRenderer = ReactFiberReconciler({
if (child._targetTextNode) {
const target = child._targetTextNode;
const nodes = target._handledTextNodes;
const referenceIndex = nodes.findIndex(({ node }) => node === child);
const nodeId = getNodeId(child);
const referenceIndex = nodes.findIndex(({ id }) => id === nodeId);

nodes.splice(referenceIndex, 1);
delete child._targetTextNode;
updateNodeValue(target);
} else if (child._handledTextNodes && child._handledTextNodes.length) {
const nodes = child._handledTextNodes;
const reference = nodes.find(({ node }) => node === child);
const nodeId = getNodeId(child);
const reference = nodes.find(({ id }) => id === nodeId);

reference.value = '';
updateNodeValue(child);
Expand Down
33 changes: 23 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2527,6 +2527,13 @@ prop-types@^15.6.0:
loose-envify "^1.3.1"
object-assign "^4.1.1"

prop-types@^15.6.2:
version "15.6.2"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.6.2.tgz#05d5ca77b4453e985d60fc7ff8c859094a497102"
dependencies:
loose-envify "^1.3.1"
object-assign "^4.1.1"

prr@~1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/prr/-/prr-1.0.1.tgz#d3fc114ba06995a45ec6893f484ceb1d78f5f476"
Expand Down Expand Up @@ -2609,23 +2616,23 @@ rc@^1.1.7:
minimist "^1.2.0"
strip-json-comments "~2.0.1"

react-reconciler@^0.11.0:
version "0.11.0"
resolved "https://registry.yarnpkg.com/react-reconciler/-/react-reconciler-0.11.0.tgz#b072b5a87bee85a0716f9ce0d8bc759c128c40ac"
react-reconciler@^0.15.0:
version "0.15.0"
resolved "https://registry.yarnpkg.com/react-reconciler/-/react-reconciler-0.15.0.tgz#d29cbfea16052a7086a49dc5e49c1c83640e736c"
dependencies:
fbjs "^0.8.16"
loose-envify "^1.1.0"
object-assign "^4.1.1"
prop-types "^15.6.0"
prop-types "^15.6.2"
schedule "^0.5.0"

react@^16.4.0:
version "16.4.0"
resolved "https://registry.yarnpkg.com/react/-/react-16.4.0.tgz#402c2db83335336fba1962c08b98c6272617d585"
react@^16.5.2:
version "16.5.2"
resolved "https://registry.yarnpkg.com/react/-/react-16.5.2.tgz#19f6b444ed139baa45609eee6dc3d318b3895d42"
dependencies:
fbjs "^0.8.16"
loose-envify "^1.1.0"
object-assign "^4.1.1"
prop-types "^15.6.0"
prop-types "^15.6.2"
schedule "^0.5.0"

read-pkg-up@^2.0.0:
version "2.0.0"
Expand Down Expand Up @@ -2805,6 +2812,12 @@ safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@~5.1.0,
version "5.1.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853"

schedule@^0.5.0:
version "0.5.0"
resolved "https://registry.yarnpkg.com/schedule/-/schedule-0.5.0.tgz#c128fffa0b402488b08b55ae74bb9df55cc29cc8"
dependencies:
object-assign "^4.1.1"

schema-utils@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-0.3.0.tgz#f5877222ce3e931edae039f17eb3716e7137f8cf"
Expand Down

0 comments on commit 8cd58ee

Please sign in to comment.