-
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.
- Loading branch information
Showing
10 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type Invariant from '~/domain/Invariant.mjs'; | ||
import StorageRepository from './StorageRepository.mjs'; | ||
import type { SemVerString } from '~/lib/SemVer.mjs'; | ||
import InvariantToJsonMapper from '~/mappers/InvariantToJsonMapper.mjs'; | ||
import pkg from '~/../package.json' with { type: 'json' }; | ||
|
||
export default class InvariantRepository extends StorageRepository<Invariant> { | ||
constructor(storage: Storage) { | ||
super('invariants', storage, new InvariantToJsonMapper(pkg.version as SemVerString)); | ||
} | ||
} |
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,6 @@ | ||
import Requirement from './Requirement.mjs'; | ||
|
||
/** | ||
* An invariant is an environment property that must be maintained | ||
*/ | ||
export default class Invariant extends Requirement { } |
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,20 @@ | ||
import Invariant from '~/domain/Invariant.mjs'; | ||
import RequirementToJsonMapper, { type RequirementJson } from './RequirementToJsonMapper.mjs'; | ||
import SemVer from '~/lib/SemVer.mjs'; | ||
|
||
export interface InvariantJson extends RequirementJson { } | ||
|
||
export default class InvariantToJsonMapper extends RequirementToJsonMapper { | ||
override mapFrom(target: InvariantJson): Invariant { | ||
const version = new SemVer(target.serializationVersion); | ||
|
||
if (version.gte('0.4.0')) | ||
return new Invariant(target); | ||
|
||
throw new Error(`Unsupported serialization version: ${version}`); | ||
} | ||
|
||
override mapTo(source: Invariant): InvariantJson { | ||
return super.mapTo(source); | ||
} | ||
} |
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
78 changes: 78 additions & 0 deletions
78
src/presentation/pages/solution/environment/InvariantsPage.mts
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,78 @@ | ||
import type { Uuid } from '~/types/Uuid.mjs'; | ||
import type Environment from '~/domain/Environment.mjs'; | ||
import Invariant from '~/domain/Invariant.mjs'; | ||
import SolutionRepository from '~/data/SolutionRepository.mjs'; | ||
import EnvironmentRepository from '~/data/EnvironmentRepository.mjs'; | ||
import InvariantRepository from '~/data/InvariantRepository.mjs'; | ||
import Page from '~/presentation/pages/Page.mjs'; | ||
import { DataTable } from '~/presentation/components/DataTable.mjs'; | ||
import html from '~/presentation/lib/html.mjs'; | ||
|
||
const { p } = html; | ||
|
||
export default class InvariantsPage extends Page { | ||
static override route = '/:solution/environment/invariants'; | ||
static { | ||
customElements.define('x-page-invariants', this); | ||
} | ||
|
||
#solutionRepository = new SolutionRepository(localStorage); | ||
#environmentRepository = new EnvironmentRepository(localStorage); | ||
#invariantRepository = new InvariantRepository(localStorage); | ||
#environment?: Environment; | ||
|
||
constructor() { | ||
super({ title: 'Constraints' }, []); | ||
|
||
const dataTable = new DataTable<Invariant>({ | ||
columns: { | ||
id: { headerText: 'ID', readonly: true, formType: 'hidden', unique: true }, | ||
statement: { headerText: 'Statement', required: true, formType: 'text', unique: true } | ||
}, | ||
select: async () => { | ||
if (!this.#environment) | ||
return []; | ||
|
||
return await this.#invariantRepository.getAll(t => this.#environment!.invariantIds.includes(t.id)); | ||
}, | ||
onCreate: async item => { | ||
const invariant = new Invariant({ ...item, id: self.crypto.randomUUID() }); | ||
this.#environment!.invariantIds.push(invariant.id); | ||
await Promise.all([ | ||
this.#invariantRepository.add(invariant), | ||
this.#environmentRepository.update(this.#environment!) | ||
]); | ||
}, | ||
onUpdate: async item => { | ||
await this.#invariantRepository.update(new Invariant({ | ||
...item | ||
})); | ||
}, | ||
onDelete: async id => { | ||
this.#environment!.invariantIds = this.#environment!.invariantIds.filter(x => x !== id); | ||
await Promise.all([ | ||
this.#invariantRepository.delete(id), | ||
this.#environmentRepository.update(this.#environment!) | ||
]); | ||
} | ||
}); | ||
|
||
this.append( | ||
p(` | ||
Invariants are properties that must always be true. They are used to | ||
constrain the possible states of a system. | ||
`), | ||
dataTable | ||
); | ||
|
||
this.#environmentRepository.addEventListener('update', () => dataTable.renderData()); | ||
this.#invariantRepository.addEventListener('update', () => dataTable.renderData()); | ||
const solutionId = this.urlParams['solution'] as Uuid; | ||
this.#solutionRepository.getBySlug(solutionId).then(solution => { | ||
this.#environmentRepository.get(solution!.environmentId).then(environment => { | ||
this.#environment = environment; | ||
dataTable.renderData(); | ||
}); | ||
}); | ||
} | ||
} |