mitja/return-results
Mitja Bezenšek 2024-04-26 15:22:10 +02:00
rodzic 760a735872
commit c596da4603
3 zmienionych plików z 45 dodań i 34 usunięć

Wyświetl plik

@ -633,8 +633,8 @@ export class Editor extends EventEmitter<TLEventMap> {
};
};
createPage(page: Partial<TLPage>): this;
createShape<T extends TLUnknownShape>(shape: OptionalKeys<TLShapePartial<T>, 'id'>): EditorResult<void>;
createShapes<T extends TLUnknownShape>(shapes: OptionalKeys<TLShapePartial<T>, 'id'>[]): EditorResult<void>;
createShape<T extends TLUnknownShape>(shape: OptionalKeys<TLShapePartial<T>, 'id'>): EditorResult<void, CreateShapeError>;
createShapes<T extends TLUnknownShape>(shapes: OptionalKeys<TLShapePartial<T>, 'id'>[]): EditorResult<void, CreateShapeError>;
deleteAssets(assets: TLAsset[] | TLAssetId[]): this;
deleteOpenMenu(id: string): this;
deletePage(page: TLPage | TLPageId): this;

Wyświetl plik

@ -129,7 +129,17 @@ import {
} from './types/event-types'
import { TLExternalAssetContent, TLExternalContent } from './types/external-content'
import { TLHistoryBatchOptions } from './types/history-types'
import { EditorResult, OptionalKeys, RequiredKeys, TLSvgOptions } from './types/misc-types'
import {
CreateShapeError,
EditorResult,
MAX_SHAPES_REACHED_ERROR_ERROR,
NOT_ARRAY_OF_SHAPES_ERROR,
NO_SHAPES_PROVIDED_ERROR,
OptionalKeys,
READONLY_ROOM_ERROR,
RequiredKeys,
TLSvgOptions,
} from './types/misc-types'
import { TLResizeHandle } from './types/selection-types'
/** @public */
@ -6243,12 +6253,12 @@ export class Editor extends EventEmitter<TLEventMap> {
*/
createShapes<T extends TLUnknownShape>(
shapes: OptionalKeys<TLShapePartial<T>, 'id'>[]
): EditorResult<void> {
): EditorResult<void, CreateShapeError> {
if (!Array.isArray(shapes)) {
return EditorResult.error('not-an-array-of-shapes')
return EditorResult.error(NOT_ARRAY_OF_SHAPES_ERROR)
}
if (this.getInstanceState().isReadonly) return EditorResult.error('readonly-room')
if (shapes.length <= 0) return EditorResult.error('no-shapes-provied')
if (this.getInstanceState().isReadonly) return EditorResult.error(READONLY_ROOM_ERROR)
if (shapes.length <= 0) return EditorResult.error(NO_SHAPES_PROVIDED_ERROR)
const currentPageShapeIds = this.getCurrentPageShapeIds()
@ -6257,7 +6267,7 @@ export class Editor extends EventEmitter<TLEventMap> {
if (maxShapesReached) {
// can't create more shapes than fit on the page
alertMaxShapes(this)
return EditorResult.error('max-shapes-reached')
return EditorResult.error(MAX_SHAPES_REACHED_ERROR_ERROR)
}
const focusedGroupId = this.getFocusedGroupId()

Wyświetl plik

@ -15,34 +15,35 @@ export type TLSvgOptions = {
preserveAspectRatio: React.SVGAttributes<SVGSVGElement>['preserveAspectRatio']
}
export type TLEditorErrorType = keyof typeof TLEditorErrorTypeMap
// General errors
export const READONLY_ROOM_ERROR = { type: 'readonly-room' as const, message: 'Room is readonly' }
export type TLEditorError = { message: string; type: TLEditorErrorType }
const TLEditorErrorTypeMap = {
'not-an-array-of-shapes': {
message: 'createShapes requires an array of shapes',
type: 'not-an-array-of-shapes' as const,
},
'no-shapes-provied': {
message: 'No shapes provided',
type: 'no-shapes-provied' as const,
},
'readonly-room': {
message: 'Room is readonly',
type: 'readonly-room' as const,
},
'max-shapes-reached': {
message: 'Max shapes reached',
type: 'max-shapes-reached' as const,
},
// Create shape errors
export const NOT_ARRAY_OF_SHAPES_ERROR = {
type: 'not-array' as const,
message: 'Expected an array',
}
export const NO_SHAPES_PROVIDED_ERROR = {
type: 'no-shapes-provided' as const,
message: 'No shapes provided',
}
export const MAX_SHAPES_REACHED_ERROR_ERROR = {
type: 'max-shapes-reached' as const,
message: 'Max shapes reached',
}
export type ErrorResult = { ok: false; error: TLEditorError }
export type OkResult = { ok: true }
export type OkResultWithValue<T> = { ok: true; value: T }
export type CreateShapeErrorType =
| (typeof READONLY_ROOM_ERROR)['type']
| (typeof NOT_ARRAY_OF_SHAPES_ERROR)['type']
| (typeof NO_SHAPES_PROVIDED_ERROR)['type']
| (typeof MAX_SHAPES_REACHED_ERROR_ERROR)['type']
export type CreateShapeError = { type: CreateShapeErrorType; message: string }
export type EditorResult<T> = ErrorResult | OkResult | OkResultWithValue<T>
export type OkResult = { readonly ok: true }
export type OkResultWithValue<T> = { readonly ok: true; readonly value: T }
export type ErrorResult<E> = { readonly ok: false; readonly error: E }
export type EditorResult<T, E> = ErrorResult<E> | OkResult | OkResultWithValue<T>
export const EditorResult = {
ok(): OkResult {
return { ok: true }
@ -50,7 +51,7 @@ export const EditorResult = {
okWithValue<T>(value: T): OkResultWithValue<T> {
return { ok: true, value }
},
error(errorType: TLEditorErrorType): ErrorResult {
return { ok: false, error: TLEditorErrorTypeMap[errorType] }
error<E>(error: E): ErrorResult<E> {
return { ok: false, error }
},
}