ember-primitives 0.41.0 → 0.42.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.
@@ -1,5 +1,6 @@
1
1
  import Modifier, { type ArgsFor } from 'ember-modifier';
2
2
  import type Owner from '@ember/owner';
3
+ export { ignoreROError } from './resize-observer.ts';
3
4
  export interface Signature {
4
5
  /**
5
6
  * Any element that is resizable can have onResize attached
@@ -23,16 +24,4 @@ declare class OnResize extends Modifier<Signature> {
23
24
  modify(element: Element, [callback]: [callback: (entry: ResizeObserverEntry) => void]): void;
24
25
  }
25
26
  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
27
  //# sourceMappingURL=on-resize.d.ts.map
@@ -1 +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"}
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;AAIxD,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAGtC,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD,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"}
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Creates or returns the ResizeObserverManager.
3
+ *
4
+ * Only one of these will exist per owner.
5
+ *
6
+ * Has only two methods:
7
+ * - observe(element, callback: (resizeObserverEntry) => void)
8
+ * - unobserve(element, callback: (resizeObserverEntry) => void)
9
+ *
10
+ * Like with the underlying ResizeObserver API (and all event listeners),
11
+ * the callback passed to unobserved must be the same reference as the one
12
+ * passed to observe.
13
+ */
14
+ export declare function resizeObserver(context: object): ResizeObserverManager;
15
+ declare class ResizeObserverManager {
16
+ #private;
17
+ constructor();
18
+ /**
19
+ * Initiate the observing of the `element` or add an additional `callback`
20
+ * if the `element` is already observed.
21
+ *
22
+ * @param {object} element
23
+ * @param {function} callback The `callback` is called whenever the size of
24
+ * the `element` changes. It is called with `ResizeObserverEntry` object
25
+ * for the particular `element`.
26
+ */
27
+ observe(element: Element, callback: (entry: ResizeObserverEntry) => unknown): void;
28
+ /**
29
+ * End the observing of the `element` or just remove the provided `callback`.
30
+ *
31
+ * It will unobserve the `element` if the `callback` is not provided
32
+ * or there are no more callbacks left for this `element`.
33
+ *
34
+ * @param {object} element
35
+ * @param {function?} callback - The `callback` to remove from the listeners
36
+ * of the `element` size changes.
37
+ */
38
+ unobserve(element: Element, callback: (entry: ResizeObserverEntry) => unknown): void;
39
+ }
40
+ /**
41
+ * Ignores "ResizeObserver loop limit exceeded" error in Ember tests.
42
+ *
43
+ * This "error" is safe to ignore as it is just a warning message,
44
+ * telling that the "looping" observation will be skipped in the current frame,
45
+ * and will be delivered in the next one.
46
+ *
47
+ * For some reason, it is fired as an `error` event at `window` failing Ember
48
+ * tests and exploding Sentry with errors that must be ignored.
49
+ */
50
+ export declare function ignoreROError(): void;
51
+ export {};
52
+ //# sourceMappingURL=resize-observer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resize-observer.d.ts","sourceRoot":"","sources":["../src/resize-observer.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,yBAS7C;AAED,cAAM,qBAAqB;;;IAwBzB;;;;;;;;OAQG;IACH,OAAO,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,mBAAmB,KAAK,OAAO;IAW3E;;;;;;;;;OASG;IACH,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,mBAAmB,KAAK,OAAO;CAc9E;AAOD;;;;;;;;;GASG;AACH,wBAAgB,aAAa,SAgB5B"}
@@ -1,3 +1,27 @@
1
+ import type Owner from '@ember/owner';
1
2
  export declare function uniqueId(): string;
2
3
  export declare function isNewable(x: any): x is new (...args: unknown[]) => NonNullable<object>;
4
+ /**
5
+ * Loose check for an "ownerish" API.
6
+ * only the ".lookup" method is required.
7
+ *
8
+ * The requirements for what an "owner" is are sort of undefined,
9
+ * as the actual owner in ember applications has too much on it,
10
+ * and the long term purpose of the owner will be questioned once we
11
+ * eliminate the need to have a registry (what lookup looks in to),
12
+ * but we'll still need "Something" to represent the lifetime of the application.
13
+ *
14
+ * Technically, the owner could be any object, including `{}`
15
+ */
16
+ export declare function isOwner(x: unknown): x is Owner;
17
+ export declare function isNonNullableObject(x: unknown): x is NonNullable<object>;
18
+ /**
19
+ * Can receive the class instance or the owner itself, and will always return return the owner.
20
+ *
21
+ * undefined will be returned if the Owner does not exist on the passed object
22
+ *
23
+ * Can be useful when combined with `createStore` to then create "services",
24
+ * which don't require string lookup.
25
+ */
26
+ export declare function findOwner(contextOrOwner: unknown): Owner | undefined;
3
27
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAEA,wBAAgB,QAAQ,IAAI,MAAM,CAQjC;AAED,wBAAgB,SAAS,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,IAAI,KAAK,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,WAAW,CAAC,MAAM,CAAC,CAGtF"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAItC,wBAAgB,QAAQ,IAAI,MAAM,CAQjC;AAED,wBAAgB,SAAS,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,IAAI,KAAK,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,WAAW,CAAC,MAAM,CAAC,CAMtF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,KAAK,CAI9C;AAED,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,WAAW,CAAC,MAAM,CAAC,CAKxE;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,cAAc,EAAE,OAAO,GAAG,KAAK,GAAG,SAAS,CAWpE"}
package/dist/on-resize.js CHANGED
@@ -2,11 +2,13 @@
2
2
  import { assert } from '@ember/debug';
3
3
  import { registerDestructor } from '@ember/destroyable';
4
4
  import Modifier from 'ember-modifier';
5
+ import { resizeObserver } from './resize-observer.js';
6
+ export { ignoreROError } from './resize-observer.js';
5
7
 
6
8
  class OnResize extends Modifier {
7
9
  #callback = null;
8
10
  #element = null;
9
- #resizeObserver = new ResizeObserverManager();
11
+ #resizeObserver = resizeObserver(this);
10
12
  constructor(owner, args) {
11
13
  super(owner, args);
12
14
  registerDestructor(this, () => {
@@ -26,92 +28,6 @@ class OnResize extends Modifier {
26
28
  }
27
29
  }
28
30
  const onResize = OnResize;
29
- const errorMessages = ['ResizeObserver loop limit exceeded', 'ResizeObserver loop completed with undelivered notifications.'];
30
- class ResizeObserverManager {
31
- #callbacks = new WeakMap();
32
- #handleResize = entries => {
33
- for (const entry of entries) {
34
- const callbacks = this.#callbacks.get(entry.target);
35
- if (callbacks) {
36
- for (const callback of callbacks) {
37
- callback(entry);
38
- }
39
- }
40
- }
41
- };
42
- #observer = new ResizeObserver(this.#handleResize);
43
- constructor() {
44
- ignoreROError();
45
- registerDestructor(this, () => {
46
- this.#observer?.disconnect();
47
- });
48
- }
49
-
50
- /**
51
- * Initiate the observing of the `element` or add an additional `callback`
52
- * if the `element` is already observed.
53
- *
54
- * @param {object} element
55
- * @param {function} callback The `callback` is called whenever the size of
56
- * the `element` changes. It is called with `ResizeObserverEntry` object
57
- * for the particular `element`.
58
- */
59
- observe(element, callback) {
60
- const callbacks = this.#callbacks.get(element);
61
- if (callbacks) {
62
- callbacks.add(callback);
63
- } else {
64
- this.#callbacks.set(element, new Set([callback]));
65
- this.#observer.observe(element);
66
- }
67
- }
68
-
69
- /**
70
- * End the observing of the `element` or just remove the provided `callback`.
71
- *
72
- * It will unobserve the `element` if the `callback` is not provided
73
- * or there are no more callbacks left for this `element`.
74
- *
75
- * @param {object} element
76
- * @param {function?} callback - The `callback` to remove from the listeners
77
- * of the `element` size changes.
78
- */
79
- unobserve(element, callback) {
80
- const callbacks = this.#callbacks.get(element);
81
- if (!callbacks) {
82
- return;
83
- }
84
- callbacks.delete(callback);
85
- if (!callback || !callbacks.size) {
86
- this.#callbacks.delete(element);
87
- this.#observer.unobserve(element);
88
- }
89
- }
90
- }
91
-
92
- /**
93
- * Ignores "ResizeObserver loop limit exceeded" error in Ember tests.
94
- *
95
- * This "error" is safe to ignore as it is just a warning message,
96
- * telling that the "looping" observation will be skipped in the current frame,
97
- * and will be delivered in the next one.
98
- *
99
- * For some reason, it is fired as an `error` event at `window` failing Ember
100
- * tests and exploding Sentry with errors that must be ignored.
101
- */
102
- function ignoreROError() {
103
- if (typeof window.onerror !== 'function') {
104
- return;
105
- }
106
- const onError = window.onerror;
107
- window.onerror = (...args) => {
108
- const [message] = args;
109
- if (typeof message === 'string') {
110
- if (errorMessages.includes(message)) return true;
111
- }
112
- onError(...args);
113
- };
114
- }
115
31
 
116
- export { ignoreROError as default, onResize };
32
+ export { onResize };
117
33
  //# sourceMappingURL=on-resize.js.map
@@ -0,0 +1,113 @@
1
+
2
+ import { assert } from '@ember/debug';
3
+ import { registerDestructor } from '@ember/destroyable';
4
+ import { createStore } from './store.js';
5
+ import { findOwner } from './utils.js';
6
+
7
+ /**
8
+ * Creates or returns the ResizeObserverManager.
9
+ *
10
+ * Only one of these will exist per owner.
11
+ *
12
+ * Has only two methods:
13
+ * - observe(element, callback: (resizeObserverEntry) => void)
14
+ * - unobserve(element, callback: (resizeObserverEntry) => void)
15
+ *
16
+ * Like with the underlying ResizeObserver API (and all event listeners),
17
+ * the callback passed to unobserved must be the same reference as the one
18
+ * passed to observe.
19
+ */
20
+ function resizeObserver(context) {
21
+ const owner = findOwner(context);
22
+ assert(`Could not find owner on the passed context (to resizeObserver). resizeObserver can only be used on an object whos lifetime is in someone entangled with the application (which incidentally has an "owner").`, owner);
23
+ return createStore(owner, ResizeObserverManager);
24
+ }
25
+ class ResizeObserverManager {
26
+ #callbacks = new WeakMap();
27
+ #handleResize = entries => {
28
+ for (const entry of entries) {
29
+ const callbacks = this.#callbacks.get(entry.target);
30
+ if (callbacks) {
31
+ for (const callback of callbacks) {
32
+ callback(entry);
33
+ }
34
+ }
35
+ }
36
+ };
37
+ #observer = new ResizeObserver(this.#handleResize);
38
+ constructor() {
39
+ ignoreROError();
40
+ registerDestructor(this, () => {
41
+ this.#observer?.disconnect();
42
+ });
43
+ }
44
+
45
+ /**
46
+ * Initiate the observing of the `element` or add an additional `callback`
47
+ * if the `element` is already observed.
48
+ *
49
+ * @param {object} element
50
+ * @param {function} callback The `callback` is called whenever the size of
51
+ * the `element` changes. It is called with `ResizeObserverEntry` object
52
+ * for the particular `element`.
53
+ */
54
+ observe(element, callback) {
55
+ const callbacks = this.#callbacks.get(element);
56
+ if (callbacks) {
57
+ callbacks.add(callback);
58
+ } else {
59
+ this.#callbacks.set(element, new Set([callback]));
60
+ this.#observer.observe(element);
61
+ }
62
+ }
63
+
64
+ /**
65
+ * End the observing of the `element` or just remove the provided `callback`.
66
+ *
67
+ * It will unobserve the `element` if the `callback` is not provided
68
+ * or there are no more callbacks left for this `element`.
69
+ *
70
+ * @param {object} element
71
+ * @param {function?} callback - The `callback` to remove from the listeners
72
+ * of the `element` size changes.
73
+ */
74
+ unobserve(element, callback) {
75
+ const callbacks = this.#callbacks.get(element);
76
+ if (!callbacks) {
77
+ return;
78
+ }
79
+ callbacks.delete(callback);
80
+ if (!callback || !callbacks.size) {
81
+ this.#callbacks.delete(element);
82
+ this.#observer.unobserve(element);
83
+ }
84
+ }
85
+ }
86
+ const errorMessages = ['ResizeObserver loop limit exceeded', 'ResizeObserver loop completed with undelivered notifications.'];
87
+
88
+ /**
89
+ * Ignores "ResizeObserver loop limit exceeded" error in Ember tests.
90
+ *
91
+ * This "error" is safe to ignore as it is just a warning message,
92
+ * telling that the "looping" observation will be skipped in the current frame,
93
+ * and will be delivered in the next one.
94
+ *
95
+ * For some reason, it is fired as an `error` event at `window` failing Ember
96
+ * tests and exploding Sentry with errors that must be ignored.
97
+ */
98
+ function ignoreROError() {
99
+ if (typeof window.onerror !== 'function') {
100
+ return;
101
+ }
102
+ const onError = window.onerror;
103
+ window.onerror = (...args) => {
104
+ const [message] = args;
105
+ if (typeof message === 'string') {
106
+ if (errorMessages.includes(message)) return true;
107
+ }
108
+ onError(...args);
109
+ };
110
+ }
111
+
112
+ export { ignoreROError, resizeObserver };
113
+ //# sourceMappingURL=resize-observer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resize-observer.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/dist/utils.js CHANGED
@@ -1,4 +1,6 @@
1
1
 
2
+ import { getOwner } from '@ember/owner';
3
+
2
4
  // this is copy pasted from https://github.com/emberjs/ember.js/blob/60d2e0cddb353aea0d6e36a72fda971010d92355/packages/%40ember/-internals/glimmer/lib/helpers/unique-id.ts
3
5
  // Unfortunately due to https://github.com/emberjs/ember.js/issues/20165 we cannot use the built-in version in template tags
4
6
  function uniqueId() {
@@ -9,9 +11,52 @@ function uniqueId() {
9
11
  return ([3e7] + -1e3 + -4e3 + -2e3 + -1e11).replace(/[0-3]/g, a => (a * 4 ^ Math.random() * 16 >> (a & 2)).toString(16));
10
12
  }
11
13
  function isNewable(x) {
14
+ // TypeScript has really bad prototype support -- they don't really
15
+ // want folks using this sort of stuff -- but it's handy for perf and all that.
16
+ //
12
17
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
13
18
  return x.prototype?.constructor === x;
14
19
  }
15
20
 
16
- export { isNewable, uniqueId };
21
+ /**
22
+ * Loose check for an "ownerish" API.
23
+ * only the ".lookup" method is required.
24
+ *
25
+ * The requirements for what an "owner" is are sort of undefined,
26
+ * as the actual owner in ember applications has too much on it,
27
+ * and the long term purpose of the owner will be questioned once we
28
+ * eliminate the need to have a registry (what lookup looks in to),
29
+ * but we'll still need "Something" to represent the lifetime of the application.
30
+ *
31
+ * Technically, the owner could be any object, including `{}`
32
+ */
33
+ function isOwner(x) {
34
+ if (!isNonNullableObject(x)) return false;
35
+ return 'lookup' in x && typeof x.lookup === 'function';
36
+ }
37
+ function isNonNullableObject(x) {
38
+ if (typeof x !== 'object') return false;
39
+ if (x === null) return false;
40
+ return true;
41
+ }
42
+
43
+ /**
44
+ * Can receive the class instance or the owner itself, and will always return return the owner.
45
+ *
46
+ * undefined will be returned if the Owner does not exist on the passed object
47
+ *
48
+ * Can be useful when combined with `createStore` to then create "services",
49
+ * which don't require string lookup.
50
+ */
51
+ function findOwner(contextOrOwner) {
52
+ if (isOwner(contextOrOwner)) return contextOrOwner;
53
+
54
+ // _ENSURE_ that we have an object, else getOwner makes no sense to call
55
+ if (!isNonNullableObject(contextOrOwner)) return;
56
+ const maybeOwner = getOwner(contextOrOwner);
57
+ if (isOwner(maybeOwner)) return maybeOwner;
58
+ return;
59
+ }
60
+
61
+ export { findOwner, isNewable, isNonNullableObject, isOwner, uniqueId };
17
62
  //# sourceMappingURL=utils.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ember-primitives",
3
- "version": "0.41.0",
3
+ "version": "0.42.0",
4
4
  "description": "Making apps easier to build",
5
5
  "keywords": [
6
6
  "ember-addon"