@ftrdotdev/pi-tracker 0.6.0 → 0.7.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/src/core/store.ts CHANGED
@@ -1,6 +1,13 @@
1
1
  import { Context, Effect, Layer, Ref, Result, Schema } from "effect";
2
2
  import { blockersOf, cyclePath, dependentsOf, formatItemRef, parseItemRef } from "./deps.ts";
3
- import { TodoItem, TodoList, TrackerState, emptyState } from "./domain.ts";
3
+ import {
4
+ TodoItem,
5
+ TodoList,
6
+ TrackerState,
7
+ emptyState,
8
+ type DeclaredProduct,
9
+ type DeclaredReference,
10
+ } from "./domain.ts";
4
11
 
5
12
  // --------------------------------------------------------------------------
6
13
  // Errors
@@ -158,17 +165,40 @@ const withItems = (list: TodoList, items: readonly TodoItem[]): TodoList =>
158
165
  // --------------------------------------------------------------------------
159
166
 
160
167
  /**
161
- * How a caller declares an item to create: the bare text, or an object that
162
- * also carries the ids of the same-list items this one waits for.
168
+ * How a caller declares an item to create: the bare title, or an object that
169
+ * also carries a description, the declared references and products, and the
170
+ * ids of the same-list items this one waits for.
163
171
  */
164
- export type ItemSpec = string | { readonly text: string; readonly deps?: readonly string[] };
172
+ export type ItemSpec =
173
+ | string
174
+ | {
175
+ readonly title: string;
176
+ readonly description?: string;
177
+ readonly refs?: readonly DeclaredReference[];
178
+ readonly produces?: readonly DeclaredProduct[];
179
+ readonly deps?: readonly string[];
180
+ };
165
181
 
166
- /** Trim a spec's text and settle its dependency refs. */
182
+ /** Trim a spec's title and settle its description, declarations, and deps. */
167
183
  const normalizeSpec = (
168
184
  spec: ItemSpec,
169
- ): { readonly text: string; readonly deps: readonly string[] } => {
170
- if (typeof spec === "string") return { text: spec.trim(), deps: [] };
171
- return { text: spec.text.trim(), deps: spec.deps ?? [] };
185
+ ): {
186
+ readonly title: string;
187
+ readonly description: string;
188
+ readonly refs: readonly DeclaredReference[];
189
+ readonly produces: readonly DeclaredProduct[];
190
+ readonly deps: readonly string[];
191
+ } => {
192
+ if (typeof spec === "string") {
193
+ return { title: spec.trim(), description: "", refs: [], produces: [], deps: [] };
194
+ }
195
+ return {
196
+ title: spec.title.trim(),
197
+ description: spec.description ?? "",
198
+ refs: spec.refs ?? [],
199
+ produces: spec.produces ?? [],
200
+ deps: spec.deps ?? [],
201
+ };
172
202
  };
173
203
 
174
204
  /**
@@ -260,7 +290,12 @@ const dependedOnError = (list: TodoList, index: number, itemId: string): Tracker
260
290
  };
261
291
 
262
292
  export interface UpdateItemPatch {
263
- readonly text?: string;
293
+ readonly title?: string;
294
+ readonly description?: string;
295
+ /** Replacement declaration list; `[]` clears it. */
296
+ readonly refs?: readonly DeclaredReference[];
297
+ /** Replacement product list; `[]` clears it. */
298
+ readonly produces?: readonly DeclaredProduct[];
264
299
  readonly done?: boolean;
265
300
  /** Replacement dependency set (same-list `listName:id` refs); `[]` clears it. */
266
301
  readonly deps?: readonly string[];
@@ -268,7 +303,7 @@ export interface UpdateItemPatch {
268
303
 
269
304
  /**
270
305
  * An `UpdateItemPatch` that targets an item by id within a single list
271
- * (mirrors `add_item`'s `list_id + text[]` shape: the list is factored out,
306
+ * (mirrors `add_items`'s `list_id + items` shape: the list is factored out,
272
307
  * the patches name their item).
273
308
  */
274
309
  export interface UpdateItemInListPatch extends UpdateItemPatch {
@@ -297,7 +332,7 @@ export class TrackerStore extends Context.Service<
297
332
  readonly setActiveList: (listId: number | null) => Effect.Effect<void, TrackerError>;
298
333
  readonly addItem: (
299
334
  listId: number,
300
- text: string,
335
+ spec: ItemSpec,
301
336
  deps?: readonly string[],
302
337
  ) => Effect.Effect<TodoItem, TrackerError>;
303
338
  readonly addItems: (
@@ -348,10 +383,10 @@ export class TrackerStore extends Context.Service<
348
383
  });
349
384
  }
350
385
  const initialSpecs = (options.initialItems ?? []).map(normalizeSpec);
351
- if (initialSpecs.some((spec) => spec.text === "")) {
386
+ if (initialSpecs.some((spec) => spec.title === "")) {
352
387
  return yield* new TrackerError({
353
388
  reason: "EmptyText",
354
- message: "Item text must not be empty",
389
+ message: "Item title must not be empty",
355
390
  });
356
391
  }
357
392
  const activate = options.activate ?? true;
@@ -370,7 +405,15 @@ export class TrackerStore extends Context.Service<
370
405
  }
371
406
  const items = initialSpecs.map(
372
407
  (spec, index) =>
373
- new TodoItem({ id: index + 1, text: spec.text, done: false, deps: [...spec.deps] }),
408
+ new TodoItem({
409
+ id: index + 1,
410
+ title: spec.title,
411
+ description: spec.description,
412
+ refs: [...spec.refs],
413
+ produces: [...spec.produces],
414
+ done: false,
415
+ deps: [...spec.deps],
416
+ }),
374
417
  );
375
418
  const list = new TodoList({
376
419
  id: s.nextListId,
@@ -444,19 +487,19 @@ export class TrackerStore extends Context.Service<
444
487
  });
445
488
 
446
489
  /**
447
- * Append items to a list, assigning ids from the list counter. Accepts
448
- * bare text or `{ text, deps }` objects; a dependency may name an item
449
- * created by this same call.
490
+ * Append items to a list, assigning ids from the list counter. Accepts a
491
+ * bare title or item objects `{title, description?, refs?, produces?,
492
+ * deps?}`; a dependency may name an item created by this same call.
450
493
  */
451
494
  const addItems = Effect.fn("TrackerStore.addItems")(function* (
452
495
  listId: number,
453
496
  items: readonly ItemSpec[],
454
497
  ) {
455
498
  const specs = items.map(normalizeSpec);
456
- if (specs.some((spec) => spec.text === "")) {
499
+ if (specs.some((spec) => spec.title === "")) {
457
500
  return yield* new TrackerError({
458
501
  reason: "EmptyText",
459
- message: "Item text must not be empty",
502
+ message: "Item title must not be empty",
460
503
  });
461
504
  }
462
505
  return yield* mutate(
@@ -478,7 +521,10 @@ export class TrackerStore extends Context.Service<
478
521
  const added = specs.map((spec) => {
479
522
  const item = new TodoItem({
480
523
  id: nextId,
481
- text: spec.text,
524
+ title: spec.title,
525
+ description: spec.description,
526
+ refs: [...spec.refs],
527
+ produces: [...spec.produces],
482
528
  done: false,
483
529
  deps: [...spec.deps],
484
530
  });
@@ -500,10 +546,11 @@ export class TrackerStore extends Context.Service<
500
546
 
501
547
  const addItem = Effect.fn("TrackerStore.addItem")(function* (
502
548
  listId: number,
503
- text: string,
549
+ spec: ItemSpec,
504
550
  deps: readonly string[] = [],
505
551
  ) {
506
- const [item] = yield* addItems(listId, [{ text, deps }]);
552
+ const resolved: ItemSpec = typeof spec === "string" ? { title: spec, deps } : spec;
553
+ const [item] = yield* addItems(listId, [resolved]);
507
554
  return item!;
508
555
  });
509
556
 
@@ -511,11 +558,11 @@ export class TrackerStore extends Context.Service<
511
558
  itemId: string,
512
559
  patch: UpdateItemPatch,
513
560
  ) {
514
- const trimmed = patch.text === undefined ? undefined : patch.text.trim();
515
- if (patch.text !== undefined && trimmed === "") {
561
+ const trimmed = patch.title === undefined ? undefined : patch.title.trim();
562
+ if (patch.title !== undefined && trimmed === "") {
516
563
  return yield* new TrackerError({
517
564
  reason: "EmptyText",
518
- message: "Item text must not be empty",
565
+ message: "Item title must not be empty",
519
566
  });
520
567
  }
521
568
  return yield* mutate(
@@ -532,13 +579,23 @@ export class TrackerStore extends Context.Service<
532
579
  const blocked = blockedError(list, resolved.index);
533
580
  if (blocked !== null) return [Result.fail(blocked), s];
534
581
  }
535
- if (patch.text === undefined && patch.done === undefined && patch.deps === undefined) {
582
+ if (
583
+ patch.title === undefined &&
584
+ patch.description === undefined &&
585
+ patch.refs === undefined &&
586
+ patch.produces === undefined &&
587
+ patch.done === undefined &&
588
+ patch.deps === undefined
589
+ ) {
536
590
  // No-op patch: return the current item, leave state untouched.
537
591
  return [Result.succeed(item), s];
538
592
  }
539
593
  const next = new TodoItem({
540
594
  id: item.id,
541
- text: trimmed ?? item.text,
595
+ title: trimmed ?? item.title,
596
+ description: patch.description ?? item.description,
597
+ refs: patch.refs === undefined ? item.refs : [...patch.refs],
598
+ produces: patch.produces === undefined ? item.produces : [...patch.produces],
542
599
  done: patch.done ?? item.done,
543
600
  deps: patch.deps === undefined ? item.deps : [...patch.deps],
544
601
  });
@@ -567,13 +624,13 @@ export class TrackerStore extends Context.Service<
567
624
  listId: number,
568
625
  patches: readonly UpdateItemInListPatch[],
569
626
  ) {
570
- // Reject empty replacement texts up front so the whole batch fails
627
+ // Reject empty replacement titles up front so the whole batch fails
571
628
  // before touching the state (atomicity).
572
629
  for (const patch of patches) {
573
- if (patch.text !== undefined && patch.text.trim() === "") {
630
+ if (patch.title !== undefined && patch.title.trim() === "") {
574
631
  return yield* new TrackerError({
575
632
  reason: "EmptyText",
576
- message: "Item text must not be empty",
633
+ message: "Item title must not be empty",
577
634
  });
578
635
  }
579
636
  }
@@ -649,7 +706,11 @@ export class TrackerStore extends Context.Service<
649
706
  at === target
650
707
  ? new TodoItem({
651
708
  id: current.id,
652
- text: patch.text === undefined ? current.text : patch.text.trim(),
709
+ title: patch.title === undefined ? current.title : patch.title.trim(),
710
+ description: patch.description ?? current.description,
711
+ refs: patch.refs === undefined ? current.refs : [...patch.refs],
712
+ produces:
713
+ patch.produces === undefined ? current.produces : [...patch.produces],
653
714
  done: patch.done ?? current.done,
654
715
  deps: patch.deps === undefined ? current.deps : [...patch.deps],
655
716
  })