Skip to content

Commit

Permalink
Move code out of MockApi and into utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Aug 20, 2024
1 parent 2e49215 commit 43dfe40
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 43 deletions.
53 changes: 10 additions & 43 deletions packages/app/src/providers/mock/mock-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
assertArrayShape,
assertDefined,
hasNumericType,
isGroup,
} from '@h5web/shared/guards';
import type {
ArrayShape,
Expand All @@ -22,9 +21,13 @@ import {
import { DataProviderApi } from '../api';
import type { ExportFormat, ExportURL, ValuesStoreParams } from '../models';
import { makeMockFile } from './mock-file';
import { findMockEntity, sliceValue } from './utils';

export const SLOW_TIMEOUT = 3000;
import {
cancellableDelay,
findMockEntity,
getChildrenPaths,
sliceValue,
SLOW_TIMEOUT,
} from './utils';

export class MockApi extends DataProviderApi {
private readonly mockFile: GroupWithChildren;
Expand All @@ -33,6 +36,7 @@ export class MockApi extends DataProviderApi {
private readonly _getExportURL?: DataProviderApi['getExportURL'],
) {
const mockFile = makeMockFile();

super(mockFile.name);
this.mockFile = mockFile;
}
Expand Down Expand Up @@ -65,7 +69,7 @@ export class MockApi extends DataProviderApi {
}

if (dataset.name.startsWith('slow')) {
await this.cancellableDelay(signal);
await cancellableDelay(signal);
}

const { value } = dataset;
Expand Down Expand Up @@ -132,43 +136,6 @@ export class MockApi extends DataProviderApi {
}

public override async getSearchablePaths(path: string): Promise<string[]> {
return this.getEntityPaths(path);
}

private getEntityPaths(entityPath: string): string[] {
const entity = findMockEntity(this.mockFile, entityPath);
if (!entity) {
return [];
}

if (!isGroup(entity)) {
return [entity.path];
}

return entity.children.reduce<string[]>(
(acc, child) => [...acc, ...this.getEntityPaths(child.path)],
[entity.path],
);
}

private async cancellableDelay(signal?: AbortSignal) {
await new Promise<void>((resolve, reject) => {
const timeout = setTimeout(() => {
signal?.removeEventListener('abort', handleAbort);
resolve();
}, SLOW_TIMEOUT);

function handleAbort() {
clearTimeout(timeout);
signal?.removeEventListener('abort', handleAbort);
reject(
new Error(
typeof signal?.reason === 'string' ? signal.reason : 'cancelled',
),
);
}

signal?.addEventListener('abort', handleAbort);
});
return getChildrenPaths(this.mockFile, path);
}
}
42 changes: 42 additions & 0 deletions packages/app/src/providers/mock/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import ndarray from 'ndarray';

import { applyMapping } from '../../vis-packs/core/utils';

export const SLOW_TIMEOUT = 3000;

export function findMockEntity(
group: GroupWithChildren,
path: string,
Expand Down Expand Up @@ -58,3 +60,43 @@ export function sliceValue<T extends DType>(

return mappedArray.data;
}

export function getChildrenPaths(
mockFile: GroupWithChildren,
entityPath: string,
): string[] {
const entity = findMockEntity(mockFile, entityPath);
if (!entity) {
return [];
}

if (!isGroup(entity)) {
return [entity.path];
}

return entity.children.reduce<string[]>(
(acc, child) => [...acc, ...getChildrenPaths(mockFile, child.path)],
[entity.path],
);
}

export async function cancellableDelay(signal?: AbortSignal) {
await new Promise<void>((resolve, reject) => {
const timeout = setTimeout(() => {
signal?.removeEventListener('abort', handleAbort);
resolve();
}, SLOW_TIMEOUT);

function handleAbort() {
clearTimeout(timeout);
signal?.removeEventListener('abort', handleAbort);
reject(
new Error(
typeof signal?.reason === 'string' ? signal.reason : 'cancelled',
),
);
}

signal?.addEventListener('abort', handleAbort);
});
}

0 comments on commit 43dfe40

Please sign in to comment.