@ftrdotdev/pi-tracker 0.8.0 → 0.9.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.
@@ -67,8 +67,8 @@ const itemProduces = Schema.Array(declaredProduct).pipe(
67
67
  Schema.withConstructorDefault(Effect.succeed([])),
68
68
  );
69
69
 
70
- export type DeclaredReference = Schema.Schema.Type<typeof declaredReference>;
71
- export type DeclaredProduct = Schema.Schema.Type<typeof declaredProduct>;
70
+ export type DeclaredReference = typeof declaredReference.Type;
71
+ export type DeclaredProduct = typeof declaredProduct.Type;
72
72
 
73
73
  /**
74
74
  * Sentinel for a list that stored no item id counter, which means the snapshot
@@ -83,7 +83,9 @@ const itemCounter = Schema.Int.pipe(
83
83
  Schema.withConstructorDefault(Effect.succeed(1)),
84
84
  );
85
85
 
86
- export class TodoItem extends Schema.Class<TodoItem>("tracker/TodoItem")({
86
+ export class TodoItem extends Schema.Class<TodoItem, { readonly brand: unique symbol }>(
87
+ "tracker/TodoItem",
88
+ )({
87
89
  id: itemId,
88
90
  title: Schema.String,
89
91
  description: itemDescription,
@@ -93,7 +95,9 @@ export class TodoItem extends Schema.Class<TodoItem>("tracker/TodoItem")({
93
95
  deps: itemDeps,
94
96
  }) {}
95
97
 
96
- export class TodoList extends Schema.Class<TodoList>("tracker/TodoList")({
98
+ export class TodoList extends Schema.Class<TodoList, { readonly brand: unique symbol }>(
99
+ "tracker/TodoList",
100
+ )({
97
101
  id: Schema.Int,
98
102
  name: Schema.String,
99
103
  // Strictly-increasing counter ⇒ stable item ids, never reused after deletes.
@@ -101,7 +105,9 @@ export class TodoList extends Schema.Class<TodoList>("tracker/TodoList")({
101
105
  items: Schema.Array(TodoItem),
102
106
  }) {}
103
107
 
104
- export class TrackerState extends Schema.Class<TrackerState>("tracker/TrackerState")({
108
+ export class TrackerState extends Schema.Class<TrackerState, { readonly brand: unique symbol }>(
109
+ "tracker/TrackerState",
110
+ )({
105
111
  lists: Schema.Array(TodoList),
106
112
  // null = no active list (avoids Option in the serialized snapshot)
107
113
  activeListId: Schema.NullOr(Schema.Int),
@@ -117,7 +123,7 @@ export class TrackerState extends Schema.Class<TrackerState>("tracker/TrackerSta
117
123
  * constructors validate their input (`new TodoItem({ text: 42, ... })` throws).
118
124
  */
119
125
  export const emptyState = (): TrackerState =>
120
- new TrackerState({ lists: [], activeListId: null, nextListId: 1 });
126
+ TrackerState.make({ lists: [], activeListId: null, nextListId: 1 });
121
127
 
122
128
  // --------------------------------------------------------------------------
123
129
  // Legacy snapshot tolerance
@@ -125,7 +131,7 @@ export const emptyState = (): TrackerState =>
125
131
 
126
132
  /** A copy of `item` with a replaced id. */
127
133
  const withId = (item: TodoItem, id: number): TodoItem =>
128
- new TodoItem({
134
+ TodoItem.make({
129
135
  id,
130
136
  title: item.title,
131
137
  description: item.description,
@@ -146,7 +152,7 @@ const withId = (item: TodoItem, id: number): TodoItem =>
146
152
  */
147
153
  const migrateList = (list: TodoList): TodoList => {
148
154
  if (list.nextItemId === LEGACY_ITEM_COUNTER) {
149
- return new TodoList({
155
+ return TodoList.make({
150
156
  id: list.id,
151
157
  name: list.name,
152
158
  items: list.items.map((item, index) => withId(item, index + 1)),
@@ -163,7 +169,7 @@ const migrateList = (list: TodoList): TodoList => {
163
169
  counter += 1;
164
170
  return assigned;
165
171
  });
166
- return new TodoList({ id: list.id, name: list.name, items, nextItemId: counter });
172
+ return TodoList.make({ id: list.id, name: list.name, items, nextItemId: counter });
167
173
  };
168
174
 
169
175
  /**
@@ -171,7 +177,7 @@ const migrateList = (list: TodoList): TodoList => {
171
177
  * every list has a counter above its ids. Pure; called once per restore.
172
178
  */
173
179
  export const migrateState = (state: TrackerState): TrackerState =>
174
- new TrackerState({
180
+ TrackerState.make({
175
181
  lists: state.lists.map(migrateList),
176
182
  activeListId: state.activeListId,
177
183
  nextListId: state.nextListId,
@@ -20,37 +20,36 @@ layer(
20
20
  }),
21
21
  )("TrackerPersistence", (it) => {
22
22
  const sampleState = (): TrackerState =>
23
- new TrackerState({
23
+ TrackerState.make({
24
24
  lists: [
25
- new TodoList({
25
+ TodoList.make({
26
26
  id: 1,
27
27
  name: "Work",
28
28
  nextItemId: 3,
29
29
  items: [
30
- new TodoItem({ id: 1, title: "write plan", done: true }),
31
- new TodoItem({ id: 2, title: "implement tracker", done: false }),
30
+ TodoItem.make({ id: 1, title: "write plan", done: true }),
31
+ TodoItem.make({ id: 2, title: "implement tracker", done: false }),
32
32
  ],
33
33
  }),
34
- new TodoList({
34
+ TodoList.make({
35
35
  id: 2,
36
36
  name: "Home",
37
37
  nextItemId: 2,
38
- items: [new TodoItem({ id: 1, title: "water plants", done: false })],
38
+ items: [TodoItem.make({ id: 1, title: "water plants", done: false })],
39
39
  }),
40
40
  ],
41
41
  activeListId: 1,
42
42
  nextListId: 3,
43
43
  });
44
44
 
45
- const decodeFailureOf = (snapshot: unknown) =>
46
- Effect.gen(function* () {
47
- const persistence = yield* TrackerPersistence;
48
- const result = yield* Effect.result(persistence.restore(snapshot));
49
- assert(Result.isFailure(result), `expected ${JSON.stringify(snapshot)} to fail`);
50
- const failure = Option.getOrThrow(Result.getFailure(result));
51
- assert.strictEqual(failure._tag, "SchemaError");
52
- return failure.message;
53
- });
45
+ const decodeFailureOf = Effect.fnUntraced(function* (snapshot: unknown) {
46
+ const persistence = yield* TrackerPersistence;
47
+ const result = yield* Effect.result(persistence.restore(snapshot));
48
+ if (!Result.isFailure(result)) assert.fail(`expected ${JSON.stringify(snapshot)} to fail`);
49
+ const failure = Result.getFailure(result).pipe(Option.getOrThrow);
50
+ assert.strictEqual(failure._tag, "SchemaError");
51
+ return failure.message;
52
+ });
54
53
 
55
54
  it.effect("save writes the encoded snapshot", () =>
56
55
  Effect.gen(function* () {
@@ -162,13 +161,13 @@ layer(
162
161
  };
163
162
 
164
163
  const restored = yield* persistence.restore(legacy);
165
- const expected = new TrackerState({
164
+ const expected = TrackerState.make({
166
165
  lists: [
167
- new TodoList({
166
+ TodoList.make({
168
167
  id: 1,
169
168
  name: "Work",
170
169
  nextItemId: 2,
171
- items: [new TodoItem({ id: 1, title: "a", done: false })],
170
+ items: [TodoItem.make({ id: 1, title: "a", done: false })],
172
171
  }),
173
172
  ],
174
173
  activeListId: 1,
@@ -176,7 +175,7 @@ layer(
176
175
  });
177
176
  assert.deepStrictEqual(encodeState(restored), encodeState(expected));
178
177
  assert.deepStrictEqual(restored.lists[0]?.items, [
179
- new TodoItem({ id: 1, title: "a", done: false }),
178
+ TodoItem.make({ id: 1, title: "a", done: false }),
180
179
  ]);
181
180
  }),
182
181
  );
@@ -2,7 +2,7 @@ import { Context, Effect, Layer, Schema } from "effect";
2
2
  import { TrackerState, decodeStateEffect, encodeStateEffect, migrateState } from "./domain.ts";
3
3
 
4
4
  /** The plain JSON shape of a `TrackerState` (what goes into the session). */
5
- export type EncodedState = Schema.Codec.Encoded<typeof TrackerState>;
5
+ export type EncodedState = typeof TrackerState.Encoded;
6
6
 
7
7
  /**
8
8
  * Persistence boundary for the tracker state.
@@ -27,7 +27,7 @@ export class TrackerPersistence extends Context.Service<
27
27
  readonly save: (state: TrackerState) => Effect.Effect<void, Schema.SchemaError>;
28
28
  readonly restore: (snapshot: unknown) => Effect.Effect<TrackerState, Schema.SchemaError>;
29
29
  }
30
- >()("tracker/TrackerPersistence") {
30
+ >()("@ftrdotdev/pi-tracker/core/persistence/TrackerPersistence") {
31
31
  static readonly layer = (
32
32
  append: (encoded: EncodedState) => void,
33
33
  ): Layer.Layer<TrackerPersistence> =>
@@ -41,7 +41,8 @@ export class TrackerPersistence extends Context.Service<
41
41
  });
42
42
 
43
43
  const restore = Effect.fn("TrackerPersistence.restore")(function* (snapshot: unknown) {
44
- return migrateState(yield* decodeStateEffect(snapshot));
44
+ const decoded = yield* decodeStateEffect(snapshot);
45
+ return migrateState(decoded);
45
46
  });
46
47
 
47
48
  return TrackerPersistence.of({ save, restore });