Skip to content

Commit

Permalink
bring back type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
foxriver76 committed Mar 12, 2024
1 parent 62c2d7f commit cf56cbf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/src/Vis/Widgets/Basic/BasicSpeechToText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export default class BasicSpeechToText extends VisRxWidget<RxData, BasicSpeechTo
width: 500,
height: 77,
},
};
} as const;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/src/Vis/visBaseWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ interface VisBaseWidgetStateUpdate {
widgetHint?: 'light' | 'dark' | 'hide';
hideHelper?: boolean;
isHidden?: boolean;
gap?: number,
gap?: number;
draggable?: boolean;
showRelativeMoveMenu?: boolean;
}
Expand Down Expand Up @@ -154,7 +154,7 @@ interface VisBaseWidget {
renderLastChange(style: unknown): React.ReactNode;
}

class VisBaseWidget extends React.Component<VisBaseWidgetProps, VisBaseWidgetState> {
class VisBaseWidget<TState extends VisBaseWidgetState = VisBaseWidgetState> extends React.Component<VisBaseWidgetProps, TState & VisBaseWidgetState> {
static FORBIDDEN_CHARS = /[^._\-/ :!#$%&()+=@^{}|~]+/g; // from https://github.com/ioBroker/ioBroker.js-controller/blob/master/packages/common/lib/common/tools.js

/** We do not store the SVG Element in the state because it is cyclic */
Expand Down Expand Up @@ -225,7 +225,7 @@ class VisBaseWidget extends React.Component<VisBaseWidgetProps, VisBaseWidgetSta
),
hideHelper: false,
gap: style.position === 'relative' ? (isVarFinite(props.context.views[props.view].settings?.rowGap) ? parseFloat(props.context.views[props.view].settings?.rowGap as string) : 0) : 0,
};
} as TState & VisBaseWidgetState;

this.onCommandBound = this.onCommand.bind(this);
}
Expand Down Expand Up @@ -405,8 +405,8 @@ class VisBaseWidget extends React.Component<VisBaseWidgetProps, VisBaseWidgetSta
}

// take actual (old) style and data
let styleStr: string = state.style?._originalData ? state.style._originalData : JSON.stringify(state.style);
let dataStr: string = state.data?._originalData ? state.data._originalData : JSON.stringify(state.data);
const styleStr: string = state.style?._originalData ? state.style._originalData : JSON.stringify(state.style);
const dataStr: string = state.data?._originalData ? state.data._originalData : JSON.stringify(state.data);

const isHidden = VisBaseWidget.isWidgetFilteredOutStatic(
props.viewsActiveFilter,
Expand Down
26 changes: 6 additions & 20 deletions src/src/Vis/visRxWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import VisBaseWidget, {
VisBaseWidgetState,
WidgetStyleState,
WidgetDataState,
GroupDataState,
GroupDataState, VisWidgetCommand,
} from './visBaseWidget';
import { addClass, getUsedObjectIDsInWidget } from './visUtils';

Expand Down Expand Up @@ -65,9 +65,7 @@ const POSSIBLE_MUI_STYLES = [
'word-spacing',
];

interface VisRxWidgetProps extends VisBaseWidgetProps {

}
type VisRxWidgetProps = VisBaseWidgetProps

interface RxData {
_originalData: any;
Expand Down Expand Up @@ -118,18 +116,7 @@ interface VisRxWidgetState extends VisBaseWidgetState {
disabled?: boolean;
}

/** TODO: this overload can be removed as soon as VisBaseWidget is written correctly in TS */
interface VisRxWidget<TRxData extends Record<string, any>, TState extends Record<string, any> = Record<string, never>> extends VisBaseWidget {
i18nPrefix?: string;
visHidden?: boolean;
adapter?: string;
version?: string;
url?: string;
custom?: any;
state: VisRxWidgetState & TState & { rxData: TRxData };
}

class VisRxWidget<TRxData extends Record<string, any>> extends VisBaseWidget<VisRxWidgetState> {
class VisRxWidget<TRxData extends Record<string, any>, TState extends Record<string, any> = Record<string, never>> extends VisBaseWidget<VisRxWidgetState & TState & { rxData: TRxData }> {
static POSSIBLE_MUI_STYLES = POSSIBLE_MUI_STYLES;

static i18nPrefix: string | undefined;
Expand Down Expand Up @@ -430,7 +417,6 @@ class VisRxWidget<TRxData extends Record<string, any>> extends VisBaseWidget<Vis
}

async componentWillUnmount() {
// @ts-expect-error check later if types wrong or call wrong
if (this.linkContext.IDs.length) {
await this.props.context.socket.unsubscribeState(this.linkContext.IDs, this.onStateChangedBind);
}
Expand Down Expand Up @@ -508,14 +494,14 @@ class VisRxWidget<TRxData extends Record<string, any>> extends VisBaseWidget<Vis

// subscribe on some new IDs and remove old IDs
const unsubscribe = oldIDs.filter(id => !this.linkContext.IDs.includes(id));
// @ts-expect-error check later if types wrong or call wrong
if (unsubscribe.length) {
// @ts-expect-error check later if types wrong or call wrong
await context.socket.unsubscribeState(unsubscribe, this.onStateChangedBind);
}

const subscribe = this.linkContext.IDs.filter(id => !oldIDs.includes(id));
// @ts-expect-error check later if types wrong or call wrong
if (subscribe.length) {
// @ts-expect-error check later if types wrong or call wrong
await context.socket.subscribeState(subscribe, this.onStateChangedBind);
}

Expand Down Expand Up @@ -1026,7 +1012,7 @@ class VisRxWidget<TRxData extends Record<string, any>> extends VisBaseWidget<Vis
* Get information about specific widget, needs to be implemented by widget class
*/
// eslint-disable-next-line class-methods-use-this
getWidgetInfo(): RxWidgetInfo {
getWidgetInfo(): Readonly<RxWidgetInfo> {
throw new Error('not implemented');
}
}
Expand Down
19 changes: 9 additions & 10 deletions src/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Connection } from '@iobroker/adapter-react-v5';
import { CustomPaletteProperties, WidgetAttributeInfo, WidgetAttributesGroupInfo } from '@/Vis/visRxWidget';
import { CommonType } from '@iobroker/types/build/objects';
import { store } from '@/Store';
import { RxWidgetAttributeType, RxWidgetInfoAttributesField } from '@/allInOneTypes';
import { RxWidgetAttributeType, RxWidgetInfoAttributesField } from '@/detailedTypes';
import type moment from 'moment';
import VisFormatUtils from '@/Vis/visFormatUtils';
import VisView from '@/Vis/visView';
Expand Down Expand Up @@ -187,7 +187,7 @@ export interface ViewSettings {
'line-height'?: string;
'letter-spacing'?: string;
'word-spacing'?: string;
}
};

useAsDefault?: boolean;
alwaysRender?: boolean;
Expand Down Expand Up @@ -414,8 +414,8 @@ interface VisBindingOperationArgument {
}

interface VisBindingOperation {
op: VisBindingOperationType,
arg?: VisBindingOperationArgument[] | string | number | string[],
op: VisBindingOperationType;
arg?: VisBindingOperationArgument[] | string | number | string[];
formula?: string;
}

Expand All @@ -425,8 +425,8 @@ interface VisBinding {
/** ioBroker state ID */
systemOid: StateID;
/** Part of the string, like {id.ack} */
token: string,
operations?: VisBindingOperation[],
token: string;
operations?: VisBindingOperation[];
format: string;
isSeconds: boolean;
}
Expand Down Expand Up @@ -457,7 +457,7 @@ export interface CustomPaletteProperties {
selectedView: string;
themeType: 'dark' | 'light';
helpers: {
deviceIcons: Record<string, React.JSX.Element>
deviceIcons: Record<string, React.JSX.Element>;
detectDevices: (socket: Connection) => Promise<any[]>;
getObjectIcon: (obj: ioBroker.Object, id?: string, imagePrefix?: string) => React.JSX.Element;
allObjects: (socket: Connection) => Promise<Record<string, ioBroker.Object>>;
Expand All @@ -467,12 +467,11 @@ export interface CustomPaletteProperties {
};
}


interface RxWidgetInfoAttributes {
/** Name of the attributes section */
name: string;
/** Fields of this attribute section */
fields: RxWidgetInfoAttributesField[];
fields: readonly RxWidgetInfoAttributesField[];
/** I18n Label */
label?: string;
indexFrom?: number;
Expand Down Expand Up @@ -503,7 +502,7 @@ interface RxWidgetInfo {
visWidgetColor?: string;

/** Groups of attributes */
visAttrs: RxWidgetInfoAttributes[];
visAttrs: (readonly RxWidgetInfoAttributes[]);
/** Default style for widget */
visDefaultStyle?: React.CSSProperties;
/** Position in the widget set */
Expand Down

0 comments on commit cf56cbf

Please sign in to comment.