Skip to content

Migrate Bento components to V1#32974

Merged
dvoytenko merged 12 commits intoampproject:masterfrom
dvoytenko:bento/elements-v1
Mar 12, 2021
Merged

Migrate Bento components to V1#32974
dvoytenko merged 12 commits intoampproject:masterfrom
dvoytenko:bento/elements-v1

Conversation

@dvoytenko
Copy link
Copy Markdown
Contributor

@dvoytenko dvoytenko commented Feb 27, 2021

Partial for #30283.

The main general changes are in the src/preact/base-element.js and they are simple:

  1. layoutCallback is gone.
  2. ensureLoaded is added. Works via loading=eager
  3. Loading is started immediately when the Preact component is rendered the first time.
  4. The ready state is propagated from the Preact component to AMP element using Ref<Api> and onReadyState event.

Other changes include:

  1. unloadOnPause is changed to allow reloading.
  2. VideoWrapper and Instagram components have been updated to V1 protocol.

@dvoytenko dvoytenko force-pushed the bento/elements-v1 branch 3 times, most recently from 2dbe91a to b3e78e5 Compare March 1, 2021 16:45
@dvoytenko dvoytenko marked this pull request as ready for review March 1, 2021 17:03
@dvoytenko dvoytenko force-pushed the bento/elements-v1 branch from edba209 to fa7832d Compare March 3, 2021 19:26
@dvoytenko dvoytenko requested a review from samouri March 4, 2021 01:11
Comment on lines +94 to +98
useLayoutEffect(() => {
if (!load) {
setLoaded(false);
}
}, [load, setLoaded]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loaded code is really difficult for me to grok. I think what's happening is we want to call onReadyStateChange when unloaded->loaded and loaded->unloaded. I don't understand why it can't simply be written like:

const lastLoadedRef = useRef(false);
const [loaded, setLoaded] = useState(false);

if (loaded && !load) {
  onReadyState?.(ReadyState.LOADING);
  lastLoadedRef.current = false;
  setLoaded(false);
} else if (loaded !== lastLoadedRef.current) {
  onReadyState?.(loaded ? ReadyState.COMPLETE : ReadyState.LOADING);
  lastLoadedRef.current = loaded;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed things a bit here to make it simpler. LMK if that helps. I renamed load variable to mount - it basically decides whether or not to actually render/mount the iframe.

The goal of this code specifically is to reset loaded to false because if the iframe is no longer in DOM, it's not loaded either. And when it's added back, it will restart the whole chain of states again.

Your code might be close, but (a) we can't send events from the component's render function and we don't want to send them on each render. So an effect is better for this.

@dvoytenko dvoytenko merged commit 0530a47 into ampproject:master Mar 12, 2021
@dvoytenko dvoytenko deleted the bento/elements-v1 branch March 12, 2021 19:24
@samouri
Copy link
Copy Markdown
Member

samouri commented Mar 12, 2021

Congrats on merging this! Gargantuan effort

westonruter added a commit to westonruter/amphtml that referenced this pull request Mar 15, 2021
* 'master' of github.com:ampproject/amphtml: (536 commits)
  ✨ Send extension versions from AMPHTML ads to FIE  (ampproject#33190)
  Update amp-autocomplete test output file (ampproject#33216)
  ✨ Changing Inline Page Attachment Button UI  (ampproject#33142)
  ✨Launch no-signing (ampproject#33229)
  ♻️ Add logic for story ad page manager. (ampproject#33201)
  Migrate Bento components to V1 (ampproject#32974)
  📦 Update dependency mocha to v8.3.2 (ampproject#33234)
  📦 Update build system devDependencies (ampproject#33178)
  🧪 [amp-consent] Turn on TCF PostMessage Proxy API Experiment (ampproject#33196)
  Fix amp-video amp_video_quality parameter name. (ampproject#33227)
  🏗 Guard access to `BASH_ENV` during CircleCI push builds (ampproject#33231)
  🏗 Clean up CircleCI merge SHA logic (ampproject#33215)
  📦 Update dependency karma to v6.1.2 (ampproject#33151)
  🏗 Ensure up-to-date and clean `package-lock.json` before running any CI job (ampproject#33223)
  Ensure that pre-stubbing always happens before the first element is upgraded (ampproject#33212)
  🏗 Restore `package-lock.json` to `lockfileVersion = 1` (ampproject#33222)
  Fixed text align of buttons (ampproject#33217)
  [amp-story] ♿ Add label for next story and add i18n for pagination buttons (ampproject#33205)
  [amp-story-player] 📖  Rename skip-next control to skip-to-next (ampproject#33164)
  🚀 Report cls-fcp and cls-vis to compare with cls (ampproject#33207)
  ...
muuki88 pushed a commit to highfivve/amphtml that referenced this pull request Mar 15, 2021
* Migrate Bento components to V1

* V1 tests

* fixed types

* fix types

* Make readyState/loaded as refs, not state

* only send state event when it changes

* Async pause API applied via ResizeObserver

* review fixes

* Redo unloadOnPause

* review fixes

* more comments on unloadOnPause

* fix bad merge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants