Skip to content

Commit

Permalink
Tweak load data types
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Jul 24, 2024
1 parent 6bd6be3 commit 32660bb
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/core/encodings/json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseChunked } from '@discoveryjs/json-ext';
import type { Encoding } from '../utils/load-data.types.js';
import type { Encoding } from '../utils/load-data.js';

export default Object.freeze({
name: 'json',
Expand Down
2 changes: 1 addition & 1 deletion src/core/encodings/jsonxl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Encoding } from '../utils/load-data.types.js';
import type { Encoding } from '../utils/load-data.js';
import { decode } from './jsonxl-snapshot9.js';

export const JSONXL_MAGIC_NUMBER = [0x00, 0x00, 0x4a, 0x53, 0x4f, 0x4e, 0x58, 0x4c]; // \0\0JSONXL
Expand Down
2 changes: 1 addition & 1 deletion src/core/encodings/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Encoding } from '../utils/load-data.types.js';
import type { Encoding } from '../utils/load-data.js';

export function validateEncodingConfig(config: any) {
if (!config || typeof config !== 'object') {
Expand Down
44 changes: 23 additions & 21 deletions src/core/utils/load-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import type {
LoadDataBaseOptions,
LoadDataFetchOptions,
ExtractResourceOptions,
LoadDataStateProgress
LoadDataStateProgress,
DatasetResource
} from './load-data.types.js';

export type * from './load-data.types.js';
export const dataSource = {
stream: loadDataFromStream,
event: loadDataFromEvent,
Expand All @@ -25,10 +27,6 @@ export const dataSource = {
push: loadDataFromPush
};

function isObject(value: unknown): value is Object {
return value !== null && typeof value === 'object';
}

function isSameOrigin(url: string) {
try {
return new URL(url, location.origin).origin === location.origin;
Expand Down Expand Up @@ -78,8 +76,12 @@ function isDiscoveryCliLegacyDataWrapper(input: any) {
return true;
}

function buildDataset(rawData: any, rawResource: any, { size, encoding }: { size?: number, encoding?: string }) {
let rawMeta = null;
function buildDataset(
rawData: any,
rawResource: LoadDataResourceMetadata | undefined,
{ size, encoding }: { size?: number, encoding: string }
) {
let rawMeta: Record<string, unknown> | null = null;

if (isDiscoveryCliLegacyDataWrapper(rawData)) {
const { data, ...rawDataMeta } = rawData;
Expand All @@ -93,14 +95,14 @@ function buildDataset(rawData: any, rawResource: any, { size, encoding }: { size
const meta = rawMeta || {};
// eslint-disable-next-line no-unused-vars
const { type, name, encoding: ignore1, size: ignore2, encodedSize, createdAt, ...restResource } = rawResource || {};
const resource = {
const resource: DatasetResource = {
type: type || 'unknown',
name: name || 'unknown',
encoding,
size,
...encodedSize ? { encodedSize } : null,
createdAt: new Date(Date.parse(createdAt) || Date.now()),
...restResource
...Number.isFinite(size) ? { size } : null,
...Number.isFinite(encodedSize) ? { encodedSize } : null,
createdAt: new Date((typeof createdAt === 'string' ? Date.parse(createdAt) : createdAt) || Date.now()),
...(restResource as Omit<{ [key: string]: unknown }, keyof DatasetResource>)
};

return {
Expand Down Expand Up @@ -252,7 +254,7 @@ async function loadDataFromStreamInternal(
resource,
meta,
data,
timing: {
timings: {
time: Number(finishedTime) - Number(requestStart),
start: requestStart,
end: finishedTime,
Expand All @@ -271,19 +273,19 @@ async function loadDataFromStreamInternal(
}
}

export function createLoadDataState(request: LoadDataRequest, extra?: any): LoadDataResult {
export function createLoadDataState<T extends Record<string, unknown>>(request: LoadDataRequest, extra?: T): LoadDataResult & T {
const state = new Observer<LoadDataState>({ stage: 'inited' });

return {
state,
// encapsulate logic into separate function since it's async,
// but we need to return observer for progress tracking purposes
dataset: loadDataFromStreamInternal(request, state),
...extra
...(extra as any)
};
}

export function loadDataFromStream(stream: ReadableStream, options: LoadDataBaseOptions) {
export function loadDataFromStream(stream: ReadableStream, options?: LoadDataBaseOptions) {
return createLoadDataState(
() => ({
method: 'stream',
Expand All @@ -294,7 +296,7 @@ export function loadDataFromStream(stream: ReadableStream, options: LoadDataBase
);
}

export function loadDataFromFile(file: File, options: LoadDataBaseOptions) {
export function loadDataFromFile(file: File, options?: LoadDataBaseOptions) {
const resource = extractResourceMetadata(file);

return createLoadDataState(
Expand All @@ -308,7 +310,7 @@ export function loadDataFromFile(file: File, options: LoadDataBaseOptions) {
);
}

export function loadDataFromEvent(event: DragEvent | InputEvent, options: LoadDataBaseOptions) {
export function loadDataFromEvent(event: DragEvent | InputEvent, options?: LoadDataBaseOptions) {
const source = event.dataTransfer || (event.target as HTMLInputElement | null);
const file = source?.files?.[0];

Expand All @@ -322,7 +324,7 @@ export function loadDataFromEvent(event: DragEvent | InputEvent, options: LoadDa
return loadDataFromFile(file, options);
}

export function loadDataFromUrl(url: string, options: LoadDataFetchOptions) {
export function loadDataFromUrl(url: string, options?: LoadDataFetchOptions) {
options = options || {};

return createLoadDataState(
Expand Down Expand Up @@ -357,7 +359,7 @@ export function loadDataFromUrl(url: string, options: LoadDataFetchOptions) {
);
}

export function loadDataFromPush(options: LoadDataBaseOptions) {
export function loadDataFromPush(options?: LoadDataBaseOptions) {
let controller: ReadableStreamDefaultController | null;
const stream = new ReadableStream({
start(controller_) {
Expand All @@ -374,7 +376,7 @@ export function loadDataFromPush(options: LoadDataBaseOptions) {
resolveRequest = resource => resolve({
method: 'push',
stream,
resource: resource ? (pushResource = resource) : options.resource, // resource takes precedence over options.resource
resource: resource ? (pushResource = resource) : options?.resource, // resource takes precedence over options.resource
options
});
});
Expand Down
11 changes: 6 additions & 5 deletions src/core/utils/load-data.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type LoadDataResourceMetadata = {
size?: number | null;
encodedSize?: number;
createdAt?: string | number;
[k: string]: unknown;
};
export type LoadDataRequestOptions = {
encodings?: Encoding[];
Expand All @@ -64,23 +65,23 @@ export type LoadDataRequestResult = {
method: LoadDataMethod;
stream: ReadableStream<Uint8Array>;
resource?: LoadDataResourceMetadata;
options: LoadDataRequestOptions;
options?: LoadDataRequestOptions;
data?: any;
}

export type Dataset = {
loadMethod: LoadDataMethod;
resource: DatasetResource;
meta: any;
meta: Record<string, unknown>;
data: any;
timing: DatasetTimings;
timings?: DatasetTimings;
};
export type DatasetResource = {
type: string;
name: string;
encoding: string;
size: number;
encodedSize: number | null;
size?: number;
encodedSize?: number;
createdAt: Date;
};
export type DatasetTimings = {
Expand Down
3 changes: 1 addition & 2 deletions src/main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import router from '../extensions/router.js';
import { createElement } from '../core/utils/dom.js';
import Progressbar, { ProgressbarOptions, loadStages } from '../core/utils/progressbar.js';
import * as navButtons from '../nav/buttons.js';
import { syncLoaderWithProgressbar } from '../core/utils/load-data.js';
import { LoadDataBaseOptions, LoadDataResult } from '../core/utils/load-data.types.js';
import { syncLoaderWithProgressbar, LoadDataBaseOptions, LoadDataResult } from '../core/utils/load-data.js';

const coalesceOption = (value: any, fallback: any) => value !== undefined ? value : fallback;

Expand Down
6 changes: 3 additions & 3 deletions src/main/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import jora from 'jora';
import Emitter, { EventMap } from '../core/emitter.js';
import ActionManager from '../core/action.js';
import { normalizeEncodings } from '../core/encodings/utils.js';
import { createExtensionApi, setupModel } from './model-extension-api.js';
import ObjectMarkerManager, { ObjectMarker, ObjectMarkerConfig, ObjectMarkerDescriptor } from '../core/object-marker.js';
import type { Dataset, Encoding, LoadDataBaseOptions, LoadDataFetchOptions, LoadDataResult } from '../core/utils/load-data.types.js';
import { querySuggestions } from './query-suggestions.js';
import type { Dataset, Encoding, LoadDataBaseOptions, LoadDataFetchOptions, LoadDataResult } from '../core/utils/load-data.js';
import { loadDataFromEvent, loadDataFromFile, loadDataFromStream, loadDataFromUrl } from '../core/utils/load-data.js';
import { createExtensionApi, setupModel } from './model-extension-api.js';
import { createLegacyExtensionApi } from './model-legacy-extension-api.js';
import { querySuggestions } from './query-suggestions.js';

export type LogLevel = 'silent' | 'error' | 'warn' | 'info' | 'perf' | 'debug';
export type LogOptions = {
Expand Down
4 changes: 2 additions & 2 deletions src/main/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import * as views from '../views/index.js';
import * as pages from '../pages/index.js';
import { WidgetNavigation } from '../nav/index.js';
import { Model, ModelEvents, ModelOptions, PageParams, PageRef, Query, SetDataOptions } from './model.js';
import { Dataset } from '../core/utils/load-data.types.js';
import Progressbar from '../core/utils/progressbar.js';
import type { Dataset } from '../core/utils/load-data.js';
import type Progressbar from '../core/utils/progressbar.js';

export type RenderSubject = 'page' | 'sidebar';
export type ValueAnnotation = { query: Query, [key: string]: any };
Expand Down

0 comments on commit 32660bb

Please sign in to comment.