Skip to content

Instantly share code, notes, and snippets.

View tomastrajan's full-sized avatar
🍰
Where is the cake?

Tomas Trajan tomastrajan

🍰
Where is the cake?
View GitHub Profile
export function cancelable() : MethodDecorator {
return function ( target : any, propertyKey : string, descriptor : PropertyDescriptor ) {
const originalFn = descriptor.value;
const subject = new BehaviorSubject(undefined);
const result$ = subject.asObservable().switchMap(args => originalFn(...args));
descriptor.value = (...args) => {
subject.next(args);
@tomastrajan
tomastrajan / ampe-app-service.ts
Last active September 16, 2019 04:58
Angular Model Pattern - orchestration
export class AuthApplicationService {
// inject all needed services
constructor(
private sessionService: SessionService,
private authService: AuthService
) {}
// orchestrate multi service execution
// note that subscription and cancelation are responsibility of the caller
@tomastrajan
tomastrajan / ampe-service.ts
Created May 4, 2017 23:10
Angular Model Pattern - use model in service
@Injectable()
export class TodosService {
private model: Model<Todo[]>;
todos$: Observable<Todo[]>;
constructor(private modelFactory: ModelFactory<Todo[]>) {
this.model = this.modelFactory.create([]);
this.todos$ = this.model.data$;
@tomastrajan
tomastrajan / ampe-core-module.ts
Created May 4, 2017 22:45
Angular Model Pattern - register model provider
import { NgModule } from '@angular/core';
import { MODEL_PROVIDER } from './model.service';
@NgModule({
/* ... */
providers: [MODEL_PROVIDER]
})
export class CoreModule { }
@tomastrajan
tomastrajan / ampe-resolve.ts
Last active September 16, 2019 04:58
Angular Model Pattern - initialize model on route change
/* route configuration */
const routes: Routes = [
{
path: 'todos',
component: TodosComponent,
resolve: {
todosInitialized: TodosService
}
}
/* ... */
@tomastrajan
tomastrajan / ampe-combine-state.ts
Created May 1, 2017 11:08
Angular Model Pattern - combine state from multiple models
/* session and clients models in some component or service */
const filteredClients$ = this.sessionService.session$
.combineLatest(this.clientService.clients$)
.map(([session, clients]) => clients.filter(client => {
if (client.vip && session.accessVips) {
return true;
} else if (!client.vip) {
return true;
}
@tomastrajan
tomastrajan / ampe-component-subscription.ts
Last active May 1, 2017 10:32
Angular Model Pattern - component state subscription
@Component({
selector: 'todos',
template: `
<!--
accessing multiple properties,
we're better off subscribing only once in component's ngOnInit()
-->
Done todos: {{count.done}}
Remaining todos: {{count.remaining}}
All todos: {{count.all}}
@tomastrajan
tomastrajan / ampe-component-mutatet.ts
Created May 1, 2017 09:59
Angular Model Pattern - component service call
export class TodosComponent {
/* ... */
onTodoToggleClick(todo: Todo) {
this.todosService.toggleTodo(todo.name);
}
}
@tomastrajan
tomastrajan / ampe-mutate.ts
Created May 1, 2017 09:54
Angular Model Pattern - model mutation
@Injectable()
export class TodosService {
/* ... */
// pass all neccessary data as funcion parameters
toggleTodo(name: string) {
// retrieve model internal state
const todos = this.model.get();