fix move bug

This commit is contained in:
monoid 2022-06-21 14:42:40 +09:00
parent 96e71cc175
commit 1ea8ceca7a
2 changed files with 20 additions and 7 deletions

View File

@ -33,6 +33,7 @@ export class ContentDiffHandler{
private async OnDeleted(cpath: string){ private async OnDeleted(cpath: string){
const basepath = dirname(cpath); const basepath = dirname(cpath);
const filename = basename(cpath); const filename = basename(cpath);
console.log("deleted ",cpath);
//if it wait to add, delete it from waiting list. //if it wait to add, delete it from waiting list.
if(this.waiting_list.hasByPath(cpath)){ if(this.waiting_list.hasByPath(cpath)){
this.waiting_list.deleteByPath(cpath); this.waiting_list.deleteByPath(cpath);
@ -41,16 +42,22 @@ export class ContentDiffHandler{
const dbc = await this.doc_cntr.findByPath(basepath,filename); const dbc = await this.doc_cntr.findByPath(basepath,filename);
//when there is no related content in db, ignore. //when there is no related content in db, ignore.
if(dbc.length === 0) return; if(dbc.length === 0) return;
const content_hash = dbc[0].content_hash;
// When a path is changed, it takes into account when the // When a path is changed, it takes into account when the
// creation event occurs first and the deletion occurs, not // creation event occurs first and the deletion occurs, not
// the change event. // the change event.
if(this.waiting_list.hasByHash(dbc[0].content_hash)){ const cf = this.waiting_list.getByHash(content_hash);
if(cf){
//if a path is changed, update the changed path. //if a path is changed, update the changed path.
console.log("update path from", cpath,"to",cf.path);
const newFilename = basename(cf.path);
const newBasepath = dirname(cf.path);
await this.waiting_list.deleteByHash(content_hash);
await this.doc_cntr.update({ await this.doc_cntr.update({
id:dbc[0].id, id:dbc[0].id,
deleted_at: null, deleted_at: null,
filename:filename, filename:newFilename,
basepath:basepath basepath:newBasepath,
}); });
return; return;
} }
@ -64,6 +71,7 @@ export class ContentDiffHandler{
private async OnCreated(cpath:string){ private async OnCreated(cpath:string){
const basepath = dirname(cpath); const basepath = dirname(cpath);
const filename = basename(cpath); const filename = basename(cpath);
console.log("createContentFile", cpath);
const content = createContentFile(this.content_type,pathjoin(basepath,filename)); const content = createContentFile(this.content_type,pathjoin(basepath,filename));
const hash = await content.getHash(); const hash = await content.getHash();
const c = this.tombstone.get(hash); const c = this.tombstone.get(hash);
@ -76,6 +84,9 @@ export class ContentDiffHandler{
}); });
return; return;
} }
if(this.waiting_list.hasByHash(hash)){
console.log("Conflict!!!");
}
this.waiting_list.set(content); this.waiting_list.set(content);
} }
private async OnChanged(prev_path:string,cur_path:string){ private async OnChanged(prev_path:string,cur_path:string){

View File

@ -36,19 +36,21 @@ export class RecursiveWatcher extends EventEmitter implements IDiffWatcher {
option.watchFile ??= true; option.watchFile ??= true;
if(option.watchFile){ if(option.watchFile){
this.watcher.on("add",path=>{ this.watcher.on("add",path=>{
const cpath = join(this.path,path); const cpath = path;
//console.log("add ", cpath);
this.emit("create",cpath); this.emit("create",cpath);
}).on("unlink",path=>{ }).on("unlink",path=>{
const cpath = join(this.path,path); const cpath = path;
//console.log("unlink ", cpath);
this.emit("delete",cpath); this.emit("delete",cpath);
}); });
} }
if(option.watchDir){ if(option.watchDir){
this.watcher.on("addDir",path=>{ this.watcher.on("addDir",path=>{
const cpath = join(this.path,path); const cpath = path;
this.emit("create",cpath); this.emit("create",cpath);
}).on("unlinkDir",path=>{ }).on("unlinkDir",path=>{
const cpath = join(this.path,path); const cpath = path;
this.emit("delete",cpath); this.emit("delete",cpath);
}) })
} }