From 65b0de5cee0f02fed1e3d607cb32adc05d0b150b Mon Sep 17 00:00:00 2001 From: BZ Libby Date: Sat, 15 Aug 2020 17:24:16 -0500 Subject: [PATCH] fix(resolve): moves labels rule check to broader scope --- docs/api/cautious-journey.md | 7 ++++- src/labels.ts | 6 ++-- src/resolve.ts | 59 ++++++++++++++++++++---------------- 3 files changed, 41 insertions(+), 31 deletions(-) diff --git a/docs/api/cautious-journey.md b/docs/api/cautious-journey.md index c679b8b..8cfd212 100644 --- a/docs/api/cautious-journey.md +++ b/docs/api/cautious-journey.md @@ -23,7 +23,6 @@ | Interface | Description | | --- | --- | -| [FlagLabel](./cautious-journey.flaglabel.md) | Individual labels: the equivalent of a checkbox. | | [Remote](./cautious-journey.remote.md) | Basic functions which every remote API must provide. | | [RemoteOptions](./cautious-journey.remoteoptions.md) | | | [ResolveInput](./cautious-journey.resolveinput.md) | Collected inputs for a resolver run. | @@ -33,3 +32,9 @@ | [StateValue](./cautious-journey.statevalue.md) | One of many values for a particular state. | | [SyncOptions](./cautious-journey.syncoptions.md) | | +## Type Aliases + +| Type Alias | Description | +| --- | --- | +| [FlagLabel](./cautious-journey.flaglabel.md) | Individual labels: the equivalent of a checkbox. | + diff --git a/src/labels.ts b/src/labels.ts index 89b7f7d..903c152 100644 --- a/src/labels.ts +++ b/src/labels.ts @@ -21,7 +21,7 @@ export interface ChangeSet { /** * Common fields for all labels. */ -export interface BaseLabel { +export interface BaseLabel extends ChangeSet { /** * Label name. */ @@ -46,9 +46,7 @@ export interface BaseLabel { /** * Individual labels: the equivalent of a checkbox. */ -export interface FlagLabel extends BaseLabel, ChangeSet { - requires: Array; -} +export type FlagLabel = BaseLabel; /** * The transition between two state values. diff --git a/src/resolve.ts b/src/resolve.ts index 3cdd9f7..5abdcea 100644 --- a/src/resolve.ts +++ b/src/resolve.ts @@ -1,4 +1,4 @@ -import { FlagLabel, getValueName, prioritySort, StateLabel } from './labels'; +import { FlagLabel, getValueName, prioritySort, StateLabel, BaseLabel } from './labels'; /** * How a label changed. @@ -63,43 +63,50 @@ export function resolveLabels(options: ResolveInput): ResolveResult { const changes: Array = []; const errors: Array = []; - const sortedFlags = prioritySort(options.flags); - for (const flag of sortedFlags) { - const { name } = flag; - - if (activeLabels.has(name)) { - let removed = false; - for (const requiredLabel of flag.requires) { + function checkLabelRules(label: BaseLabel) { + let isRemoved = false; + if (activeLabels.has(label.name)) { + for (const requiredLabel of label.requires) { if (!activeLabels.has(requiredLabel.name)) { - activeLabels.delete(name); - removed = true; + activeLabels.delete(label.name); + isRemoved = true; } } - - if (removed) { - break; - } - - for (const addedLabel of flag.adds) { - activeLabels.add(addedLabel.name); - } - - for (const removedLabel of flag.removes) { - activeLabels.delete(removedLabel.name); - } } + if (isRemoved) { + return; + } + + for (const addedLabel of label.adds) { + activeLabels.add(addedLabel.name); + } + + for (const removedLabel of label.removes) { + activeLabels.delete(removedLabel.name); + } + } + + const sortedFlags = prioritySort(options.flags); + for (const flag of sortedFlags) { + checkLabelRules(flag); } const sortedStates = prioritySort(options.states); for (const state of sortedStates) { + let firstActive = true; const sortedValues = prioritySort(state.values); for (const value of sortedValues) { const name = getValueName(state, value); if (activeLabels.has(name)) { - // TODO: check higher-priority values - // TODO: check removes - // TODO: check requires - // TODO: check becomes + if (firstActive) { + // TODO: check requires + // TODO: check adds + // TODO: check removes + // TODO: check becomes + firstActive = false; + } else { + // removes all other values for this state and breaks? + } } } }