Skip to content

Commit

Permalink
Use semantic string aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
ajthinking committed Jan 8, 2025
1 parent 8502133 commit b39fb95
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 48 deletions.
11 changes: 1 addition & 10 deletions packages/core/src/Application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,4 @@ describe('descriptions', () => {
{ name: 'Signal' }
])
})
})

interface STRICT {
name: string
age: number
}

const chill = {
name: 'chiller'
} as STRICT
})
5 changes: 3 additions & 2 deletions packages/core/src/DiagramBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Param, ParamValue, StringableInputValue } from './Param';
import { Computer } from './types/Computer';
import { Node } from './types/Node';
import { NodeDescription } from './types/NodeDescription';
import { PortId } from './types/PortId';
import { isStringableParam } from './utils/isStringableParam';

// type ParamConfig = Record<string, ParamValue> | Partial<Param>
Expand Down Expand Up @@ -109,7 +110,7 @@ export class DiagramBuilder {
return this;
}

connect(connections? : string | [fromPortId: string, toPortId: string][]) {
connect(connections? : string | [fromPortId: PortId, toPortId: PortId][]) {
if(!connections) return this.guessConnections()
if(typeof connections === 'string') return this.connectByString(connections)

Expand All @@ -122,7 +123,7 @@ export class DiagramBuilder {
return this
}

connectByArray(connections: [fromPortId: string, toPortId: string][]) {
connectByArray(connections: [fromPortId: PortId, toPortId: PortId][]) {
for(const [fromPortId, toPortId] of connections) {
const sourceNodeId = fromPortId.split('.').slice(0, -1).join('.');
const targetNodeId = toPortId.split('.').slice(0, -1).join('.');
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/DiagramQuery.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Diagram } from './Diagram';
import { PortId } from './types/PortId';

export type NodeLabel = string
export type NodeId = string
Expand All @@ -19,7 +20,7 @@ export class DiagramQuery {
})
}

protected getPortId(portDescriptor: PortDescriptor): string {
protected getPortId(portDescriptor: PortDescriptor): PortId {
const [nodeType, portName] = portDescriptor.split('.')

const node = this.diagram.nodes.find(node => node.type === nodeType)
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/InputDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Node } from './types/Node'
import { ItemValue } from './types/ItemValue'
import { InputObserverController } from './InputObserverController';
import { UnfoldedDiagram } from './UnfoldedDiagram'
import { PortName } from './types/Port'

export class InputDevice {
constructor(
Expand All @@ -27,7 +28,7 @@ export class InputDevice {
/**
* Removes and return items at edges connected to input with name
*/
pullFrom(name: string, count: number = Infinity): ItemWithParams[] {
pullFrom(name: PortName, count: number = Infinity): ItemWithParams[] {
let remaining = count
const pulled: ItemValue[] = []
const links = this.unfoldedDiagram.diagram.linksAtInput(this.node, name)
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export { ComputerFactory } from './ComputerFactory'
export { type Computer } from './types/Computer'
export { Diagram } from './Diagram'
export type { Node } from './types/Node'
export type { Link } from './types/Link'
export { PositionGuesser } from './PositionGuesser'
export { LinkGuesser } from './LinkGuesser'
export { InMemoryStorage } from './InMemoryStorage'
Expand Down Expand Up @@ -50,7 +49,7 @@ export type { GetDataFromStorage } from './types/GetDataFromStorage'
export * as nodes from './computers'
export * from './Param'
export { Registry } from './Registry'
export type { LinkId } from './types/Link';
export type { Link, LinkId, LinkCount } from './types/Link';
export type { NodeId } from './types/Node';
export type { NodeStatus } from './Executor';
export type { ObserverStorage, GetLinkItemsParams } from './types/ObserverStorage'
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/types/ExecutionObserver.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { RequestObserverType } from './InputObserveConfig';
import { NotifyObserversCallback } from './NotifyObserversCallback';
import { LinkId } from './Link';
import { LinkCount, LinkId } from './Link';
import { NodeStatus } from '../Executor';
import { NodeId } from './Node';

export type ObserveLinkItems = {
type: RequestObserverType.observeLinkItems,
linkIds: string[],
linkIds: LinkId[],
onReceive: NotifyObserversCallback,
observerId: string,
direction?: 'pull' | 'push',
Expand All @@ -16,13 +16,13 @@ export type ObserveLinkItems = {
}

export interface LinkCountInfo {
count: number;
linkId: string;
count: LinkCount;
linkId: LinkId;
}

export type ObserveLinkCounts = {
type: RequestObserverType.observeLinkCounts,
linkIds: string[],
linkIds: LinkId[],
observerId: string,
throttleMs?: number,
msgId?: string,
Expand All @@ -44,7 +44,7 @@ export type ObserveNodeStatus = {

export type ObserveLinkUpdate = {
type: RequestObserverType.observeLinkUpdate,
linkIds: string[],
linkIds: LinkId[],
observerId: string,
onReceive: (linkIds: LinkId[]) => void,
throttleMs?: number,
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/types/InputObserveConfig.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { LinkId } from './Link';
import { NodeId } from './Node';
import { PortId } from './PortId';

export enum RequestObserverType {
observeLinkCounts = 'observeLinkCounts',
observeLinkItems = 'observeLinkItems',
observeLinkUpdate = 'observeLinkUpdate',
observeNodeStatus = 'observeNodeStatus',
cancelObservation = 'cancelObservation',
}
export type InputObserveConfig = {nodeId: string, portId?: string, type: RequestObserverType}
| {linkId: string, type: RequestObserverType};
export type InputObserveConfig = {nodeId: NodeId, portId?: PortId, type: RequestObserverType}
| {linkId: LinkId, type: RequestObserverType};
1 change: 1 addition & 0 deletions packages/core/src/types/Link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PortId } from './PortId';
import type { CSSProperties, ReactNode } from 'react';

export type LinkId = string
export type LinkCount = number

export type Link = {
id: LinkId,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/types/LinkItemsParam.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { RequestObserverType } from './InputObserveConfig';
import { ItemValue } from './ItemValue';
import { LinkId } from './Link';

export type LinkItemsParam = {
type: RequestObserverType.observeLinkItems;
linkId: string;
linkId: LinkId;
items: ItemValue[];
}
3 changes: 2 additions & 1 deletion packages/core/src/types/LinksCountParam.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { RequestObserverType } from './InputObserveConfig';
import { LinkId } from './Link';

export type LinksCountParam = {
type: RequestObserverType.observeLinkCounts;
linkId: string;
linkId: LinkId;
count: number;
}
4 changes: 3 additions & 1 deletion packages/core/src/types/Port.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PortId } from './PortId'

export type PortName = string

export type AbstractPort = {
Expand All @@ -8,7 +10,7 @@ export type AbstractPort = {
}

export type Port = {
id: string,
id: PortId,
name: PortName,
schema: {
[key: string]: any,
Expand Down
14 changes: 7 additions & 7 deletions packages/ds-ext/src/duckDBStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GetLinkItemsParams, ItemValue, ObserverStorage } from '@data-story/core';
import { GetLinkItemsParams, ItemValue, LinkCount, LinkId, NodeId, ObserverStorage } from '@data-story/core';
import type { Database as DatabaseType} from 'duckdb-async';
import { createDataStoryDBPath } from './commands/createDataStoryDBPath';
export class DuckDBStorage implements ObserverStorage {
Expand Down Expand Up @@ -50,15 +50,15 @@ export class DuckDBStorage implements ObserverStorage {
return this.insertSequence++;
}

async getLinkCount(linkId: string): Promise<number | undefined> {
async getLinkCount(linkId: LinkId): Promise<LinkCount | undefined> {
const result = await this.db?.all('SELECT count FROM linkCounts WHERE linkId = ?', linkId);
if (result && result.length > 0) {
return result[0].count;
}
return undefined;
}

async setLinkCount(linkId: string, count: number): Promise<void> {
async setLinkCount(linkId: LinkId, count: LinkCount): Promise<void> {
const currentTime = new Date();
await this.db?.all('INSERT INTO linkCounts (linkId, count, createTime, updateTime) VALUES (?, ?, ?, ?) ON CONFLICT(linkId) DO UPDATE SET count = ?, updateTime = ?',
linkId, count, currentTime, currentTime, count, currentTime);
Expand All @@ -73,13 +73,13 @@ export class DuckDBStorage implements ObserverStorage {
return result;
}

async setLinkItems(linkId: string, items: ItemValue[]): Promise<void> {
async setLinkItems(linkId: LinkId, items: ItemValue[]): Promise<void> {
this.resetSequence();
await this.db?.all('DELETE FROM linkItems WHERE linkId = ?', linkId);
await this.appendLinkItems(linkId, items);
}

async appendLinkItems(linkId: string, items: ItemValue[]): Promise<void> {
async appendLinkItems(linkId: LinkId, items: ItemValue[]): Promise<void> {
const currentTime = new Date().toISOString();
const values = items.map(item => {
const data = JSON.stringify(item);
Expand All @@ -93,15 +93,15 @@ export class DuckDBStorage implements ObserverStorage {
await this.db?.run(sql);
}

async getNodeStatus(nodeId: string): Promise<'BUSY' | 'COMPLETE' | undefined> {
async getNodeStatus(nodeId: NodeId): Promise<'BUSY' | 'COMPLETE' | undefined> {
const result = await this.db?.all('SELECT status FROM nodes WHERE nodeId = ?', nodeId);
if (!result || result.length === 0) {
return undefined;
}
return result[0].status;
}

async setNodeStatus(nodeId: string, status: 'BUSY' | 'COMPLETE'): Promise<void> {
async setNodeStatus(nodeId: NodeId, status: 'BUSY' | 'COMPLETE'): Promise<void> {
const currentTime = new Date();
await this.db?.all('INSERT INTO nodes (nodeId, status, createTime, updateTime) VALUES (?, ?, ?, ?) ON CONFLICT(nodeId) DO UPDATE SET status = ?, updateTime = ?',
nodeId, status, currentTime, currentTime, status, currentTime);
Expand Down
14 changes: 7 additions & 7 deletions packages/ds-ext/src/fileStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GetLinkItemsParams, ItemValue, ObserverStorage } from '@data-story/core';
import { GetLinkItemsParams, ItemValue, LinkId, NodeId, ObserverStorage } from '@data-story/core';
import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
Expand All @@ -20,12 +20,12 @@ export class FileStorage implements ObserverStorage {

async close() {}

async getLinkCount(linkId: string): Promise<number | undefined> {
async getLinkCount(linkId: LinkId): Promise<number | undefined> {
const data = this.read();
return data.linkCounts[linkId];
}

async setLinkCount(linkId: string, count: number): Promise<void> {
async setLinkCount(linkId: LinkId, count: number): Promise<void> {
const data = this.read();
data.linkCounts[linkId] = count;
this.write(data);
Expand All @@ -36,24 +36,24 @@ export class FileStorage implements ObserverStorage {
return data.linkItems[linkId].slice(offset, offset + limit);
}

async setLinkItems(linkId: string, items: ItemValue[]): Promise<void> {
async setLinkItems(linkId: LinkId, items: ItemValue[]): Promise<void> {
const data = this.read();
data.linkItems[linkId] = items;
this.write(data);
}

async appendLinkItems(linkId: string, items: ItemValue[]): Promise<void> {
async appendLinkItems(linkId: LinkId, items: ItemValue[]): Promise<void> {
const data = this.read();
data.linkItems[linkId].push(...items);
this.write(data);
}

async getNodeStatus(nodeId: string): Promise<'BUSY' | 'COMPLETE' | undefined> {
async getNodeStatus(nodeId: NodeId): Promise<'BUSY' | 'COMPLETE' | undefined> {
const data = this.read();
return data.nodes[nodeId];
}

async setNodeStatus(nodeId: string, status: 'BUSY' | 'COMPLETE'): Promise<void> {
async setNodeStatus(nodeId: NodeId, status: 'BUSY' | 'COMPLETE'): Promise<void> {
const data = this.read();
data.nodes[nodeId] = status;
this.write(data);
Expand Down
4 changes: 2 additions & 2 deletions packages/nodejs/src/server/messageHandlers/getItems.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import WebSocket from 'ws';
import { Application, InMemoryStorage } from '@data-story/core';
import { Application, InMemoryStorage, NodeId } from '@data-story/core';
import { MessageHandler, MessageHandlerParams } from '../MessageHandler';

export type GetItemsMessage = {
type: 'getItems',
atNodeId: string,
atNodeId: NodeId,
id: string,
offset?: number,
limit?: number,
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/components/DataStory/DataStoryCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { HotkeyManager, useHotkeys } from './useHotkeys';
import { useEscapeKey } from './hooks/useEscapeKey';
import { keyManager } from './keyManager';
import { getNodesWithNewSelection } from './getNodesWithNewSelection';
import { createDataStoryId, NodeStatus, RequestObserverType } from '@data-story/core';
import { createDataStoryId, LinkCount, LinkId, NodeStatus, RequestObserverType } from '@data-story/core';

const nodeTypes = {
commentNodeComponent: CommentNodeComponent,
Expand Down Expand Up @@ -125,7 +125,7 @@ const Flow = ({
onReceive: ({ links }) => {
if (!links || links.length === 0) return;

const edgeCounts = links.reduce((acc, link) => {
const edgeCounts: Record<LinkId, LinkCount> = links.reduce((acc, link) => {
acc[link.linkId] = link.count;
return acc;
}, {});
Expand Down
7 changes: 4 additions & 3 deletions packages/ui/src/components/DataStory/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
Param,
RepeatableParam,
type ReportCallback,
type ExecutionObserver, NodeStatus
type ExecutionObserver, NodeStatus,
NodeId
} from '@data-story/core';
import { ReactFlowNode } from '../Node/ReactFlowNode';
import { Edge, OnConnect, OnEdgesChange, OnNodesChange, ReactFlowInstance } from '@xyflow/react';
Expand Down Expand Up @@ -103,13 +104,13 @@ export type StoreSchema = {
addNodeFromDescription: (nodeDescription: NodeDescription) => void;
onNodesChange: OnNodesChange;
setNodes: (nodes: ReactFlowNode[]) => void;
selectNode: (nodeId: string) => void;
selectNode: (nodeId: NodeId) => void;

/** The Edges */
edges: Edge[];
onEdgesChange: OnEdgesChange;
updateEdgeCounts: (edgeCounts: Record<string, number>) => void;
updateEdgeStatus: (edgeStatus: {nodeId: string, status: NodeStatus}[]) => void
updateEdgeStatus: (edgeStatus: {nodeId: NodeId, status: NodeStatus}[]) => void
setEdges: (edges: Edge[]) => void;
connect: OnConnect;

Expand Down

0 comments on commit b39fb95

Please sign in to comment.