MCPcopy Create free account
hub / github.com/NativeScript/NativeScript / safeReadDefault

Function safeReadDefault

packages/vite/hmr/client/utils.ts:10–41  ·  view source on GitHub ↗
(mod: any)

Source from the content-addressed store, hash-verified

8 * If a TDZ error happens, retry several microtasks and finally one macrotask tick.
9 */
10export async function safeReadDefault(mod: any): Promise<any | null> {
11 const isTdz = (err: any) => /before initialization|TDZ/i.test(String(err?.message || err || ''));
12 // Attempt immediate read
13 try {
14 return mod?.default ?? mod ?? null;
15 } catch (e) {
16 if (!isTdz(e)) throw e;
17 }
18 // A few microtask retries
19 for (let i = 0; i < 4; i++) {
20 await Promise.resolve();
21 try {
22 return mod?.default ?? mod ?? null;
23 } catch (e) {
24 if (!isTdz(e)) throw e;
25 }
26 }
27 // One macrotask tick (allow dependent modules to finish evaluating)
28 await new Promise<void>((r) => setTimeout(r, 0));
29 try {
30 return mod?.default ?? mod ?? null;
31 } catch (e) {
32 if (!isTdz(e)) throw e;
33 }
34 // Final microtask after macrotask
35 await Promise.resolve();
36 try {
37 return mod?.default ?? mod ?? null;
38 } catch (e) {
39 throw e;
40 }
41}
42
43// Resolve NativeScript core classes/Application from the vendor realm or globalThis.
44export function getCore(name: 'Application' | 'Frame' | 'Page' | 'Label'): any {

Callers 1

loadSfcComponentFunction · 0.85

Calls 3

isTdzFunction · 0.85
resolveMethod · 0.80
setTimeoutFunction · 0.50

Tested by

no test coverage detected