Suppose I structure my Angular app using observable data services, backed by serverside endpoints:
@Injectable()
export class TodoService {
todos: Observable<Todo[]>
private _todos: BehaviorSubject<Todo[]>;
constructor(private http: Http) {
this._todos = <BehaviorSubject<Todo[]>>new BehaviorSubject([]);
this.todos = this._todos.asObservable();
}
loadData() {
this.http.get(dataUrl).subscribe(data => this._todos.next(data));
}
}
Now suppose my data has reference to some other model and exposed by some other service (some form of Many-to-Many relationship):
interface ToDo {
title: string;
status: StatusEnum;
requiredResouces: Resource[];
}
interface Resource {
name: string;
todos: ToDo[];
}
@Injectable()
export class ResourcesService {
resources: Observable<Resource[]>
private _resources: BehaviorSubject<Resource[]>;
...
}
Now, suppose I add a method to either service that "links" a todo and a resource, that service will be able to push an updated state down the subject, but the other service will be unaware of the change. For example:
export class TodoService {
...
addResourceRequirement(todo: ToDo, resource: Resource) {
this.http.post(`${url}/${todo.id}/`, {addResource: resource.id})
.subscribe(() => this.loadData());
}
}
Would cause any "todos" observer to refresh, but any "resources" observer would still show the old state...
What design / pattern / architecture would you use to synchronize both services?
(I know there are architectures that avoid this difficulty as a whole - particularly flux based solutions - NgRX etc... but I'm interested in a solution specifically for the observable data services pattern)