ember-primitives 0.31.0 → 0.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,38 @@
1
+ import Modifier, { type ArgsFor } from 'ember-modifier';
2
+ import type Owner from '@ember/owner';
3
+ export interface Signature {
4
+ /**
5
+ * Any element that is resizable can have onResize attached
6
+ */
7
+ Element: Element;
8
+ Args: {
9
+ Positional: [
10
+ /**
11
+ * The ResizeObserver callback will only receive
12
+ * one entry per resize event.
13
+ *
14
+ * See: [ResizeObserverEntry](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry)
15
+ */
16
+ callback: (entry: ResizeObserverEntry) => void
17
+ ];
18
+ };
19
+ }
20
+ declare class OnResize extends Modifier<Signature> {
21
+ #private;
22
+ constructor(owner: Owner, args: ArgsFor<Signature>);
23
+ modify(element: Element, [callback]: [callback: (entry: ResizeObserverEntry) => void]): void;
24
+ }
25
+ export declare const onResize: typeof OnResize;
26
+ /**
27
+ * Ignores "ResizeObserver loop limit exceeded" error in Ember tests.
28
+ *
29
+ * This "error" is safe to ignore as it is just a warning message,
30
+ * telling that the "looping" observation will be skipped in the current frame,
31
+ * and will be delivered in the next one.
32
+ *
33
+ * For some reason, it is fired as an `error` event at `window` failing Ember
34
+ * tests and exploding Sentry with errors that must be ignored.
35
+ */
36
+ export default function ignoreROError(): void;
37
+ export {};
38
+ //# sourceMappingURL=on-resize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"on-resize.d.ts","sourceRoot":"","sources":["../src/on-resize.ts"],"names":[],"mappings":"AAGA,OAAO,QAAQ,EAAE,EAAE,KAAK,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAExD,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAEtC,MAAM,WAAW,SAAS;IACxB;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE;QACJ,UAAU,EAAE;YACV;;;;;eAKG;YACH,QAAQ,EAAE,CAAC,KAAK,EAAE,mBAAmB,KAAK,IAAI;SAC/C,CAAC;KACH,CAAC;CACH;AAED,cAAM,QAAS,SAAQ,QAAQ,CAAC,SAAS,CAAC;;gBAM5B,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,SAAS,CAAC;IAUlD,MAAM,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,mBAAmB,KAAK,IAAI,CAAC;CAetF;AAED,eAAO,MAAM,QAAQ,iBAAW,CAAC;AA6EjC;;;;;;;;;GASG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,SAgBpC"}
@@ -0,0 +1,116 @@
1
+ import { assert } from '@ember/debug';
2
+ import { registerDestructor } from '@ember/destroyable';
3
+ import Modifier from 'ember-modifier';
4
+
5
+ class OnResize extends Modifier {
6
+ #callback = null;
7
+ #element = null;
8
+ #resizeObserver = new ResizeObserverManager();
9
+ constructor(owner, args) {
10
+ super(owner, args);
11
+ registerDestructor(this, () => {
12
+ if (this.#element && this.#callback) {
13
+ this.#resizeObserver.unobserve(this.#element, this.#callback);
14
+ }
15
+ });
16
+ }
17
+ modify(element, [callback]) {
18
+ assert(`{{onResize}}: callback must be a function, but was ${callback}`, typeof callback === 'function');
19
+ if (this.#element && this.#callback) {
20
+ this.#resizeObserver.unobserve(this.#element, this.#callback);
21
+ }
22
+ this.#resizeObserver.observe(element, callback);
23
+ this.#callback = callback;
24
+ this.#element = element;
25
+ }
26
+ }
27
+ const onResize = OnResize;
28
+ const errorMessages = ['ResizeObserver loop limit exceeded', 'ResizeObserver loop completed with undelivered notifications.'];
29
+ class ResizeObserverManager {
30
+ #callbacks = new WeakMap();
31
+ #handleResize = entries => {
32
+ for (const entry of entries) {
33
+ const callbacks = this.#callbacks.get(entry.target);
34
+ if (callbacks) {
35
+ for (const callback of callbacks) {
36
+ callback(entry);
37
+ }
38
+ }
39
+ }
40
+ };
41
+ #observer = new ResizeObserver(this.#handleResize);
42
+ constructor() {
43
+ ignoreROError();
44
+ registerDestructor(this, () => {
45
+ this.#observer?.disconnect();
46
+ });
47
+ }
48
+
49
+ /**
50
+ * Initiate the observing of the `element` or add an additional `callback`
51
+ * if the `element` is already observed.
52
+ *
53
+ * @param {object} element
54
+ * @param {function} callback The `callback` is called whenever the size of
55
+ * the `element` changes. It is called with `ResizeObserverEntry` object
56
+ * for the particular `element`.
57
+ */
58
+ observe(element, callback) {
59
+ const callbacks = this.#callbacks.get(element);
60
+ if (callbacks) {
61
+ callbacks.add(callback);
62
+ } else {
63
+ this.#callbacks.set(element, new Set([callback]));
64
+ this.#observer.observe(element);
65
+ }
66
+ }
67
+
68
+ /**
69
+ * End the observing of the `element` or just remove the provided `callback`.
70
+ *
71
+ * It will unobserve the `element` if the `callback` is not provided
72
+ * or there are no more callbacks left for this `element`.
73
+ *
74
+ * @param {object} element
75
+ * @param {function?} callback - The `callback` to remove from the listeners
76
+ * of the `element` size changes.
77
+ */
78
+ unobserve(element, callback) {
79
+ const callbacks = this.#callbacks.get(element);
80
+ if (!callbacks) {
81
+ return;
82
+ }
83
+ callbacks.delete(callback);
84
+ if (!callback || !callbacks.size) {
85
+ this.#callbacks.delete(element);
86
+ this.#observer.unobserve(element);
87
+ }
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Ignores "ResizeObserver loop limit exceeded" error in Ember tests.
93
+ *
94
+ * This "error" is safe to ignore as it is just a warning message,
95
+ * telling that the "looping" observation will be skipped in the current frame,
96
+ * and will be delivered in the next one.
97
+ *
98
+ * For some reason, it is fired as an `error` event at `window` failing Ember
99
+ * tests and exploding Sentry with errors that must be ignored.
100
+ */
101
+ function ignoreROError() {
102
+ if (typeof window.onerror !== 'function') {
103
+ return;
104
+ }
105
+ const onError = window.onerror;
106
+ window.onerror = (...args) => {
107
+ const [message] = args;
108
+ if (typeof message === 'string') {
109
+ if (errorMessages.includes(message)) return true;
110
+ }
111
+ onError(...args);
112
+ };
113
+ }
114
+
115
+ export { ignoreROError as default, onResize };
116
+ //# sourceMappingURL=on-resize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"on-resize.js","sources":["../src/on-resize.ts"],"sourcesContent":["import { assert } from '@ember/debug';\nimport { registerDestructor } from '@ember/destroyable';\n\nimport Modifier, { type ArgsFor } from 'ember-modifier';\n\nimport type Owner from '@ember/owner';\n\nexport interface Signature {\n /**\n * Any element that is resizable can have onResize attached\n */\n Element: Element;\n Args: {\n Positional: [\n /**\n * The ResizeObserver callback will only receive\n * one entry per resize event.\n *\n * See: [ResizeObserverEntry](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry)\n */\n callback: (entry: ResizeObserverEntry) => void,\n ];\n };\n}\n\nclass OnResize extends Modifier<Signature> {\n #callback: ((entry: ResizeObserverEntry) => void) | null = null;\n #element: Element | null = null;\n\n #resizeObserver: ResizeObserverManager = new ResizeObserverManager();\n\n constructor(owner: Owner, args: ArgsFor<Signature>) {\n super(owner, args);\n\n registerDestructor(this, () => {\n if (this.#element && this.#callback) {\n this.#resizeObserver.unobserve(this.#element, this.#callback);\n }\n });\n }\n\n modify(element: Element, [callback]: [callback: (entry: ResizeObserverEntry) => void]) {\n assert(\n `{{onResize}}: callback must be a function, but was ${callback as unknown as string}`,\n typeof callback === 'function'\n );\n\n if (this.#element && this.#callback) {\n this.#resizeObserver.unobserve(this.#element, this.#callback);\n }\n\n this.#resizeObserver.observe(element, callback);\n\n this.#callback = callback;\n this.#element = element;\n }\n}\n\nexport const onResize = OnResize;\n\nconst errorMessages = [\n 'ResizeObserver loop limit exceeded',\n 'ResizeObserver loop completed with undelivered notifications.',\n];\n\nclass ResizeObserverManager {\n #callbacks = new WeakMap<Element, Set<(entry: ResizeObserverEntry) => unknown>>();\n\n #handleResize = (entries: ResizeObserverEntry[]) => {\n for (const entry of entries) {\n const callbacks = this.#callbacks.get(entry.target);\n\n if (callbacks) {\n for (const callback of callbacks) {\n callback(entry);\n }\n }\n }\n };\n #observer = new ResizeObserver(this.#handleResize);\n\n constructor() {\n ignoreROError();\n\n registerDestructor(this, () => {\n this.#observer?.disconnect();\n });\n }\n\n /**\n * Initiate the observing of the `element` or add an additional `callback`\n * if the `element` is already observed.\n *\n * @param {object} element\n * @param {function} callback The `callback` is called whenever the size of\n * the `element` changes. It is called with `ResizeObserverEntry` object\n * for the particular `element`.\n */\n observe(element: Element, callback: (entry: ResizeObserverEntry) => unknown) {\n const callbacks = this.#callbacks.get(element);\n\n if (callbacks) {\n callbacks.add(callback);\n } else {\n this.#callbacks.set(element, new Set([callback]));\n this.#observer.observe(element);\n }\n }\n\n /**\n * End the observing of the `element` or just remove the provided `callback`.\n *\n * It will unobserve the `element` if the `callback` is not provided\n * or there are no more callbacks left for this `element`.\n *\n * @param {object} element\n * @param {function?} callback - The `callback` to remove from the listeners\n * of the `element` size changes.\n */\n unobserve(element: Element, callback: (entry: ResizeObserverEntry) => unknown) {\n const callbacks = this.#callbacks.get(element);\n\n if (!callbacks) {\n return;\n }\n\n callbacks.delete(callback);\n\n if (!callback || !callbacks.size) {\n this.#callbacks.delete(element);\n this.#observer.unobserve(element);\n }\n }\n}\n\n/**\n * Ignores \"ResizeObserver loop limit exceeded\" error in Ember tests.\n *\n * This \"error\" is safe to ignore as it is just a warning message,\n * telling that the \"looping\" observation will be skipped in the current frame,\n * and will be delivered in the next one.\n *\n * For some reason, it is fired as an `error` event at `window` failing Ember\n * tests and exploding Sentry with errors that must be ignored.\n */\nexport default function ignoreROError() {\n if (typeof window.onerror !== 'function') {\n return;\n }\n\n const onError = window.onerror;\n\n window.onerror = (...args) => {\n const [message] = args;\n\n if (typeof message === 'string') {\n if (errorMessages.includes(message)) return true;\n }\n\n onError(...args);\n };\n}\n"],"names":["OnResize","Modifier","ResizeObserverManager","constructor","owner","args","registerDestructor","unobserve","modify","element","callback","assert","observe","onResize","errorMessages","WeakMap","entries","entry","callbacks","get","target","ResizeObserver","ignoreROError","disconnect","add","set","Set","delete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AACA,QAAQ,IAAI,CAACQ,SAAS,CAACU,IAAI,EAAE;AAChC,MAAA,IAAI,CAAC,UAAU,CAACD,MAAM,CAAClB,OAAO,CAAC;AAC/B,MAAA,IAAI,CAAC,SAAS,CAACF,SAAS,CAACE,OAAO,CAAC;AACnC;AACF;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASa,aAAaA,GAAG;AACtC,EAAA,IAAI,OAAOO,MAAM,CAACC,OAAO,KAAK,UAAU,EAAE;AACxC,IAAA;AACF;AAEA,EAAA,MAAMC,OAAO,GAAGF,MAAM,CAACC,OAAO;AAE9BD,EAAAA,MAAM,CAACC,OAAO,GAAG,CAAC,GAAGzB,IAAI,KAAK;AAC5B,IAAA,MAAM,CAAC2B,OAAO,CAAC,GAAG3B,IAAI;AAEtB,IAAA,IAAI,OAAO2B,OAAO,KAAK,QAAQ,EAAE;MAC/B,IAAIlB,aAAa,CAACmB,QAAQ,CAACD,OAAO,CAAC,EAAE,OAAO,IAAI;AAClD;IAEAD,OAAO,CAAC,GAAG1B,IAAI,CAAC;GACjB;AACH;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ember-primitives",
3
- "version": "0.31.0",
3
+ "version": "0.32.0",
4
4
  "description": "Making apps easier to build",
5
5
  "sideEffects": [
6
6
  "*.css"