« 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: d2d860ce959e68c7dceaf410e29846bc619ddb81 (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
import CacheableObject from '#cacheable-object';
import find from '#find';
import {withEntries} from '#sugar';
import {linkWikiDataArrays} from '#yaml';

export function linkAndBindWikiData(wikiData, {
  inferAlbumsOwnTrackData = true,
} = {}) {
  function customLinkWikiDataArrays(wikiData, options = {}) {
    linkWikiDataArrays(
      (options.XXX_decacheWikiData
        ? withEntries(wikiData, entries => entries
            .map(([key, value]) => [key, value.slice()]))
        : wikiData));

    // 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.
    //
    // XXX_decacheWikiData option should be used specifically to mark points
    // where you *aren't* replacing any of the arrays under wikiData with
    // new values, and are using linkWikiDataArrays to instead "decache" data
    // properties which depend on any of them. It's currently not possible for
    // a CacheableObject to depend directly on the value of a property exposed
    // on some other CacheableObject, so when those values change, you have to
    // manually decache before the object will realize its cache isn't valid
    // anymore.
    //
    // The previous implementation for this involved overwriting the relevant
    // wikiData properties with null, then replacing it with the original
    // array, which effectively cleared a CacheableObject cache. But it isn't
    // enough to clear other caches that depend on the identity of wikiData
    // arrays, such as withReverseReferenceList, so now it replaces with fresh
    // copies of the data arrays instead; the original identities don't get
    // reused.
    XXX_decacheWikiData:
      customLinkWikiDataArrays
        .bind(null, wikiData, {XXX_decacheWikiData: true}),
  };
}