ionian/src/diff/diff.ts

45 lines
No EOL
1.6 KiB
TypeScript

import { DocumentAccessor } from '../model/doc';
import {ContentDiffHandler} from './content_handler';
import { IDiffWatcher } from './watcher';
import asyncPool from 'tiny-async-pool';
export class DiffManager{
watching: {[content_type:string]:ContentDiffHandler};
doc_cntr: DocumentAccessor;
constructor(contorller: DocumentAccessor){
this.watching = {};
this.doc_cntr = contorller;
}
async register(content_type:string,watcher:IDiffWatcher){
if(this.watching[content_type] === undefined){
this.watching[content_type] = new ContentDiffHandler(this.doc_cntr,content_type);
}
this.watching[content_type].register(watcher);
await watcher.setup(this.doc_cntr);
}
async commit(type:string,path:string){
const list = this.watching[type].waiting_list;
const c = list.getByPath(path);
if(c===undefined){
throw new Error("path is not exist");
}
await list.delete(c);
const body = await c.createDocumentBody();
const id = await this.doc_cntr.add(body);
return id;
}
async commitAll(type:string){
const list = this.watching[type].waiting_list;
const contentFiles = list.getAll();
list.clear();
const bodies = await asyncPool(30,contentFiles,async (x)=>await x.createDocumentBody());
const ids = await this.doc_cntr.addList(bodies);
return ids;
}
getAdded(){
return Object.keys(this.watching).map(x=>({
type:x,
value:this.watching[x].waiting_list.getAll(),
}));
}
};