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

✨ Initial amp-timeago Bento component #26507

Merged
merged 41 commits into from
Mar 5, 2020
Merged
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
f2c903d
amp-timeago v2
caroqliu Jan 27, 2020
4271a9c
Remove parseInt
caroqliu Jan 27, 2020
7c52654
Refactor logic into getFuzzyTimestampValue
caroqliu Jan 28, 2020
f979136
Lint
caroqliu Jan 28, 2020
0ec8325
Add IntersectionObserver to rerender
caroqliu Jan 28, 2020
f357dd4
Modify useIntersect
caroqliu Jan 28, 2020
1d4ed68
Refresh timestamp when entering view
caroqliu Jan 29, 2020
ec88e89
Move useIntersect
caroqliu Jan 29, 2020
170c55c
Pass props to getFuzzyTimestampValue
caroqliu Jan 29, 2020
178d13d
Use useRerenderer
caroqliu Jan 29, 2020
7546ac2
Add callback comments
caroqliu Jan 30, 2020
ef07436
Pass empty array to `useLayoutEffect`
caroqliu Jan 30, 2020
3ef8b19
Add timeago.js to build check allowlist
caroqliu Jan 30, 2020
08d869d
Remove useRerenderer and implement useInView
caroqliu Jan 30, 2020
b02dd4b
Update bind example on test page
caroqliu Jan 30, 2020
7d9c279
Correct amp-bind example text
caroqliu Jan 31, 2020
c4890a1
Refactor useIntersect as useInViewEffect
caroqliu Feb 11, 2020
45d6581
Rename use-intersect.js
caroqliu Feb 11, 2020
b8c30c2
Allowlist 3p timeago dependency
caroqliu Feb 11, 2020
b7b61f6
Restructure shape of useInViewEffect
caroqliu Feb 11, 2020
a6beb2d
Pass ref.current to useInViewEffect
caroqliu Feb 11, 2020
5755f4e
Clean up typing
caroqliu Feb 13, 2020
748f2f9
useInView -> useIntersect, useOnEnterViewport
caroqliu Feb 13, 2020
630ceb1
Reset isIntersecting in cleanup function
caroqliu Feb 14, 2020
78df30a
Restore useInView implementation
caroqliu Feb 20, 2020
4c4464a
Refactor new logic
caroqliu Feb 20, 2020
7eb6bfd
Share IntersectionOb across timeago instances
caroqliu Feb 21, 2020
9f14bfc
Replace useFnInView with useIsIntersecting
caroqliu Feb 21, 2020
225d596
Update DOM on enter viewport only
caroqliu Feb 21, 2020
ef30173
Move file + lint
caroqliu Feb 21, 2020
ea197ca
Delete file
caroqliu Feb 21, 2020
e018573
Rename timestamp to timestampRef
caroqliu Feb 21, 2020
1453b39
Initialize timestamp with null
caroqliu Feb 25, 2020
69242f2
Add type annotation to timestampRef
caroqliu Feb 26, 2020
96fba1f
Map elements to setters
caroqliu Feb 26, 2020
b112d05
Place IntersectionObserver in component
caroqliu Feb 28, 2020
395e1ae
Delete useIsIntersecting
caroqliu Feb 28, 2020
6b78d30
unobserve -> disconnect
caroqliu Feb 29, 2020
d7cd8bf
Array destructure
caroqliu Feb 29, 2020
fb36fd5
Specify props more granularly
caroqliu Mar 4, 2020
f790b35
Types
caroqliu Mar 4, 2020
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
Prev Previous commit
Next Next commit
Share IntersectionOb across timeago instances
  • Loading branch information
caroqliu committed Feb 29, 2020
commit 7eb6bfd82698e762d5b857390d941d335d41b345
24 changes: 10 additions & 14 deletions src/preact/use-in-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,19 @@
* limitations under the License.
*/

import {dev} from '../log';
import {useEffect, useRef} from './index';

/** @type {null|IntersectionObserver} */ let sharedObserver = null;

/**
* @param {{current: HTMLElement}} ref
* @param {function():(function():undefined|undefined)} fn
*/
export function useFnInView(ref, fn) {
const isIntersectingRef = useRef(false);

// TODO: The IntersectionObserver should be shared across instances via context
// Currently assumes observing one node at a time -- does this by disconnecting all nodes
/** @type {{current: (null|IntersectionObserver)}} */ const observerRef = useRef(
null
);
if (observerRef.current === null) {
observerRef.current = new IntersectionObserver(entries => {
if (sharedObserver === null) {
sharedObserver = new IntersectionObserver(entries => {
const {isIntersecting} = entries[entries.length - 1];
if (isIntersecting !== isIntersectingRef.current) {
isIntersectingRef.current = isIntersecting;
Expand All @@ -39,17 +36,16 @@ export function useFnInView(ref, fn) {
}
});
}

// Hook for changes in IntersectionObserver dependencies
useEffect(() => {
const node = ref.current;
const observer = observerRef.current;
if (node) {
observer.observe(node);
sharedObserver.observe(dev().assertElement(node));
}
return () => {
isIntersectingRef.current = false;
observer.disconnect();
if (node) {
sharedObserver.unobserve(dev().assertElement(node));
}
};
}, [ref.current, observerRef.current]);
}, [ref.current]);
}