MCPcopy Index your code
hub / github.com/NativeScript/NativeScript / createRootFactory

Function createRootFactory

packages/vite/hmr/client/index.ts:1188–1247  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1186 // Factory that mounts the component and returns a Page (from core aggregator realm)
1187 type RootFactory = { readonly kind: 'page' | 'frame'; create: () => any };
1188 function createRootFactory(): RootFactory {
1189 let rootKind: 'page' | 'frame' = 'page';
1190 let cachedRoot: any = undefined;
1191 const state = {
1192 getRootKind: () => rootKind,
1193 setRootKind: (k: 'page' | 'frame') => {
1194 rootKind = k;
1195 },
1196 getCachedRoot: () => cachedRoot,
1197 setCachedRoot: (r: any) => {
1198 cachedRoot = r;
1199 },
1200 };
1201 const factory: RootFactory = {
1202 get kind() {
1203 return rootKind;
1204 },
1205 create: () => {
1206 if (cachedRoot) return cachedRoot;
1207 try {
1208 switch (__NS_TARGET_FLAVOR__) {
1209 case 'vue':
1210 cachedRoot = getRootForVue(newComponent, state);
1211 break;
1212 case 'typescript': {
1213 // For TS flavor, treat the component as a factory or direct NS view.
1214 let root: any = null;
1215 try {
1216 if (typeof newComponent === 'function') {
1217 root = newComponent();
1218 } else {
1219 root = newComponent;
1220 }
1221 } catch (e) {
1222 console.warn('[hmr-client][ts] root factory invocation failed', e);
1223 }
1224 cachedRoot = root || {};
1225 // Heuristic: if the root "looks" like a Frame, prefer frame semantics
1226 try {
1227 const name = String(cachedRoot?.constructor?.name || '').replace(/^_+/, '');
1228 if (/^Frame(\$\d+)?$/.test(name)) {
1229 rootKind = 'frame';
1230 }
1231 } catch {}
1232 break;
1233 }
1234 }
1235 return cachedRoot;
1236 } catch (e) {
1237 console.warn('[hmr-client] createRootFactory failed', e);
1238 if (VERBOSE) console.warn('[hmr-client] [createRoot] stack', (e as any)?.stack);
1239 const GPage: any = getCore('Page');
1240 rootKind = 'page';
1241 cachedRoot = GPage ? new GPage() : ({} as any);
1242 return cachedRoot;
1243 }
1244 },
1245 } as RootFactory;

Callers 1

performResetRootFunction · 0.85

Calls 6

getRootForVueFunction · 0.85
warnMethod · 0.80
replaceMethod · 0.80
getCoreFunction · 0.70
StringClass · 0.50
testMethod · 0.45

Tested by

no test coverage detected