Skip to content

Instantly share code, notes, and snippets.

@aakashns
aakashns / fromDb.js
Created March 22, 2017 16:31
First version of fromDb
const fromDb = (db, dispatch) => {
db.ref('/message').on('value', data => {
if (data.val()) {
dispatch({ type: 'SET_MESSAGE', payload: data.val() });
}
});
};
@aakashns
aakashns / fromStore.js
Created March 22, 2017 17:17
Basic version of fromStore.
const fromStore = (state, db) => {
db.ref('message').set(state.message);
}
@aakashns
aakashns / linkStoreWithDb.js
Created March 23, 2017 08:54
Simple version of linkStoreWithDb
const linkStoreWithDb = (fromDb, fromStore) => {
return (db, store) => {
fromDb(db, store.dispatch);
store.subscribe(() => fromStore(store.getState(), db)
}
}
@aakashns
aakashns / linkMessage.js
Created March 23, 2017 08:59
Simple linkMessage example.
const linkMessage = linkStoreWithDb(fromDb, fromStore)
linkMessage(firebase.database, store)
let mustWrite = true; // Should we write the database?
const fromDb = (db, dispatch) => {
const listener = db.ref('/message').on('value', snap => {
if (snap.val()) {
mustWrite = false; // Don't write the next dispatch
dispatch({ type: 'SET_MESSAGE', payload: data.val()});
mustWrite = true; // Done! Start writing again
}
});
@aakashns
aakashns / linkStoreWithDb.js
Last active March 23, 2017 13:47
Smart version of linkStoreWithDb
const linkStoreWithDb = (fromDb, fromStore) => {
return (db, store) => {
const unlinkFromDb = fromDb && fromDb(db, store.dispatch);
const unlinkFromStore = fromStore && store.subscribe(() => fromStore(store.getState, db));
const unlink = () => {
unlinkFromDb && unlinkFromDb();
unlinkFromStore && unlinkFromStore();
}
return unlink;
componentDidMount() {
const { store } = this.props.store;
this.unlink = linkStoreWithDb(fromDb, fromStore)(
firebase.database(),
store
);
}
componentWillUnmount() {
this.unlink();
@aakashns
aakashns / linkStoreWithPath.js
Last active March 23, 2017 15:54
Working instance of linkStateWithPath
const linkStoreWithPath = (path, actionCreator, selector) => {
return (db, store) => {
let previous = selector(store.getState());
let mustWrite = true;
const fromDb = (db, dispatch) => {
const listener = db.ref(path).on("value", snap => {
if (snap.val()) {
mustWrite = false;
dispatch(actionCreator(snap.val()));
const messagePath = '/dummy';
const messageActionCreator = (value) => ({
type: 'SET_MESSAGE',
payload: value
});
const messageSelector = (state) => state.dummy;
const linkMessage = linkStoreWithPath(messagePath, messageActionCreator, messageSelector)
@aakashns
aakashns / install-opencv-2.4.13.4-in-ubuntu.sh
Last active December 1, 2017 13:04 — forked from arthurbeggs/install_opencv2_ubuntu.sh
Install opencv-2.4.13.4 in Ubuntu
#!/bin/bash
# install dependencies
sudo apt-get update
sudo apt-get install -y build-essential
sudo apt-get install -y cmake
sudo apt-get install -y libgtk2.0-dev
sudo apt-get install -y pkg-config
sudo apt-get install -y python-numpy python-dev
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install -y libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev