@fictjs/runtime 0.0.3 → 0.0.4

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/dist/slim.cjs CHANGED
@@ -1313,6 +1313,27 @@ function removeNodes(nodes) {
1313
1313
  function isReactive(value) {
1314
1314
  return typeof value === "function" && value.length === 0;
1315
1315
  }
1316
+ function callEventHandler(handler, event, node, data) {
1317
+ if (!handler) return;
1318
+ const context = node ?? event.currentTarget ?? void 0;
1319
+ const invoke = (fn) => {
1320
+ if (typeof fn === "function") {
1321
+ const result = data === void 0 ? fn.call(context, event) : fn.call(context, data, event);
1322
+ if (typeof result === "function" && result !== fn) {
1323
+ if (data === void 0) {
1324
+ result.call(context, event);
1325
+ } else {
1326
+ result.call(context, data, event);
1327
+ }
1328
+ } else if (result && typeof result.handleEvent === "function") {
1329
+ result.handleEvent.call(result, event);
1330
+ }
1331
+ } else if (fn && typeof fn.handleEvent === "function") {
1332
+ fn.handleEvent.call(fn, event);
1333
+ }
1334
+ };
1335
+ invoke(handler);
1336
+ }
1316
1337
  var PRIMITIVE_PROXY = Symbol("fict:primitive-proxy");
1317
1338
  var PRIMITIVE_PROXY_RAW_VALUE = Symbol("fict:primitive-proxy:raw-value");
1318
1339
  function createValueProxy(read) {
@@ -1799,14 +1820,10 @@ function globalEventHandler(e) {
1799
1820
  const hasData = rawData !== void 0;
1800
1821
  const resolvedNodeData = hasData ? resolveData(rawData) : void 0;
1801
1822
  if (typeof handler === "function") {
1802
- if (hasData) {
1803
- handler.call(node, resolvedNodeData, e);
1804
- } else {
1805
- handler.call(node, e);
1806
- }
1823
+ callEventHandler(handler, e, node, hasData ? resolvedNodeData : void 0);
1807
1824
  } else if (Array.isArray(handler)) {
1808
1825
  const tupleData = resolveData(handler[1]);
1809
- handler[0].call(node, tupleData, e);
1826
+ callEventHandler(handler[0], e, node, tupleData);
1810
1827
  }
1811
1828
  if (e.cancelBubble) return false;
1812
1829
  }
@@ -1854,23 +1871,15 @@ function bindEvent(el, eventName, handler, options2) {
1854
1871
  if (DelegatedEvents.has(eventName) && !options2) {
1855
1872
  const key = `$$${eventName}`;
1856
1873
  delegateEvents([eventName]);
1857
- const createWrapped = (resolve) => {
1858
- const wrapped2 = function(...args) {
1859
- try {
1860
- const fn = resolve();
1861
- if (typeof fn === "function") {
1862
- return fn.apply(this, args);
1863
- } else if (fn && typeof fn.handleEvent === "function") {
1864
- return fn.handleEvent.apply(fn, args);
1865
- }
1866
- } catch (err) {
1867
- handleError(err, { source: "event", eventName }, rootRef);
1868
- }
1869
- };
1870
- return wrapped2;
1871
- };
1872
1874
  const resolveHandler = isReactive(handler) ? handler : () => handler;
1873
- el[key] = createWrapped(resolveHandler);
1875
+ el[key] = function(...args) {
1876
+ try {
1877
+ const fn = resolveHandler();
1878
+ callEventHandler(fn, args[0], el);
1879
+ } catch (err) {
1880
+ handleError(err, { source: "event", eventName }, rootRef);
1881
+ }
1882
+ };
1874
1883
  return () => {
1875
1884
  el[key] = void 0;
1876
1885
  };
@@ -1879,13 +1888,7 @@ function bindEvent(el, eventName, handler, options2) {
1879
1888
  const wrapped = (event) => {
1880
1889
  try {
1881
1890
  const resolved = getHandler();
1882
- if (typeof resolved === "function") {
1883
- ;
1884
- resolved(event);
1885
- } else if (resolved && typeof resolved.handleEvent === "function") {
1886
- ;
1887
- resolved.handleEvent(event);
1888
- }
1891
+ callEventHandler(resolved, event, el);
1889
1892
  } catch (err) {
1890
1893
  if (handleError(err, { source: "event", eventName }, rootRef)) {
1891
1894
  return;