Skip to content

Instantly share code, notes, and snippets.

View LayZeeDK's full-sized avatar
🇩🇰
Denmark

Lars Gyrup Brink Nielsen LayZeeDK

🇩🇰
Denmark
View GitHub Profile
import { Component, ContentChild } from '@angular/core';
import { map } from 'rxjs/operators';
import { ConnectionService } from './connection.service';
import { FastDirective} from './fast.directive';
import { SlowDirective} from './slow.directive';
@Component({
selector: 'connection',
template: `
@LayZeeDK
LayZeeDK / search.presenter.ts
Last active October 19, 2018 21:12
Basic search presenter
import { Subject } from 'rxjs';
export class SearchPresenter {
private _searchQuery: Subject<string> = new Subject();
searchQuery: Observable<string> = this._searchQuery.asObservable();
search(query: string): void {
this._searchQuery.next(query);
}
@LayZeeDK
LayZeeDK / search.presenter.ts
Last active October 19, 2018 21:12
Debounced, distinct search presenter
import { Subject } from 'rxjs';
import { debounceTime, distinctUntilChanged } from 'rxjs/operators';
export class SearchPresenter {
private _searchQuery: Subject<string> = new Subject();
searchQuery: Observable<string> = this._searchQuery.pipe(
debounceTime(150),
distinctUntilChanged(),
);
@LayZeeDK
LayZeeDK / bloc-file-structure-for-angular-apps.txt
Last active October 19, 2018 21:41
BLoC file structure for Angular apps
src
+---app
| |---search
| | +---app-search-repository.ts
| | +---app-search.bloc.ts
| | +---index.ts
| | +---search.component.css
| | +---search.component.html
| | +---search.component.ts
| | \---search.module.ts
@LayZeeDK
LayZeeDK / app-search-repository.ts
Created October 19, 2018 21:50
Search repository implementation for search BLoC
import { Injectable } from '@angular/core';
import * as faker from 'faker';
import { of as observableOf } from 'rxjs';
import { delay } from 'rxjs/operators';
import { SearchRepository } from '../../business-logic/search';
@Injectable()
export class AppSearchRepository implements SearchRepository {
async search(query: string) {
@LayZeeDK
LayZeeDK / app-search.bloc.ts
Created October 19, 2018 21:51
Angular wrapper around search BLoC
import { Injectable, OnDestroy } from '@angular/core';
import { SearchBloc, SearchRepository } from '../../business-logic/search';
@Injectable()
export class AppSearchBloc extends SearchBloc implements OnDestroy {
constructor(repository: SearchRepository) {
super(repository);
}
@LayZeeDK
LayZeeDK / search.component.html
Last active October 19, 2018 22:00
Search component with search BLoC
<div cdkTrapFocus [cdkTrapFocusAutoCapture]="false">
<mat-form-field appearance="outline" style="width: 80%;">
<input
matInput
placeholder="Search for..."
ngModel (ngModelChange)="bloc.query.next($event)">
</mat-form-field>
</div>
<span> {{ bloc.preamble$ | async }} </span>
@LayZeeDK
LayZeeDK / search.component.html
Created October 19, 2018 21:59
Search component that delegates to search BLoC
<div cdkTrapFocus [cdkTrapFocusAutoCapture]="false">
<mat-form-field appearance="outline" style="width: 80%;">
<input
matInput
placeholder="Search for..."
ngModel (ngModelChange)="search($event)">
</mat-form-field>
</div>
<span> {{ preamble$ | async }} </span>
@LayZeeDK
LayZeeDK / dashboard.component.ts
Created October 20, 2018 21:29
Dashboard: Mixed component model after extracting a container component
import { Component } from '@angular/core';
import { Hero } from '../hero';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: [ './dashboard.component.css' ]
})
export class DashboardComponent {
@LayZeeDK
LayZeeDK / heroes.container.spec.ts
Last active October 25, 2018 18:58
Heroes: Testing deletion of a hero
describe('deletes a hero', () => {
it(`delegates to ${HeroService.name}`, () => {
const gamora: Hero = femaleMarvelHeroes.find(x => x.name === 'Gamora');
container.delete(gamora);
expect(heroServiceStub.deleteHero).toHaveBeenCalledTimes(1);
expect(heroServiceStub.deleteHero).toHaveBeenCalledWith(gamora);
});
});