-
Notifications
You must be signed in to change notification settings - Fork 0
Adapter
The file adapter.ts serves as an universal adapter for each of the algorithms.
It consists of the generic functions toTSVisState, toRSVisState, toTSGraph and toRSGraph.
Each of these functions is capable of receiving the algorithm-specific types (VisualisationState, Node, Link ) as input and returning a general type. Therefore, an additional as <AlgorithmSpecificType> is needed when being used. This usage can be seen in the algorithm-specific stores.
For a detailed explanation of the visState and Graph types, refer to VisState & Graph types.
The function toTSVisState takes a Rust visState (GeneralVisualisationStateRS) and return a visState of type GeneralVisualisationStateTS.
If the input visState has a defined distance property (used in Dijkstra), the distances are transformed from the format used in Rust to the format in Typescript by the use of the function jsonToF64.
To create the Typescript graph, the function toTSGraph is used. As the Rust Graph does not have the node property dist, it is added before calling toTSGraph. Also, the Unicode signs for Infinity are added here.
The function toRSVisState behaves similarly. Here the distances are transformed to the correct Rust format using f64ToJson.
As there are no dist properties for the Rust graph nodes, the function toRSGraph can be called without making changes to the Typescript graph.
The function toTSGraph takes a Rust Graph and returns a Typescript graph.
The function toRSGraph takes a Typescript Graph and returns a Rust graph.
If algorithms that need new properties in the VisStates or Graphs are added, the adapter needs to be updated accordingly. In general, this is only required for properties that need a special transformation (e.g distance), as properties that only need to be copied from Rust type to Typescript type or vice versa are already covered by this implementation.
When properties need transformation, the general types declared at the top of the file need to be updated with the according properties marked as optional (<newProperty>?: <TypeOfProperty>;). The functions need to handle them according to the expected behavior.
It is always necessary to handle these properties as truly optional, to avoid type conflicts for other algorithms.