Skip to content

Commit

Permalink
Update to delay on fetch call in a microtask
Browse files Browse the repository at this point in the history
  • Loading branch information
calinoracation committed Mar 1, 2024
1 parent 48765d0 commit 4f99a6f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 81 deletions.
63 changes: 61 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,65 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { initPolyfill } from './initialize';
import {
ScrollTimeline,
ViewTimeline,
} from "./scroll-timeline-base";
import {
animate,
elementGetAnimations,
documentGetAnimations,
ProxyAnimation
} from "./proxy-animation.js";

initPolyfill('*');
import { initCSSPolyfill } from "./scroll-timeline-css"

/**
* Initializes the polyfill and by default will parse all stylesheets on the
* page.
* @param {'*' | string | RegExp | Array<string | RegExp>} allowedOrigins
* Defaults to *, which attempts to parse all linked stylesheets. It also
* accepts a string, a RegExp or an array of either.
*/
export function initPolyfill(allowedOrigins) {
// initCSSPolyfill returns true iff the host browser supports SDA
if (initCSSPolyfill(allowedOrigins)) {
return;
}

if (
!Reflect.defineProperty(window, 'ScrollTimeline', { value: ScrollTimeline })
) {
throw Error(
'Error installing ScrollTimeline polyfill: could not attach ScrollTimeline to window'
);
}
if (
!Reflect.defineProperty(window, 'ViewTimeline', { value: ViewTimeline })
) {
throw Error(
'Error installing ViewTimeline polyfill: could not attach ViewTimeline to window'
);
}

if (
!Reflect.defineProperty(Element.prototype, 'animate', { value: animate })
) {
throw Error(
"Error installing ScrollTimeline polyfill: could not attach WAAPI's animate to DOM Element"
);
}
if (!Reflect.defineProperty(window, 'Animation', { value: ProxyAnimation })) {
throw Error('Error installing Animation constructor.');
}
if (!Reflect.defineProperty(Element.prototype, "getAnimations", { value: elementGetAnimations })) {
throw Error(
"Error installing ScrollTimeline polyfill: could not attach WAAPI's getAnimations to DOM Element"
);
}
if (!Reflect.defineProperty(document, "getAnimations", { value: documentGetAnimations })) {
throw Error(
"Error installing ScrollTimeline polyfill: could not attach WAAPI's getAnimations to document"
);
}
}
76 changes: 0 additions & 76 deletions src/initialize.js

This file was deleted.

12 changes: 9 additions & 3 deletions src/scroll-timeline-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ScrollTimeline, ViewTimeline, getScrollParent, calculateRange,
calculateRelativePosition, measureSubject, measureSource } from "./scroll-timeline-base";

const parser = new StyleParser();
let hasFetchedRemoteStylesheets = false;

/**
* @param {'*' | string | RegExp | Array<string | RegExp>} allowedOrigins
Expand Down Expand Up @@ -84,9 +85,14 @@ function initMutationObserver(allowedOrigins) {
}

document.querySelectorAll("style").forEach((tag) => handleStyleTag(tag));
document
.querySelectorAll("link")
.forEach((tag) => handleLinkedStylesheet(tag));
if (!hasFetchedRemoteStylesheets) {
hasFetchedRemoteStylesheets = true;
Promise.resolve().then(() => {
document
.querySelectorAll("link")
.forEach((tag) => handleLinkedStylesheet(tag));
});
}
}

function relativePosition(phase, container, target, axis, optionsInset, percent) {
Expand Down

0 comments on commit 4f99a6f

Please sign in to comment.