forked from magicismight/react-native-root-siblings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (95 loc) · 2.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import React, { Component, cloneElement } from 'react';
import { StyleSheet, View, AppRegistry } from 'react-native';
import StaticContainer from 'static-container';
import { Provider } from 'react-redux';
const styles = StyleSheet.create({
container: {
flex: 1
}
});
let uuid = 0;
const triggers = [];
AppRegistry.setWrapperComponentProvider(function () {
return class extends Component {
static displayName = 'RootSiblingsWrapper';
constructor(props) {
super(props);
this._siblings = {};
}
componentWillMount() {
triggers.push(this._update);
}
componentWillUnmount() {
triggers.splice(triggers.indexOf(this._update), 1);
}
_updatedSiblings = {};
_siblings = {};
_stores = {};
_update = (id, element, callback, store) => {
const siblings = { ...this._siblings };
const stores = { ...this._stores };
if (siblings[id] && !element) {
delete siblings[id];
delete stores[id];
} else if (element) {
siblings[id] = element;
stores[id] = store;
}
this._updatedSiblings[id] = true;
this._siblings = siblings;
this._stores = stores;
this.forceUpdate(callback);
};
render() {
const siblings = this._siblings;
const stores = this._stores;
const elements = [];
Object.keys(siblings).forEach((key) => {
const element = siblings[key];
if (element) {
const sibling = (
<StaticContainer
key={`root-sibling-${key}`}
shouldUpdate={!!this._updatedSiblings[key]}
>
{element}
</StaticContainer>
);
const store = stores[key];
elements.push(store ? (
<Provider store={store} key={`root-sibling-${key}-provider`}>
{sibling}
</Provider>
) : sibling);
}
});
this._updatedSiblings = {};
return (
<View style={styles.container}>
<StaticContainer shouldUpdate={false}>
{this.props.children}
</StaticContainer>
{elements}
</View>
);
}
}
})
export default class _ {
constructor(element, callback, store) {
const id = uuid++;
function update(element, callback, store) {
triggers.forEach(function (trigger) {
trigger(id, element, callback, store);
});
};
function destroy (callback) {
triggers.forEach(function (trigger) {
trigger(id, null, callback);
});
};
update(element, callback, store);
this.update = update;
this.destroy = destroy;
}
}