Created
April 17, 2019 20:09
-
-
Save airportyh/45a548ff92e2040d3d2394cd87d2eafc to your computer and use it in GitHub Desktop.
This file contains 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
import { Subject, BehaviorSubject, Observable } from "rxjs"; | |
const subject: Observable<string> = new BehaviorSubject("Hello"); | |
subject.subscribe((value) => { | |
console.log("1st sub: received value", value); | |
}); | |
subject.next("How are you doing?"); | |
subject.next("Goodbye"); | |
subject.subscribe((value) => { | |
console.log("2nd: sub: received value", value); | |
}); | |
class SomeComponent { | |
@Input() thing$: BehaviorSubject<any>; | |
} |
This file contains 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
import { GlobalEventService } from "../global-event"; | |
const globalEventService = new GlobalEventService(); | |
window["globalEventService"] = globalEventService; | |
const ping$ = globalEventService.listenToEvent("ping"); | |
ping$.subscribe((value) => { | |
console.log("got pinged with", value); | |
}); | |
globalEventService.sendEvent("ping", 1); | |
This file contains 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
import { Subject, Observable } from "rxjs"; | |
import { filter, map } from "rxjs/operators"; | |
class GlobalEventService { | |
subject$ = new Subject(); | |
listenToEvent(event: string): Observable<any> { | |
return this.subject$.pipe( | |
filter((event: any) => { | |
return event.type === event; | |
}), | |
map((event) => event.data) | |
); | |
} | |
sendEvent(event: string, data: any): void { | |
this.subject$.next({ | |
type: event, | |
data: data | |
}); | |
} | |
} |
This file contains 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
import { Subject, ReplaySubject } from "rxjs"; | |
const subject: Subject<string> = new ReplaySubject(); | |
subject.subscribe((value) => { | |
console.log("1st sub: received value", value); | |
}); | |
subject.next("Hello"); | |
subject.next("How are you doing?"); | |
subject.next("Goodbye"); | |
subject.subscribe((value) => { | |
console.log("2nd: sub: received value", value); | |
}); |
This file contains 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
import { Store } from "../store"; | |
interface IUser { | |
username: string; | |
email: string; | |
} | |
interface IAppState { | |
user?: IUser; | |
} | |
const initialState = { | |
user: { | |
username: "Don" | |
} | |
}; | |
const store = new Store(initialState); | |
store.subject$.subscribe((value) => { | |
console.log("the whole app state changed", value); | |
}); | |
window["store"] = store; | |
const username$ = store.get(["user", "username"]); | |
const user$ = store.get(["user"]); | |
user$.subscribe((value) => { | |
value.email = "dog@cat.com"; | |
}); | |
username$.subscribe((value) => { | |
console.log("got username", value); | |
}); | |
store.set(["user", "username"], "Daria"); |
This file contains 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
import { Observable, BehaviorSubject, Subject } from "rxjs"; | |
import _ = require("lodash"); | |
import { set } from "lodash/fp"; | |
import { map, distinctUntilChanged } from "rxjs/operators"; | |
export class Store { | |
subject$: BehaviorSubject<any>; | |
constructor(initialState: any) { | |
this.subject$ = new BehaviorSubject(initialState); | |
} | |
get(path: string[]): Observable<any> { | |
return this.subject$.pipe( | |
map((state) => _.get(state, path)), | |
distinctUntilChanged() | |
); | |
} | |
set(path: string[], value: any): void { | |
_.set(this.subject$.value, path, value); | |
this.subject$.next(this.subject$.value); | |
} | |
} |
This file contains 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
import { Subject, Observable } from "rxjs"; | |
const subject: Subject<string> = new Subject(); | |
window["subject"] = subject; | |
subject.subscribe((value) => { | |
console.log("1st sub: received value", value); | |
}); | |
subject.next("Hello"); | |
subject.next("How are you doing?"); | |
subject.subscribe((value) => { | |
console.log("2nd: sub: received value", value); | |
}); | |
subject.next("Goodbye"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment