MCPcopy Create free account
hub / github.com/NativeScript/SimDeck / reloadPreview

Function reloadPreview

scripts/experimental/swiftui-preview.mjs:233–371  ·  view source on GitHub ↗
(context, sourceFile, args, xcode)

Source from the content-addressed store, hash-verified

231}
232
233async function reloadPreview(context, sourceFile, args, xcode) {
234 const started = Date.now();
235 const timings = [];
236 const timed = (label, action) => {
237 const stageStarted = Date.now();
238 const result = action();
239 timings.push([label, Date.now() - stageStarted]);
240 return result;
241 };
242 const timedAsync = async (label, action) => {
243 const stageStarted = Date.now();
244 const result = await action();
245 timings.push([label, Date.now() - stageStarted]);
246 return result;
247 };
248 const source = timed("read source", () =>
249 fs.readFileSync(sourceFile, "utf8"),
250 );
251 const previews = timed("extract previews", () => extractPreviews(source));
252 if (previews.length === 0) {
253 throw new Error(`No #Preview blocks found in ${sourceFile}.`);
254 }
255 const preview = timed("select preview", () =>
256 selectPreview(previews, args.preview),
257 );
258 const stamp = `${Date.now()}-${process.pid}`;
259 const payloadDir = path.join(context.buildRoot, "payloads", stamp);
260 timed("prepare payload dir", () =>
261 fs.mkdirSync(payloadDir, { recursive: true }),
262 );
263
264 const sanitizedPath = path.join(payloadDir, "PreviewSource.swift");
265 const wrapperPath = path.join(payloadDir, "SimDeckPreviewPayload.swift");
266 const dylibPath = path.join(
267 payloadDir,
268 `SimDeckPreviewPayload-${stamp}.dylib`,
269 );
270 const sanitizedSource = timed("sanitize source", () =>
271 sanitizePreviewSource(
272 removeRanges(
273 source,
274 previews.map((item) => item.range),
275 ),
276 xcode?.moduleName,
277 ),
278 );
279 const sourceModule = args.splitCompile
280 ? timed("resolve source cache", () =>
281 previewSourceModule(context, sanitizedSource, args, xcode),
282 )
283 : null;
284 timed("write generated swift", () => {
285 fs.writeFileSync(sanitizedPath, sanitizedSource);
286 fs.writeFileSync(
287 wrapperPath,
288 payloadWrapperSource(
289 preview.body,
290 xcode?.moduleName,

Callers 1

mainFunction · 0.85

Calls 15

timedFunction · 0.85
extractPreviewsFunction · 0.85
selectPreviewFunction · 0.85
sanitizePreviewSourceFunction · 0.85
removeRangesFunction · 0.85
previewSourceModuleFunction · 0.85
payloadWrapperSourceFunction · 0.85
splitWrapperCompileArgsFunction · 0.85
monolithicCompileArgsFunction · 0.85
timedAsyncFunction · 0.85
sendTcpReloadBytesFunction · 0.85

Tested by

no test coverage detected