@cocreate/lazy-loader 1.11.3 → 1.12.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,22 @@
1
+ # [1.12.0](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.11.4...v1.12.0) (2023-12-18)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * assigning modules to window.CoCreate ([15037d8](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/15037d8caf95463e629ab162f9f41008070a3f20))
7
+
8
+
9
+ ### Features
10
+
11
+ * dispatch event "<component>Loaded" ([5e489e6](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/5e489e6a9dbf876ee4d17492be713b8dda01811f))
12
+
13
+ ## [1.11.4](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.11.3...v1.11.4) (2023-12-10)
14
+
15
+
16
+ ### Bug Fixes
17
+
18
+ * handle data.crud object ([15ffb7c](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/15ffb7c407503fd48c22874df455b8256b33b3bb))
19
+
1
20
  ## [1.11.3](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.11.2...v1.11.3) (2023-12-09)
2
21
 
3
22
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/lazy-loader",
3
- "version": "1.11.3",
3
+ "version": "1.12.0",
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,14 +1,15 @@
1
1
  import observer from '@cocreate/observer';
2
2
 
3
- function listen(callback, selector) {
3
+ function listen(name, callback, selector) {
4
4
 
5
- function observerCallback({ target }) {
6
- // let isInit = target.querySelector(selector)/testtt
7
- // if (isInit) {
8
- callback()
9
- // console.log('lazyloaded', selector)
5
+ async function observerCallback({ target }) {
6
+ const module = await callback()
10
7
  observer.uninit(observerCallback)
11
- // }
8
+ Object.assign(window.CoCreate, {
9
+ [name]: module.default || module
10
+ });
11
+
12
+ dispatchComponentLoaded(name)
12
13
  }
13
14
 
14
15
  observer.init({
@@ -50,12 +51,19 @@ export async function lazyLoad(name, selector, callback) {
50
51
  if (document.querySelector(selector))
51
52
  await dependency(name, await callback())
52
53
  else
53
- listen(callback, selector)
54
+ listen(name, callback, selector)
54
55
  }
55
56
 
56
57
  export async function dependency(name, promise) {
57
58
  let component = await promise;
58
59
  Object.assign(window.CoCreate, {
59
- [name]: component.default
60
+ [name]: component.default || component
60
61
  });
61
- }
62
+ dispatchComponentLoaded(name)
63
+ }
64
+
65
+ function dispatchComponentLoaded(name) {
66
+ document.dispatchEvent(new CustomEvent(name + 'Loaded', {
67
+ detail: { name }
68
+ }));
69
+ }
package/src/server.js CHANGED
@@ -79,7 +79,7 @@ class CoCreateLazyLoader {
79
79
  if (valideUrl.pathname.startsWith('/webhooks/')) {
80
80
  let name = req.url.split('/')[2]; // Assuming URL structure is /webhook/name/...
81
81
  if (this.modules[name]) {
82
- this.executeScriptWithTimeout(name, { req, res, crud: this.crud, organization, valideUrl, organization_id: organization._id })
82
+ this.executeScriptWithTimeout(name, { req, res, organization, valideUrl, organization_id: organization._id })
83
83
  } else {
84
84
  // Handle unknown module or missing webhook method
85
85
  res.writeHead(404, { 'Content-Type': 'application/json' });
@@ -118,8 +118,10 @@ class CoCreateLazyLoader {
118
118
 
119
119
  if (this.modules[name].content) {
120
120
  data.apis = await this.getApiKey(data.organization_id, name)
121
+ data.crud = this.crud
121
122
  data = await this.modules[name].content.send(data)
122
123
  delete data.apis
124
+ delete data.crud
123
125
  if (data.socket)
124
126
  this.wsManager.send(data)
125
127
  } else