@cocreate/lazy-loader 1.23.1 → 1.23.3

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [1.23.3](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.23.2...v1.23.3) (2025-04-11)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * update observer obseve param to type and and attributeName to attributeFilter ([cb6a6d4](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/cb6a6d40ea407c360a183fcff141ec7b210c7320))
7
+
8
+ ## [1.23.2](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.23.1...v1.23.2) (2024-12-22)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * if (!window.CoCreate[name]) ([06f52b2](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/06f52b2f203d3860db065a786c095e8900d41508))
14
+
1
15
  ## [1.23.1](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.23.0...v1.23.1) (2024-12-09)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/lazy-loader",
3
- "version": "1.23.1",
3
+ "version": "1.23.3",
4
4
  "description": "A simple lazy-loader component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "lazy-loader",
package/src/client.js CHANGED
@@ -1,80 +1,78 @@
1
- import observer from '@cocreate/observer';
1
+ import observer from "@cocreate/observer";
2
2
 
3
3
  function listen(name, callback, selector) {
4
+ const observerName = name + "LazyloadAddedNodesObserver";
5
+ async function observerCallback(mutation) {
6
+ observer.uninit(observerName);
4
7
 
5
- async function observerCallback({ target }) {
6
- if (!window.CoCreate)
7
- window.CoCreate = {}
8
+ if (!window.CoCreate) window.CoCreate = {};
8
9
 
9
- if (window.CoCreate[name])
10
- return
11
- window.CoCreate[name] = {}
12
- observer.uninit(observerCallback)
10
+ if (window.CoCreate[name]) return;
11
+ window.CoCreate[name] = {};
12
+ // observer.uninit(name);
13
13
 
14
- const module = await callback()
15
- observer.uninit(observerCallback)
16
- window.CoCreate[name] = module.default || module
14
+ const module = await callback();
15
+ // observer.uninit(name);
16
+ window.CoCreate[name] = module.default || module;
17
17
 
18
- dispatchComponentLoaded(name)
19
- }
18
+ dispatchComponentLoaded(name);
19
+ }
20
20
 
21
- observer.init({
22
- name: 'lazyloadObserver',
23
- observe: ['childList'],
24
- selector,
25
- callback: observerCallback
26
- })
27
-
28
- let selectorAttributes = [];
29
- let attributes = selector.split(",")
30
- for (let attribute of attributes) {
31
- let attr = attribute.trim()
32
- if (attr.startsWith("[")) {
33
- let pos = attr.indexOf("*")
34
- if (pos == -1)
35
- pos = attr.indexOf("=")
36
- if (pos !== -1) {
37
- attr = attr.slice(1, pos)
38
- } else {
39
- attr = attr.slice(1, -1)
40
- }
41
- selectorAttributes.push(attr)
42
- }
43
-
44
- }
45
- if (selectorAttributes.length > 0)
46
- observer.init({
47
- name: 'lazyloadAttributeObserver',
48
- observe: ['attributes'],
49
- attributeName: selectorAttributes,
50
- selector,
51
- callback: observerCallback
52
- });
21
+ observer.init({
22
+ name: observerName,
23
+ types: ["addedNodes"],
24
+ selector,
25
+ callback: observerCallback
26
+ });
53
27
 
28
+ let selectorAttributes = [];
29
+ let attributes = selector.split(",");
30
+ for (let attribute of attributes) {
31
+ let attr = attribute.trim();
32
+ if (attr.startsWith("[")) {
33
+ let pos = attr.indexOf("*");
34
+ if (pos == -1) pos = attr.indexOf("=");
35
+ if (pos !== -1) {
36
+ attr = attr.slice(1, pos);
37
+ } else {
38
+ attr = attr.slice(1, -1);
39
+ }
40
+ selectorAttributes.push(attr);
41
+ }
42
+ }
43
+ if (selectorAttributes.length > 0)
44
+ observer.init({
45
+ name: observerName,
46
+ types: ["attributes"],
47
+ attributeFilter: selectorAttributes,
48
+ selector,
49
+ callback: observerCallback
50
+ });
54
51
  }
55
52
 
56
53
  export async function lazyLoad(name, selector, callback) {
57
- if (document.querySelector(selector))
58
- await dependency(name, await callback())
59
- else
60
- listen(name, callback, selector)
54
+ if (document.querySelector(selector))
55
+ await dependency(name, await callback());
56
+ else listen(name, callback, selector);
61
57
  }
62
58
 
63
59
  export async function dependency(name, promise) {
64
- try {
65
- let component = await promise;
66
- if (!window.CoCreate)
67
- window.CoCreate = {}
68
-
69
- window.CoCreate[name] = component.default || component
70
- dispatchComponentLoaded(name)
71
- } catch (error) {
72
- console.error('error loading chunck: ', error)
73
- }
60
+ try {
61
+ let component = await promise;
62
+ if (!window.CoCreate) window.CoCreate = {};
63
+ if (!window.CoCreate[name]) {
64
+ window.CoCreate[name] = component.default || component;
65
+ dispatchComponentLoaded(name);
66
+ }
67
+ } catch (error) {
68
+ console.error("error loading chunck: ", error);
69
+ }
74
70
  }
75
71
 
76
72
  function dispatchComponentLoaded(name) {
77
- document.dispatchEvent(new CustomEvent(name + 'Loaded', {
78
- detail: { name }
79
- }));
73
+ document.dispatchEvent(
74
+ new CustomEvent(name + "Loaded", {
75
+ detail: { name }
76
+ })
77
+ );
80
78
  }