Skip to content

Commit

Permalink
Fix static-container
Browse files Browse the repository at this point in the history
  • Loading branch information
magicismight committed May 4, 2017
1 parent da03334 commit 9cd2e11
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 54 deletions.
111 changes: 58 additions & 53 deletions lib/AppRegistryInjection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StyleSheet, View, AppRegistry } from 'react-native';
import React, { Component } from 'react';
import StaticContainer from 'react-native/Libraries/Components/StaticContainer';
import StaticContainer from 'static-container';
import EventEmitter from 'react-native/Libraries/EventEmitter/EventEmitter';

const styles = StyleSheet.create({
Expand All @@ -10,67 +10,72 @@ const styles = StyleSheet.create({
}
});

const emitter = new EventEmitter();
let emitter = AppRegistry.rootSiblingsEmitter;

// inject modals into app entry component
const originRegister = AppRegistry.registerComponent;
if (!(emitter instanceof EventEmitter)) {
emitter = new EventEmitter();
// inject modals into app entry component
const originRegister = AppRegistry.registerComponent;

AppRegistry.registerComponent = function (appKey, getAppComponent) {
const siblings = new Map();
const updates = new Set();
AppRegistry.registerComponent = function (appKey, getAppComponent) {
const siblings = new Map();
const updates = new Set();

return originRegister(appKey, function () {
const OriginAppComponent = getAppComponent();
return originRegister(appKey, function () {
const OriginAppComponent = getAppComponent();

return class extends Component {
static displayName = `Root(${appKey})`;
return class extends Component {
static displayName = `Root(${appKey})`;

componentWillMount() {
this._update = this._update.bind(this);
emitter.addListener('siblings.update', this._update);
};
componentWillMount() {
this._update = this._update.bind(this);
emitter.addListener('siblings.update', this._update);
};

componentWillUnmount() {
emitter.removeListener('siblings.update', this._update);
siblings.clear();
updates.clear();
};
componentWillUnmount() {
emitter.removeListener('siblings.update', this._update);
siblings.clear();
updates.clear();
};

_update(id, element, callback) {
if (siblings.has(id) && !element) {
siblings.delete(id);
} else {
siblings.set(id, element);
}
updates.add(id);
this.forceUpdate(callback);
};
_update(id, element, callback) {
if (siblings.has(id) && !element) {
siblings.delete(id);
} else {
siblings.set(id, element);
}
updates.add(id);
this.forceUpdate(callback);
};

render() {
const elements = [];
siblings.forEach((element, id) => {
elements.push(
<StaticContainer
key={`root-sibling-${id}`}
shouldUpdate={updates.has(id)}
>
{element}
</StaticContainer>
);
});
updates.clear();
render() {
const elements = [];
siblings.forEach((element, id) => {
elements.push(
<StaticContainer
key={`root-sibling-${id}`}
shouldUpdate={updates.has(id)}
>
{element}
</StaticContainer>
);
});
updates.clear();

return (
<View style={styles.container}>
<StaticContainer shouldUpdate={false}>
<OriginAppComponent {...this.props} />
</StaticContainer>
{elements}
</View>
);
return (
<View style={styles.container}>
<StaticContainer shouldUpdate={false}>
<OriginAppComponent {...this.props} />
</StaticContainer>
{elements}
</View>
);
};
};
};
});
};
});
};

AppRegistry.rootSiblingsEmitter = emitter;
}

export default emitter;
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.1.3",
"version": "1.2.1",
"name": "react-native-root-siblings",
"repository": {
"type": "git",
Expand All @@ -8,6 +8,9 @@
"license": "MIT",
"main": "./lib/SiblingsManager",
"description": "react native root sibling elements manager",
"dependencies": {
"static-container": "^1.0.0"
},
"keywords": [
"react-component",
"react-native",
Expand Down

0 comments on commit 9cd2e11

Please sign in to comment.