1 | import {Map} from './shims/es6-collections.js';
|
---|
2 | import ResizeObserverController from './ResizeObserverController.js';
|
---|
3 | import ResizeObserverSPI from './ResizeObserverSPI.js';
|
---|
4 |
|
---|
5 | // Registry of internal observers. If WeakMap is not available use current shim
|
---|
6 | // for the Map collection as it has all required methods and because WeakMap
|
---|
7 | // can't be fully polyfilled anyway.
|
---|
8 | const observers = typeof WeakMap !== 'undefined' ? new WeakMap() : new Map();
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * ResizeObserver API. Encapsulates the ResizeObserver SPI implementation
|
---|
12 | * exposing only those methods and properties that are defined in the spec.
|
---|
13 | */
|
---|
14 | class ResizeObserver {
|
---|
15 | /**
|
---|
16 | * Creates a new instance of ResizeObserver.
|
---|
17 | *
|
---|
18 | * @param {ResizeObserverCallback} callback - Callback that is invoked when
|
---|
19 | * dimensions of the observed elements change.
|
---|
20 | */
|
---|
21 | constructor(callback) {
|
---|
22 | if (!(this instanceof ResizeObserver)) {
|
---|
23 | throw new TypeError('Cannot call a class as a function.');
|
---|
24 | }
|
---|
25 | if (!arguments.length) {
|
---|
26 | throw new TypeError('1 argument required, but only 0 present.');
|
---|
27 | }
|
---|
28 |
|
---|
29 | const controller = ResizeObserverController.getInstance();
|
---|
30 | const observer = new ResizeObserverSPI(callback, controller, this);
|
---|
31 |
|
---|
32 | observers.set(this, observer);
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | // Expose public methods of ResizeObserver.
|
---|
37 | [
|
---|
38 | 'observe',
|
---|
39 | 'unobserve',
|
---|
40 | 'disconnect'
|
---|
41 | ].forEach(method => {
|
---|
42 | ResizeObserver.prototype[method] = function () {
|
---|
43 | return observers.get(this)[method](...arguments);
|
---|
44 | };
|
---|
45 | });
|
---|
46 |
|
---|
47 | export default ResizeObserver;
|
---|