-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
215 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,85 @@ | ||
import { useCallback } from "react"; | ||
import { call } from "components/script-editor/call"; | ||
import { get, keyBy, toLower as lower, startCase } from "lodash"; | ||
import memoizee from "memoizee"; | ||
import { TraceEventType } from "protocol"; | ||
import { useMemo } from "react"; | ||
import { UploadedTrace } from "slices/UIState"; | ||
import { useLayer } from "slices/layers"; | ||
|
||
|
||
export type Comparator = { | ||
key: string; | ||
apply: (value: number, reference: number) => boolean; | ||
}; | ||
|
||
export type Breakpoint = { | ||
key: string; | ||
property?: string; | ||
reference?: number; | ||
condition?: Comparator; | ||
active?: boolean; | ||
type?: TraceEventType; | ||
}; | ||
|
||
export type DebugLayerData = { | ||
code?: string; | ||
monotonicF?: boolean; | ||
monotonicG?: boolean; | ||
breakpoints?: Breakpoint[]; | ||
trace?: UploadedTrace; | ||
}; | ||
export function useBreakpoints(key?: string) { | ||
const { layer } = useLayer<DebugLayerData>(key); | ||
const {monotonicF, monotonicG,breakpoints ,code,trace} = layer?.source??{} | ||
// TODO: | ||
return useMemo(() => { | ||
const memo = keyBy(trace?.content?.events, "id"); | ||
return memoizee((step: number) => { | ||
const event = trace?.content?.events?.[step]; | ||
if (event) { | ||
try { | ||
// Check monotonic f or g values | ||
if (step) { | ||
for (const p of [monotonicF && "f", monotonicG && "g"]) { | ||
if (p && get(memo[`${event.pId}`], p) > get(event, p)) { | ||
return { result: `Monotonicity violation on ${p}` }; | ||
} | ||
} | ||
} | ||
// Check breakpoints in the breakpoints section | ||
for (const { | ||
active, | ||
condition, | ||
type, | ||
property = "", | ||
reference = 0, | ||
} of breakpoints??[]) { | ||
const isType = !type || type === event.type; | ||
const match = condition?.apply?.(get(event, property), reference); | ||
if (active && isType && match) { | ||
return { | ||
result: `${property} ${lower( | ||
startCase(condition?.key) | ||
)} ${reference}`, | ||
}; | ||
} | ||
} | ||
// Check breakpoints in the script editor section | ||
if ( | ||
call(code ?? "", "shouldBreak", [ | ||
step, | ||
event, | ||
trace?.content?.events?? [], | ||
]) | ||
) { | ||
return { result: "Script editor" }; | ||
} | ||
} catch (e) { | ||
return { error: `${e}` }; | ||
} | ||
} | ||
return { result: "" }; | ||
}); | ||
}, [code, trace?.content, breakpoints, monotonicF, monotonicG]); | ||
|
||
export function useBreakpoints() { | ||
//TODO: | ||
// const [{ specimen }] = useSpecimen(); | ||
// const [{ code, breakpoints = [], monotonicF, monotonicG }] = useUIState(); | ||
// return useMemo(() => { | ||
// const memo = keyBy(specimen?.eventList, "id"); | ||
// return memoize((step: number) => { | ||
// const event = specimen?.eventList?.[step]; | ||
// if (event) { | ||
// try { | ||
// // Check monotonic f or g values | ||
// if (step) { | ||
// for (const p of [monotonicF && "f", monotonicG && "g"]) { | ||
// if (p && get(memo[`${event.pId}`], p) > get(event, p)) { | ||
// return { result: `Monotonicity violation on ${p}` }; | ||
// } | ||
// } | ||
// } | ||
// // Check breakpoints in the breakpoints section | ||
// for (const { | ||
// active, | ||
// condition, | ||
// type, | ||
// property = "", | ||
// reference = 0, | ||
// } of breakpoints) { | ||
// const isType = !type || type === event.type; | ||
// const match = condition?.apply?.(get(event, property), reference); | ||
// if (active && isType && match) { | ||
// return { | ||
// result: `${property} ${lower( | ||
// startCase(condition?.key) | ||
// )} ${reference}`, | ||
// }; | ||
// } | ||
// } | ||
// // Check breakpoints in the script editor section | ||
// if ( | ||
// call(code ?? "", "shouldBreak", [ | ||
// step, | ||
// event, | ||
// specimen?.eventList ?? [], | ||
// ]) | ||
// ) { | ||
// return { result: "Script editor" }; | ||
// } | ||
// } catch (e) { | ||
// return { error: `${e}` }; | ||
// } | ||
// } | ||
// return { result: "" }; | ||
// }); | ||
// }, [code, specimen, breakpoints, monotonicF, monotonicG]); | ||
return useCallback( | ||
(_i: number) => ({ result: "", error: undefined, offset: 0 }), | ||
[] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.