@htmlplus/element 2.1.4 → 2.1.5

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.
@@ -5,7 +5,9 @@ const outsides = [];
5
5
  export const off = (target, type, handler, options) => {
6
6
  if (type != 'outside')
7
7
  return target.removeEventListener(type, handler, options);
8
- const index = outsides.findIndex((outside) => outside.target == target && outside.handler == handler && outside.options == options);
8
+ const index = outsides.findIndex((outside) => {
9
+ return outside.target == target && outside.handler == handler && outside.options == options;
10
+ });
9
11
  const outside = outsides[index];
10
12
  if (!outside)
11
13
  return;
@@ -8,7 +8,9 @@ export const merge = (target, ...sources) => {
8
8
  continue;
9
9
  }
10
10
  for (const key of Object.keys(source)) {
11
- if (target[key] instanceof Object && source[key] instanceof Object && target[key] !== source[key]) {
11
+ if (target[key] instanceof Object &&
12
+ source[key] instanceof Object &&
13
+ target[key] !== source[key]) {
12
14
  target[key] = merge(target[key], source[key]);
13
15
  }
14
16
  else {
@@ -15,52 +15,52 @@ export const request = (target, name, previous, callback) => {
15
15
  var _a, _b;
16
16
  // Creates/Gets a stacks.
17
17
  const stacks = (target[_a = CONSTANTS.API_STACKS] || (target[_a] = new Map()));
18
- // Creates/Updates a stack
18
+ // Creates/Updates a stack.
19
19
  const stack = stacks.get(name) || { callbacks: [], previous };
20
20
  // Adds the callback to the stack, if exists.
21
21
  callback && stack.callbacks.push(callback);
22
22
  // Stores the stack.
23
23
  stacks.set(name, stack);
24
- // Creates/Gets a micro task function.
25
- target[_b = CONSTANTS.API_REQUEST] || (target[_b] = task({
26
- run: () => {
27
- // Skips the rendering phase if DOM isn't ready.
28
- if (!target[CONSTANTS.API_CONNECTED])
29
- return;
30
- // Calculates the states to pass into lifecycles' callbacks.
31
- const states = new Map(Array.from(stacks)
32
- .filter((stack) => stack[0])
33
- .map((stack) => [stack[0], stack[1].previous]));
34
- // Calls the lifecycle's callback before the rendering phase.
35
- call(target, CONSTANTS.LIFECYCLE_UPDATE, states);
36
- // Calculates the template.
37
- const template = () => {
38
- // Calculates the markup.
39
- const markup = call(target, CONSTANTS.METHOD_RENDER);
40
- // Calculates the styles.
41
- const styles = getStyles(target);
42
- // Returns the markup if styles don't exist.
43
- if (!styles)
44
- return markup;
45
- // Returns the markup and styles together.
46
- return html `<style>${styles}</style>${markup}`;
47
- };
48
- // Renders template to the DOM.
49
- render(shadowRoot(target), template);
50
- // Invokes requests' callback.
51
- stacks.forEach((state) => {
52
- state.callbacks.forEach((callback, index, callbacks) => {
53
- callback(callbacks.length - 1 != index);
54
- });
24
+ // Defines a handler.
25
+ const handler = () => {
26
+ // Skips the rendering phase if DOM isn't ready.
27
+ if (!target[CONSTANTS.API_CONNECTED])
28
+ return;
29
+ // Calculates the states to pass into lifecycles' callbacks.
30
+ const states = new Map(Array.from(stacks)
31
+ .filter((stack) => stack[0])
32
+ .map((stack) => [stack[0], stack[1].previous]));
33
+ // Calls the lifecycle's callback before the rendering phase.
34
+ call(target, CONSTANTS.LIFECYCLE_UPDATE, states);
35
+ // Calculates the template.
36
+ const template = () => {
37
+ // Calculates the markup.
38
+ const markup = call(target, CONSTANTS.METHOD_RENDER);
39
+ // Calculates the styles.
40
+ const styles = getStyles(target);
41
+ // Returns the markup if styles don't exist.
42
+ if (!styles)
43
+ return markup;
44
+ // Returns the markup and styles together.
45
+ return html `<style>${styles}</style>${markup}`;
46
+ };
47
+ // Renders template to the DOM.
48
+ render(shadowRoot(target), template);
49
+ // Invokes requests' callback.
50
+ stacks.forEach((state) => {
51
+ state.callbacks.forEach((callback, index, callbacks) => {
52
+ callback(callbacks.length - 1 != index);
55
53
  });
56
- // Calls the lifecycle's callback after the rendering phase.
57
- call(target, CONSTANTS.LIFECYCLE_UPDATED, states);
58
- // Clears stacks.
59
- stacks.clear();
60
- // TODO: releated to the @Watch decorator.
61
- target[CONSTANTS.API_RENDER_COMPLETED] = true;
62
- }
63
- }));
54
+ });
55
+ // Calls the lifecycle's callback after the rendering phase.
56
+ call(target, CONSTANTS.LIFECYCLE_UPDATED, states);
57
+ // Clears stacks.
58
+ stacks.clear();
59
+ // TODO: releated to the @Watch decorator.
60
+ target[CONSTANTS.API_RENDER_COMPLETED] = true;
61
+ };
62
+ // Creates/Gets a micro task function.
63
+ target[_b = CONSTANTS.API_REQUEST] || (target[_b] = task({ handler }));
64
64
  // Calls the micro task.
65
65
  call(target, CONSTANTS.API_REQUEST);
66
66
  };
@@ -1,6 +1,6 @@
1
1
  export interface QueueOptions {
2
2
  canStart?: () => boolean;
3
3
  canRun?: () => boolean;
4
- run: () => void;
4
+ handler: () => void;
5
5
  }
6
6
  export declare const task: (options: QueueOptions) => () => Promise<boolean>;
@@ -1,14 +1,14 @@
1
1
  export const task = (options) => {
2
- let isPending, promise;
2
+ let running, promise;
3
3
  const run = () => {
4
4
  if (options.canStart && !options.canStart())
5
5
  return Promise.resolve(false);
6
- if (!isPending)
6
+ if (!running)
7
7
  promise = enqueue();
8
8
  return promise;
9
9
  };
10
10
  const enqueue = async () => {
11
- isPending = true;
11
+ running = true;
12
12
  try {
13
13
  await promise;
14
14
  }
@@ -16,17 +16,17 @@ export const task = (options) => {
16
16
  Promise.reject(error);
17
17
  }
18
18
  // TODO: maybe is optional
19
- if (!isPending)
19
+ if (!running)
20
20
  return promise;
21
21
  try {
22
22
  if (options.canRun && !options.canRun())
23
- return (isPending = false);
24
- options.run();
25
- isPending = false;
23
+ return (running = false);
24
+ options.handler();
25
+ running = false;
26
26
  return true;
27
27
  }
28
28
  catch (error) {
29
- isPending = false;
29
+ running = false;
30
30
  throw error;
31
31
  }
32
32
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@htmlplus/element",
3
- "version": "2.1.4",
3
+ "version": "2.1.5",
4
4
  "license": "MIT",
5
5
  "sideEffects": false,
6
6
  "author": "Masood Abdolian <m.abdolian@gmail.com>",
@@ -37,11 +37,10 @@
37
37
  "@babel/template": "^7.23.9",
38
38
  "@babel/traverse": "^7.23.9",
39
39
  "@babel/types": "^7.23.9",
40
- "@types/node": "^20.11.19",
40
+ "@types/node": "^20.11.20",
41
41
  "change-case": "^5.4.3",
42
42
  "fs-extra": "^11.2.0",
43
43
  "glob": "^10.3.10",
44
- "handlebars": "^4.7.8",
45
44
  "ora": "^8.0.1",
46
45
  "typescript": "^4.9.5"
47
46
  },
@@ -58,6 +57,6 @@
58
57
  "rimraf": "^5.0.5",
59
58
  "semantic-release": "^23.0.2",
60
59
  "tsx": "^4.7.1",
61
- "vite": "^5.1.3"
60
+ "vite": "^5.1.4"
62
61
  }
63
62
  }
@@ -48,7 +48,10 @@ export const customElement = (options) => {
48
48
  const { name, value } = path.node;
49
49
  if (name.name != 'className')
50
50
  return;
51
- const hasClass = path.parentPath.node.attributes.some((attribute) => { var _a; return ((_a = attribute.name) === null || _a === void 0 ? void 0 : _a.name) == 'class'; });
51
+ const hasClass = path.parentPath.node.attributes.some((attribute) => {
52
+ var _a;
53
+ return ((_a = attribute.name) === null || _a === void 0 ? void 0 : _a.name) == 'class';
54
+ });
52
55
  if (hasClass)
53
56
  return path.remove();
54
57
  path.replaceWith(t.jsxAttribute(t.jsxIdentifier('class'), value));
@@ -108,7 +111,9 @@ export const customElement = (options) => {
108
111
  const children = node.children.map(render).flat();
109
112
  const parts = [];
110
113
  parts.push('<', name);
111
- const hasSpreadAttribute = attributes.some((attribute) => attribute.type == 'JSXSpreadAttribute');
114
+ const hasSpreadAttribute = attributes.some((attribute) => {
115
+ return attribute.type == 'JSXSpreadAttribute';
116
+ });
112
117
  if (hasSpreadAttribute) {
113
118
  parts.push(' ', TODO(t.identifier('TODO'), attributes));
114
119
  }
@@ -251,7 +256,8 @@ export const customElement = (options) => {
251
256
  break;
252
257
  }
253
258
  // TODO
254
- if ((input === null || input === void 0 ? void 0 : input.type) == 'TSParenthesizedType' && ((_a = input === null || input === void 0 ? void 0 : input.typeAnnotation) === null || _a === void 0 ? void 0 : _a.type) == 'TSIntersectionType') {
259
+ if ((input === null || input === void 0 ? void 0 : input.type) == 'TSParenthesizedType' &&
260
+ ((_a = input === null || input === void 0 ? void 0 : input.typeAnnotation) === null || _a === void 0 ? void 0 : _a.type) == 'TSIntersectionType') {
255
261
  let types = input.types || input.typeAnnotation.types;
256
262
  if (types.length != 2)
257
263
  return;
@@ -14,7 +14,8 @@ export const extract = () => {
14
14
  context.classMembers = ((_b = (_a = context.class) === null || _a === void 0 ? void 0 : _a.body) === null || _b === void 0 ? void 0 : _b.body) || [];
15
15
  // TODO
16
16
  if (path.parentPath.isExportNamedDeclaration() && !((_c = context.class) === null || _c === void 0 ? void 0 : _c.leadingComments)) {
17
- context.class['_leadingComments'] = t.cloneNode(path.parentPath.node, true).leadingComments || [];
17
+ context.class['_leadingComments'] =
18
+ t.cloneNode(path.parentPath.node, true).leadingComments || [];
18
19
  }
19
20
  path.skip();
20
21
  }
@@ -1,6 +1,6 @@
1
1
  import { parse } from '@babel/parser';
2
- import { glob } from 'glob';
3
2
  import fs from 'fs-extra';
3
+ import { glob } from 'glob';
4
4
  import { dirname, resolve } from 'path';
5
5
  import { join } from 'path';
6
6
  import { visitor } from './visitor.js';