@reventlessdev/trait-attachments 1.0.0-alpha.1 → 1.0.0-alpha.11

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