Skip to content

Commit

Permalink
Make IntegrationVariable type safe
Browse files Browse the repository at this point in the history
  • Loading branch information
haydar-metin committed Aug 8, 2024
1 parent 0e7af7e commit 8c3d7e6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/workflow-test/tests/features/hover/popup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ test.describe('The popup', () => {
test.beforeEach(async ({ integration, glspServer }) => {
app = new WorkflowApp({
type: 'integration',
integration: integration
integration
});
graph = app.graph;
expectedManualPopupText.setServer(glspServer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test.describe('The marker navigator', () => {
integration
});
graph = app.graph;
const navigatorProvider = new IntegrationVariable({
const navigatorProvider = new IntegrationVariable<MarkerNavigator>({
value: {
Standalone: new StandaloneMarkerNavigator(app),
Theia: new TheiaMarkerNavigator(app)
Expand Down
38 changes: 20 additions & 18 deletions packages/glsp-playwright/src/test/dynamic-variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,47 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { Integration, IntegrationType } from '~/integration';
import type { GLSPServer } from '../glsp-server';
import { Integration } from '../integration';

export interface DynamicVariableOptions<T> {
defaultValue?: T;
value?: Record<string, T>;
type RecordKey = string | number | symbol;

export interface DynamicVariableOptions<TKey extends RecordKey, TValue> {
defaultValue?: TValue;
value?: Partial<Record<TKey, TValue | undefined>>;
}

export interface DynamicVariable<T> {
getOrThrow(key: string): T;
export interface DynamicVariable<TKey extends RecordKey, TValue> {
getOrThrow(key: TKey): TValue;
}

export abstract class BaseDynamicVariable<T> {
protected readonly value: Record<string, T>;
protected readonly defaultValue?: T;
export abstract class BaseDynamicVariable<TKey extends RecordKey, TValue> {
protected readonly value: Partial<Record<TKey, TValue | undefined>>;
protected readonly defaultValue?: TValue;

constructor(protected readonly options: DynamicVariableOptions<T>) {
constructor(protected readonly options: DynamicVariableOptions<TKey, TValue>) {
this.defaultValue = options?.defaultValue;
this.value = options?.value ?? {};
}

getOrThrow(key: string): T {
getOrThrow(key: TKey): TValue {
const value = this.value[key] ?? this.defaultValue;
if (value === undefined) {
throw new Error(`No value found for key ${key}`);
throw new Error(`No value found for key ${String(key)}`);
}
return value;
}
}

export class IntegrationVariable<T> extends BaseDynamicVariable<T> {
export class IntegrationVariable<TValue> extends BaseDynamicVariable<IntegrationType, TValue> {
protected integration?: Integration;

constructor(options: DynamicVariableOptions<T> & { integration?: Integration }) {
constructor(options: DynamicVariableOptions<IntegrationType, TValue> & { integration?: Integration }) {
super(options);
this.integration = options.integration;
}

get(): T {
get(): TValue {
if (this.integration === undefined) {
throw new Error('No integration set');
}
Expand All @@ -65,15 +67,15 @@ export class IntegrationVariable<T> extends BaseDynamicVariable<T> {
}
}

export class ServerVariable<T> extends BaseDynamicVariable<T> {
export class ServerVariable<TValue> extends BaseDynamicVariable<string, TValue> {
protected server?: GLSPServer;

constructor(options: DynamicVariableOptions<T> & { server?: GLSPServer }) {
constructor(options: DynamicVariableOptions<string, TValue> & { server?: GLSPServer }) {
super(options);
this.server = options.server;
}

get(): T {
get(): TValue {
if (this.server === undefined) {
throw new Error('No GLSP server set');
}
Expand Down

0 comments on commit 8c3d7e6

Please sign in to comment.