@widgetic/workflows 0.2.12 → 0.2.13

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.
@@ -0,0 +1,10 @@
1
+ import type { WorkflowList, WorkflowListItem } from '../api/types.js';
2
+ export type WorkflowsListCachePayload = {
3
+ items: WorkflowListItem[];
4
+ total: number;
5
+ cachedAt: number;
6
+ };
7
+ export declare function workflowsListEntityKey(workspaceId: string): string;
8
+ export declare function readWorkflowsListCache(workspaceId: string): Promise<WorkflowsListCachePayload | null>;
9
+ export declare function writeWorkflowsListCache(workspaceId: string, page: WorkflowList): Promise<void>;
10
+ export declare function deleteWorkflowsListCache(workspaceId: string): Promise<void>;
@@ -0,0 +1,70 @@
1
+ const LIST_KIND = 'workflows.list';
2
+ async function cacheLayer() {
3
+ try {
4
+ return await import('@widgetic/cache-layer');
5
+ }
6
+ catch {
7
+ return null;
8
+ }
9
+ }
10
+ function bindCacheOwner(layer) {
11
+ layer.ensureCacheOwnerFromSessionHint?.();
12
+ }
13
+ export function workflowsListEntityKey(workspaceId) {
14
+ return `${workspaceId}:default`;
15
+ }
16
+ export async function readWorkflowsListCache(workspaceId) {
17
+ const layer = await cacheLayer();
18
+ if (!layer)
19
+ return null;
20
+ bindCacheOwner(layer);
21
+ const ownerId = layer.getCacheOwner();
22
+ if (!ownerId)
23
+ return null;
24
+ try {
25
+ const payload = await layer.readEntity(ownerId, LIST_KIND, workflowsListEntityKey(workspaceId), layer.CACHE_POLICIES['workflows.list'].staleTtlMs);
26
+ if (payload?.items?.length) {
27
+ console.info('[workflows-cache] Dexie hit', workspaceId, payload.items.length, 'rows');
28
+ }
29
+ return payload;
30
+ }
31
+ catch (error) {
32
+ console.warn('[workflows] list cache read failed (non-fatal)', error);
33
+ return null;
34
+ }
35
+ }
36
+ export async function writeWorkflowsListCache(workspaceId, page) {
37
+ const layer = await cacheLayer();
38
+ if (!layer)
39
+ return;
40
+ bindCacheOwner(layer);
41
+ const ownerId = layer.getCacheOwner();
42
+ if (!ownerId)
43
+ return;
44
+ const payload = {
45
+ items: page.items,
46
+ total: page.total,
47
+ cachedAt: Date.now()
48
+ };
49
+ try {
50
+ await layer.writeEntity(ownerId, LIST_KIND, workflowsListEntityKey(workspaceId), payload);
51
+ }
52
+ catch (error) {
53
+ console.warn('[workflows] list cache write failed (non-fatal)', error);
54
+ }
55
+ }
56
+ export async function deleteWorkflowsListCache(workspaceId) {
57
+ const layer = await cacheLayer();
58
+ if (!layer)
59
+ return;
60
+ bindCacheOwner(layer);
61
+ const ownerId = layer.getCacheOwner();
62
+ if (!ownerId)
63
+ return;
64
+ try {
65
+ await layer.deleteEntity(ownerId, LIST_KIND, workflowsListEntityKey(workspaceId));
66
+ }
67
+ catch (error) {
68
+ console.warn('[workflows] list cache delete failed (non-fatal)', error);
69
+ }
70
+ }
@@ -47,7 +47,8 @@
47
47
  class: className = '',
48
48
  onError,
49
49
  fetchImplementation,
50
- assistant = null
50
+ assistant = null,
51
+ initialListPage = null
51
52
  }: WorkflowsProps = $props();
52
53
 
53
54
  // Why stores and effects below instead of `$derived`: the design system's language (`update()`) and direction
@@ -109,6 +110,7 @@
109
110
  {onShowEmbedCode}
110
111
  {presence}
111
112
  {assistant}
113
+ {initialListPage}
112
114
  onError={reportToPage}
113
115
  />
114
116
  {/key}
@@ -28,6 +28,7 @@
28
28
  presence?: WorkflowsProps['presence'];
29
29
  assistant?: WorkflowsProps['assistant'];
30
30
  onError: (error: WorkflowsError) => void;
31
+ initialListPage?: WorkflowsProps['initialListPage'];
31
32
  }
32
33
 
33
34
  let {
@@ -40,7 +41,8 @@
40
41
  onShowEmbedCode,
41
42
  presence = false,
42
43
  assistant = null,
43
- onError
44
+ onError,
45
+ initialListPage = null
44
46
  }: Props = $props();
45
47
 
46
48
  // The client keeps what it was made with: a new workspace is a new screen, so reading the props once is what is meant.
@@ -91,6 +93,7 @@
91
93
  <div class="flex min-h-0 flex-1 flex-col">
92
94
  <WorkflowList
93
95
  {workspaceId}
96
+ {initialListPage}
94
97
  onopen={(id) => open(id)}
95
98
  onerror={onError}
96
99
  onshowembedcode={onShowEmbedCode}
@@ -10,6 +10,7 @@ interface Props {
10
10
  presence?: WorkflowsProps['presence'];
11
11
  assistant?: WorkflowsProps['assistant'];
12
12
  onError: (error: WorkflowsError) => void;
13
+ initialListPage?: WorkflowsProps['initialListPage'];
13
14
  }
14
15
  declare const WorkflowsWorkspace: import("svelte").Component<Props, {}, "">;
15
16
  type WorkflowsWorkspace = ReturnType<typeof WorkflowsWorkspace>;
@@ -52,9 +52,18 @@
52
52
  * Off unless it asks, here too: a list that was not told about it never says anything about others. Read once.
53
53
  */
54
54
  presence?: boolean;
55
+ /** From Dexie via the host layout load — default list paints before `load()` runs. */
56
+ initialListPage?: { items: readonly import('../../api/types.js').WorkflowListItem[]; total: number } | null;
55
57
  }
56
58
 
57
- let { workspaceId, onopen, onerror, onshowembedcode, presence = false }: Props = $props();
59
+ let {
60
+ workspaceId,
61
+ onopen,
62
+ onerror,
63
+ onshowembedcode,
64
+ presence = false,
65
+ initialListPage = null
66
+ }: Props = $props();
58
67
 
59
68
  const api = useWorkflowsApi();
60
69
  const { t } = useWorkflowsLanguage();
@@ -64,7 +73,8 @@
64
73
  workspaceId: untrack(() => workspaceId),
65
74
  reportError: (error) => onerror(error),
66
75
  // Read once, like the list: presence belongs to this opening of it.
67
- presence: untrack(() => presence)
76
+ presence: untrack(() => presence),
77
+ initialPage: untrack(() => initialListPage)
68
78
  });
69
79
  provideListState(list);
70
80
 
@@ -13,6 +13,11 @@ interface Props {
13
13
  * Off unless it asks, here too: a list that was not told about it never says anything about others. Read once.
14
14
  */
15
15
  presence?: boolean;
16
+ /** From Dexie via the host layout load — default list paints before `load()` runs. */
17
+ initialListPage?: {
18
+ items: readonly import('../../api/types.js').WorkflowListItem[];
19
+ total: number;
20
+ } | null;
16
21
  }
17
22
  declare const WorkflowList: import("svelte").Component<Props, {}, "">;
18
23
  type WorkflowList = ReturnType<typeof WorkflowList>;
@@ -20,6 +20,11 @@ export interface WorkflowListOptions {
20
20
  /** Keys list view state in `sessionStorage` and dialog drafts in `localStorage`. */
21
21
  workspaceId: string;
22
22
  reportError: (error: WorkflowsError) => void;
23
+ /** Hydrates the default list before the first paint (from Dexie via the host page). */
24
+ initialPage?: {
25
+ items: readonly WorkflowListItem[];
26
+ total: number;
27
+ } | null;
23
28
  /** For tests. */
24
29
  searchDelayMilliseconds?: number;
25
30
  /** For tests. */
@@ -155,6 +160,7 @@ export declare class WorkflowListState {
155
160
  private buildQuery;
156
161
  /** Reads the first page again with the search, the filter and the order as they are now. */
157
162
  load(): Promise<void>;
163
+ private fetchFirstPage;
158
164
  /** Reads the next page and adds it to the end. */
159
165
  loadMore(): Promise<void>;
160
166
  /** The person typed in the search: ask again once they stop. */
@@ -8,6 +8,7 @@ import { BLANK_WORKFLOW_NAME, createBlankWorkflowAndOpen } from './createBlankWo
8
8
  import { detectBrowserTimeZone } from './zonedTime.js';
9
9
  import { createDraftJournal } from '../drafts/draftJournal.js';
10
10
  import { WORKFLOW_NEW_DIALOG_DRAFT_NAMESPACE, listViewStateStorageKey, parseListViewStateEntry, serializeListViewState, validateWorkflowListViewState } from '../drafts/workflowListViewState.js';
11
+ import { readWorkflowsListCache, writeWorkflowsListCache } from '../cache/workflowsListCache.js';
11
12
  /** The tabs of the list, in the order the design draws them. */
12
13
  export const LIST_TABS = ['all', ...WORKFLOW_STATUSES];
13
14
  /** Whether what a control reported is one of the tabs (a select reports plain text). */
@@ -115,6 +116,13 @@ export class WorkflowListState {
115
116
  this.pageSize = options.pageSize ?? LIST_PAGE_SIZE;
116
117
  this.presenceWanted = options.presence === true;
117
118
  this.presenceOptions = options.presenceOptions;
119
+ const initial = options.initialPage;
120
+ if (initial && initial.items.length > 0) {
121
+ this.items = initial.items;
122
+ this.total = initial.total ?? initial.items.length;
123
+ this.loadState = 'ready';
124
+ console.info('[workflows-cache] initial page', this.workspaceId, initial.items.length, 'rows');
125
+ }
118
126
  }
119
127
  /**
120
128
  * Starts telling which teammate is editing which workflow of the list ("Anna is editing" on a row). Only when the page asked
@@ -328,8 +336,37 @@ export class WorkflowListState {
328
336
  this.controller?.abort();
329
337
  const controller = new AbortController();
330
338
  this.controller = controller;
331
- this.loadState = this.items.length === 0 ? 'loading' : this.loadState;
332
339
  this.failure = null;
340
+ const isDefaultQuery = this.search.trim() === '' && this.tab === 'all';
341
+ const hadItems = this.items.length > 0;
342
+ let paintedFromCache = hadItems;
343
+ if (isDefaultQuery && !hadItems) {
344
+ try {
345
+ const layer = await import('@widgetic/cache-layer');
346
+ layer.ensureCacheOwnerFromSessionHint?.();
347
+ }
348
+ catch {
349
+ /* no cache layer */
350
+ }
351
+ const cached = await readWorkflowsListCache(this.workspaceId);
352
+ if (cached && cached.items.length > 0 && !controller.signal.aborted && !this.disposed) {
353
+ this.items = cached.items;
354
+ this.total = cached.total;
355
+ this.loadState = 'ready';
356
+ paintedFromCache = true;
357
+ this.reconcileOpenMenuWithItems();
358
+ }
359
+ }
360
+ if (!paintedFromCache && !hadItems) {
361
+ this.loadState = 'loading';
362
+ }
363
+ if (paintedFromCache) {
364
+ void this.fetchFirstPage(controller, isDefaultQuery, true);
365
+ return;
366
+ }
367
+ await this.fetchFirstPage(controller, isDefaultQuery, false);
368
+ }
369
+ async fetchFirstPage(controller, isDefaultQuery, keepCachedOnFailure) {
333
370
  try {
334
371
  const page = await this.api.listWorkflows(this.buildQuery(0), { signal: controller.signal });
335
372
  if (controller.signal.aborted || this.disposed)
@@ -337,12 +374,19 @@ export class WorkflowListState {
337
374
  this.items = page.items;
338
375
  this.total = page.total;
339
376
  this.loadState = 'ready';
377
+ if (isDefaultQuery) {
378
+ void writeWorkflowsListCache(this.workspaceId, page);
379
+ }
340
380
  this.reconcileOpenMenuWithItems();
341
381
  this.editingWatcher?.lookSoon();
342
382
  }
343
383
  catch (error) {
344
384
  if (isCancelled(error) || this.disposed)
345
385
  return;
386
+ if (keepCachedOnFailure && this.items.length > 0) {
387
+ console.warn('[workflows-cache] list revalidate failed; keeping cached rows', error);
388
+ return;
389
+ }
346
390
  this.loadState = 'failed';
347
391
  this.failure = pickApiErrorMessage(error) ?? 'workflows.error.generic';
348
392
  this.noteFailure('Reading the list of workflows failed', error);
@@ -1,5 +1,5 @@
1
1
  import type { TextDirection } from '@widgetic/design-system/i18n';
2
- import type { AccessTokenFunction } from '../api/types.js';
2
+ import type { AccessTokenFunction, WorkflowListItem } from '../api/types.js';
3
3
  import type { WorkflowAssistantConfig } from '../assistant/types.js';
4
4
  export type { WorkflowAssistantConfig } from '../assistant/types.js';
5
5
  /** Why `onError` was called. */
@@ -81,4 +81,12 @@ export interface WorkflowsProps {
81
81
  assistant?: WorkflowAssistantConfig | null;
82
82
  /** For tests and previews: the `fetch` the screen asks the API with. The page's own `fetch` when left out. */
83
83
  fetchImplementation?: typeof fetch;
84
+ /**
85
+ * First page of the default list from Dexie (site layout load). Rows paint synchronously on return visits; `load()` still
86
+ * revalidates in the background.
87
+ */
88
+ initialListPage?: {
89
+ items: readonly WorkflowListItem[];
90
+ total: number;
91
+ } | null;
84
92
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@widgetic/workflows",
3
- "version": "0.2.12",
3
+ "version": "0.2.13",
4
4
  "description": "Workflows for Widgetic: chain compositions together with filters that decide which composition an embed shows. One Svelte package that the Widgetic site, partners and Shopify all load.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "type": "module",
@@ -55,11 +55,15 @@
55
55
  "kaykay": "0.3.0"
56
56
  },
57
57
  "peerDependencies": {
58
+ "@widgetic/cache-layer": ">=0.1.0",
58
59
  "@widgetic/chat": ">=0.1.36",
59
60
  "@widgetic/design-system": ">=0.5.23",
60
61
  "svelte": "^5.25.6"
61
62
  },
62
63
  "peerDependenciesMeta": {
64
+ "@widgetic/cache-layer": {
65
+ "optional": true
66
+ },
63
67
  "@widgetic/chat": {
64
68
  "optional": true
65
69
  }
@@ -72,6 +76,7 @@
72
76
  "@sveltejs/vite-plugin-svelte": "^5.0.3",
73
77
  "@tailwindcss/postcss": "^4.1.4",
74
78
  "@types/node": "^26.4.1",
79
+ "@widgetic/cache-layer": "^0.1.6",
75
80
  "@widgetic/chat": "^0.1.36",
76
81
  "@widgetic/design-system": "^0.5.23",
77
82
  "@widgetic/dev-tools": "^0.3.7",