Skip to content

Instantly share code, notes, and snippets.

View CharlieGreenman's full-sized avatar
🦘
razroo.com | Code Hive Mind

Charlie Greenman CharlieGreenman

🦘
razroo.com | Code Hive Mind
View GitHub Profile
@CharlieGreenman
CharlieGreenman / moment.d.ts
Created November 14, 2019 10:41
Example snippet from the moment.js declaration file
declare namespace moment {
//..
interface Moment extends Object {
format(format?: string): string;
startOf(unitOfTime: unitOfTime.StartOf): Moment;
endOf(unitOfTime: unitOfTime.StartOf): Moment;
//..
}
//..
@CharlieGreenman
CharlieGreenman / actions.d.ts
Created November 11, 2019 10:57
actions.d.ts file from the ngrx/effects library
export declare class Actions<V = Action> extends Observable<V> {
constructor(source?: Observable<V>);
lift<R>(operator: Operator<V, R>): Observable<R>;
//..
}
@CharlieGreenman
CharlieGreenman / data-persistence.d.ts
Created November 11, 2019 10:55
Nrwl data-persistence file
/**
* @whatItDoes Provides convenience methods for implementing common operations of persisting data.
*/
export declare class DataPersistence <T> {
store: Store<T>;
actions: Actions;
constructor(store: Store<T>, actions: Actions);
// ...
@CharlieGreenman
CharlieGreenman / posts.effects.ts
Created November 11, 2019 10:51
Posts Effects Example, for use of hot observables
@Effect() loadPosts$ = this.dataPersistence.fetch(
PostsActionTypes.LoadPosts,
{
run: (action: LoadPosts, state: PostsPartialState) => {
return this.postsService.getPosts().pipe(
map((posts: Post[]) => new PostsLoaded(posts))
);
},
onError: (action: LoadPosts, error) => {
console.error('Error', error);
@CharlieGreenman
CharlieGreenman / hot-observable.ts
Created November 11, 2019 10:49
Hot Observable with two subscribers
import { Subject } from 'rxjs';
const random = Math.random();
const randomVal$ = new Subject().next(random);
// Subscribe to run the combined functions
randomVals$.subscribe(x => console.log(x));
// 0.8771441274333971 (random number)
randomVals$.subscribe(x => console.log(x));
// 0.8771441274333971 (random number)
@CharlieGreenman
CharlieGreenman / two-subscribes.ts
Created November 11, 2019 10:39
Rxjs, cold, with two subscribes
import { Subject } from 'rxjs';
const randomVals$ = new Subject().next(Math.random());}
// Subscribe to run the combined functions
randomVals$.subscribe(x => console.log(x));
// 0.24957144215097515 (random number)
randomVals$.subscribe(x => console.log(x));
// 0.004617340049055896 (random number)
@CharlieGreenman
CharlieGreenman / two-subscribes.ts
Created November 11, 2019 10:39
Rxjs, cold, with two subscribes
import { Subject } from 'rxjs';
const randomVals$ = new Subject().next(Math.random());}
// Subscribe to run the combined functions
randomVals$.subscribe(x => console.log(x));
// 0.24957144215097515 (random number)
randomVals$.subscribe(x => console.log(x));
// 0.004617340049055896 (random number)
@CharlieGreenman
CharlieGreenman / pokemon.effects.ts
Created November 10, 2019 14:57
Pokemon map example
@Effect()
loadAllBlogPosts$: Observable<any> = this.actions$.pipe(
ofType(PokemonActions.loadPokemon),
mergeMap(() =>
this.postsService.getAll().pipe(
map(posts => PokemonActions.loadPokemonSuccess({ posts })),
catchError(message => of(PokemonActions.loadPostsFailed({ message })))
)
)
);
@CharlieGreenman
CharlieGreenman / search-bar.effects.ts
Created November 10, 2019 14:47
switchMap Example
@Effect()
findAddresses: Observable<any> = this.actions.pipe(
ofType(LocationActionTypes.FindAddresses),
map(action => action.partialAddress),
debounceTime(400),
distinctUntilChanged(),
switchMap(partialAddress => this.backend
.findAddresses(partialAddress)
.pipe(
map(results => new FindAddressesFulfilled(results)),
@CharlieGreenman
CharlieGreenman / pokemon.effects.ts
Created November 10, 2019 14:42
mergeMap effects example
@Effect()
loadAllBlogPosts$: Observable<any> = this.actions$.pipe(
ofType(PokemonActions.loadPokemon),
mergeMap(() =>
this.postsService.getAll().pipe(
map(posts => PokemonActions.loadPokemonSuccess({ posts })),
catchError(message => of(PokemonActions.loadPostsFailed({ message })))
)
)
);