From b076c87e435bbe2403122158ee03e4934c220c6c Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 7 Sep 2023 10:32:41 -0300 Subject: data: earlyExitIfNotFound -> notFoundMode --- src/data/things/thing.js | 21 +++++++++------- src/data/things/track.js | 65 +++++++++++++++++++++++++++--------------------- 2 files changed, 48 insertions(+), 38 deletions(-) (limited to 'src/data/things') diff --git a/src/data/things/thing.js b/src/data/things/thing.js index 5d407153..93f19799 100644 --- a/src/data/things/thing.js +++ b/src/data/things/thing.js @@ -375,31 +375,34 @@ export function withResolvedContribs({from, to}) { // Resolves a reference by using the provided find function to match it // within the provided thingData dependency. This will early exit if the -// data dependency is null, or, if earlyExitIfNotFound is set to true, -// if the find function doesn't match anything for the reference. -// Otherwise, the data object is provided on the output dependency; -// or null, if the reference doesn't match anything or itself was null -// to begin with. +// data dependency is null, or, if notFoundMode is set to 'exit', if the find +// function doesn't match anything for the reference. Otherwise, the data +// object is provided on the output dependency; or null, if the reference +// doesn't match anything or itself was null to begin with. export function withResolvedReference({ ref, data, find: findFunction, to = '#resolvedReference', - earlyExitIfNotFound = false, + notFoundMode = 'null', }) { + if (!['exit', 'null'].includes(notFoundMode)) { + throw new TypeError(`Expected notFoundMode to be exit or null`); + } + return compositeFrom(`withResolvedReference`, [ raiseWithoutDependency(ref, {map: {to}, raise: {to: null}}), exitWithoutDependency(data), { - options: {findFunction, earlyExitIfNotFound}, + options: {findFunction, notFoundMode}, mapDependencies: {ref, data}, mapContinuation: {match: to}, - compute({ref, data, '#options': {findFunction, earlyExitIfNotFound}}, continuation) { + compute({ref, data, '#options': {findFunction, notFoundMode}}, continuation) { const match = findFunction(ref, data, {mode: 'quiet'}); - if (match === null && earlyExitIfNotFound) { + if (match === null && notFoundMode === 'exit') { return continuation.exit(null); } diff --git a/src/data/things/track.js b/src/data/things/track.js index a7f96e42..9e1942e3 100644 --- a/src/data/things/track.js +++ b/src/data/things/track.js @@ -59,7 +59,7 @@ export class Track extends Thing { color: compositeFrom(`Track.color`, [ exposeUpdateValueOrContinue(), - withContainingTrackSection({earlyExitIfNotFound: false}), + withContainingTrackSection(), { dependencies: ['#trackSection'], @@ -358,12 +358,13 @@ function inheritFromOriginalRelease({ ]); } -// Gets the track's album. Unless earlyExitIfNotFound is overridden false, -// this will early exit with null in two cases - albumData being missing, -// or not including an album whose .tracks array includes this track. +// Gets the track's album. This will early exit if albumData is missing. +// By default, if there's no album whose list of tracks includes this track, +// the output dependency will be null; set {notFoundMode: 'exit'} to early +// exit instead. function withAlbum({ to = '#album', - earlyExitIfNotFound = true, + notFoundMode = 'null', } = {}) { return compositeFrom(`withAlbum`, [ withResultOfAvailabilityCheck({ @@ -374,16 +375,16 @@ function withAlbum({ { dependencies: ['#albumDataAvailability'], - options: {earlyExitIfNotFound}, + options: {notFoundMode}, mapContinuation: {to}, compute: ({ '#albumDataAvailability': albumDataAvailability, - '#options': {earlyExitIfNotFound}, + '#options': {notFoundMode}, }, continuation) => (albumDataAvailability ? continuation() - : (earlyExitIfNotFound + : (notFoundMode === 'exit' ? continuation.exit(null) : continuation.raise({to: null}))), }, @@ -392,38 +393,38 @@ function withAlbum({ dependencies: ['this', 'albumData'], compute: ({this: track, albumData}, continuation) => continuation({ - '#album': - albumData.find(album => album.tracks.includes(track)), + '#album': albumData.find(album => album.tracks.includes(track)), }), }, { dependencies: ['#album'], - options: {earlyExitIfNotFound}, + options: {notFoundMode}, mapContinuation: {to}, + compute: ({ '#album': album, - '#options': {earlyExitIfNotFound}, + '#options': {notFoundMode}, }, continuation) => (album ? continuation.raise({to: album}) - : (earlyExitIfNotFound + : (notFoundMode === 'exit' ? continuation.exit(null) - : continuation.raise({to: album}))), + : continuation.raise({to: null}))), }, ]); } // Gets a single property from this track's album, providing it as the same // property name prefixed with '#album.' (by default). If the track's album -// isn't available, and earlyExitIfNotFound hasn't been set, the property -// will be provided as null. +// isn't available, then by default, the property will be provided as null; +// set {notFoundMode: 'exit'} to early exit instead. function withAlbumProperty(property, { to = '#album.' + property, - earlyExitIfNotFound = false, + notFoundMode = 'null', } = {}) { return compositeFrom(`withAlbumProperty`, [ - withAlbum({earlyExitIfNotFound}), + withAlbum({notFoundMode}), { dependencies: ['#album'], @@ -443,15 +444,16 @@ function withAlbumProperty(property, { // Gets the listed properties from this track's album, providing them as // dependencies (by default) with '#album.' prefixed before each property -// name. If the track's album isn't available, and earlyExitIfNotFound -// hasn't been set, the same dependency names will be provided as null. +// name. If the track's album isn't available, then by default, the same +// dependency names will be provided as null; set {notFoundMode: 'exit'} +// to early exit instead. function withAlbumProperties({ properties, prefix = '#album', - earlyExitIfNotFound = false, + notFoundMode = 'null', }) { return compositeFrom(`withAlbumProperties`, [ - withAlbum({earlyExitIfNotFound}), + withAlbum({notFoundMode}), { dependencies: ['#album'], @@ -480,23 +482,28 @@ function withAlbumProperties({ } // Gets the track section containing this track from its album's track list. -// Unless earlyExitIfNotFound is overridden false, this will early exit if -// the album can't be found or if none of its trackSections includes the -// track for some reason. +// If notFoundMode is set to 'exit', this will early exit if the album can't be +// found or if none of its trackSections includes the track for some reason. function withContainingTrackSection({ to = '#trackSection', - earlyExitIfNotFound = true, + notFoundMode = 'null', } = {}) { + if (!['exit', 'null'].includes(notFoundMode)) { + throw new TypeError(`Expected notFoundMode to be exit or null`); + } + return compositeFrom(`withContainingTrackSection`, [ - withAlbumProperty('trackSections', {earlyExitIfNotFound}), + withAlbumProperty('trackSections', {notFoundMode}), { dependencies: ['this', '#album.trackSections'], + options: {notFoundMode}, mapContinuation: {to}, compute({ this: track, '#album.trackSections': trackSections, + '#options': {notFoundMode}, }, continuation) { if (!trackSections) { return continuation.raise({to: null}); @@ -507,7 +514,7 @@ function withContainingTrackSection({ if (trackSection) { return continuation.raise({to: trackSection}); - } else if (earlyExitIfNotFound) { + } else if (notFoundMode === 'exit') { return continuation.exit(null); } else { return continuation.raise({to: null}); @@ -533,7 +540,7 @@ function withOriginalRelease({ data: 'trackData', to: '#originalRelease', find: find.track, - earlyExitIfNotFound: true, + notFoundMode: 'exit', }), { -- cgit 1.3.0-6-gf8a5