@timeax/digital-service-engine 0.3.0 → 0.3.1

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.
@@ -362,6 +362,70 @@ var React6 = __toESM(require("react"), 1);
362
362
  function setLoadableError4(updater, error) {
363
363
  updater((s) => ({ ...s, loading: false, error }));
364
364
  }
365
+ function parseTimestamp(value) {
366
+ if (value === void 0 || value === null) return void 0;
367
+ if (typeof value === "number") {
368
+ return Number.isFinite(value) ? value : void 0;
369
+ }
370
+ const parsed = Date.parse(value);
371
+ return Number.isFinite(parsed) ? parsed : void 0;
372
+ }
373
+ function templateTime(template) {
374
+ var _a;
375
+ return (_a = parseTimestamp(template.updatedAt)) != null ? _a : parseTimestamp(template.createdAt);
376
+ }
377
+ function shouldReplaceTemplates(params) {
378
+ if (!params.requestedSince) return true;
379
+ if (!params.lastUpdatedAt) return false;
380
+ const requested = parseTimestamp(params.requestedSince);
381
+ const last = parseTimestamp(params.lastUpdatedAt);
382
+ if (requested === void 0 || last === void 0) {
383
+ return false;
384
+ }
385
+ return requested < last;
386
+ }
387
+ function pickNewestTemplate(current, incoming) {
388
+ const currentTime = templateTime(current);
389
+ const incomingTime = templateTime(incoming);
390
+ if (currentTime !== void 0 && incomingTime !== void 0) {
391
+ return incomingTime >= currentTime ? incoming : current;
392
+ }
393
+ if (currentTime === void 0 && incomingTime !== void 0) return incoming;
394
+ if (currentTime !== void 0 && incomingTime === void 0) return current;
395
+ return incoming;
396
+ }
397
+ function mergeTemplates(current, incoming, opts) {
398
+ var _a;
399
+ const sinceTime = parseTimestamp(opts == null ? void 0 : opts.since);
400
+ const incomingIds = new Set(incoming.map((template) => template.id));
401
+ const deletedIds = new Set((_a = opts == null ? void 0 : opts.deletedIds) != null ? _a : []);
402
+ const byId = /* @__PURE__ */ new Map();
403
+ for (const template of current != null ? current : []) {
404
+ if (deletedIds.has(template.id)) continue;
405
+ const updatedTime = templateTime(template);
406
+ const shouldHaveAppearedInDelta = (opts == null ? void 0 : opts.reconcileMissingSince) === true && sinceTime !== void 0 && updatedTime !== void 0 && updatedTime > sinceTime;
407
+ const missingFromDelta = shouldHaveAppearedInDelta && !incomingIds.has(template.id);
408
+ if (!missingFromDelta) {
409
+ byId.set(template.id, template);
410
+ }
411
+ }
412
+ for (const template of incoming) {
413
+ if (deletedIds.has(template.id)) continue;
414
+ const existing = byId.get(template.id);
415
+ byId.set(
416
+ template.id,
417
+ existing ? pickNewestTemplate(existing, template) : template
418
+ );
419
+ }
420
+ return Array.from(byId.values()).sort((a, b) => {
421
+ const aTime = templateTime(a);
422
+ const bTime = templateTime(b);
423
+ if (aTime !== void 0 && bTime !== void 0 && aTime !== bTime) {
424
+ return bTime - aTime;
425
+ }
426
+ return a.name.localeCompare(b.name);
427
+ });
428
+ }
365
429
  function useTemplatesSlice(params) {
366
430
  const {
367
431
  backend,
@@ -389,16 +453,26 @@ function useTemplatesSlice(params) {
389
453
  };
390
454
  }
391
455
  setTemplates((s) => ({ ...s, loading: true }));
456
+ const requestedSince = (_b = params2 == null ? void 0 : params2.since) != null ? _b : templates.updatedAt;
392
457
  const res = await backend.templates.refresh({
393
458
  workspaceId,
394
459
  branchId,
395
- since: (_b = params2 == null ? void 0 : params2.since) != null ? _b : templates.updatedAt
460
+ since: requestedSince
396
461
  });
397
462
  if (res.ok) {
398
- setTemplates({
399
- data: res.value,
400
- loading: false,
401
- updatedAt: runtime.now()
463
+ setTemplates((current) => {
464
+ const replace = shouldReplaceTemplates({
465
+ requestedSince,
466
+ lastUpdatedAt: current.updatedAt
467
+ });
468
+ return {
469
+ data: replace ? res.value : mergeTemplates(current.data, res.value, {
470
+ since: requestedSince,
471
+ reconcileMissingSince: false
472
+ }),
473
+ loading: false,
474
+ updatedAt: runtime.now()
475
+ };
402
476
  });
403
477
  return res;
404
478
  } else {
@@ -416,14 +490,14 @@ function useTemplatesSlice(params) {
416
490
  );
417
491
  const createTemplate = React6.useCallback(
418
492
  async (input) => {
419
- var _a, _b;
493
+ var _a;
420
494
  const res = await backend.templates.create(workspaceId, {
421
495
  ...input,
422
- branchId: (_a = input.branchId) != null ? _a : getCurrentBranchId()
496
+ branchId: input.branchId !== null ? input.branchId : getCurrentBranchId()
423
497
  });
424
498
  if (res.ok) {
425
499
  await refreshTemplates({
426
- branchId: (_b = res.value.branchId) != null ? _b : getCurrentBranchId()
500
+ branchId: (_a = res.value.branchId) != null ? _a : getCurrentBranchId()
427
501
  });
428
502
  }
429
503
  return res;
@@ -483,11 +557,23 @@ function useTemplatesSlice(params) {
483
557
  async (id) => {
484
558
  const res = await backend.templates.delete(id);
485
559
  if (res.ok) {
486
- await refreshTemplates({ branchId: getCurrentBranchId() });
560
+ const deleteRefreshSince = runtime.now();
561
+ setTemplates((current) => {
562
+ var _a, _b;
563
+ return {
564
+ ...current,
565
+ data: (_b = (_a = current.data) == null ? void 0 : _a.filter((template) => template.id !== id)) != null ? _b : current.data,
566
+ updatedAt: deleteRefreshSince
567
+ };
568
+ });
569
+ await refreshTemplates({
570
+ branchId: getCurrentBranchId(),
571
+ since: deleteRefreshSince
572
+ });
487
573
  }
488
574
  return res;
489
575
  },
490
- [backend.templates, getCurrentBranchId, refreshTemplates]
576
+ [backend.templates, getCurrentBranchId, refreshTemplates, runtime]
491
577
  );
492
578
  const invalidateTemplates = React6.useCallback(() => {
493
579
  setTemplates((s) => ({ ...s, updatedAt: void 0 }));
@@ -2750,7 +2836,7 @@ function WorkspaceProvider(props) {
2750
2836
  },
2751
2837
  branchContext: bootCtl.refreshBranchContext,
2752
2838
  templates: async (params) => {
2753
- await templatesSlice.refreshTemplates(params);
2839
+ return await templatesSlice.refreshTemplates(params);
2754
2840
  },
2755
2841
  participants: async (params) => {
2756
2842
  await branchesSlice.refreshParticipants(params);