« get me outta code hell

contribution.js « things « data « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/things/contribution.js
blob: 5594055c9f68a893671f7e70b9954fc902a74ca6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {inspect} from 'node:util';

import CacheableObject from '#cacheable-object';
import {colors} from '#cli';
import {input} from '#composite';
import {empty} from '#sugar';
import Thing from '#thing';
import {isStringNonEmpty, isThing, validateReference} from '#validators';

import {exposeDependency} from '#composite/control-flow';
import {withResolvedReference} from '#composite/wiki-data';
import {flag} from '#composite/wiki-properties';

import {
  inheritFromContributionPresets,
  withContributionArtist,
  withContributionContext,
  withMatchingContributionPresets,
} from '#composite/things/contribution';

export class Contribution extends Thing {
  static [Thing.getPropertyDescriptors] = () => ({
    // Update & expose

    thing: {
      flags: {update: true, expose: true},
      update: {validate: isThing},
    },

    thingProperty: {
      flags: {update: true, expose: true},
      update: {validate: isStringNonEmpty},
    },

    artist: [
      withContributionArtist({
        ref: input.updateValue({
          validate: validateReference('artist'),
        }),
      }),

      exposeDependency({
        dependency: '#artist',
      }),
    ],

    annotation: {
      flags: {update: true, expose: true},
      update: {validate: isStringNonEmpty},
    },

    countInContributionTotals: [
      inheritFromContributionPresets({
        property: input.thisProperty(),
      }),

      flag(true),
    ],

    countInDurationTotals: [
      inheritFromContributionPresets({
        property: input.thisProperty(),
      }),

      flag(true),
    ],

    // Expose only

    context: [
      withContributionContext(),

      {
        dependencies: [
          '#contributionTarget',
          '#contributionProperty',
        ],

        compute: ({
          ['#contributionTarget']: target,
          ['#contributionProperty']: property,
        }) => ({
          target,
          property,
        }),
      },
    ],

    matchingPresets: [
      withMatchingContributionPresets(),

      exposeDependency({
        dependency: '#matchingContributionPresets',
      }),
    ],
  });

  [inspect.custom](depth, options, inspect) {
    const parts = [];
    const accentParts = [];

    parts.push(Thing.prototype[inspect.custom].apply(this));

    if (this.annotation) {
      accentParts.push(colors.green(`"${this.annotation}"`));
    }

    let artistRef;
    if (depth >= 0) {
      let artist;
      try {
        artist = this.artist;
      } catch (_error) {
        // Computing artist might crash for any reason - don't distract from
        // other errors as a result of inspecting this contribution.
      }

      if (artist) {
        artistRef =
          colors.blue(Thing.getReference(artist));
      }
    } else {
      artistRef =
        colors.green(CacheableObject.getUpdateValue(this, 'artist'));
    }

    if (artistRef) {
      accentParts.push(`by ${artistRef}`);
    }

    if (this.thing) {
      if (depth >= 0) {
        const newOptions = {
          ...options,
          depth:
            (options.depth === null
              ? null
              : options.depth - 1),
        };

        accentParts.push(`to ${inspect(this.thing, newOptions)}`);
      } else {
        accentParts.push(`to ${colors.blue(Thing.getReference(this.thing))}`);
      }
    }

    if (!empty(accentParts)) {
      parts.push(` (${accentParts.join(', ')})`);
    }

    return parts.join('');
  }
}