-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapt useObserver from ylem-with-hooks; add more teststo check differ…
…ent cases
- Loading branch information
1 parent
62d5d6d
commit 126094c
Showing
5 changed files
with
253 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import canReflect from 'can-reflect'; | ||
import ObservationRecorder from 'can-observation-recorder'; | ||
import recorderHelpers from 'can-observation/recorder-dependency-helpers'; | ||
import queues from 'can-queues'; | ||
|
||
ObservationRecorder.resume = function resume(deps) { | ||
ObservationRecorder.stack.push(deps); | ||
}; | ||
|
||
let ORDER; | ||
let weLeftSomethingOnTheStack = false; | ||
|
||
export default class Observer { | ||
constructor(onUpdate) { | ||
this.newDependencies = ObservationRecorder.makeDependenciesRecorder(); | ||
this.oldDependencies = null; | ||
this.onUpdate = onUpdate; | ||
|
||
this.onDependencyChange = (newVal, oldVal) => { | ||
this.dependencyChange(newVal, oldVal); | ||
}; | ||
} | ||
|
||
startRecording() { | ||
if (weLeftSomethingOnTheStack) { | ||
const deps = ObservationRecorder.stop(); | ||
weLeftSomethingOnTheStack = false; | ||
|
||
if (!deps.ylem) { | ||
throw new Error( | ||
'If you see this error with another error, clearing that should solve this. If you see ' + | ||
'this error alone, please open and issue on our github and tag Christopher and Justin.' | ||
); | ||
} | ||
} | ||
|
||
this.oldDependencies = this.newDependencies; | ||
this.nextDependencies = ObservationRecorder.start(); | ||
this.nextDependencies.ylem = true; | ||
weLeftSomethingOnTheStack = true; | ||
|
||
if (this.order !== undefined) { | ||
ORDER = this.order; | ||
} else if (ORDER !== undefined) { | ||
this.order = ORDER; | ||
ORDER += 1; | ||
} else { | ||
// the root component | ||
ORDER = 0; | ||
this.order = ORDER; | ||
} | ||
} | ||
|
||
stopRecording() { | ||
if (weLeftSomethingOnTheStack) { | ||
const deps = ObservationRecorder.stop(); | ||
weLeftSomethingOnTheStack = false; | ||
|
||
if (!deps.ylem) { | ||
throw new Error( | ||
'If you see this error with another error, clearing that should solve this. If you see ' + | ||
'this error alone, please open and issue on our github and tag Christopher and Justin.' | ||
); | ||
} | ||
} | ||
|
||
this.newDependencies = this.nextDependencies; | ||
recorderHelpers.updateObservations(this); | ||
} | ||
|
||
dependencyChange() { | ||
queues.deriveQueue.enqueue(this.onUpdate, this, [], { | ||
priority: this.order, | ||
}); | ||
} | ||
|
||
teardown() { | ||
recorderHelpers.stopObserving(this.newDependencies, this.onDependencyChange); | ||
queues.deriveQueue.dequeue(this.onUpdate); | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
ignore(fn) { | ||
return ObservationRecorder.ignore(fn)(); | ||
} | ||
} | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
canReflect.assignSymbols(Observer.prototype, { | ||
'can.getName': function getName() { | ||
return `${canReflect.getName(this.constructor)}<${canReflect.getName(this.onUpdate)}>`; | ||
}, | ||
}); | ||
} |
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,26 @@ | ||
import Observer from './can-observer'; | ||
|
||
// Unlike the ylem version, this hook is passed a | ||
// React instance so it can be used seamlessly | ||
// by Preact et al. | ||
export default function useObserver(React) { | ||
const { useEffect, useLayoutEffect, useState, useRef } = React; | ||
|
||
|
||
const [, update] = useState(); | ||
|
||
const observer = useRef(new Observer(() => update({}))); | ||
|
||
observer.current.startRecording(); | ||
useLayoutEffect(() => { | ||
observer.current.stopRecording(); | ||
}); | ||
|
||
// eslint-disable-next-line arrow-body-style | ||
useEffect(() => { | ||
return () => { | ||
observer.current.teardown(); | ||
observer.current = null; | ||
}; | ||
}, []); | ||
} |
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
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