@reventlessdev/trait-attachments 1.0.0-alpha.2 → 1.0.0-alpha.4

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.
@@ -3,15 +3,18 @@
3
3
  import * as Sury from "sury";
4
4
  import * as Belt_Array from "@rescript/runtime/lib/es6/Belt_Array.js";
5
5
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
6
+ import * as Attachments_Rules$TraitAttachments from "./Attachments_Rules.res.mjs";
6
7
 
7
8
  let configSchema = Sury.$schema(s => ({
8
9
  entity: s.m(Sury.string),
9
10
  entityId: s.m(Sury.string),
10
11
  noun: s.m(Sury.string),
11
12
  file: s.m(Sury.string),
13
+ cardinality: s.m(Sury.$option(Attachments_Rules$TraitAttachments.cardinalitySchema)),
12
14
  created: s.m(Sury.string),
13
15
  createdCarriesEntityId: s.m(Sury.$option(Sury.bool)),
14
16
  view: s.m(Sury.string),
17
+ lifecycleType: s.m(Sury.$option(Sury.string)),
15
18
  refType: s.m(Sury.$option(Sury.string)),
16
19
  authorize: s.m(Sury.$option(Sury.string)),
17
20
  transition: s.m(Sury.$option(Sury.array(Sury.string))),
@@ -19,11 +22,21 @@ let configSchema = Sury.$schema(s => ({
19
22
  refB: s.m(Sury.$option(Sury.string))
20
23
  }));
21
24
 
25
+ function cardinalityOf(c) {
26
+ return Stdlib_Option.getOr(c.cardinality, "Many");
27
+ }
28
+
29
+ function isSingle(c) {
30
+ return Stdlib_Option.getOr(c.cardinality, "Many") === "Single";
31
+ }
32
+
22
33
  function namesOf(c) {
23
34
  let subject = c.entity + c.noun;
24
35
  return {
25
36
  slice: subject + "s",
26
- attachCmd: "Attach" + subject,
37
+ attachCmd: (
38
+ Stdlib_Option.getOr(c.cardinality, "Many") === "Single" ? "Set" : "Attach"
39
+ ) + subject,
27
40
  removeCmd: "Remove" + subject,
28
41
  setPrimaryCmd: "SetPrimary" + subject,
29
42
  setAltTextCmd: "Set" + subject + "AltText",
@@ -40,6 +53,35 @@ function refTypeOf(c) {
40
53
  return Stdlib_Option.getOr(c.refType, "Reventless.UploadableImage.t");
41
54
  }
42
55
 
56
+ function viewTypeOf(c) {
57
+ if (Stdlib_Option.getOr(c.refType, "Reventless.UploadableImage.t").includes("Image")) {
58
+ return "Reventless.CaptionedImage.t";
59
+ } else {
60
+ return Stdlib_Option.getOr(c.refType, "Reventless.UploadableImage.t");
61
+ }
62
+ }
63
+
64
+ function setFieldOf(c) {
65
+ return c.file + "s";
66
+ }
67
+
68
+ let selectionBinding = "selected";
69
+
70
+ function selectionTypeOf(_c) {
71
+ return `@s.matches(` + selectionBinding + `) string`;
72
+ }
73
+
74
+ function contentArgOf(c) {
75
+ let ref = Stdlib_Option.getOr(c.refType, "Reventless.UploadableImage.t");
76
+ if (ref.includes("Image")) {
77
+ return `~content=Reventless.Semantic.Id.imageRef, `;
78
+ } else if (ref.includes("File")) {
79
+ return `~content=Reventless.Semantic.Id.fileRef, `;
80
+ } else {
81
+ return "";
82
+ }
83
+ }
84
+
43
85
  function commandAttributes(c) {
44
86
  let a = c.authorize;
45
87
  if (a !== undefined) {
@@ -49,18 +91,32 @@ function commandAttributes(c) {
49
91
  }
50
92
  }
51
93
 
52
- function commandTransitionBinding(c) {
94
+ function commandNames(c) {
53
95
  let n = namesOf(c);
54
- let arms = [
55
- n.attachCmd,
56
- n.removeCmd,
57
- n.setPrimaryCmd,
58
- n.setAltTextCmd
59
- ].map(cmd => ` | ` + cmd + `(_)`).join("\n");
96
+ if (Stdlib_Option.getOr(c.cardinality, "Many") === "Single") {
97
+ return [
98
+ n.attachCmd,
99
+ n.removeCmd,
100
+ n.setAltTextCmd
101
+ ];
102
+ } else {
103
+ return [
104
+ n.attachCmd,
105
+ n.removeCmd,
106
+ n.setPrimaryCmd,
107
+ n.setAltTextCmd
108
+ ];
109
+ }
110
+ }
111
+
112
+ function commandTransitionBinding(c) {
113
+ let arms = commandNames(c).map(cmd => ` | ` + cmd + `(_)`).join("\n");
114
+ let t = c.lifecycleType;
115
+ let lifecycleType = t !== undefined ? c.view + `.` + t : c.view + `.<lifecycle> // TODO(graft): the enum's name`;
60
116
  let states = c.transition;
61
117
  if (states !== undefined) {
62
118
  return [
63
- `type lifecycleState = ` + c.view + `.<lifecycle> // TODO(graft): the enum's name`,
119
+ `type lifecycleState = ` + lifecycleType,
64
120
  `let commandTransition = (command: command): Reventless.Transition.t<lifecycleState> => {`,
65
121
  ` open Reventless.Transition`,
66
122
  ` switch command {`,
@@ -96,13 +152,28 @@ function lines(ls) {
96
152
  function sliceSpec(c) {
97
153
  let n = namesOf(c);
98
154
  let ref = Stdlib_Option.getOr(c.refType, "Reventless.UploadableImage.t");
155
+ let sel = selectionTypeOf(c);
156
+ let single = Stdlib_Option.getOr(c.cardinality, "Many") === "Single";
99
157
  let attrs = commandAttributes(c);
100
158
  let createdArm = Stdlib_Option.getOr(c.createdCarriesEntityId, true) ? ` | ` + c.created + `({ ` + c.entityId + `: string})` : ` | ` + c.created;
159
+ let primaryArm = prefix => {
160
+ if (single) {
161
+ return [];
162
+ } else {
163
+ return [` | ` + n.primarySet + `({ ` + prefix + c.file + `: ` + ref + `})`];
164
+ }
165
+ };
166
+ let headline = single ? [
167
+ `// ` + n.slice + ` StateChangeSlice: ` + c.entity + `'s single attachment — set it, remove`,
168
+ `// it, caption it. A graft of the Attachments trait; the set's rules are the`
169
+ ] : [
170
+ `// ` + n.slice + ` StateChangeSlice: ` + c.entity + `'s attachment set — attach, remove,`,
171
+ `// choose the primary, caption. A graft of the Attachments trait; the rules are`
172
+ ];
101
173
  return Belt_Array.concatMany([
174
+ headline,
102
175
  [
103
- `// ` + n.slice + ` StateChangeSlice: ` + c.entity + `'s attachment set — attach, remove,`,
104
- `// choose the primary, caption. A graft of the Attachments trait; the set's rules`,
105
- `// are the trait's and are asserted by its conformance suite, bound in the tests.`,
176
+ `// trait's and are asserted by its conformance suite, bound in the tests.`,
106
177
  `//`,
107
178
  `// Emitted by the trait. Everything below is this host's own vocabulary, so it is`,
108
179
  `// ordinary source from here on — edit it freely.`,
@@ -112,19 +183,43 @@ function sliceSpec(c) {
112
183
  `@schema`,
113
184
  `type consumedEvent =`,
114
185
  createdArm,
115
- ` | ` + n.attached + `({ ` + c.file + `: string})`,
116
- ` | ` + n.removed + `({ ` + c.file + `: string})`,
117
- ` | ` + n.primarySet + `({ ` + c.file + `: string})`,
118
- ` | ` + n.altTextSet + `({ ` + c.file + `: string, altText: string})`,
186
+ ` | ` + n.attached + `({ ` + c.file + `: ` + ref + `})`,
187
+ ` | ` + n.removed + `({ ` + c.file + `: ` + ref + `})`
188
+ ],
189
+ primaryArm(""),
190
+ [
191
+ ` | ` + n.altTextSet + `({ ` + c.file + `: ` + ref + `, altText: string})`,
119
192
  ` // TODO(graft): add the events this host's own refusal turns on — whatever`,
120
193
  ` // moves it into a state where attachments may not be changed.`,
121
- ``,
194
+ ``
195
+ ],
196
+ single ? [
197
+ `// One reference field, on ` + n.attachCmd + `, and it accepts a new file — so`,
198
+ `// it is typed as the uploadable it is and a form binds an upload input to it.`,
199
+ `// Neither other command names a reference: with one attachment there is`,
200
+ `// nothing to choose between, and asking a caller to name it would be asking`,
201
+ `// them to repeat what the row already says.`
202
+ ] : [
203
+ `// The reference fields divide into two kinds, and the division is the point.`,
204
+ `// The one on ` + n.attachCmd + ` accepts a new file, so it is typed as the`,
205
+ `// uploadable it is and a form binds an upload input to it. The others name a`,
206
+ `// file the row ALREADY holds, so they are typed as selections out of`,
207
+ `// \`` + (c.file + "s") + `\` and a form offers those instead of an uploader.`,
208
+ `//`,
209
+ `// Bound once rather than spelled three times: the collection is one answer,`,
210
+ `// and three copies are three chances for one to name a field that has moved.`,
211
+ `let ` + selectionBinding + ` = Reventless.MemberRef.of_(~view="` + c.view + `", ` + contentArgOf(c) + `~field="` + (c.file + "s") + `")`,
212
+ ``
213
+ ],
214
+ [
122
215
  `@schema`,
123
216
  `type command =`,
124
217
  attrs + n.attachCmd + `({ ` + c.entityId + `: string, ` + c.file + `: ` + ref + `, altText?: string})`,
125
- attrs + n.removeCmd + `({ ` + c.entityId + `: string, ` + c.file + `: ` + ref + `})`,
126
- attrs + n.setPrimaryCmd + `({ ` + c.entityId + `: string, ` + c.file + `: ` + ref + `})`,
127
- attrs + n.setAltTextCmd + `({ ` + c.entityId + `: string, ` + c.file + `: ` + ref + `, altText: string})`,
218
+ single ? attrs + n.removeCmd + `({ ` + c.entityId + `: string})` : attrs + n.removeCmd + `({ ` + c.entityId + `: string, ` + c.file + `: ` + sel + `})`
219
+ ],
220
+ single ? [] : [attrs + n.setPrimaryCmd + `({ ` + c.entityId + `: string, ` + c.file + `: ` + sel + `})`],
221
+ [
222
+ single ? attrs + n.setAltTextCmd + `({ ` + c.entityId + `: string, altText: string})` : attrs + n.setAltTextCmd + `({ ` + c.entityId + `: string, ` + c.file + `: ` + sel + `, altText: string})`,
128
223
  ``,
129
224
  `@schema`,
130
225
  `type error =`,
@@ -135,8 +230,10 @@ function sliceSpec(c) {
135
230
  `@schema`,
136
231
  `type event =`,
137
232
  ` | ` + n.attached + `({ ` + c.entityId + `: string, ` + c.file + `: ` + ref + `, altText?: string})`,
138
- ` | ` + n.removed + `({ ` + c.entityId + `: string, ` + c.file + `: ` + ref + `})`,
139
- ` | ` + n.primarySet + `({ ` + c.entityId + `: string, ` + c.file + `: ` + ref + `})`,
233
+ ` | ` + n.removed + `({ ` + c.entityId + `: string, ` + c.file + `: ` + ref + `})`
234
+ ],
235
+ primaryArm(c.entityId + `: string, `),
236
+ [
140
237
  ` | ` + n.altTextSet + `({ ` + c.entityId + `: string, ` + c.file + `: ` + ref + `, altText: string})`,
141
238
  ``
142
239
  ],
@@ -153,170 +250,276 @@ function sliceSpec(c) {
153
250
 
154
251
  function sliceBehavior(c) {
155
252
  let n = namesOf(c);
156
- return [
157
- `@@reventless.behavior`,
158
- ``,
159
- `// The set's rules are the trait's. What is left here is this host's own refusal`,
160
- `// and the mapping between its constructors and the trait's ops and facts.`,
161
- `module Attachments = TraitAttachments.Attachments_Rules`,
162
- ``,
163
- `type state = {exists: bool, attachments: Attachments.t}`,
164
- ``,
165
- `let initialState = {exists: false, attachments: Attachments.empty}`,
166
- ``,
167
- `let evolve = (state, event) => {`,
168
- ` let fold = fact => {...state, attachments: state.attachments->Attachments.evolve(fact)}`,
169
- ` switch event {`,
170
- ` | ` + c.created + `(_) => {...state, exists: true}`,
171
- ` | ` + n.attached + `({` + c.file + `}) => fold(Attached({ref: ` + c.file + `, altText: None}))`,
172
- ` | ` + n.removed + `({` + c.file + `}) => fold(Removed({ref: ` + c.file + `}))`,
173
- ` | ` + n.primarySet + `({` + c.file + `}) => fold(PrimarySet({ref: ` + c.file + `}))`,
174
- ` | ` + n.altTextSet + `({` + c.file + `, altText}) => fold(AltTextSet({ref: ` + c.file + `, altText}))`,
175
- ` // TODO(graft): fold this host's own events into its own state.`,
176
- ` }`,
177
- `}`,
178
- ``,
179
- `let toOp = command =>`,
180
- ` switch command {`,
181
- ` | ` + n.attachCmd + `({` + c.entityId + `, ` + c.file + `, altText: ?altText}) => (`,
182
- ` ` + c.entityId + `,`,
183
- ` Attachments.Attach({ref: ` + c.file + `, altText}),`,
184
- ` )`,
185
- ` | ` + n.removeCmd + `({` + c.entityId + `, ` + c.file + `}) => (`,
186
- ` ` + c.entityId + `,`,
187
- ` Attachments.Remove({ref: ` + c.file + `}),`,
188
- ` )`,
189
- ` | ` + n.setPrimaryCmd + `({` + c.entityId + `, ` + c.file + `}) => (`,
190
- ` ` + c.entityId + `,`,
191
- ` Attachments.SetPrimary({ref: ` + c.file + `}),`,
192
- ` )`,
193
- ` | ` + n.setAltTextCmd + `({` + c.entityId + `, ` + c.file + `, altText}) => (`,
194
- ` ` + c.entityId + `,`,
195
- ` Attachments.SetAltText({ref: ` + c.file + `, altText}),`,
196
- ` )`,
197
- ` }`,
198
- ``,
199
- `let toEvent = (` + c.entityId + `, fact) =>`,
200
- ` switch fact {`,
201
- ` | Attachments.Attached({ref, altText}) =>`,
202
- ` ` + n.attached + `({` + c.entityId + `, ` + c.file + `: ref, altText: ?altText})`,
203
- ` | Attachments.Removed({ref}) => ` + n.removed + `({` + c.entityId + `, ` + c.file + `: ref})`,
204
- ` | Attachments.PrimarySet({ref}) => ` + n.primarySet + `({` + c.entityId + `, ` + c.file + `: ref})`,
205
- ` | Attachments.AltTextSet({ref, altText}) =>`,
206
- ` ` + n.altTextSet + `({` + c.entityId + `, ` + c.file + `: ref, altText})`,
207
- ` }`,
208
- ``,
209
- `let decide = (state, command) =>`,
210
- ` if !state.exists {`,
211
- ` Error(` + n.notFound + `)`,
212
- ` } else {`,
213
- ` // TODO(graft): this host's own refusal goes here, ahead of the set's rules —`,
214
- ` // an \`else if\` returning the error added above. A graft with no extra refusal`,
215
- ` // is a complete graft, so leaving this is legitimate.`,
216
- ` let (` + c.entityId + `, op) = toOp(command)`,
217
- ` switch state.attachments->Attachments.decide(op) {`,
218
- ` | Error(#NotAttached) => Error(` + n.notAttached + `)`,
219
- ` | Ok(None) => Ok([])`,
220
- ` | Ok(Some(fact)) => Ok([toEvent(` + c.entityId + `, fact)])`,
221
- ` }`,
222
- ` }`,
223
- ``
224
- ].join("\n");
253
+ let single = Stdlib_Option.getOr(c.cardinality, "Many") === "Single";
254
+ return Belt_Array.concatMany([
255
+ [
256
+ `@@reventless.behavior`,
257
+ ``,
258
+ `// The set's rules are the trait's. What is left here is this host's own refusal`,
259
+ `// and the mapping between its constructors and the trait's ops and facts.`,
260
+ `module Attachments = TraitAttachments.Attachments_Rules`,
261
+ ``,
262
+ `type state = {exists: bool, attachments: Attachments.t}`,
263
+ ``,
264
+ `let initialState = {exists: false, attachments: Attachments.empty}`,
265
+ ``,
266
+ `let evolve = (state, event) => {`,
267
+ ` let fold = fact => {...state, attachments: state.attachments->Attachments.evolve(fact)}`,
268
+ ` switch event {`,
269
+ Stdlib_Option.getOr(c.createdCarriesEntityId, true) ? ` | ` + c.created + `(_) => {...state, exists: true}` : ` | ` + c.created + ` => {...state, exists: true}`,
270
+ ` | ` + n.attached + `({` + c.file + `}) => fold(Attached({ref: ` + c.file + `, altText: None}))`,
271
+ ` | ` + n.removed + `({` + c.file + `}) => fold(Removed({ref: ` + c.file + `}))`
272
+ ],
273
+ single ? [] : [` | ` + n.primarySet + `({` + c.file + `}) => fold(PrimarySet({ref: ` + c.file + `}))`],
274
+ [
275
+ ` | ` + n.altTextSet + `({` + c.file + `, altText}) => fold(AltTextSet({ref: ` + c.file + `, altText}))`,
276
+ ` // TODO(graft): fold this host's own events into its own state.`,
277
+ ` }`,
278
+ `}`,
279
+ ``,
280
+ `let toOp = command =>`,
281
+ ` switch command {`,
282
+ ` | ` + n.attachCmd + `({` + c.entityId + `, ` + c.file + `, altText: ?altText}) => (`,
283
+ ` ` + c.entityId + `,`,
284
+ ` Attachments.Attach({ref: ` + c.file + `, altText}),`,
285
+ ` )`
286
+ ],
287
+ single ? [` | ` + n.removeCmd + `({` + c.entityId + `}) => (` + c.entityId + `, Attachments.Clear)`] : [
288
+ ` | ` + n.removeCmd + `({` + c.entityId + `, ` + c.file + `}) => (`,
289
+ ` ` + c.entityId + `,`,
290
+ ` Attachments.Remove({ref: ` + c.file + `}),`,
291
+ ` )`,
292
+ ` | ` + n.setPrimaryCmd + `({` + c.entityId + `, ` + c.file + `}) => (`,
293
+ ` ` + c.entityId + `,`,
294
+ ` Attachments.SetPrimary({ref: ` + c.file + `}),`,
295
+ ` )`
296
+ ],
297
+ single ? [
298
+ ` | ` + n.setAltTextCmd + `({` + c.entityId + `, altText}) => (`,
299
+ ` ` + c.entityId + `,`,
300
+ ` Attachments.SetPrimaryAltText({altText: altText}),`,
301
+ ` )`
302
+ ] : [
303
+ ` | ` + n.setAltTextCmd + `({` + c.entityId + `, ` + c.file + `, altText}) => (`,
304
+ ` ` + c.entityId + `,`,
305
+ ` Attachments.SetAltText({ref: ` + c.file + `, altText}),`,
306
+ ` )`
307
+ ],
308
+ [
309
+ ` }`,
310
+ ``,
311
+ `let toEvent = (` + c.entityId + `, fact) =>`,
312
+ ` switch fact {`,
313
+ ` | Attachments.Attached({ref, altText}) =>`,
314
+ ` ` + (
315
+ single ? "Some(" : ""
316
+ ) + n.attached + `({` + c.entityId + `, ` + c.file + `: ref, altText: ?altText})` + (
317
+ single ? ")" : ""
318
+ ),
319
+ ` | Attachments.Removed({ref}) => ` + (
320
+ single ? `Some(` + n.removed + `({` + c.entityId + `, ` + c.file + `: ref}))` : n.removed + `({` + c.entityId + `, ` + c.file + `: ref})`
321
+ )
322
+ ],
323
+ single ? [
324
+ ` // Unreachable: no command of this graft chooses a primary, because a set`,
325
+ ` // of one has nothing to choose between. It contributes no event.`,
326
+ ` | Attachments.PrimarySet(_) => None`
327
+ ] : [` | Attachments.PrimarySet({ref}) => ` + n.primarySet + `({` + c.entityId + `, ` + c.file + `: ref})`],
328
+ [
329
+ ` | Attachments.AltTextSet({ref, altText}) =>`,
330
+ ` ` + (
331
+ single ? "Some(" : ""
332
+ ) + n.altTextSet + `({` + c.entityId + `, ` + c.file + `: ref, altText})` + (
333
+ single ? ")" : ""
334
+ ),
335
+ ` }`,
336
+ ``,
337
+ `let decide = (state, command) =>`,
338
+ ` if !state.exists {`,
339
+ ` Error(` + n.notFound + `)`,
340
+ ` } else {`,
341
+ ` // TODO(graft): this host's own refusal goes here, ahead of the set's rules —`,
342
+ ` // an \`else if\` returning the error added above. A graft with no extra refusal`,
343
+ ` // is a complete graft, so leaving this is legitimate.`,
344
+ ` let (` + c.entityId + `, op) = toOp(command)`,
345
+ single ? ` switch state.attachments->Attachments.decide(~cardinality=Single, op) {` : ` switch state.attachments->Attachments.decide(op) {`,
346
+ ` | Error(#NotAttached) => Error(` + n.notAttached + `)`,
347
+ single ? ` | Ok(facts) => Ok(facts->Array.filterMap(toEvent(` + c.entityId + `, _)))` : ` | Ok(facts) => Ok(facts->Array.map(toEvent(` + c.entityId + `, _)))`,
348
+ ` }`,
349
+ ` }`,
350
+ ``
351
+ ]
352
+ ]).join("\n");
225
353
  }
226
354
 
227
355
  function conformanceBinding(c) {
228
356
  let n = namesOf(c);
357
+ let single = Stdlib_Option.getOr(c.cardinality, "Many") === "Single";
229
358
  let id = "e1";
230
359
  let refA = Stdlib_Option.getOr(c.refA, `/uploads/00000000-0000-4000-8000-000000000001/a`);
231
360
  let refB = Stdlib_Option.getOr(c.refB, `/uploads/00000000-0000-4000-8000-000000000002/b`);
232
361
  let createdValue = Stdlib_Option.getOr(c.createdCarriesEntityId, true) ? c.created + `({ ` + c.entityId + `: "` + id + `"})` : c.created;
233
- return [
234
- `// The Attachments trait's conformance suite, bound to \`` + n.slice + `\`.`,
235
- `// Emitted whole: every name here is one the graft already declared.`,
236
- ``,
237
- `module Binding = {`,
238
- ` type ref = string`,
239
- ` let refA = "` + refA + `"`,
240
- ` let refB = "` + refB + `"`,
241
- ``,
242
- ` module Spec = ` + n.slice,
243
- ` module Behavior = ` + n.slice + `_Behavior`,
244
- ``,
245
- ` // Annotated: the slice consumes and emits same-named constructors.`,
246
- ` let created: array<` + n.slice + `.consumedEvent> = [` + createdValue + `]`,
247
- ` let attachedC = (ref): ` + n.slice + `.consumedEvent => ` + n.attached + `({ ` + c.file + `: ref})`,
248
- ` let removedC = (ref): ` + n.slice + `.consumedEvent => ` + n.removed + `({ ` + c.file + `: ref})`,
249
- ` let primarySetC = (ref): ` + n.slice + `.consumedEvent => ` + n.primarySet + `({ ` + c.file + `: ref})`,
250
- ` let altTextSetC = (ref, altText): ` + n.slice + `.consumedEvent =>`,
251
- ` ` + n.altTextSet + `({ ` + c.file + `: ref, altText})`,
252
- ``,
253
- ` let attach = ref => ` + n.slice + `.` + n.attachCmd + `({ ` + c.entityId + `: "` + id + `", ` + c.file + `: ref})`,
254
- ` let remove = ref => ` + n.slice + `.` + n.removeCmd + `({ ` + c.entityId + `: "` + id + `", ` + c.file + `: ref})`,
255
- ` let setPrimary = ref =>`,
256
- ` ` + n.slice + `.` + n.setPrimaryCmd + `({ ` + c.entityId + `: "` + id + `", ` + c.file + `: ref})`,
257
- ` let setAltText = (ref, altText) =>`,
258
- ` ` + n.slice + `.` + n.setAltTextCmd + `({ ` + c.entityId + `: "` + id + `", ` + c.file + `: ref, altText})`,
259
- ``,
260
- ` let attached = ref => ` + n.slice + `.` + n.attached + `({ ` + c.entityId + `: "` + id + `", ` + c.file + `: ref})`,
261
- ` let removed = ref => ` + n.slice + `.` + n.removed + `({ ` + c.entityId + `: "` + id + `", ` + c.file + `: ref})`,
262
- ` let primarySet = ref =>`,
263
- ` ` + n.slice + `.` + n.primarySet + `({ ` + c.entityId + `: "` + id + `", ` + c.file + `: ref})`,
264
- ` let altTextSet = (ref, altText) =>`,
265
- ` ` + n.slice + `.` + n.altTextSet + `({ ` + c.entityId + `: "` + id + `", ` + c.file + `: ref, altText})`,
266
- ` let notAttached = ` + n.slice + `.` + n.notAttached,
267
- `}`,
268
- ``,
269
- `module Conformance = TraitAttachments.Attachments_Conformance.Make(Binding)`,
270
- ``,
271
- `Conformance.register()`,
272
- ``
273
- ].join("\n");
362
+ return Belt_Array.concatMany([
363
+ [
364
+ `// The Attachments trait's conformance suite, bound to \`` + n.slice + `\`.`,
365
+ `// Emitted whole: every name here is one the graft already declared.`,
366
+ ``,
367
+ `module Binding = {`,
368
+ ` type ref = string`,
369
+ ` let refA = "` + refA + `"`,
370
+ ` let refB = "` + refB + `"`,
371
+ ``,
372
+ ` module Spec = ` + n.slice,
373
+ ` module Behavior = ` + n.slice + `_Behavior`,
374
+ ``,
375
+ ` // Annotated: the slice consumes and emits same-named constructors.`,
376
+ ` let created: array<` + n.slice + `.consumedEvent> = [` + createdValue + `]`,
377
+ ` let attachedC = (ref): ` + n.slice + `.consumedEvent => ` + n.attached + `({ ` + c.file + `: ref})`,
378
+ ` let removedC = (ref): ` + n.slice + `.consumedEvent => ` + n.removed + `({ ` + c.file + `: ref})`
379
+ ],
380
+ single ? [] : [` let primarySetC = (ref): ` + n.slice + `.consumedEvent => ` + n.primarySet + `({ ` + c.file + `: ref})`],
381
+ [
382
+ ` let altTextSetC = (ref, altText): ` + n.slice + `.consumedEvent =>`,
383
+ ` ` + n.altTextSet + `({ ` + c.file + `: ref, altText})`,
384
+ ``,
385
+ ` let attach = ref => ` + n.slice + `.` + n.attachCmd + `({ ` + c.entityId + `: "` + id + `", ` + c.file + `: ref})`
386
+ ],
387
+ single ? [` let clear = ` + n.slice + `.` + n.removeCmd + `({ ` + c.entityId + `: "` + id + `"})`] : [
388
+ ` let remove = ref => ` + n.slice + `.` + n.removeCmd + `({ ` + c.entityId + `: "` + id + `", ` + c.file + `: ref})`,
389
+ ` let setPrimary = ref =>`,
390
+ ` ` + n.slice + `.` + n.setPrimaryCmd + `({ ` + c.entityId + `: "` + id + `", ` + c.file + `: ref})`
391
+ ],
392
+ [
393
+ single ? ` let setAltText = altText => ` + n.slice + `.` + n.setAltTextCmd + `({ ` + c.entityId + `: "` + id + `", altText})` : ` let setAltText = (ref, altText) =>\n ` + n.slice + `.` + n.setAltTextCmd + `({ ` + c.entityId + `: "` + id + `", ` + c.file + `: ref, altText})`,
394
+ ``,
395
+ ` let attached = ref => ` + n.slice + `.` + n.attached + `({ ` + c.entityId + `: "` + id + `", ` + c.file + `: ref})`,
396
+ ` let removed = ref => ` + n.slice + `.` + n.removed + `({ ` + c.entityId + `: "` + id + `", ` + c.file + `: ref})`
397
+ ],
398
+ single ? [] : [
399
+ ` let primarySet = ref =>`,
400
+ ` ` + n.slice + `.` + n.primarySet + `({ ` + c.entityId + `: "` + id + `", ` + c.file + `: ref})`
401
+ ],
402
+ [
403
+ ` let altTextSet = (ref, altText) =>`,
404
+ ` ` + n.slice + `.` + n.altTextSet + `({ ` + c.entityId + `: "` + id + `", ` + c.file + `: ref, altText})`,
405
+ ` let notAttached = ` + n.slice + `.` + n.notAttached,
406
+ `}`,
407
+ ``,
408
+ single ? `module Conformance = TraitAttachments.Attachments_Conformance.MakeSingle(Binding)` : `module Conformance = TraitAttachments.Attachments_Conformance.Make(Binding)`,
409
+ ``,
410
+ `Conformance.register()`,
411
+ ``
412
+ ]
413
+ ]).join("\n");
274
414
  }
275
415
 
276
- function projectionPatch(c) {
416
+ function singleProjectionPatch(c) {
277
417
  let n = namesOf(c);
278
418
  return {
279
419
  into: `StateViewSliceStream/` + c.view + `_Projection.res`,
280
- at: `the projection's \`switch\`, and two fields on \`` + c.view + `\`'s state`,
420
+ at: `the projection's \`switch\`, and one field on \`` + c.view + `\`'s state`,
281
421
  contents: [
282
- `// On the view's state, two fields — the set, and its primary as one string.`,
283
- `// The second is not redundancy: a card, a gallery tile and a reference cell`,
284
- `// each read one image-semantic string per row, so without it every tile is blank.`,
422
+ `// On the view's state, one field — the reference and its text, in one value.`,
423
+ `// No collection: this entity holds one attachment, so the field a card, a`,
424
+ `// gallery tile and a list cell read IS the whole of what it has.`,
285
425
  `//`,
286
- `// ` + c.file + `s: array<{` + c.file + `: string, altText?: string}>,`,
287
- `// ` + c.file + `?: string,`,
426
+ `// ` + c.file + `?: ` + viewTypeOf(c) + `,`,
288
427
  ``,
289
428
  `| ` + n.attached + `({` + c.entityId + `, ` + c.file + `, altText: ?altText}) =>`,
290
429
  ` Update(` + c.entityId + `, state => {`,
291
- ` let ` + c.file + `s = Array.concat(state.` + c.file + `s, [{` + c.file + `: ` + c.file + `, altText: ?altText}])`,
292
- ` {...state, ` + c.file + `s, ` + c.file + `: ?withPrimary(` + c.file + `s, state.primaryChosen)}`,
430
+ ` ...state,`,
431
+ ` ` + c.file + `: {ref: ` + c.file + `, altText: ?altText},`,
293
432
  ` })`,
294
433
  `| ` + n.removed + `({` + c.entityId + `, ` + c.file + `}) =>`,
434
+ ` Update(` + c.entityId + `, state =>`,
435
+ ` // Guarded on the reference: a removal that names something this row no`,
436
+ ` // longer holds — the first half of a replacement, arriving late — must not`,
437
+ ` // blank the one it does.`,
438
+ ` heldRef(state) == Some(` + c.file + `) ? {...state, ` + c.file + `: ?None} : state`,
439
+ ` )`,
440
+ `| ` + n.altTextSet + `({` + c.entityId + `, ` + c.file + `, altText}) =>`,
441
+ ` Update(` + c.entityId + `, state =>`,
442
+ ` switch state.` + c.file + ` {`,
443
+ ` | Some(held) if held.ref == ` + c.file + ` => {...state, ` + c.file + `: {...held, altText}}`,
444
+ ` | _ => state`,
445
+ ` }`,
446
+ ` )`,
447
+ ``,
448
+ `// The reference this row holds, if it holds one. Named because both guards`,
449
+ `// above ask the same question of a value that is no longer the reference itself.`,
450
+ `let heldRef = (state: ` + c.view + `.state) => state.` + c.file + `->Option.map(held => held.ref)`
451
+ ].join("\n")
452
+ };
453
+ }
454
+
455
+ function manyProjectionPatch(c) {
456
+ let n = namesOf(c);
457
+ let set = c.file + "s";
458
+ return {
459
+ into: `StateViewSliceStream/` + c.view + `_Projection.res`,
460
+ at: `the projection's \`switch\`, and one field on \`` + c.view + `\`'s state`,
461
+ contents: [
462
+ `// On the view's state, one field — the set, primary first.`,
463
+ `//`,
464
+ `// The primary is the FIRST member rather than a scalar beside the set. That is`,
465
+ `// what a card, a gallery tile and a list cell read, so there is no second field`,
466
+ `// to keep in step with the set and no arm that can forget to. The text rides`,
467
+ `// inside each member for the same reason: a cell renderer is handed a field and`,
468
+ `// a value and never the row, so a caption in a sibling field is one no cell can`,
469
+ `// draw.`,
470
+ `//`,
471
+ `// What it costs, stated plainly: attachment order stops being readable off the`,
472
+ `// view. The log still has it.`,
473
+ `//`,
474
+ `// ` + set + `: array<` + viewTypeOf(c) + `>,`,
475
+ ``,
476
+ `// Appended, so the first attached is the primary until one is chosen.`,
477
+ `| ` + n.attached + `({` + c.entityId + `, ` + c.file + `, altText: ?altText}) =>`,
478
+ ` Update(` + c.entityId + `, state =>`,
479
+ ` state.` + set + `->Array.some(m => m.ref == ` + c.file + `)`,
480
+ ` ? state`,
481
+ ` : {`,
482
+ ` ...state,`,
483
+ ` ` + set + `: state.` + set + `->Array.concat([{ref: ` + c.file + `, altText: ?altText}]),`,
484
+ ` }`,
485
+ ` )`,
486
+ `// Removing the head promotes the next member with no arm to say so.`,
487
+ `| ` + n.removed + `({` + c.entityId + `, ` + c.file + `}) =>`,
295
488
  ` Update(` + c.entityId + `, state => {`,
296
- ` let ` + c.file + `s = state.` + c.file + `s->Array.filter(m => m.` + c.file + ` != ` + c.file + `)`,
297
- ` {...state, ` + c.file + `s, ` + c.file + `: ?withPrimary(` + c.file + `s, state.primaryChosen)}`,
489
+ ` ...state,`,
490
+ ` ` + set + `: state.` + set + `->Array.filter(m => m.ref != ` + c.file + `),`,
298
491
  ` })`,
299
492
  `| ` + n.primarySet + `({` + c.entityId + `, ` + c.file + `}) =>`,
300
- ` Update(` + c.entityId + `, state => {...state, ` + c.file + `: Some(` + c.file + `)})`,
493
+ ` Update(` + c.entityId + `, state => primaryFirst(state, ` + c.file + `))`,
301
494
  `| ` + n.altTextSet + `({` + c.entityId + `, ` + c.file + `, altText}) =>`,
302
495
  ` Update(` + c.entityId + `, state => {`,
303
496
  ` ...state,`,
304
- ` ` + c.file + `s: state.` + c.file + `s->Array.map(m =>`,
305
- ` m.` + c.file + ` == ` + c.file + ` ? {...m, altText} : m`,
306
- ` ),`,
497
+ ` ` + set + `: state.` + set + `->Array.map(m => m.ref == ` + c.file + ` ? {...m, altText} : m),`,
307
498
  ` })`,
308
499
  ``,
309
- `// The primary a reader should show: the one chosen, else the first attached —`,
310
- `// the same rule the trait applies, over the view's own rows.`,
311
- `let withPrimary = (members, chosen) =>`,
312
- ` TraitAttachments.Attachments_Rules.primaryOf(`,
500
+ `// Choosing the primary is moving it to the front the trait's rule, applied`,
501
+ `// over the view's rows. The only arm that reorders; the rest leave the head`,
502
+ `// where they found it.`,
503
+ `let primaryFirst = (state: ` + c.view + `.state, chosen) => {`,
504
+ ` ...state,`,
505
+ ` ` + set + `: TraitAttachments.Attachments_Rules.primaryFirst(`,
313
506
  ` ~chosen,`,
314
- ` ~attached=members->Array.map(m => m.` + c.file + `),`,
315
- ` )`
507
+ ` ~members=state.` + set + `,`,
508
+ ` ~ref=m => m.ref,`,
509
+ ` ),`,
510
+ `}`
316
511
  ].join("\n")
317
512
  };
318
513
  }
319
514
 
515
+ function projectionPatch(c) {
516
+ if (Stdlib_Option.getOr(c.cardinality, "Many") === "Single") {
517
+ return singleProjectionPatch(c);
518
+ } else {
519
+ return manyProjectionPatch(c);
520
+ }
521
+ }
522
+
320
523
  function emit(config, into, tests) {
321
524
  let n = namesOf(config);
322
525
  return {
@@ -340,14 +543,24 @@ function emit(config, into, tests) {
340
543
 
341
544
  export {
342
545
  configSchema,
546
+ cardinalityOf,
547
+ isSingle,
343
548
  namesOf,
344
549
  refTypeOf,
550
+ viewTypeOf,
551
+ setFieldOf,
552
+ selectionBinding,
553
+ selectionTypeOf,
554
+ contentArgOf,
345
555
  commandAttributes,
556
+ commandNames,
346
557
  commandTransitionBinding,
347
558
  lines,
348
559
  sliceSpec,
349
560
  sliceBehavior,
350
561
  conformanceBinding,
562
+ singleProjectionPatch,
563
+ manyProjectionPatch,
351
564
  projectionPatch,
352
565
  emit,
353
566
  }