@makeswift/runtime 0.29.0-canary.19 → 0.29.0-canary.21

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.
@@ -171,128 +171,14 @@ Received "${apiKey}" instead.`
171
171
  const swatches = await this.getSwatches(swatchIds, siteVersion);
172
172
  return { ...unversionedResources, swatches };
173
173
  }
174
- // TODO: Consolidate this method with the introspectMany method once the
175
- // unstable_getComponentSnapshots method is stable and tested in production.
176
174
  async introspect(element, siteVersion, locale) {
177
- const descriptors = this.getElementDescriptors();
178
- const swatchIds = /* @__PURE__ */ new Set();
179
- const fileIds = /* @__PURE__ */ new Set();
180
- const typographyIds = /* @__PURE__ */ new Set();
181
- const tableIds = /* @__PURE__ */ new Set();
182
- const pageIds = /* @__PURE__ */ new Set();
183
- const globalElements = /* @__PURE__ */ new Map();
184
- const localizedGlobalElements = /* @__PURE__ */ new Map();
185
- const localizedResourcesMap = /* @__PURE__ */ new Map();
186
- const remaining = [element];
187
- const seen = /* @__PURE__ */ new Set();
188
- let current;
189
- while (current = remaining.pop()) {
190
- let getResourcesFromElementDescriptors2 = function(elementDescriptors2, props) {
191
- Object.entries(elementDescriptors2).forEach(([propName, descriptor]) => {
192
- getSwatchIds(descriptor, props[propName]).forEach((swatchId) => {
193
- swatchIds.add(swatchId);
194
- });
195
- getFileIds(descriptor, props[propName]).forEach((fileId) => fileIds.add(fileId));
196
- getTypographyIds(descriptor, props[propName]).forEach(
197
- (typographyId) => typographyIds.add(typographyId)
198
- );
199
- getTableIds(descriptor, props[propName]).forEach((tableId) => tableIds.add(tableId));
200
- getPageIds(descriptor, props[propName]).forEach((pageId) => pageIds.add(pageId));
201
- getElementChildren(descriptor, props[propName]).forEach((child) => {
202
- if (!seen.has(child.key)) {
203
- seen.add(child.key);
204
- remaining.push(child);
205
- }
206
- });
207
- });
208
- };
209
- var getResourcesFromElementDescriptors = getResourcesFromElementDescriptors2;
210
- let element2;
211
- if (isElementReference(current)) {
212
- const globalElementId = current.value;
213
- const globalElement = await this.getGlobalElement(globalElementId, siteVersion);
214
- let elementData = globalElement?.data;
215
- if (locale) {
216
- const localizedGlobalElement = await this.getLocalizedGlobalElement(
217
- globalElementId,
218
- locale,
219
- siteVersion
220
- );
221
- if (localizedGlobalElement) {
222
- elementData = localizedGlobalElement.data;
223
- localizedResourcesMap.set(globalElementId, localizedGlobalElement.id);
224
- localizedGlobalElements.set(localizedGlobalElement.id, localizedGlobalElement);
225
- } else {
226
- localizedResourcesMap.set(globalElementId, null);
227
- }
228
- }
229
- globalElements.set(globalElementId, globalElement);
230
- if (elementData == null)
231
- continue;
232
- element2 = elementData;
233
- } else {
234
- element2 = current;
235
- }
236
- const elementDescriptors = descriptors.get(element2.type);
237
- if (elementDescriptors == null)
238
- continue;
239
- getResourcesFromElementDescriptors2(elementDescriptors, element2.props);
240
- }
241
- const typographies = await this.getTypographies([...typographyIds], siteVersion);
242
- typographies.forEach((typography) => {
243
- typography?.style.forEach((style) => {
244
- const swatchId = style.value.color?.swatchId;
245
- if (swatchId != null)
246
- swatchIds.add(swatchId);
247
- });
248
- });
249
- const pagePathnames = await this.getPagePathnameSlices([...pageIds], siteVersion, { locale });
250
- const { swatches, files, tables } = await this.getIntrospectedResources(
251
- {
252
- swatchIds: [...swatchIds],
253
- fileIds: [...fileIds],
254
- tableIds: [...tableIds]
255
- },
256
- siteVersion
175
+ const treeId = "root";
176
+ const cacheDataMap = await this.introspectMany(
177
+ [{ id: treeId, data: element }],
178
+ siteVersion,
179
+ locale
257
180
  );
258
- const apiResources = {
259
- [APIResourceType.Swatch]: [...swatchIds].map((id) => ({
260
- id,
261
- value: swatches.find((swatch) => swatch?.id === id) ?? null
262
- })),
263
- [APIResourceType.File]: [...fileIds].map((id) => ({
264
- id,
265
- value: files.find((file) => file?.id === id) ?? null
266
- })),
267
- [APIResourceType.Typography]: [...typographyIds].map((id) => ({
268
- id,
269
- value: typographies.find((typography) => typography?.id === id) ?? null
270
- })),
271
- [APIResourceType.Table]: [...tableIds].map((id) => ({
272
- id,
273
- value: tables.find((table) => table?.id === id) ?? null
274
- })),
275
- [APIResourceType.PagePathnameSlice]: [...pageIds].map((id) => ({
276
- id,
277
- value: pagePathnames.find((pagePathnameSlice) => pagePathnameSlice?.id === id) ?? null,
278
- locale
279
- })),
280
- [APIResourceType.GlobalElement]: [...globalElements.entries()].map(([id, value]) => ({
281
- id,
282
- value
283
- })),
284
- [APIResourceType.LocalizedGlobalElement]: [...localizedGlobalElements.entries()].map(
285
- ([id, value]) => ({
286
- id,
287
- value,
288
- locale
289
- })
290
- )
291
- };
292
- return {
293
- apiResources,
294
- localizedResourcesMap: locale != null ? { [locale]: Object.fromEntries(localizedResourcesMap.entries()) } : {}
295
- };
181
+ return cacheDataMap.get(treeId);
296
182
  }
297
183
  async introspectMany(trees, siteVersion, locale) {
298
184
  if (trees.length === 0)
@@ -300,106 +186,116 @@ Received "${apiKey}" instead.`
300
186
  const descriptors = this.getElementDescriptors();
301
187
  const globalElementCache = /* @__PURE__ */ new Map();
302
188
  const localizedGlobalElementCache = /* @__PURE__ */ new Map();
303
- const treeCaches = new Map(
189
+ function createTreeCache() {
190
+ return {
191
+ swatchIds: /* @__PURE__ */ new Set(),
192
+ fileIds: /* @__PURE__ */ new Set(),
193
+ typographyIds: /* @__PURE__ */ new Set(),
194
+ tableIds: /* @__PURE__ */ new Set(),
195
+ pageIds: /* @__PURE__ */ new Set(),
196
+ globalElements: /* @__PURE__ */ new Map(),
197
+ localizedGlobalElements: /* @__PURE__ */ new Map(),
198
+ localizedResourcesMap: /* @__PURE__ */ new Map()
199
+ };
200
+ }
201
+ const traversals = new Map(
304
202
  trees.map((tree) => [
305
203
  tree.id,
306
204
  {
307
- data: tree.data,
308
- swatchIds: /* @__PURE__ */ new Set(),
309
- fileIds: /* @__PURE__ */ new Set(),
310
- typographyIds: /* @__PURE__ */ new Set(),
311
- tableIds: /* @__PURE__ */ new Set(),
312
- pageIds: /* @__PURE__ */ new Set(),
313
- globalElements: /* @__PURE__ */ new Map(),
314
- localizedGlobalElements: /* @__PURE__ */ new Map(),
315
- localizedResourcesMap: /* @__PURE__ */ new Map()
205
+ cache: createTreeCache(),
206
+ remaining: [tree.data],
207
+ seen: /* @__PURE__ */ new Set()
316
208
  }
317
209
  ])
318
210
  );
319
- for (const currentTreeCache of treeCaches.values()) {
320
- const remaining = [currentTreeCache.data];
321
- const seen = /* @__PURE__ */ new Set();
322
- let current;
323
- while (current = remaining.pop()) {
324
- let getResourcesFromElementDescriptors2 = function(elementDescriptors2, props) {
325
- Object.entries(elementDescriptors2).forEach(([propName, descriptor]) => {
326
- getSwatchIds(descriptor, props[propName]).forEach((swatchId) => {
327
- currentTreeCache.swatchIds.add(swatchId);
328
- });
329
- getFileIds(descriptor, props[propName]).forEach((fileId) => {
330
- currentTreeCache.fileIds.add(fileId);
211
+ while (true) {
212
+ const pendingGlobalElementIds = /* @__PURE__ */ new Set();
213
+ const deferred = [];
214
+ for (const [treeId, traversal] of traversals) {
215
+ const { remaining, seen, cache: currentTreeCache } = traversal;
216
+ let current;
217
+ while (current = remaining.pop()) {
218
+ let getResourcesFromElementDescriptors2 = function(elementDescriptors2, props) {
219
+ Object.entries(elementDescriptors2).forEach(([propName, descriptor]) => {
220
+ getSwatchIds(descriptor, props[propName]).forEach((swatchId) => {
221
+ currentTreeCache.swatchIds.add(swatchId);
222
+ });
223
+ getFileIds(descriptor, props[propName]).forEach((fileId) => {
224
+ currentTreeCache.fileIds.add(fileId);
225
+ });
226
+ getTypographyIds(descriptor, props[propName]).forEach((typographyId) => {
227
+ currentTreeCache.typographyIds.add(typographyId);
228
+ });
229
+ getTableIds(descriptor, props[propName]).forEach((tableId) => {
230
+ currentTreeCache.tableIds.add(tableId);
231
+ });
232
+ getPageIds(descriptor, props[propName]).forEach((pageId) => {
233
+ currentTreeCache.pageIds.add(pageId);
234
+ });
235
+ getElementChildren(descriptor, props[propName]).forEach((child) => {
236
+ if (!seen.has(child.key)) {
237
+ seen.add(child.key);
238
+ remaining.push(child);
239
+ }
240
+ });
331
241
  });
332
- getTypographyIds(descriptor, props[propName]).forEach((typographyId) => {
333
- currentTreeCache.typographyIds.add(typographyId);
334
- });
335
- getTableIds(descriptor, props[propName]).forEach((tableId) => {
336
- currentTreeCache.tableIds.add(tableId);
337
- });
338
- getPageIds(descriptor, props[propName]).forEach((pageId) => {
339
- currentTreeCache.pageIds.add(pageId);
340
- });
341
- getElementChildren(descriptor, props[propName]).forEach((child) => {
342
- if (!seen.has(child.key)) {
343
- seen.add(child.key);
344
- remaining.push(child);
345
- }
346
- });
347
- });
348
- };
349
- var getResourcesFromElementDescriptors = getResourcesFromElementDescriptors2;
350
- let element;
351
- if (isElementReference(current)) {
352
- const globalElementId = current.value;
353
- let globalElement;
354
- if (globalElementCache.has(globalElementId)) {
355
- globalElement = globalElementCache.get(globalElementId);
356
- } else {
357
- globalElement = await this.getGlobalElement(globalElementId, siteVersion);
358
- globalElementCache.set(globalElementId, globalElement);
359
- }
360
- let elementData = globalElement?.data;
361
- if (locale) {
362
- let localizedGlobalElement;
363
- if (localizedGlobalElementCache.has(globalElementId)) {
364
- localizedGlobalElement = localizedGlobalElementCache.get(globalElementId);
365
- } else {
366
- localizedGlobalElement = await this.getLocalizedGlobalElement(
367
- globalElementId,
368
- locale,
369
- siteVersion
370
- );
371
- localizedGlobalElementCache.set(globalElementId, localizedGlobalElement);
242
+ };
243
+ var getResourcesFromElementDescriptors = getResourcesFromElementDescriptors2;
244
+ let element;
245
+ if (isElementReference(current)) {
246
+ const globalElementId = current.value;
247
+ if (!globalElementCache.has(globalElementId)) {
248
+ pendingGlobalElementIds.add(globalElementId);
249
+ deferred.push({ treeId, element: current });
250
+ continue;
372
251
  }
373
- if (localizedGlobalElement) {
374
- elementData = localizedGlobalElement.data;
375
- currentTreeCache.localizedResourcesMap.set(globalElementId, localizedGlobalElement.id);
376
- currentTreeCache.localizedGlobalElements.set(
377
- localizedGlobalElement.id,
378
- localizedGlobalElement
379
- );
380
- } else {
381
- currentTreeCache.localizedResourcesMap.set(globalElementId, null);
252
+ const globalElement = globalElementCache.get(globalElementId) ?? null;
253
+ let elementData = globalElement?.data;
254
+ if (locale) {
255
+ const localizedGlobalElement = localizedGlobalElementCache.get(globalElementId) ?? null;
256
+ if (localizedGlobalElement) {
257
+ elementData = localizedGlobalElement.data;
258
+ currentTreeCache.localizedResourcesMap.set(
259
+ globalElementId,
260
+ localizedGlobalElement.id
261
+ );
262
+ currentTreeCache.localizedGlobalElements.set(
263
+ localizedGlobalElement.id,
264
+ localizedGlobalElement
265
+ );
266
+ } else {
267
+ currentTreeCache.localizedResourcesMap.set(globalElementId, null);
268
+ }
382
269
  }
270
+ currentTreeCache.globalElements.set(globalElementId, globalElement);
271
+ if (elementData == null)
272
+ continue;
273
+ element = elementData;
274
+ } else {
275
+ element = current;
383
276
  }
384
- currentTreeCache.globalElements.set(globalElementId, globalElement);
385
- if (elementData == null)
277
+ const elementDescriptors = descriptors.get(element.type);
278
+ if (elementDescriptors == null)
386
279
  continue;
387
- element = elementData;
388
- } else {
389
- element = current;
280
+ getResourcesFromElementDescriptors2(elementDescriptors, element.props);
390
281
  }
391
- const elementDescriptors = descriptors.get(element.type);
392
- if (elementDescriptors == null)
393
- continue;
394
- getResourcesFromElementDescriptors2(elementDescriptors, element.props);
395
282
  }
283
+ if (pendingGlobalElementIds.size === 0)
284
+ break;
285
+ const ids = [...pendingGlobalElementIds];
286
+ const results = await this.getGlobalElements(ids, siteVersion, { locale });
287
+ ids.forEach((id, index) => {
288
+ globalElementCache.set(id, results[index]?.base ?? null);
289
+ localizedGlobalElementCache.set(id, results[index]?.localized ?? null);
290
+ });
291
+ deferred.forEach(({ treeId, element }) => traversals.get(treeId).remaining.push(element));
396
292
  }
397
293
  const allSwatchIds = /* @__PURE__ */ new Set();
398
294
  const allFileIds = /* @__PURE__ */ new Set();
399
295
  const allTypographyIds = /* @__PURE__ */ new Set();
400
296
  const allTableIds = /* @__PURE__ */ new Set();
401
297
  const allPageIds = /* @__PURE__ */ new Set();
402
- for (const currentTreeCache of treeCaches.values()) {
298
+ for (const { cache: currentTreeCache } of traversals.values()) {
403
299
  currentTreeCache.swatchIds.forEach((id) => allSwatchIds.add(id));
404
300
  currentTreeCache.fileIds.forEach((id) => allFileIds.add(id));
405
301
  currentTreeCache.typographyIds.forEach((id) => allTypographyIds.add(id));
@@ -419,7 +315,7 @@ Received "${apiKey}" instead.`
419
315
  }
420
316
  });
421
317
  if (secondarySwatchIds.length > 0) {
422
- for (const currentTreeCache of treeCaches.values()) {
318
+ for (const { cache: currentTreeCache } of traversals.values()) {
423
319
  if (currentTreeCache.typographyIds.has(typography.id)) {
424
320
  secondarySwatchIds.forEach((id) => currentTreeCache.swatchIds.add(id));
425
321
  }
@@ -438,7 +334,7 @@ Received "${apiKey}" instead.`
438
334
  )
439
335
  ]);
440
336
  const result = /* @__PURE__ */ new Map();
441
- for (const [treeId, currentTreeCache] of treeCaches) {
337
+ for (const [treeId, { cache: currentTreeCache }] of traversals) {
442
338
  const apiResources = {
443
339
  [APIResourceType.Swatch]: [...currentTreeCache.swatchIds].map((id) => ({
444
340
  id,
@@ -529,71 +425,33 @@ Received "${apiKey}" instead.`
529
425
  async getComponentSnapshot(id, {
530
426
  siteVersion: siteVersionPromise,
531
427
  locale,
532
- allowLocaleFallback = true,
533
- unstable_enforceSuccess = false
428
+ allowLocaleFallback = true
534
429
  }) {
535
430
  const siteVersion = await siteVersionPromise;
536
431
  const key = deterministicUUID({ id, locale, seed: this.apiKey.split("-").at(0) });
537
- const baseLocaleWasRequested = locale == null;
538
- const canAttemptLocaleFallback = !baseLocaleWasRequested && allowLocaleFallback;
539
- const parseElementTreeResponse = async (response) => {
540
- if (!response.ok) {
541
- const failedBody = await failedResponseBody(response);
542
- if (response.status === 404) {
543
- return null;
544
- }
545
- throw new RestApiClientError(`Failed to get component snapshot for '${id}'`, response, {
546
- body: failedBody,
547
- siteVersion,
548
- locale
549
- });
550
- }
551
- const responseBody = await response.json();
552
- const parsed = Schema.componentDocumentResponse.parse(responseBody);
553
- if ("notFound" in parsed)
554
- return null;
555
- return parsed;
556
- };
557
- const getElementTree = async (searchParams) => {
558
- const response = await this.fetch(
559
- `v2/element-trees/${encodeURIComponent(id)}?${searchParams.toString()}`,
560
- siteVersion
561
- );
562
- return await parseElementTreeResponse(response);
563
- };
564
- const getPrimaryTree = async () => {
565
- const searchParams = new URLSearchParams();
566
- if (locale)
567
- searchParams.set("locale", locale);
568
- if (unstable_enforceSuccess)
569
- searchParams.set("unstable_enforceSuccess", "true");
570
- return await getElementTree(searchParams);
571
- };
572
- const getFallbackTree = async () => {
573
- const searchParams = new URLSearchParams();
574
- if (unstable_enforceSuccess)
575
- searchParams.set("unstable_enforceSuccess", "true");
576
- return await getElementTree(searchParams);
577
- };
578
- const primaryTree = await getPrimaryTree();
579
- const result = primaryTree == null && canAttemptLocaleFallback ? await getFallbackTree() : primaryTree;
580
- if (result == null) {
581
- return {
582
- document: {
583
- id,
584
- locale: locale ?? null,
585
- data: null
586
- },
587
- key,
588
- cacheData: CacheData.empty(),
589
- meta: {
590
- allowLocaleFallback,
591
- requestedLocale: locale ?? null
592
- }
593
- };
432
+ const searchParams = new URLSearchParams();
433
+ if (locale)
434
+ searchParams.set("locale", locale);
435
+ const response = await this.fetch(
436
+ `content/v1/element-trees/${encodeURIComponent(id)}?${searchParams.toString()}`,
437
+ siteVersion
438
+ );
439
+ if (!response.ok) {
440
+ const failedBody = await failedResponseBody(response);
441
+ throw new RestApiClientError(`Failed to get component snapshot for '${id}'`, response, {
442
+ body: failedBody,
443
+ siteVersion,
444
+ locale
445
+ });
594
446
  }
595
- const document = result;
596
- const cacheData = await this.introspect(document.data, siteVersion, document.locale);
447
+ const responseBody = await response.json();
448
+ const documentResponse = Schema.componentDocumentResponse.parse(responseBody);
449
+ const document = !allowLocaleFallback && documentResponse.locale != locale ? {
450
+ id,
451
+ locale: locale ?? null,
452
+ data: null
453
+ } : documentResponse;
454
+ const cacheData = document.data == null ? CacheData.empty() : await this.introspect(document.data, siteVersion, document.locale);
597
455
  return {
598
456
  document,
599
457
  cacheData,