Skip to content

Commit

Permalink
fix: mixed single mode in ls and watcher; close #18
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Sep 6, 2019
1 parent 2e5ab36 commit 45bb024
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/DOM/LiveSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ export class LiveSelector<T, SingleMode extends boolean = false> {
* Let developer knows where does this LiveSelector created.
*/
private readonly stack = new Error().stack
private singleMode = false
/**
* Is this LiveSelector run in the SingleMode
*/
public isSingleMode = false
/**
* Enable single mode. Only 1 result will be emitted.
*/
enableSingleMode(): LiveSelector<T, true> {
this.singleMode = true
this.isSingleMode = true
return this as any
}
/**
Expand All @@ -86,7 +89,7 @@ export class LiveSelector<T, SingleMode extends boolean = false> {
clone() {
const ls = new LiveSelector<T, SingleMode>(this.initialElements)
ls.selectorChain.push(...this.selectorChain)
ls.singleMode = this.singleMode
ls.isSingleMode = this.isSingleMode
return ls
}
//#region Add elements
Expand Down Expand Up @@ -327,7 +330,7 @@ export class LiveSelector<T, SingleMode extends boolean = false> {
let previouslyNulled = false
for (const op of this.selectorChain) {
// if in single mode, drop other results.
if (this.singleMode && arr.length > 1) arr = [arr[0]]
if (this.isSingleMode && arr.length > 1) arr = [arr[0]]
switch (op.type) {
case 'querySelector': {
if (!previouslyNulled) {
Expand Down Expand Up @@ -419,7 +422,7 @@ export class LiveSelector<T, SingleMode extends boolean = false> {
throw new TypeError('Unknown operation type')
}
}
if (this.singleMode) return (arr.filter(nonNull) as T[])[0] as any
if (this.isSingleMode) return (arr.filter(nonNull) as T[])[0] as any
return (arr.filter(nonNull) as T[]) as any
}
/**
Expand Down
1 change: 1 addition & 0 deletions src/DOM/Watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export abstract class Watcher<T, Before extends Element, After extends Element,
protected readonly liveSelector: LiveSelector<T, SingleMode>
constructor(liveSelector: LiveSelector<T, SingleMode>) {
this.liveSelector = liveSelector.clone()
if (this.liveSelector.isSingleMode) this.enableSingleMode()
}
//#region How to start and stop the watcher
/** Let the watcher start to watching */
Expand Down

0 comments on commit 45bb024

Please sign in to comment.