This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const square = (operand: number): number => operand * operand | |
const four = square(2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function accountReducerBootstrapFactory(reducer: AccountReducer) { | |
// bootstrap() returns a Promise | |
return () => reducer.bootstrap() | |
} | |
@NgModule({ | |
... | |
providers: [ | |
{ | |
provide: APP_INITIALIZER, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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())) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 { | |
... |
OlderNewer