MCPcopy
hub / github.com/QwikDev/qwik / dispatch

Function dispatch

packages/qwik/src/qwikloader.ts:77–182  ·  view source on GitHub ↗
(
  element: Element & { _qc_?: QContext | undefined },
  onPrefix: string,
  ev: Event,
  eventName = ev.type
)

Source from the content-addressed store, hash-verified

75 }) as T;
76
77const dispatch = async (
78 element: Element & { _qc_?: QContext | undefined },
79 onPrefix: string,
80 ev: Event,
81 eventName = ev.type
82) => {
83 const attrName = 'on' + onPrefix + ':' + eventName;
84 if (element.hasAttribute('preventdefault:' + eventName)) {
85 ev.preventDefault();
86 }
87 if (element.hasAttribute('stoppropagation:' + eventName)) {
88 ev.stopPropagation();
89 }
90 const ctx = element._qc_;
91 const relevantListeners = ctx && ctx.li.filter((li) => li[0] === attrName);
92 if (relevantListeners && relevantListeners.length > 0) {
93 for (const listener of relevantListeners) {
94 // listener[1] holds the QRL
95 const results = listener[1].getFn([element, ev], () => element.isConnected)(ev, element);
96 const cancelBubble = ev.cancelBubble;
97 if (isPromise(results)) {
98 await results;
99 }
100 // forcing async with await resets ev.cancelBubble to false
101 if (cancelBubble) {
102 ev.stopPropagation();
103 }
104 }
105 return;
106 }
107 const attrValue = element.getAttribute(attrName);
108 if (attrValue) {
109 const container = element.closest('[q\\:container]')! as QContainerElement;
110 const qBase = container.getAttribute('q:base')!;
111 const qVersion = container.getAttribute('q:version') || 'unknown';
112 const qManifest = container.getAttribute('q:manifest-hash') || 'dev';
113 const base = new URL(qBase, doc.baseURI);
114 for (const qrl of attrValue.split('\n')) {
115 const url = new URL(qrl, base);
116 const href = url.href;
117 const symbol = url.hash.replace(/^#?([^?[|]*).*$/, '$1') || 'default';
118 const reqTime = performance.now();
119 let handler: undefined | any;
120 let importError: undefined | 'sync' | 'async' | 'no-symbol';
121 let error: undefined | Error;
122 const isSync = qrl.startsWith('#');
123 const eventData: QwikSymbolEvent['detail'] = {
124 qBase,
125 qManifest,
126 qVersion,
127 href,
128 symbol,
129 element,
130 reqTime,
131 };
132 if (isSync) {
133 const hash = container.getAttribute('q:instance')!;
134 handler = ((doc as any)['qFuncs_' + hash] || [])[Number.parseInt(symbol)];

Callers 3

broadcastFunction · 0.70
processDocumentEventFunction · 0.70
processReadyStateChangeFunction · 0.70

Calls 7

resolveContainerFunction · 0.85
isPromiseFunction · 0.70
emitEventFunction · 0.70
hasAttributeMethod · 0.65
handlerFunction · 0.50
getAttributeMethod · 0.45
closestMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…