Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh(Scanner): Improve move matching #1774

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/lib/Scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ export default class Scanner<L1 extends TItemLocation, L2 extends TItemLocation>
// give the browser time to breathe
await Promise.resolve()
const removedItem = removeAction.payload
const oldItem = removedItem.findItemFilter(createdItem.type, item => this.mergeable(item, createdItem))
const oldItem = removedItem.findItemFilter(
createdItem.type,
item => this.mergeable(item, createdItem),
item => item.childrenSimilarity(createdItem)
)
if (oldItem) {
let oldIndex
this.result.CREATE.retract(createAction)
Expand Down Expand Up @@ -215,7 +219,11 @@ export default class Scanner<L1 extends TItemLocation, L2 extends TItemLocation>
await this.diffItem(oldItem, createdItem)
}
} else {
const newItem = createdItem.findItemFilter(removedItem.type, item => this.mergeable(removedItem, item))
const newItem = createdItem.findItemFilter(
removedItem.type,
item => this.mergeable(removedItem, item),
item => item.childrenSimilarity(createdItem)
)
let index
if (newItem) {
this.result.REMOVE.retract(removeAction)
Expand Down
23 changes: 20 additions & 3 deletions src/lib/Tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export class Bookmark<L extends TItemLocation> {
return false
}

childrenSimilarity<L2 extends TItemLocation>(otherItem: TItem<L2>): number {
return 0
}

async hash():Promise<string> {
if (!this.hashValue) {
this.hashValue = await Crypto.sha256(
Expand Down Expand Up @@ -107,7 +111,7 @@ export class Bookmark<L extends TItemLocation> {
}

// TODO: Make this return the correct type based on the type param
findItemFilter(type:TItemType, fn:(Item)=>boolean):TItem<L>|null {
findItemFilter(type:TItemType, fn:(item:TItem<L>)=>boolean, prefer:(item: TItem<L>)=>number = () => 1):TItem<L>|null {
if (type === ItemType.BOOKMARK && fn(this)) {
return this
}
Expand Down Expand Up @@ -185,11 +189,13 @@ export class Folder<L extends TItemLocation> {
}

// eslint-disable-next-line no-use-before-define
findItemFilter(type:TItemType, fn:(Item)=>boolean):TItem<L>|null {
findItemFilter(type:TItemType, fn:(Item)=>boolean, prefer:(Item)=>number = () => 1):TItem<L>|null {
if (!this.index) {
this.createIndex()
}
return Object.values(this.index[type]).find(fn)
const candidates = Object.values(this.index[type]).filter(fn)
// return the preferred match based on a preference measure
return candidates.sort((a,b) => prefer(a) - prefer(b)).pop()
}

findFolder(id:string|number): Folder<L> {
Expand Down Expand Up @@ -256,6 +262,17 @@ export class Folder<L extends TItemLocation> {
return false
}

childrenSimilarity<L2 extends TItemLocation>(otherItem: TItem<L2>): number {
if (otherItem instanceof Folder) {
return this.children.reduce(
(count, item) =>
otherItem.children.filter(i => i.title === item.title).length > 0 ? count + 1 : count,
0
) / Math.max(this.children.length, otherItem.children.length)
}
return 0
}

async hash(preserveOrder = false): Promise<string> {
if (this.hashValue && this.hashValue[String(preserveOrder)]) {
return this.hashValue[String(preserveOrder)]
Expand Down
Loading