Skip to content

Instantly share code, notes, and snippets.

View dpalita's full-sized avatar

David Palita dpalita

View GitHub Profile
const square = (operand: number): number => operand * operand
const four = square(2)
const fetchNews = (subject: string): Promise<News[]> => {
return fetch('http://newsApi?subject=' + subject)
// promise
.then(res => res.text() /* re promise ^^ */)
}
const trumpNews = fetchNews('Trump')
const kimNews = fetchNews('Kim Jong-Un')
trumpNews.then(news => readNewsAndLaugh(news))
kimNews.then(news => readNewsAndLaugh(news))
Promise.all(trumpNews, kimNews)
const observableOfOddSquares =
// observable source
Observable.of(
// stream d'entier
1, 2, 3, 4, 5, 6
)
// observer du stream d'entier, observable d'impairs
.filter(num => num % 2)
// observer du stream d'impairs, observable de carrés d'impairs
.map(odd => odd * odd)
const bitcoinQuotePromise = fetchQuoteAsPromise('btc') // la requête est émise ici
bitcoinQuotePromise.then(news => buy())
bitcoinQuotePromise.then(news => tweet())
// une seule requête http a été émise
const bitcoinQuoteObservable = fetchQuoteAsObservable('btc') // rien
bitcoinQuoteObservable.subscribe(news => buy()) // la requête est émise ici
bitcoinQuoteObservable.subscribe(news => tweet()) // une autre requête est émise ici
// deux requêtes ont été émises
function handleNotifications () {
const notifications = getNotifications() // as Notification[]
log(notifications)
displayNotificationCounter(notifications.length)
updateNotificationPanel(notifications)
...
}
// vs différents blocs indépendants du moment que l'observable de notifications est accessible
const notifications$ = getNotifications() // as Observable<Notification[]>
notifications$.subscribe(notifs => log(notifs))
@dpalita
dpalita / medium-ngrx-indexeddb-APP_INITIALYZER.ts
Last active August 2, 2018 20:18
Provide promises to the angular APP_INITIALYZER to put application initialization on hold
export function accountReducerBootstrapFactory(reducer: AccountReducer) {
// bootstrap() returns a Promise
return () => reducer.bootstrap()
}
@NgModule({
...
providers: [
{
provide: APP_INITIALIZER,
@dpalita
dpalita / medium-ngrx-indexedb-bootstrap.ts
Last active August 2, 2018 20:18
Store ngrx initial state read via a promise in indexedDb
@Injectable()
export class AccountReducer {
private initialState: AccountState
...
bootstrap(): Promise<AccountState> {
return new Promise(resolve =>
// rehydrate uses indexedDb to read the state
this.rehydrate(this.storage).subscribe(initialState => {
this.initialState = initialState
resolve()
@dpalita
dpalita / medium-ngrx-indexedb-bootstrap.ts
Last active August 2, 2018 20:17
Store ngrx initial state read via a promise in indexedDb
@Injectable()
export class AccountReducer {
private initialState: AccountState
...
bootstrap(): Promise<AccountState> {
return new Promise(resolve =>
// rehydrate uses indexedDb to read the state
this.rehydrate(this.storage).subscribe(initialState => {
this.initialState = initialState
resolve()
@dpalita
dpalita / medium-ngrx-indexeddb-AppInitAction.ts
Last active August 2, 2018 20:17
Trigger an action at app init
@NgModule({
...
})
export class AppModule {
constructor(applicationInitStatus: ApplicationInitStatus,
store: Store<AppState>) {
// dispatch the app init action when all stores have add a chance to rehydrate from indexedDb
applicationInitStatus.donePromise.then(() => store.dispatch(new AppInitAction()))
}
}
@dpalita
dpalita / medium-ngrx-indexeddb-initialState.ts
Created August 2, 2018 20:09
Use the AppInitAction and the previously stored initial state to bootstrap the app
@Injectable()
export class AccountReducer extends EntityReducer<Account> {
private initialState: AccountState
createReducer() {
return (state: AccountState, action: Action) => {
if (action instanceof AppInitAction && state === undefined) {
return initialState
} else {
...