@cat-of-summer/st-script 1.0.13 → 1.0.14

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/src/app/index.js CHANGED
@@ -1,5 +1,8 @@
1
1
  export default class App extends HTMLElement {
2
2
  static #boolean_attributes = new Set(['disabled', 'checked', 'readonly', 'required', 'selected', 'hidden', 'open', 'autofocus']);
3
+ // Атрибут у них - только начальное значение: после действий пользователя браузер
4
+ // смотрит на свойство, поэтому привязка выставляет и его.
5
+ static #live_properties = new Set(['checked', 'selected', 'value']);
3
6
  static #apps = new Map();
4
7
  static #instances = new Set();
5
8
  static #selectSync = new WeakMap();
@@ -61,11 +64,14 @@ export default class App extends HTMLElement {
61
64
  #cleanupBindings(bindings) {
62
65
  if (!bindings || !bindings.length) return;
63
66
  bindings.forEach(binding => {
67
+ // Слушатели снимаются в следующей задаче. Между слушателями пользовательского
68
+ // события браузер выполняет микрозадачи: #model меняет данные, #if убирает ветку,
69
+ // и снятый сразу @input того же события уже не был бы вызван.
64
70
  if (binding.type === 'event' && binding.element && binding.handler)
65
- binding.element.removeEventListener(binding.eventName, binding.handler);
71
+ setTimeout(() => binding.element.removeEventListener(binding.eventName, binding.handler));
66
72
  else if (binding.type === 'model' && binding.element && binding.handler) {
67
73
  let eventName = binding.element.tagName === 'SELECT' ? 'change' : 'input';
68
- binding.element.removeEventListener(eventName, binding.handler);
74
+ setTimeout(() => binding.element.removeEventListener(eventName, binding.handler));
69
75
  } else if (binding.type === 'attrSync' && binding.unwatch)
70
76
  binding.unwatch();
71
77
  binding.dispose?.();
@@ -812,14 +818,22 @@ export default class App extends HTMLElement {
812
818
  parts[index] = value == null ? '' : String(value);
813
819
  });
814
820
  let finalValue = parts.join('');
821
+ let live = App.#live_properties.has(attrName) && attrName in element
822
+ && (attrName !== 'value' || element.tagName === 'INPUT');
815
823
  if (App.#boolean_attributes.has(attrName)) {
816
824
  let falsy = finalValue === '' || finalValue === 'false' || finalValue === '0' || finalValue === 'null' || finalValue === 'undefined' || !finalValue;
817
825
  if (falsy)
818
826
  element.removeAttribute(attrName);
819
827
  else
820
828
  element.setAttribute(attrName, '');
821
- } else
829
+ if (live)
830
+ element[attrName] = !falsy;
831
+ } else {
822
832
  element.setAttribute(attrName, finalValue);
833
+ // Сравнение - чтобы не сбрасывать курсор в поле при каждом проходе эффекта.
834
+ if (live && element.value !== finalValue)
835
+ element.value = finalValue;
836
+ }
823
837
  });
824
838
  this.#bindings.push({ type: 'attribute', element, attrName, effect });
825
839
  }