123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- /*!
- * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
- * v1.0.0-beta.97
- */
- import { Build } from '@stencil/core/internal/client/index.js';
- /**
- * This utility ensures observers are created only for browser contexts.
- *
- * @param type - the type of observer to create
- * @param callback - the observer callback
- * @param options - the observer options
- */
- function createObserver(type, callback, options) {
- if (!Build.isBrowser) {
- return undefined;
- }
- const Observer = getObserver(type);
- return new Observer(callback, options);
- }
- function getObserver(type) {
- // based on https://github.com/whatwg/dom/issues/126#issuecomment-1049814948
- class ExtendedMutationObserver extends window.MutationObserver {
- constructor(callback) {
- super(callback);
- this.observedEntry = [];
- this.callback = callback;
- }
- observe(target, options) {
- this.observedEntry.push({ target, options });
- return super.observe(target, options);
- }
- unobserve(target) {
- const newObservedEntries = this.observedEntry.filter((observed) => observed.target !== target);
- this.observedEntry = [];
- this.callback(super.takeRecords(), this);
- this.disconnect();
- newObservedEntries.forEach((observed) => this.observe(observed.target, observed.options));
- }
- }
- return (function () {
- return (type === "intersection"
- ? window.IntersectionObserver
- : type === "mutation"
- ? ExtendedMutationObserver
- : window.ResizeObserver);
- })();
- }
- export { createObserver as c };
|