-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
createDiffWatcher
function (#1)
* feat: add diff watcher * test: add basic test cases for diff watcher * test: add a test case for multiple blocks diff watcher * feat: add `add` and `remove` methods on watcher * docs: add createDiffWatcher
- Loading branch information
Showing
7 changed files
with
381 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import assert = require('assert') | ||
import { SFCBlock, parseComponent, SFCDescriptor } from './index' | ||
|
||
export class SFCDiffWatcher { | ||
private prevMap: Record<string, SFCDescriptor> = {} | ||
|
||
add(filename: string, content: string): void { | ||
this.prevMap[filename] = parseComponent(content) | ||
} | ||
|
||
remove(filename: string): void { | ||
delete this.prevMap[filename] | ||
} | ||
|
||
diff(filename: string, content: string): SFCDiff { | ||
assert( | ||
this.prevMap.hasOwnProperty(filename), | ||
'must call `add` before calling `diff`' | ||
) | ||
|
||
const prev = this.prevMap[filename] | ||
const curr = (this.prevMap[filename] = parseComponent(content)) | ||
return new SFCDiff(prev, curr) | ||
} | ||
} | ||
|
||
export class SFCDiff { | ||
constructor(private prev: SFCDescriptor, private curr: SFCDescriptor) {} | ||
|
||
template(cb: (block: SFCBlock | null) => void): this { | ||
const prev = this.prev.template | ||
const curr = this.curr.template | ||
if (this.hasDiff(prev, curr)) { | ||
cb(curr) | ||
} | ||
|
||
return this | ||
} | ||
|
||
script(cb: (block: SFCBlock | null) => void): this { | ||
const prev = this.prev.script | ||
const curr = this.curr.script | ||
if (this.hasDiff(prev, curr)) { | ||
cb(curr) | ||
} | ||
|
||
return this | ||
} | ||
|
||
styles(cb: (blocks: SFCBlock[]) => void): this { | ||
const prev = this.prev.styles | ||
const curr = this.curr.styles | ||
if (this.hasListDiff(prev, curr)) { | ||
cb(curr) | ||
} | ||
|
||
return this | ||
} | ||
|
||
customBlocks(name: string, cb: (blocks: SFCBlock[]) => void): this { | ||
const prev = this.prev.customBlocks | ||
const curr = this.curr.customBlocks | ||
if (this.hasListDiff(prev, curr)) { | ||
cb(curr) | ||
} | ||
|
||
return this | ||
} | ||
|
||
private hasDiff(prev: SFCBlock | null, curr: SFCBlock | null): boolean { | ||
if (prev === null || curr === null) { | ||
return prev !== curr | ||
} | ||
|
||
return !prev.equals(curr) | ||
} | ||
|
||
private hasListDiff(prev: SFCBlock[], curr: SFCBlock[]): boolean { | ||
if (prev.length !== curr.length) { | ||
return true | ||
} | ||
|
||
return prev.reduce((acc, p, i) => { | ||
return acc && this.hasDiff(p, curr[i]) | ||
}, true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.