firstly 0.5.1 → 0.6.0
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.
- package/CHANGELOG.md +20 -0
- package/esm/core/FF_Filter.d.ts +2 -2
- package/esm/core/FF_Filter.js +2 -2
- package/esm/core/FF_Validators.d.ts +2 -0
- package/esm/core/FF_Validators.js +8 -10
- package/esm/core/containsWords.d.ts +2 -2
- package/esm/core/containsWords.js +2 -2
- package/esm/core/tailwind.d.ts +3 -4
- package/esm/core/tailwind.js +3 -4
- package/esm/svelte/DemoForm.svelte +121 -0
- package/esm/svelte/DemoForm.svelte.d.ts +42 -0
- package/esm/svelte/DemoGrid.svelte +146 -55
- package/esm/svelte/DemoGrid.svelte.d.ts +10 -1
- package/esm/svelte/DialogOpenTest.svelte +10 -0
- package/esm/svelte/DialogOpenTest.svelte.d.ts +8 -0
- package/esm/svelte/FF_Config.svelte +13 -0
- package/esm/svelte/FF_Config.svelte.d.ts +3 -0
- package/esm/svelte/FF_Config.svelte.js +38 -0
- package/esm/svelte/FF_DialogManager.svelte +251 -0
- package/esm/svelte/FF_DialogManager.svelte.d.ts +13 -0
- package/esm/svelte/FF_PromptDefault.svelte +85 -0
- package/esm/svelte/FF_PromptDefault.svelte.d.ts +9 -0
- package/esm/svelte/FF_ToastHtml.svelte +9 -0
- package/esm/svelte/FF_ToastHtml.svelte.d.ts +6 -0
- package/esm/svelte/FF_ToastManager.svelte +22 -0
- package/esm/svelte/FF_ToastManager.svelte.d.ts +4 -0
- package/esm/svelte/dialog.svelte.d.ts +209 -0
- package/esm/svelte/dialog.svelte.js +243 -0
- package/esm/svelte/ff.svelte.d.ts +294 -0
- package/esm/svelte/ff.svelte.js +599 -0
- package/esm/svelte/index.d.ts +13 -2
- package/esm/svelte/index.js +8 -1
- package/esm/svelte/infiniteScroll.d.ts +1 -1
- package/esm/svelte/infiniteScroll.js +1 -1
- package/esm/svelte/toast.d.ts +59 -0
- package/esm/svelte/toast.js +92 -0
- package/esm/virtual/StateDemoEnum.js +1 -1
- package/package.json +2 -1
- package/esm/svelte/FF_Repo.svelte.d.ts +0 -198
- package/esm/svelte/FF_Repo.svelte.js +0 -305
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
import { repo as remultRepo, } from 'remult';
|
|
2
|
-
/**
|
|
3
|
-
* The reactive handle implementation. Not exported directly - consumers use a per-mode
|
|
4
|
-
* alias (`FF_RepoLoad`/`FF_RepoLive`/`FF_RepoPaginate`/`FF_RepoOne`) or the umbrella
|
|
5
|
-
* union `FF_Repo` (any mode). Each verb returns the Omit'd per-mode view of this.
|
|
6
|
-
*/
|
|
7
|
-
class FF_RepoHandle {
|
|
8
|
-
#repo;
|
|
9
|
-
#opts;
|
|
10
|
-
#mode;
|
|
11
|
-
#defaultOrderBy;
|
|
12
|
-
#paginator;
|
|
13
|
-
#seq = 0;
|
|
14
|
-
items = $state([]);
|
|
15
|
-
/** Single-record slot: the loaded row in `one` mode, or a create/edit draft (see `create`). */
|
|
16
|
-
item = $state(undefined);
|
|
17
|
-
loading = $state({
|
|
18
|
-
init: true,
|
|
19
|
-
fetching: false,
|
|
20
|
-
more: false,
|
|
21
|
-
saving: false,
|
|
22
|
-
deleting: false,
|
|
23
|
-
});
|
|
24
|
-
error = $state(undefined);
|
|
25
|
-
hasNextPage = $state(false);
|
|
26
|
-
/** Aggregations for the whole query (paginate mode). `aggregates.$count` is the total row count. */
|
|
27
|
-
aggregates = $state(undefined);
|
|
28
|
-
constructor(r, opts, mode) {
|
|
29
|
-
this.#repo = r;
|
|
30
|
-
this.#opts = opts;
|
|
31
|
-
this.#mode = mode;
|
|
32
|
-
this.#defaultOrderBy = r.metadata.options.defaultOrderBy;
|
|
33
|
-
$effect(() => {
|
|
34
|
-
const o = this.#resolve();
|
|
35
|
-
if (o.enabled === false) {
|
|
36
|
-
this.loading.init = false;
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
if (mode === 'live') {
|
|
40
|
-
// Pass orderBy so liveQuery re-sorts incrementally-added rows too;
|
|
41
|
-
// without it a freshly inserted row is appended and `items[0]` (the latest) goes stale.
|
|
42
|
-
const unsub = this.#repo
|
|
43
|
-
.liveQuery({ where: o.where, orderBy: o.orderBy, limit: o.limit, include: o.include })
|
|
44
|
-
.subscribe({
|
|
45
|
-
next: (info) => {
|
|
46
|
-
this.items = info.items;
|
|
47
|
-
this.loading.init = false;
|
|
48
|
-
},
|
|
49
|
-
error: (e) => {
|
|
50
|
-
this.error = e instanceof Error ? e.message : String(e);
|
|
51
|
-
this.loading.init = false;
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
return () => unsub();
|
|
55
|
-
}
|
|
56
|
-
// load | paginate | one: (re)fetch; a newer opts() invalidates older responses.
|
|
57
|
-
void this.#load(o, ++this.#seq);
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
#resolve() {
|
|
61
|
-
const o = this.#opts();
|
|
62
|
-
return { ...o, orderBy: o.orderBy ?? this.#defaultOrderBy };
|
|
63
|
-
}
|
|
64
|
-
async #load(o, seq, keepCount) {
|
|
65
|
-
this.loading.fetching = true;
|
|
66
|
-
this.error = undefined;
|
|
67
|
-
try {
|
|
68
|
-
if (this.#mode === 'paginate') {
|
|
69
|
-
// One request returns the page AND the aggregates ($count always, plus any
|
|
70
|
-
// requested): on the client REST proxy remult fetches both together. It sets
|
|
71
|
-
// `hasNextPage` from whether a full page came back (no count probe); `more()`
|
|
72
|
-
// then fetches the next page via a keyset cursor (orderBy + PK).
|
|
73
|
-
const p = await this.#repo
|
|
74
|
-
.query({
|
|
75
|
-
where: o.where,
|
|
76
|
-
orderBy: o.orderBy,
|
|
77
|
-
pageSize: keepCount ?? o.pageSize ?? 25,
|
|
78
|
-
include: o.include,
|
|
79
|
-
aggregate: { ...o.aggregate },
|
|
80
|
-
})
|
|
81
|
-
.paginator();
|
|
82
|
-
if (seq !== this.#seq)
|
|
83
|
-
return;
|
|
84
|
-
this.#paginator = p;
|
|
85
|
-
this.items = p.items;
|
|
86
|
-
this.hasNextPage = p.hasNextPage;
|
|
87
|
-
// `aggregates` is only on the paginator type when the aggregate is non-empty,
|
|
88
|
-
// but remult returns `$count` for the empty case too - so read it through a cast.
|
|
89
|
-
this.aggregates = p.aggregates;
|
|
90
|
-
}
|
|
91
|
-
else if (this.#mode === 'one') {
|
|
92
|
-
const found = await this.#repo.findFirst(o.where, {
|
|
93
|
-
orderBy: o.orderBy,
|
|
94
|
-
include: o.include,
|
|
95
|
-
});
|
|
96
|
-
if (seq !== this.#seq)
|
|
97
|
-
return;
|
|
98
|
-
this.item = found ?? undefined;
|
|
99
|
-
this.items = found ? [found] : [];
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
const items = await this.#repo.find({
|
|
103
|
-
where: o.where,
|
|
104
|
-
orderBy: o.orderBy,
|
|
105
|
-
limit: o.limit,
|
|
106
|
-
include: o.include,
|
|
107
|
-
});
|
|
108
|
-
if (seq !== this.#seq)
|
|
109
|
-
return;
|
|
110
|
-
this.items = items;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
catch (e) {
|
|
114
|
-
if (seq === this.#seq)
|
|
115
|
-
this.error = e instanceof Error ? e.message : String(e);
|
|
116
|
-
}
|
|
117
|
-
finally {
|
|
118
|
-
if (seq === this.#seq) {
|
|
119
|
-
this.loading.init = false;
|
|
120
|
-
this.loading.fetching = false;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
/** Re-run the current query (load/paginate/one), back to the first page. */
|
|
125
|
-
async refresh() {
|
|
126
|
-
if (this.#mode === 'live')
|
|
127
|
-
throw new Error('FF_Repo: refresh() is not available in live mode');
|
|
128
|
-
await this.#load(this.#resolve(), ++this.#seq);
|
|
129
|
-
}
|
|
130
|
-
/** Load and append the next page (paginate mode). */
|
|
131
|
-
async more() {
|
|
132
|
-
if (this.#mode !== 'paginate')
|
|
133
|
-
throw new Error('FF_Repo: more() requires paginate mode');
|
|
134
|
-
if (!this.#paginator || this.loading.more || !this.hasNextPage)
|
|
135
|
-
return;
|
|
136
|
-
this.loading.more = true;
|
|
137
|
-
try {
|
|
138
|
-
const next = await this.#paginator.nextPage();
|
|
139
|
-
this.#paginator = next;
|
|
140
|
-
this.items = [...this.items, ...next.items];
|
|
141
|
-
this.hasNextPage = next.hasNextPage;
|
|
142
|
-
}
|
|
143
|
-
finally {
|
|
144
|
-
this.loading.more = false;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Run `fn` once - the first time a row exists (`items[0]`).
|
|
149
|
-
*
|
|
150
|
-
* The point: seed editable UI state from the latest row WITHOUT a live query
|
|
151
|
-
* clobbering in-progress edits. It fires a single time, on the first non-empty
|
|
152
|
-
* result, and never again - later ticks (an edit, a delete, a re-sort) are
|
|
153
|
-
* ignored. Empty snapshots are skipped (a liveQuery often emits one before the
|
|
154
|
-
* data lands; there is nothing to seed from an empty result).
|
|
155
|
-
*
|
|
156
|
-
* For pure derived state prefer `$derived`; reach for `onFirst` only when the
|
|
157
|
-
* seed must become independently editable (a draft the user then mutates).
|
|
158
|
-
*
|
|
159
|
-
* ```svelte
|
|
160
|
-
* const list = ffRepo(Plan).listen(() => ({ where: { ownerDid } }))
|
|
161
|
-
* let draft = $state({ title: '' })
|
|
162
|
-
* list.onFirst((latest) => (draft.title = latest.title)) // seed once, then edit freely
|
|
163
|
-
* ```
|
|
164
|
-
*/
|
|
165
|
-
onFirst(fn) {
|
|
166
|
-
let done = false;
|
|
167
|
-
$effect(() => {
|
|
168
|
-
if (done)
|
|
169
|
-
return;
|
|
170
|
-
const latest = this.items[0];
|
|
171
|
-
if (latest == null)
|
|
172
|
-
return;
|
|
173
|
-
fn(latest);
|
|
174
|
-
done = true;
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
/** Create a new unsaved entity into the `item` slot (for an edit form). */
|
|
178
|
-
create(...args) {
|
|
179
|
-
this.item = this.#repo.create(...args);
|
|
180
|
-
return this.item;
|
|
181
|
-
}
|
|
182
|
-
// Mutations: run `op`, then the post-write sync on SUCCESS only (a failed write
|
|
183
|
-
// leaves the result untouched). On failure we fill `error` AND re-throw - the
|
|
184
|
-
// caller still gets the rejection (not silenced); `error` is for a reactive
|
|
185
|
-
// UI that wants it. `finally` only flips `loading` (no `await`, which would
|
|
186
|
-
// mask the original error).
|
|
187
|
-
async #write(flag, op, after) {
|
|
188
|
-
this.loading[flag] = true;
|
|
189
|
-
this.error = undefined;
|
|
190
|
-
try {
|
|
191
|
-
const res = await op();
|
|
192
|
-
await after();
|
|
193
|
-
return res;
|
|
194
|
-
}
|
|
195
|
-
catch (e) {
|
|
196
|
-
this.error = e instanceof Error ? e.message : String(e);
|
|
197
|
-
throw e;
|
|
198
|
-
}
|
|
199
|
-
finally {
|
|
200
|
-
this.loading[flag] = false;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
/** Save the current `item` (from `one` / `create()`). To save a specific row, use `.repo.save(row)`. */
|
|
204
|
-
save() {
|
|
205
|
-
return this.#write('saving', () => this.#repo.save(this.#requireItem()), () => this.#resync());
|
|
206
|
-
}
|
|
207
|
-
/** Delete the current `item`. To delete a specific row/id, use `.repo.delete(idOrRow)`. */
|
|
208
|
-
delete() {
|
|
209
|
-
const target = this.#requireItem();
|
|
210
|
-
return this.#write('deleting', () => this.#repo.delete(target), () => {
|
|
211
|
-
// live: liveQuery removes it. one: re-fetch (likely empty now).
|
|
212
|
-
// load/paginate: drop it locally (no refetch).
|
|
213
|
-
if (this.#mode === 'live')
|
|
214
|
-
return;
|
|
215
|
-
if (this.#mode === 'one')
|
|
216
|
-
return this.#resync();
|
|
217
|
-
this.#removeLocal(target);
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
/** The current `item` (or throw) - backs the argless `save()`/`delete()`. */
|
|
221
|
-
#requireItem() {
|
|
222
|
-
if (this.item === undefined)
|
|
223
|
-
throw new Error('FF_Repo: no `item` to save/delete - load one first (`one` mode or `create()`), or write a specific row through `.repo`.');
|
|
224
|
-
return this.item;
|
|
225
|
-
}
|
|
226
|
-
// Client-side list reconcilers (no server I/O) - reflect a change you made
|
|
227
|
-
// elsewhere (e.g. via `.repo`) in the reactive `items`. `load`/`paginate` only;
|
|
228
|
-
// `listen` reconciles itself via the liveQuery. `add`/`remove` also adjust
|
|
229
|
-
// `aggregates.$count` (not the other aggregates). For authoritative state, call
|
|
230
|
-
// `refresh()` (it re-pulls and, for paginate, resets to the first page).
|
|
231
|
-
/** Insert into `items` at `top` (default) / `bottom` / an index (`-1` = last). +1 to `$count`. */
|
|
232
|
-
addItem(item, options) {
|
|
233
|
-
const at = options?.at ?? 'top';
|
|
234
|
-
const list = this.items;
|
|
235
|
-
const idx = at === 'top'
|
|
236
|
-
? 0
|
|
237
|
-
: at === 'bottom'
|
|
238
|
-
? list.length
|
|
239
|
-
: at < 0
|
|
240
|
-
? Math.max(0, list.length + at + 1)
|
|
241
|
-
: Math.min(at, list.length);
|
|
242
|
-
this.items = [...list.slice(0, idx), item, ...list.slice(idx)];
|
|
243
|
-
if (this.aggregates)
|
|
244
|
-
this.aggregates.$count += 1;
|
|
245
|
-
}
|
|
246
|
-
/** Replace the row whose id matches `item`'s id (no `$count` change). */
|
|
247
|
-
updateItem(item) {
|
|
248
|
-
const id = this.#repo.metadata.idMetadata.getId(item);
|
|
249
|
-
this.items = this.items.map((x) => (this.#repo.metadata.idMetadata.getId(x) === id ? item : x));
|
|
250
|
-
}
|
|
251
|
-
/** Drop the matching row (pass an id or the item). -1 to `$count`. */
|
|
252
|
-
removeItem(idOrItem) {
|
|
253
|
-
this.#removeLocal(idOrItem);
|
|
254
|
-
}
|
|
255
|
-
#removeLocal(idOrItem) {
|
|
256
|
-
const id = idOrItem != null && typeof idOrItem === 'object'
|
|
257
|
-
? this.#repo.metadata.idMetadata.getId(idOrItem)
|
|
258
|
-
: idOrItem;
|
|
259
|
-
this.items = this.items.filter((i) => this.#repo.metadata.idMetadata.getId(i) !== id);
|
|
260
|
-
if (this.aggregates)
|
|
261
|
-
this.aggregates.$count = Math.max(0, this.aggregates.$count - 1);
|
|
262
|
-
}
|
|
263
|
-
/** After insert/update (or a `one` delete) in a non-live mode, re-fetch keeping the current count. */
|
|
264
|
-
async #resync() {
|
|
265
|
-
if (this.#mode === 'live')
|
|
266
|
-
return;
|
|
267
|
-
const keepCount = this.#mode === 'paginate' ? this.items.length || undefined : undefined;
|
|
268
|
-
await this.#load(this.#resolve(), ++this.#seq, keepCount);
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* The entity's remult metadata - the single escape hatch for everything not on
|
|
272
|
-
* this handle: permissions (`apiInsertAllowed()`, `apiUpdateAllowed(item)`,
|
|
273
|
-
* `apiDeleteAllowed(item)`, `apiReadAllowed`), `fields`, `idMetadata`, `options`,
|
|
274
|
-
* `key`. Reflects the current `remult.user`.
|
|
275
|
-
*/
|
|
276
|
-
get meta() {
|
|
277
|
-
return this.#repo.metadata;
|
|
278
|
-
}
|
|
279
|
-
/** Escape hatch to the underlying repo (count, findId, upsert, projections, ...). */
|
|
280
|
-
get repo() {
|
|
281
|
-
return this.#repo;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
export function ffRepo(entity) {
|
|
285
|
-
const r = remultRepo(entity);
|
|
286
|
-
const builder = {
|
|
287
|
-
load(o) {
|
|
288
|
-
return new FF_RepoHandle(r, o, 'load');
|
|
289
|
-
},
|
|
290
|
-
listen(o) {
|
|
291
|
-
return new FF_RepoHandle(r, o, 'live');
|
|
292
|
-
},
|
|
293
|
-
paginate(o) {
|
|
294
|
-
return new FF_RepoHandle(r, o, 'paginate');
|
|
295
|
-
},
|
|
296
|
-
one(o) {
|
|
297
|
-
return new FF_RepoHandle(r, o, 'one');
|
|
298
|
-
},
|
|
299
|
-
get meta() {
|
|
300
|
-
return r.metadata;
|
|
301
|
-
},
|
|
302
|
-
repo: r,
|
|
303
|
-
};
|
|
304
|
-
return builder;
|
|
305
|
-
}
|