Skip to content

Commit

Permalink
Support shadow DOM and same-origin iframes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Voytenko committed Jan 6, 2021
1 parent 4a14845 commit 323414b
Show file tree
Hide file tree
Showing 10 changed files with 2,241 additions and 1,594 deletions.
1,423 changes: 825 additions & 598 deletions dist/ResizeObserver.js

Large diffs are not rendered by default.

43 changes: 31 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "resize-observer-polyfill",
"author": "Denis Rul <[email protected]>",
"version": "1.5.1",
"version": "1.5.2",
"description": "A polyfill for the Resize Observer API",
"main": "dist/ResizeObserver.js",
"module": "dist/ResizeObserver.es.js",
Expand Down
83 changes: 83 additions & 0 deletions src/GlobalResizeObserverController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import ResizeObserverController from './ResizeObserverController.js';

/**
* Singleton controller class which handles updates of ResizeObserver instances.
*/
export default class GlobalResizeObserverController {
/**
* A mapping from a DOM root node and a respective controller. A root node
* could be the main document, a same-origin iframe, or a shadow root.
* See https://developer.mozilla.org/en-US/docs/Web/API/Node/getRootNode
* for more info.
*
* @private {Map<Node, ResizeObserverController>}
*/
rootNodeControllers_ = typeof WeakMap !== 'undefined' ? new WeakMap() : new Map();

/**
* Holds reference to the controller's instance.
*
* @private {GlobalResizeObserverController}
*/
static instance_ = null;

/**
* Adds observer to observers list.
*
* @param {Node} rootNode - The root node for which the observer is added.
* @param {ResizeObserverSPI} observer - Observer to be added.
* @returns {void}
*/
addObserver(rootNode, observer) {
let rootNodeController = this.rootNodeControllers_.get(rootNode);

if (!rootNodeController) {
rootNodeController = new ResizeObserverController(rootNode, this);
this.rootNodeControllers_.set(rootNode, rootNodeController);
}
rootNodeController.addObserver(observer);
}

/**
* Removes observer from observers list.
*
* @param {Node} rootNode - The root node from which the observer is removed.
* @param {ResizeObserverSPI} observer - Observer to be removed.
* @returns {void}
*/
removeObserver(rootNode, observer) {
const rootNodeController = this.rootNodeControllers_.get(rootNode);

if (rootNodeController) {
rootNodeController.removeObserver(observer);
}
}

/**
* Invokes the update of observers. It will continue running updates insofar
* it detects changes.
*
* @param {Node} rootNode - The root node to refresh.
* @returns {void}
*/
refresh(rootNode) {
const rootNodeController = this.rootNodeControllers_.get(rootNode);

if (rootNodeController) {
rootNodeController.refresh();
}
}

/**
* Returns instance of the GlobalResizeObserverController.
*
* @returns {GlobalResizeObserverController}
*/
static getInstance() {
if (!this.instance_) {
this.instance_ = new GlobalResizeObserverController();
}

return this.instance_;
}
}
14 changes: 13 additions & 1 deletion src/ResizeObservation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ export default class ResizeObservation {
*/
target;

/**
* The root node of the observed element at the time of subscription. If
* the root node of the element changes, the `ResizeObserver` implementation
* will resubscribe.
*
* @type {Node}
*/
rootNode;

/**
* Broadcasted width of content rectangle.
*
Expand All @@ -37,9 +46,12 @@ export default class ResizeObservation {
* Creates an instance of ResizeObservation.
*
* @param {Element} target - Element to be observed.
* @param {Node} rootNode - The root node of the element at the time
* of subscription.
*/
constructor(target) {
constructor(target, rootNode) {
this.target = target;
this.rootNode = rootNode;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/ResizeObserver.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import GlobalResizeObserverController from './GlobalResizeObserverController.js';
import {Map} from './shims/es6-collections.js';
import ResizeObserverController from './ResizeObserverController.js';
import ResizeObserverSPI from './ResizeObserverSPI.js';

// Registry of internal observers. If WeakMap is not available use current shim
Expand All @@ -26,7 +26,7 @@ class ResizeObserver {
throw new TypeError('1 argument required, but only 0 present.');
}

const controller = ResizeObserverController.getInstance();
const controller = GlobalResizeObserverController.getInstance();
const observer = new ResizeObserverSPI(callback, controller, this);

observers.set(this, observer);
Expand Down
Loading

0 comments on commit 323414b

Please sign in to comment.