enso/app/gui2/shared/util/assert.ts
Paweł Grabarz b7a8909818
Vue dependency update, better selection performance, visible quotes in text inputs (#9204)
- Improved performance by batching simulatenous node edits, including metadata updates when dragging many selected nodes together.
- Updated Vue to new version, allowing us to use `defineModel`.
- Fixed #9161
- Unified all handling of auto-blur by making `useAutoBlur` cheap to register - all logic goes through a single window event handler.
- Combined all `ResizeObserver`s into one.
- Fixed the behaviour of repeated toast messages. Now only the latest compilation status is visible at any given time, and the errors disappear once compilation passes.
- Actually fixed broken interaction of node and visualization widths. There no longer is a style feedback loop and the visible node backdrop width no longer jumps or randomly fails to update.
2024-03-06 15:34:07 +00:00

70 lines
2.5 KiB
TypeScript

export function assertNever(x: never): never {
bail('Unexpected object: ' + JSON.stringify(x))
}
export function assert(condition: boolean, message?: string): asserts condition {
if (!condition) bail(message ? `Assertion failed: ${message}` : 'Assertion failed')
}
/**
* Checks if the given iterable has the specified length and throws an assertion error
* if the lengths do not match.
*
* @param iterable The iterable to check.
* @param length The expected length of the iterable.
* @param message Optional message for the assertion error.
* @return void
* @throws Error Will throw an error if the length does not match.
*
* The first five elements of the iterable will be displayed in the error message
* if the assertion fails. If the iterable contains more than five elements,
* the remaining elements will be represented as '...'.
*/
export function assertLength<T>(iterable: Iterable<T>, length: number, message?: string): void {
const convertedArray = Array.from(iterable)
const messagePrefix = message ? message + ' ' : ''
const elementRepresentation =
convertedArray.length > 5 ?
`${convertedArray.slice(0, 5).join(', ')},...`
: convertedArray.join(', ')
assert(
convertedArray.length === length,
`${messagePrefix}Expected iterable of length ${length}, got length ${convertedArray.length}. Elements: [${elementRepresentation}]`,
)
}
export function assertEmpty<T>(iterable: Iterable<T>, message?: string): void {
assertLength(iterable, 0, message)
}
export function assertEqual<T>(actual: T, expected: T, message?: string) {
const messagePrefix = message ? message + ' ' : ''
assert(actual === expected, `${messagePrefix}Expected ${expected}, got ${actual}.`)
}
export function assertNotEqual<T>(actual: T, unexpected: T, message?: string) {
const messagePrefix = message ? message + ' ' : ''
assert(actual !== unexpected, `${messagePrefix}Expected not ${unexpected}, got ${actual}.`)
}
export function assertDefined<T>(x: T | undefined, message?: string): asserts x is T {
const messagePrefix = message ? message + ' ' : ''
assert(x !== undefined, `${messagePrefix}Expected value to be defined.`)
}
export function assertUnreachable(): never {
bail('Unreachable code')
}
/**
* Throw an error with provided message.
*
* It is convenient to use at the end of a nullable chain:
* ```ts
* const x = foo?.bar.baz?.() ?? bail('Expected foo.bar.baz to exist')
* ```
*/
export function bail(message: string): never {
throw new Error(message)
}