« get me outta code hell

wiki-data.js « lib « test - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/test/lib/wiki-data.js
blob: 5433de29a36b90039c4fd8e3b28bca8c159ac70d (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
import CacheableObject from '#cacheable-object';
import find from '#find';
import {linkWikiDataArrays} from '#yaml';

export function linkAndBindWikiData(wikiData, {
  inferAlbumsOwnTrackData = true,
} = {}) {
  function customLinkWikiDataArrays(...args) {
    linkWikiDataArrays(...args);

    // If albumData is present, automatically set albums' ownTrackData values
    // by resolving track sections' references against the full array. This is
    // just a nicety for working with albums throughout tests.
    if (inferAlbumsOwnTrackData && wikiData.albumData && wikiData.trackData) {
      for (const album of wikiData.albumData) {
        const trackSections =
          CacheableObject.getUpdateValue(album, 'trackSections');

        const trackRefs =
          trackSections.flatMap(section => section.tracks);

        album.ownTrackData =
          trackRefs.map(ref =>
            find.track(ref, wikiData.trackData, {mode: 'error'}));
      }
    }
  }

  customLinkWikiDataArrays(wikiData);

  return {
    // Mutate to make the below functions aware of new data objects, or of
    // reordering the existing ones. Don't mutate arrays such as trackData
    // in-place; assign completely new arrays to this wikiData object instead.
    wikiData,

    // Use this after you've mutated wikiData to assign new data arrays.
    // It'll automatically relink everything on wikiData so all the objects
    // are caught up to date.
    linkWikiDataArrays:
      customLinkWikiDataArrays
        .bind(null, wikiData),

    // Use this if you HAVEN'T mutated wikiData and just need to decache
    // indirect dependencies on exposed properties of other data objects.
    // See documentation on linkWikiDataArarys (in yaml.js) for more info.
    XXX_decacheWikiData:
      customLinkWikiDataArrays
        .bind(null, wikiData, {XXX_decacheWikiData: true}),
  };
}