[js][internals] _NAMESPACE & _NATIVE_MODULE static props for all modules

This commit is contained in:
Salakar
2017-08-17 17:58:28 +01:00
parent 75c6a8f787
commit 9825226665
14 changed files with 287 additions and 42 deletions

92
tests/src/bench.js Normal file
View File

@@ -0,0 +1,92 @@
import React, { Component } from 'react';
import { View, Button, Text } from 'react-native';
import sinon from 'sinon';
import 'should-sinon';
import Promise from 'bluebird';
import firebase from './firebase';
import DatabaseContents from './tests/support/DatabaseContents';
export default class HomeScreen extends Component {
constructor(props) {
super(props);
this.state = {
timeTaken: '',
};
}
clickMe = () => {
this.setState({ timeTaken: 'Running...' });
let start = null;
Promise.all([
firebase.native.database().ref('tests/types').set(DatabaseContents.DEFAULT),
firebase.native.database().ref('tests/priority').setWithPriority({
foo: 'bar',
}, 666),
firebase.native.database().ref('tests/query').set(DatabaseContents.QUERY),
]).then(() => {
start = Date.now();
return Promise.each(Object.keys(DatabaseContents.DEFAULT), async (dataRef) => {
// Setup
const ref = firebase.native.database().ref(`tests/types/${dataRef}`);
const currentDataValue = DatabaseContents.DEFAULT[dataRef];
const callbackA = sinon.spy();
const callbackB = sinon.spy();
// Test
await new Promise((resolve) => {
ref.on('value', (snapshot) => {
callbackA(snapshot.val());
resolve();
});
});
await new Promise((resolve) => {
ref.on('value', (snapshot) => {
callbackB(snapshot.val());
resolve();
});
});
callbackA.should.be.calledWith(currentDataValue);
callbackA.should.be.calledOnce();
callbackB.should.be.calledWith(currentDataValue);
callbackB.should.be.calledOnce();
const newDataValue = DatabaseContents.NEW[dataRef];
await ref.set(newDataValue);
await new Promise((resolve) => {
setTimeout(() => resolve(), 5);
});
callbackA.should.be.calledWith(newDataValue);
callbackB.should.be.calledWith(newDataValue);
callbackA.should.be.calledTwice();
callbackB.should.be.calledTwice();
// Tear down
ref.off('value');
return Promise.resolve();
});
}).then(() => {
this.setState({ timeTaken: `Took ${Date.now() - start}` });
}).catch(console.error);
};
render() {
return (
<View style={{ marginTop: 15, backgroundColor: '#000' }}>
<Button title="Run Test" onPress={this.clickMe} />
<Text style={{ color: '#fff' }}>{this.state.timeTaken || ''}</Text>
</View>
);
}
}

View File

@@ -1,5 +1,5 @@
import React, { Component } from 'react';
import { View, Text, Button } from 'react-native';
import { View, SectionList, Text, Button } from 'react-native';
export default class HomeScreen extends Component {
@@ -28,3 +28,132 @@ export default class HomeScreen extends Component {
);
}
}
const sampleData = {
somePostId1: {
title: 'today now',
timestamp: Date.now(),
startOfDay: 1502838000,
},
somePostId3: {
title: 'today but older',
timestamp: Date.now() - 10000000,
startOfDay: 1502838000,
},
somePostId4: {
title: 'today but even older',
timestamp: Date.now() - 60000000,
startOfDay: 1502838000,
},
somePostId2: {
title: 'hello yesterday',
timestamp: Date.now() - 82000000, // minus 23 hours - just to make it yesterday ;p
startOfDay: 1502751600, // yesterday ;p
},
somePostId5: {
title: 'hello yesterday but older',
timestamp: Date.now() - 82800000, // minus 23 hours - just to make it yesterday ;p
startOfDay: 1502751600, // yesterday ;p
},
};
// export default class PostsScreen extends Component {
// constructor(props) {
// super(props);
// this.ref = null;
// this.state = {
// postSections: [],
// };
// }
//
// componentDidMount() {
// // this.ref = firebase.database().ref('posts');
// // this.ref.on('value', this._onPostsUpdate);
// // just fake it to test
// this._onPostsUpdate({
// val() {
// return sampleData;
// },
// });
// }
//
// componentWillUnmount() {
// // always unsubscribe from realtime events when component unmounts
// // if (this.ref) {
// // this.ref.off('value', this._onPostsUpdate);
// // }
// }
//
// _onPostsUpdate(snapshot) {
// const value = snapshot.val() || {};
// const keys = Object.keys(value);
// const sections = {};
//
// // we'll group them now by date
// for (let i = 0, len = keys.length; i < len; i++) {
// const key = keys[i];
// const post = value[key];
//
// // assuming post will have a 'timestamp' field and a `startOfDay` field
// // start of day can be calculated as above `startOfToday`
//
// if (!sections[post.startOfDay]) {
// sections[post.startOfDay] = {
// title: 'Header - I will leave this up to you', // todo today/yesterday/3 days ago etc
// // will use this later to sort the sections so today is on top
// key: post.startOfDay,
// data: [],
// };
// }
//
// const data = Object.assign({ key }, post);
// // add a post to a specific section date
// // we'll push/unshift depending on the date, so they' appear in order
// if (!sections[post.startOfDay].data.length) {
// // array is empty so nothing to compare sort, just push it
// sections[post.startOfDay].data.push(data);
// } else {
// const previousTimestamp = sections[post.startOfDay].data[sections[post.startOfDay].data.length - 1].timestamp;
// if (previousTimestamp < data.timestamp) sections[post.startOfDay].data.unshift(data);
// else sections[post.startOfDay].data.push(data);
// }
// }
//
// this.setState({
// postSections: Object.values(sections).sort((a, b) => a.key > b.key).reverse(),
// });
// }
//
// _renderSectionItem = ({ item }) => {
// // todo your custom section item component
// // return (
// // <EventCell
// // userName={item.userName}
// // postTitle={item.postTitle}
// // />
// // );
//
// return <Text>{`${item.title} - ${item.timestamp}`}</Text>;
// };
//
// _renderSectionHeader = ({ section }) => {
// // todo your custom section header
// return (
// <Text style={{ backgroundColor: '#000', color: '#fff' }}>{section.title}</Text>
// );
// };
//
// render() {
// return (
// <SectionList
// sections={this.state.postSections}
// renderItem={this._renderSectionItem}
// renderSectionHeader={this._renderSectionHeader}
// />
// );
// }
// }