Skip to content

Instantly share code, notes, and snippets.

nanlabsweb

Block or report user

Report or block nanlabsweb

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View AwesomeComponent.js
import { ageSelector, regionSelector, isEligibleSelector} from './CoolModuleSelectors.js';
(...)
const mapStateToProps = state => ({
age: ageSelector(state),
region: regionSelector(state),
isEligible: isEligibleSelector(state),
});
View CoolModuleSelectors.js
(...)
import { createSelector } from 'reselect';
const birthDateSelector = state => state.user.birthdate;
const countrySelector = state => state.user.location.country;
export const ageSelector = createSelector(
birthDateSelector,
dateString => moment().year() - moment(dateString).year(),
);
View postSelectorsMapStateToProps.js
const mapStateToProps = state => ({
birthDate: state.user.birthdate,
country: state.user.location.country,
});
@nanlabsweb
nanlabsweb / suspendCoroutine.kt
Created Apr 5, 2018
Managing exceptions inside a coroutine.
View suspendCoroutine.kt
suspend fun <T> Task<T>.await(): T = suspendCoroutine { continuation ->
addOnCompleteListener { task ->
when {
(task.result as QuerySnapshot).metadata.isFromCache ->
continuation.resumeWithException(Exception("without internet"))
task.isSuccessful -> continuation.resume(task.result)
else -> continuation.resumeWithException(task.exception!!)
}
}
}
@nanlabsweb
nanlabsweb / getAllPetsWithCoroutines.kt
Created Apr 5, 2018
Get data from firestore with Coroutines.
View getAllPetsWithCoroutines.kt
suspend fun getAllPets(): List<Pet> {
val result = firestoreDataBase.collection("pets").get().await()
return setPetsFromSnapshotIntoMutableList(result)
}
@nanlabsweb
nanlabsweb / getAllPets.kt
Created Apr 5, 2018
Get data from firestore.
View getAllPets.kt
fun getAllPets(): LiveData<List<Pet>> {
var pets: MutableLiveData<List<Pet>> = MutableLiveData()
firestoreDataBase.collection("pets").get().addOnSuccessListener {
pets.value = setPetsFromSnapshotIntoMutableList(it)
}
return pets
}
@nanlabsweb
nanlabsweb / SyncRepoWithCoroutines.kt
Created Apr 5, 2018
Sync repository with coroutines.
View SyncRepoWithCoroutines.kt
suspend fun syncPets() {
val pets = firestoreWrapper.getAllPets()
appDataBase.petDao().deleteAllPets()
pets.forEach {
appDataBase.petDao().insert(it)
}
}
@nanlabsweb
nanlabsweb / SyncRepoWithoutCoroutines.kt
Created Apr 5, 2018
Sync Repository without coroutines.
View SyncRepoWithoutCoroutines.kt
fun syncPets(): LiveData<List<Pet>> {
liveDataMerger.addSource(firestoreWrapper.getAllPets()) {
value -> liveDataMerger.value = value
appDataBase.petDao().deleteAllPets()
value?.forEach {
appDataBase.petDao().insert(it)
}
}
return liveDataMerger
}
@nanlabsweb
nanlabsweb / syncPetsFn.kt
Created Apr 5, 2018
Sync pets with exception handling.
View syncPetsFn.kt
suspend fun syncPets(): String {
var message = "Congratulations!!"
try {
async { repository.syncPets() }.await()
} catch (e: Exception) {
message = e.message!!
}
return message
}
@nanlabsweb
nanlabsweb / syncPetsCoroutines.kt
Created Apr 5, 2018
Sync Pets with coroutines
View syncPetsCoroutines.kt
suspend fun syncPets() = repository.syncPets()
You can’t perform that action at this time.