feat(ui): typing for logging context

This commit is contained in:
psychedelicious 2024-08-01 19:52:54 +10:00
parent 81adce3238
commit 74b6674af6
8 changed files with 41 additions and 32 deletions

View File

@ -1,9 +1,8 @@
import type { JSONObject } from 'common/types';
import { rgbaColorToString } from 'common/util/colorCodeTransformers'; import { rgbaColorToString } from 'common/util/colorCodeTransformers';
import { deepClone } from 'common/util/deepClone'; import { deepClone } from 'common/util/deepClone';
import type { CanvasLayer } from 'features/controlLayers/konva/CanvasLayer'; import type { CanvasLayer } from 'features/controlLayers/konva/CanvasLayer';
import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager'; import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager';
import type { EraserLine } from 'features/controlLayers/store/types'; import type { EraserLine, GetLoggingContext } from 'features/controlLayers/store/types';
import { RGBA_RED } from 'features/controlLayers/store/types'; import { RGBA_RED } from 'features/controlLayers/store/types';
import Konva from 'konva'; import Konva from 'konva';
import type { Logger } from 'roarr'; import type { Logger } from 'roarr';
@ -17,7 +16,7 @@ export class CanvasEraserLine {
parent: CanvasLayer; parent: CanvasLayer;
manager: CanvasManager; manager: CanvasManager;
log: Logger; log: Logger;
getLoggingContext: (extra?: JSONObject) => JSONObject; getLoggingContext: GetLoggingContext;
state: EraserLine; state: EraserLine;
konva: { konva: {

View File

@ -1,10 +1,10 @@
import type { JSONObject } from 'common/types';
import { deepClone } from 'common/util/deepClone'; import { deepClone } from 'common/util/deepClone';
import type { CanvasLayer } from 'features/controlLayers/konva/CanvasLayer'; import type { CanvasLayer } from 'features/controlLayers/konva/CanvasLayer';
import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager'; import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager';
import type { CanvasStagingArea } from 'features/controlLayers/konva/CanvasStagingArea';
import { FILTER_MAP } from 'features/controlLayers/konva/filters'; import { FILTER_MAP } from 'features/controlLayers/konva/filters';
import { loadImage } from 'features/controlLayers/konva/util'; import { loadImage } from 'features/controlLayers/konva/util';
import type { ImageObject } from 'features/controlLayers/store/types'; import type { GetLoggingContext, ImageObject } from 'features/controlLayers/store/types';
import { t } from 'i18next'; import { t } from 'i18next';
import Konva from 'konva'; import Konva from 'konva';
import type { Logger } from 'roarr'; import type { Logger } from 'roarr';
@ -19,10 +19,10 @@ export class CanvasImage {
static PLACEHOLDER_TEXT_NAME = `${CanvasImage.TYPE}_placeholder-text`; static PLACEHOLDER_TEXT_NAME = `${CanvasImage.TYPE}_placeholder-text`;
id: string; id: string;
parent: CanvasLayer; parent: CanvasLayer | CanvasStagingArea;
manager: CanvasManager; manager: CanvasManager;
log: Logger; log: Logger;
getLoggingContext: (extra?: JSONObject) => JSONObject; getLoggingContext: GetLoggingContext;
state: ImageObject; state: ImageObject;
konva: { konva: {
@ -34,7 +34,7 @@ export class CanvasImage {
isLoading: boolean; isLoading: boolean;
isError: boolean; isError: boolean;
constructor(state: ImageObject, parent: CanvasLayer) { constructor(state: ImageObject, parent: CanvasLayer | CanvasStagingArea) {
const { id, width, height, x, y } = state; const { id, width, height, x, y } = state;
this.id = id; this.id = id;
this.parent = parent; this.parent = parent;

View File

@ -1,7 +1,7 @@
import type { JSONObject } from 'common/types';
import type { CanvasLayer } from 'features/controlLayers/konva/CanvasLayer'; import type { CanvasLayer } from 'features/controlLayers/konva/CanvasLayer';
import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager'; import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager';
import { getPrefixedId } from 'features/controlLayers/konva/util'; import { getPrefixedId } from 'features/controlLayers/konva/util';
import type { GetLoggingContext } from 'features/controlLayers/store/types';
import Konva from 'konva'; import Konva from 'konva';
import type { Logger } from 'roarr'; import type { Logger } from 'roarr';
@ -12,7 +12,7 @@ export class CanvasInteractionRect {
parent: CanvasLayer; parent: CanvasLayer;
manager: CanvasManager; manager: CanvasManager;
log: Logger; log: Logger;
getLoggingContext: (extra?: JSONObject) => JSONObject; getLoggingContext: GetLoggingContext;
konva: { konva: {
rect: Konva.Rect; rect: Konva.Rect;

View File

@ -1,7 +1,6 @@
import { getStore } from 'app/store/nanostores/store'; import { getStore } from 'app/store/nanostores/store';
import { deepClone } from 'common/util/deepClone'; import { deepClone } from 'common/util/deepClone';
import { CanvasBrushLine } from 'features/controlLayers/konva/CanvasBrushLine'; import { CanvasBrushLine } from 'features/controlLayers/konva/CanvasBrushLine';
import { CanvasEntity } from 'features/controlLayers/konva/CanvasEntity';
import { CanvasEraserLine } from 'features/controlLayers/konva/CanvasEraserLine'; import { CanvasEraserLine } from 'features/controlLayers/konva/CanvasEraserLine';
import { CanvasImage } from 'features/controlLayers/konva/CanvasImage'; import { CanvasImage } from 'features/controlLayers/konva/CanvasImage';
import { CanvasManager } from 'features/controlLayers/konva/CanvasManager'; import { CanvasManager } from 'features/controlLayers/konva/CanvasManager';
@ -9,22 +8,24 @@ import { CanvasRect } from 'features/controlLayers/konva/CanvasRect';
import { CanvasTransformer } from 'features/controlLayers/konva/CanvasTransformer'; import { CanvasTransformer } from 'features/controlLayers/konva/CanvasTransformer';
import { getPrefixedId, konvaNodeToBlob, mapId, previewBlob } from 'features/controlLayers/konva/util'; import { getPrefixedId, konvaNodeToBlob, mapId, previewBlob } from 'features/controlLayers/konva/util';
import { layerRasterized } from 'features/controlLayers/store/canvasV2Slice'; import { layerRasterized } from 'features/controlLayers/store/canvasV2Slice';
import { import type {
type BrushLine, BrushLine,
type CanvasV2State, CanvasV2State,
type Coordinate, Coordinate,
type EraserLine, EraserLine,
imageDTOToImageObject, GetLoggingContext,
type LayerEntity, LayerEntity,
type Rect, Rect,
type RectShape, RectShape,
} from 'features/controlLayers/store/types'; } from 'features/controlLayers/store/types';
import { imageDTOToImageObject } from 'features/controlLayers/store/types';
import Konva from 'konva'; import Konva from 'konva';
import { debounce, get } from 'lodash-es'; import { debounce, get } from 'lodash-es';
import type { Logger } from 'roarr';
import { uploadImage } from 'services/api/endpoints/images'; import { uploadImage } from 'services/api/endpoints/images';
import { assert } from 'tsafe'; import { assert } from 'tsafe';
export class CanvasLayer extends CanvasEntity { export class CanvasLayer {
static TYPE = 'layer'; static TYPE = 'layer';
static LAYER_NAME = `${CanvasLayer.TYPE}_layer`; static LAYER_NAME = `${CanvasLayer.TYPE}_layer`;
static TRANSFORMER_NAME = `${CanvasLayer.TYPE}_transformer`; static TRANSFORMER_NAME = `${CanvasLayer.TYPE}_transformer`;
@ -33,6 +34,11 @@ export class CanvasLayer extends CanvasEntity {
static OBJECT_GROUP_NAME = `${CanvasLayer.TYPE}_object-group`; static OBJECT_GROUP_NAME = `${CanvasLayer.TYPE}_object-group`;
static BBOX_NAME = `${CanvasLayer.TYPE}_bbox`; static BBOX_NAME = `${CanvasLayer.TYPE}_bbox`;
id: string;
manager: CanvasManager;
log: Logger;
getLoggingContext: GetLoggingContext;
drawingBuffer: BrushLine | EraserLine | RectShape | null; drawingBuffer: BrushLine | EraserLine | RectShape | null;
state: LayerEntity; state: LayerEntity;
@ -54,7 +60,10 @@ export class CanvasLayer extends CanvasEntity {
bbox: Rect; bbox: Rect;
constructor(state: LayerEntity, manager: CanvasManager) { constructor(state: LayerEntity, manager: CanvasManager) {
super(state.id, manager); this.id = state.id;
this.manager = manager;
this.getLoggingContext = this.manager.buildEntityGetLoggingContext(this);
this.log = this.manager.buildLogger(this.getLoggingContext);
this.log.debug({ state }, 'Creating layer'); this.log.debug({ state }, 'Creating layer');
this.konva = { this.konva = {

View File

@ -22,7 +22,7 @@ import {
} from 'features/controlLayers/konva/util'; } from 'features/controlLayers/konva/util';
import type { Extents, ExtentsResult, GetBboxTask, WorkerLogMessage } from 'features/controlLayers/konva/worker'; import type { Extents, ExtentsResult, GetBboxTask, WorkerLogMessage } from 'features/controlLayers/konva/worker';
import { $lastProgressEvent, $shouldShowStagedImage } from 'features/controlLayers/store/canvasV2Slice'; import { $lastProgressEvent, $shouldShowStagedImage } from 'features/controlLayers/store/canvasV2Slice';
import type { CanvasV2State, Coordinate, GenerationMode } from 'features/controlLayers/store/types'; import type { CanvasV2State, Coordinate, GenerationMode, GetLoggingContext } from 'features/controlLayers/store/types';
import type Konva from 'konva'; import type Konva from 'konva';
import { atom } from 'nanostores'; import { atom } from 'nanostores';
import type { Logger } from 'roarr'; import type { Logger } from 'roarr';
@ -595,7 +595,7 @@ export class CanvasManager {
buildObjectGetLoggingContext = ( buildObjectGetLoggingContext = (
instance: CanvasBrushLine | CanvasEraserLine | CanvasRect | CanvasImage | CanvasTransformer | CanvasInteractionRect instance: CanvasBrushLine | CanvasEraserLine | CanvasRect | CanvasImage | CanvasTransformer | CanvasInteractionRect
) => { ): GetLoggingContext => {
return (extra?: JSONObject): JSONObject => { return (extra?: JSONObject): JSONObject => {
return { return {
...instance.parent.getLoggingContext(), ...instance.parent.getLoggingContext(),
@ -605,7 +605,7 @@ export class CanvasManager {
}; };
}; };
buildEntityGetLoggingContext = (instance: CanvasLayer) => { buildEntityGetLoggingContext = (instance: CanvasLayer | CanvasStagingArea): GetLoggingContext => {
return (extra?: JSONObject): JSONObject => { return (extra?: JSONObject): JSONObject => {
return { return {
...instance.manager.getLoggingContext(), ...instance.manager.getLoggingContext(),

View File

@ -1,9 +1,8 @@
import type { JSONObject } from 'common/types';
import { rgbaColorToString } from 'common/util/colorCodeTransformers'; import { rgbaColorToString } from 'common/util/colorCodeTransformers';
import { deepClone } from 'common/util/deepClone'; import { deepClone } from 'common/util/deepClone';
import type { CanvasLayer } from 'features/controlLayers/konva/CanvasLayer'; import type { CanvasLayer } from 'features/controlLayers/konva/CanvasLayer';
import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager'; import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager';
import type { RectShape } from 'features/controlLayers/store/types'; import type { GetLoggingContext, RectShape } from 'features/controlLayers/store/types';
import Konva from 'konva'; import Konva from 'konva';
import type { Logger } from 'roarr'; import type { Logger } from 'roarr';
@ -16,7 +15,7 @@ export class CanvasRect {
parent: CanvasLayer; parent: CanvasLayer;
manager: CanvasManager; manager: CanvasManager;
log: Logger; log: Logger;
getLoggingContext: (extra?: JSONObject) => JSONObject; getLoggingContext: GetLoggingContext;
state: RectShape; state: RectShape;
konva: { konva: {

View File

@ -1,8 +1,7 @@
import type { JSONObject } from 'common/types';
import type { CanvasLayer } from 'features/controlLayers/konva/CanvasLayer'; import type { CanvasLayer } from 'features/controlLayers/konva/CanvasLayer';
import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager'; import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager';
import { getPrefixedId } from 'features/controlLayers/konva/util'; import { getPrefixedId } from 'features/controlLayers/konva/util';
import type { Coordinate } from 'features/controlLayers/store/types'; import type { Coordinate , GetLoggingContext } from 'features/controlLayers/store/types';
import Konva from 'konva'; import Konva from 'konva';
import type { Logger } from 'roarr'; import type { Logger } from 'roarr';
@ -10,10 +9,10 @@ export class CanvasTransformer {
static TYPE = 'transformer'; static TYPE = 'transformer';
id: string; id: string;
parent: CanvasLayer; parent: CanvasLayer
manager: CanvasManager; manager: CanvasManager;
log: Logger; log: Logger;
getLoggingContext: (extra?: JSONObject) => JSONObject; getLoggingContext: GetLoggingContext
isActive: boolean; isActive: boolean;
konva: { konva: {

View File

@ -1,3 +1,4 @@
import type { JSONObject } from 'common/types';
import type { CanvasControlAdapter } from 'features/controlLayers/konva/CanvasControlAdapter'; import type { CanvasControlAdapter } from 'features/controlLayers/konva/CanvasControlAdapter';
import { CanvasInpaintMask } from 'features/controlLayers/konva/CanvasInpaintMask'; import { CanvasInpaintMask } from 'features/controlLayers/konva/CanvasInpaintMask';
import { CanvasLayer } from 'features/controlLayers/konva/CanvasLayer'; import { CanvasLayer } from 'features/controlLayers/konva/CanvasLayer';
@ -963,3 +964,5 @@ export function isDrawableEntityType(
): entityType is 'layer' | 'regional_guidance' | 'inpaint_mask' { ): entityType is 'layer' | 'regional_guidance' | 'inpaint_mask' {
return entityType === 'layer' || entityType === 'regional_guidance' || entityType === 'inpaint_mask'; return entityType === 'layer' || entityType === 'regional_guidance' || entityType === 'inpaint_mask';
} }
export type GetLoggingContext = (extra?: JSONObject) => JSONObject;