import { action , observable , ObservableMap } from "mobx" ; import { IParsedText , ITextInfo , ITextProgress , Text } from "~/app/model" ; import { TextRepository } from "~/app/TextRepository" ; import { flowed } from "~/util/MobxUtils" ; export class TextStore { public texts : ObservableMap < number , Text > = observable . map < number , Text > ( undefined , { deep : false } ) ; public constructor ( private _textRepository ) { } @ flowed public * loadAll ( ) { const texts = yield this . _textRepository . loadAll ( ) ; texts . forEach ( t => this . texts . set ( t . id , t ) ) ; } public async add ( info , parsed ) { const newText = await this . _textRepository . save ( info , parsed ) ; this . setText ( newText ) ; } @ flowed public * delete ( id ) { yield this . _textRepository . delete ( id ) ; this . texts . delete ( id ) ; } public async setTextProgress ( id , progress ) { const updatedText = this . _textRepository . updateProgress ( id , progress ) ; this . setText ( await updatedText ) ; } @ action private setText ( text ) { this . texts . set ( text . id , text ) ; } }