Skip to content

Commit

Permalink
refactor: reduce size of node positions in url
Browse files Browse the repository at this point in the history
  • Loading branch information
adalinesimonian committed Feb 1, 2024
1 parent 99e0da7 commit f9505d7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 28 deletions.
27 changes: 5 additions & 22 deletions src/providers/app-state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Article } from './api-client.js';
import { Dictionary, Vector2D } from '../types/index.js';
import * as msgpack from '@msgpack/msgpack';
import { Dictionary, NodePositions, Vector2D } from '../types/index.js';

/**
* Manages state for the application. Allows for state to be shared between
Expand Down Expand Up @@ -35,7 +34,7 @@ export class AppState {
/**
* The node positions for the current graph view.
*/
nodePositions?: { [id: string]: Vector2D };
nodePositions?: NodePositions;

/**
* State of keyboard modifiers.
Expand Down Expand Up @@ -93,12 +92,7 @@ export class AppState {
if (this.debug) addParam('debug', 'true');

if (all && this.nodePositions) {
const nodePositions: { [id: string]: { x: number; y: number } } = {};
for (const [key, value] of Object.entries(this.nodePositions)) {
nodePositions[key] = { x: value.x, y: value.y };
}
const encoded = msgpack.encode(nodePositions);
addParam('nodePositions', btoa(String.fromCharCode(...encoded)));
addParam('nodePositions', this.nodePositions.toString());
}

return params.toString();
Expand All @@ -122,7 +116,7 @@ export class AppState {
try {
state[key] = valueFn(value);
} catch (e) {
console.warn(`Failed to deserialize ${key}: ${e}`);
console.warn(`Failed to deserialize ${key}:`, e);
}
};

Expand All @@ -131,18 +125,7 @@ export class AppState {
trySet('zoomLevel', (value) => Number.parseInt(value, 10));
trySet('translation', (value) => Vector2D.parse(value));
trySet('debug', (value) => value === 'true');
trySet('nodePositions', (value) => {
const decoded = msgpack.decode(
Uint8Array.from(atob(value), (c) => c.charCodeAt(0)),
);
const nodePositions: { [id: string]: Vector2D } = {};
for (const [key, value] of Object.entries(
decoded as Record<string, { x: number; y: number }>,
)) {
nodePositions[key] = new Vector2D(value.x, value.y);
}
return nodePositions;
});
trySet('nodePositions', (value) => NodePositions.fromString(value));

return state;
}
Expand Down
17 changes: 11 additions & 6 deletions src/rendering/node-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
ScopedAppStateManager,
} from '../providers/index.js';
import { NodeSelection } from './node-selection.js';
import { IndexedSet, TwoKeyMap, Vector2D } from '../types/index.js';
import {
IndexedSet,
NodePositions,
TwoKeyMap,
Vector2D,
} from '../types/index.js';
import {
GraphBehaviourManager,
GraphDebugBehaviour,
Expand Down Expand Up @@ -156,11 +161,11 @@ export class NodeGraph {
});

this.#appStateManager.on('request-node-positions', () => {
const nodePositions: { [id: number]: Vector2D } = {};
const nodePositions = new NodePositions();
this.#simulation.nodes().forEach((node) => {
nodePositions[node.id] = new Vector2D(
Math.round(node.x!),
Math.round(node.y!),
nodePositions.set(
node.id,
new Vector2D(Math.round(node.x!), Math.round(node.y!)),
);
});
this.#appStateManager.set('nodePositions', nodePositions);
Expand Down Expand Up @@ -252,7 +257,7 @@ export class NodeGraph {
let missed = false;

for (const node of this.#simulation.nodes()) {
const position = nodePositions[node.id];
const position = nodePositions.get(node.id);
if (position) {
node.x = position.x;
node.y = position.y;
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './dictionary.js';
export * from './indexed-set.js';
export * from './inflection-tag.js';
export * from './node-positions.js';
export * from './graphql-error.js';
export * from './rect-2d.js';
export * from './two-key-map.js';
Expand Down
41 changes: 41 additions & 0 deletions src/types/node-positions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Vector2D } from './vector-2d.js';
import { encode, decode } from '@msgpack/msgpack';

/**
* Data type for storing node positions.
*/
export class NodePositions extends Map<number, Vector2D> {
/**
* Converts a NodePositions object to a string.
*/
toString(): string {
const flatPositions: number[] = [];

for (const [id, { x, y }] of this) {
flatPositions.push(id, x, y);
}

const encoded = encode(flatPositions);

console.log(JSON.stringify(flatPositions));

return btoa(String.fromCharCode(...encoded));
}

/**
* Converts a string to a NodePositions object.
*/
static fromString(str: string): NodePositions {
const decoded = decode(
Uint8Array.from(atob(str), (c) => c.charCodeAt(0)),
) as number[];

const positions = new NodePositions();

for (let i = 0; i < decoded.length; i += 3) {
positions.set(decoded[i], new Vector2D(decoded[i + 1], decoded[i + 2]));
}

return positions;
}
}

0 comments on commit f9505d7

Please sign in to comment.