##// END OF EJS Templates
Corrected Scope.own() to cleanup the supplied object immediately when the scope is disposed already
Corrected Scope.own() to cleanup the supplied object immediately when the scope is disposed already

File last commit:

r118:e07418577cbc v1.6.1 default
r131:c7d9ad82b374 v1.8.1 default
Show More
MainModel.ts
74 lines | 2.1 KiB | video/mp2t | TypeScriptLexer
import { id as mid } from "module";
import { BehaviorSubject, Observer, Unsubscribable } from "rxjs";
import { IDestroyable } from "@implab/core-amd/interfaces";
import { Observable } from "@implab/djx/observable";
import { OrderedUpdate } from "@implab/djx/store";
import { Appointment, Member } from "./Appointment";
import { MainContext } from "./MainContext";
import { LocalDate } from "@js-joda/core";
import { error } from "../logging";
import { TraceSource } from "@implab/core-amd/log/TraceSource";
import { whenRendered } from "@implab/djx/tsx/render";
const trace = TraceSource.get(mid);
export interface State {
appointments: Observable<OrderedUpdate<Appointment>>;
dateTo: LocalDate;
dateFrom: LocalDate;
title: string;
}
export default class MainModel implements IDestroyable {
private readonly _state: BehaviorSubject<State>;
private readonly _context = new MainContext();
constructor() {
this._state = new BehaviorSubject<State>({
dateTo: LocalDate.now(),
dateFrom: LocalDate.now().minusMonths(1),
appointments: this._context.queryAppointments(),
title: "Appointments"
});
}
getState() {
return this._state.getValue();
}
subscribe(observer: Partial<Observer<State>>): Unsubscribable {
return this._state.subscribe(observer);
}
protected dispatch(command: Partial<State>) {
const state = this.getState();
this._state.next({ ...state, ...command });
}
addMember(appointmentId: string, member: Member) {
this._context.addMember(appointmentId, member).catch(error(trace));
}
addAppointment(title: string, startAt: Date, duration: number) {
this._context.createAppointment(title,startAt, duration, [])
.then(() => {
trace.debug("addAppointment done");
return whenRendered();
})
.then(() => {
trace.debug("Render dome");
})
.catch(error(trace));
}
load() {
this._context.load().catch(error(trace));
}
destroy() {
this._context.destroy();
}
}