MainModel.ts
62 lines
| 1.8 KiB
| video/mp2t
|
TypeScriptLexer
cin
|
r110 | import { id as mid } from "module"; | |
import { BehaviorSubject, Observer, Unsubscribable } from "rxjs"; | |||
import { IDestroyable } from "@implab/core-amd/interfaces"; | |||
import { OrderUpdate, Observable } from "@implab/djx/observable"; | |||
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"; | |||
cin
|
r107 | ||
cin
|
r110 | const trace = TraceSource.get(mid); | |
export interface State { | |||
appointments: Observable<OrderUpdate<Appointment>>; | |||
cin
|
r107 | ||
cin
|
r110 | dateTo: LocalDate; | |
cin
|
r107 | ||
cin
|
r110 | dateFrom: LocalDate; | |
title: string; | |||
cin
|
r107 | } | |
export default class MainModel implements IDestroyable { | |||
private readonly _state: BehaviorSubject<State>; | |||
cin
|
r110 | ||
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" | |||
}); | |||
cin
|
r107 | } | |
getState() { | |||
return this._state.getValue(); | |||
} | |||
subscribe(observer: Partial<Observer<State>>): Unsubscribable { | |||
return this._state.subscribe(observer); | |||
cin
|
r110 | } | |
cin
|
r107 | protected dispatch(command: Partial<State>) { | |
const state = this.getState(); | |||
cin
|
r110 | this._state.next({ ...state, ...command }); | |
} | |||
addMember(appointmentId: string, member: Member) { | |||
this._context.addMember(appointmentId, member).catch(error(trace)); | |||
cin
|
r107 | } | |
cin
|
r110 | addAppointment(title: string, startAt: Date, duration: number) { | |
this._context.createAppointment(title,startAt, duration, []).catch(error(trace)); | |||
} | |||
load() { | |||
} | |||
destroy() { | |||
this._context.destroy(); | |||
} | |||
cin
|
r107 | } |