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
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); |
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
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 |
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
@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$; |
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 { NgModule } from '@angular/core'; | |
import { MODEL_PROVIDER } from './model.service'; | |
@NgModule({ | |
/* ... */ | |
providers: [MODEL_PROVIDER] | |
}) | |
export class CoreModule { } |
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
/* route configuration */ | |
const routes: Routes = [ | |
{ | |
path: 'todos', | |
component: TodosComponent, | |
resolve: { | |
todosInitialized: TodosService | |
} | |
} | |
/* ... */ |
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
/* 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; | |
} |
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
@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}} |
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
export class TodosComponent { | |
/* ... */ | |
onTodoToggleClick(todo: Todo) { | |
this.todosService.toggleTodo(todo.name); | |
} | |
} |
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
@Injectable() | |
export class TodosService { | |
/* ... */ | |
// pass all neccessary data as funcion parameters | |
toggleTodo(name: string) { | |
// retrieve model internal state | |
const todos = this.model.get(); |
NewerOlder