-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decouple DecisionContext and DecisionValueContext (#59)
- Loading branch information
Showing
66 changed files
with
245 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// @index(['./*.{ts,tsx}', '!./*.test.ts', '!./*.test.tsx', './!(private|parts|functions)*/index.{ts,tsx}'], f => `export * from '${f.path.replace(/\/index$/, '')}';`) | ||
export * from './isLookupContext'; |
28 changes: 28 additions & 0 deletions
28
packages/libs/designer-decisions/src/context/isLookupContext.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import type { LookupContexts } from '../types'; | ||
|
||
import { isLookupContext } from './isLookupContext'; | ||
|
||
describe('isLookupContext', () => { | ||
it('returns true for a valid LookupContexts object', () => { | ||
const validContext: LookupContexts = { all: [] }; | ||
expect(isLookupContext(validContext)).toBe(true); | ||
}); | ||
|
||
it('returns false for an object missing the "all" property', () => { | ||
const invalidContext = { some: [] }; | ||
expect(isLookupContext(invalidContext)).toBe(false); | ||
}); | ||
|
||
it('returns false for null', () => { | ||
expect(isLookupContext(null)).toBe(false); | ||
}); | ||
|
||
it('returns false for non-object types', () => { | ||
expect(isLookupContext(undefined)).toBe(false); | ||
expect(isLookupContext('string')).toBe(false); | ||
expect(isLookupContext(123)).toBe(false); | ||
expect(isLookupContext([])).toBe(false); | ||
}); | ||
}); |
5 changes: 5 additions & 0 deletions
5
packages/libs/designer-decisions/src/context/isLookupContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { LookupContexts } from '../types'; | ||
|
||
export const isLookupContext = (contexts: unknown): contexts is LookupContexts => { | ||
return typeof contexts === 'object' && contexts !== null && 'all' in contexts; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 0 additions & 46 deletions
46
packages/libs/designer-decisions/src/decisions/createDecisionFactory.ts
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
packages/libs/designer-decisions/src/decisions/createLookupContexts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/libs/designer-decisions/src/decisions/createStaticDecisionMap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { StaticDecisionMap, StaticInputMap } from '../inputs'; | ||
import type { Decision, DecisionContext, DecisionInputBase, DecisionRef } from '../types'; | ||
|
||
import { | ||
createDecisionContext, | ||
createInputNotFoundError, | ||
createStaticStoreDecision, | ||
createUnexpectedError, | ||
} from '.'; | ||
|
||
export const createStaticDecisionMap = (inputStore: StaticInputMap): StaticDecisionMap => { | ||
const _createDecision = (context: DecisionContext, inputs: DecisionInputBase[]) => { | ||
try { | ||
return createStaticStoreDecision(context, inputs); | ||
} catch (error) { | ||
const err = createUnexpectedError(context, error); | ||
context.addError(err); | ||
} | ||
}; | ||
|
||
const resolver = <V = unknown>( | ||
ref: DecisionRef, | ||
): [DecisionContext, Decision<V> | undefined] => { | ||
const inputs = inputStore.records(r => '$name' in ref && r.name === ref.$name); // WIP ref matching | ||
if (inputs.length) { | ||
const context = createDecisionContext(ref, resolver, inputs); | ||
const decision = _createDecision(context, inputs); | ||
return [context, decision as Decision<V>]; | ||
} | ||
const context = createDecisionContext(ref, resolver, []); | ||
const error = createInputNotFoundError(context, ref); | ||
context.addError(error); | ||
return [context, undefined]; | ||
}; | ||
|
||
return { | ||
resolve: resolver, | ||
}; | ||
}; |
33 changes: 23 additions & 10 deletions
33
packages/libs/designer-decisions/src/decisions/createStaticStoreDecision.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
packages/libs/designer-decisions/src/decisions/errors/createUnexpectedError.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
// @index(['./*.{ts,tsx}', './!(private|parts|functions)*/index.{ts,tsx}'], f => `export * from '${f.path.replace(/\/index$/, '')}';`) | ||
export * from './createDecisionContext'; | ||
export * from './createDecisionFactory'; | ||
export * from './createLookupContexts'; | ||
export * from './createStaticDecisionMap'; | ||
export * from './createStaticStoreDecision'; | ||
export * from './errors'; | ||
export * from './getDecisionModelFactory'; |
27 changes: 0 additions & 27 deletions
27
packages/libs/designer-decisions/src/helpers/getDecisionType.test.ts
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
packages/libs/designer-decisions/src/helpers/getDecisionType.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
packages/libs/designer-decisions/src/helpers/isDecisionOfType.test.ts
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
packages/libs/designer-decisions/src/helpers/isDecisionOfType.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
// @index(['./*.{ts,tsx}', './!(private|parts|functions)*/index.{ts,tsx}'], f => `export * from '${f.path.replace(/\/index$/, '')}';`) | ||
export * from './constants'; | ||
export * from './sample-data'; | ||
export * from './decisions'; | ||
export * from './helpers'; | ||
export * from './inputs'; | ||
export * from './meta'; | ||
export * from './models'; | ||
export * from './primitives'; | ||
export * from './sample-data'; | ||
export * from './types'; | ||
export * from './values'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,11 @@ | ||
import type { | ||
Decision, | ||
DecisionContext, | ||
DecisionInputBase, | ||
DecisionInputError, | ||
DecisionRef, | ||
DecisionRefResolver, | ||
LookupContexts, | ||
} from '../types'; | ||
import type { DecisionInputBase, DecisionInputError, DecisionRefResolver } from '../types'; | ||
|
||
export type StaticInputMap = { | ||
hasErrors: () => boolean; | ||
validationErrors: () => DecisionInputError[]; | ||
records: (filter?: (item: DecisionInputBase) => boolean) => DecisionInputBase[]; | ||
record: (ref: DecisionRef, contexts?: LookupContexts) => DecisionInputBase | undefined; | ||
}; | ||
|
||
export type StaticDecisionMap = { | ||
create: <V = unknown>( | ||
input: DecisionInputBase, | ||
contexts?: LookupContexts, | ||
) => [DecisionContext, Decision<V> | undefined]; | ||
resolve: DecisionRefResolver; | ||
}; |
3 changes: 1 addition & 2 deletions
3
...gner-decisions/src/primitives/color/oklab-chroma-scale/isColorOklabChromaScaleDecision.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
import { DECISION_COLOR_OKLAB_CHROMA_SCALE } from '../../../constants'; | ||
import { isDecisionOfType } from '../../../helpers'; | ||
import type { ColorOklabChromaScaleDecision, DecisionUnknown } from '../../../types'; | ||
|
||
export const isColorOklabChromaScaleDecision = ( | ||
decision: DecisionUnknown, | ||
): decision is ColorOklabChromaScaleDecision => { | ||
return isDecisionOfType(decision, DECISION_COLOR_OKLAB_CHROMA_SCALE); | ||
return decision.type() === DECISION_COLOR_OKLAB_CHROMA_SCALE; | ||
}; |
3 changes: 1 addition & 2 deletions
3
...gner-decisions/src/primitives/color/oklab-chroma-value/isColorOklabChromaValueDecision.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
import { DECISION_COLOR_OKLAB_CHROMA_VALUE } from '../../../constants'; | ||
import { isDecisionOfType } from '../../../helpers'; | ||
import type { ColorOklabChromaValueDecision, DecisionUnknown } from '../../../types'; | ||
|
||
export const isColorOklabChromaValueDecision = ( | ||
decision: DecisionUnknown, | ||
): decision is ColorOklabChromaValueDecision => { | ||
return isDecisionOfType(decision, DECISION_COLOR_OKLAB_CHROMA_VALUE); | ||
return decision.type() === DECISION_COLOR_OKLAB_CHROMA_VALUE; | ||
}; |
Oops, something went wrong.