Skip to content

Commit 840cceb

Browse files
committed
Prototype spec of algorithm via URL
1 parent 443d8af commit 840cceb

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/algorithms/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import * as Instructions from './instructions';
2222
src/context/actions.js had better be deployed!
2323
XXX Design of noDeploy stuff was done with the aim of minimal code change
2424
and could be re-thought when there are fewer merges going on.
25+
XXX we could export and use allalgs in key places in the system,
26+
eg src/context/actions.js so we can still access them via the URL, but
27+
not have them appear in the index.
2528
2629
Each imported algorithm is expected to be an object of the form:
2730
{ pseudocode: String, explanation: String, run: Function }

src/context/actions.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,26 @@ export function dispatcher(state, setState) {
420420
}
421421

422422
export function initialState() {
423+
// experimenting with specifying algorithm etc using URL
424+
// eg, from https://dev-aia.vercel.app/?alg=heapSort;mode=sort
425+
// we extract the 'search' part, convert to
426+
// [["alg", "heapSort"], ["mode", "sort"]], extract the alg+mode
427+
// and (if they exist) use them for the default.
428+
const currentUrl = new URL(window.location.href);
429+
let search = currentUrl.search.substring(1);
430+
const param_vals = search.split(';').map(s => s.split('='));
431+
const alg_spec = param_vals.find(a => a[0] === "alg");
432+
const alg = (alg_spec? alg_spec[1]: undefined);
433+
const mode_spec = param_vals.find(a => a[0] === "mode");
434+
const mode = (mode_spec? mode_spec[1]: undefined);
435+
// XXX could import+use allalgs instead of algorithms throughout this
436+
// file so we can access algorithms with the noDeploy flag set, even
437+
// though they are not in the menu
438+
if (alg && mode && alg in algorithms && mode in algorithms[alg].pseudocode)
439+
return GlobalActions.LOAD_ALGORITHM(undefined, {
440+
name: alg,
441+
mode: mode,
442+
});
423443
return GlobalActions.LOAD_ALGORITHM(undefined, {
424444
name: DEFAULT_ALGORITHM,
425445
mode: DEFAULT_MODE,

0 commit comments

Comments
 (0)