Files
speckle-server/packages/frontend-2/lib/common/helpers/debugging.ts
T
Kristaps Fabians Geikins 9686099863 fix(fe2): invalid totalCount cache updates for project.versions, project.models and model.versions (#1756)
* converting various 'brittle' subscriptions to use locks

* minor cache update fixes

* added useful debugging utils to local cache updates

* fixed incorrect project.versions, project.models, model.versions cache updates
2023-08-11 11:01:02 +03:00

48 lines
1.2 KiB
TypeScript

/**
* Wrap refs with writable computeds that output where reads/writes are coming from
*/
export function wrapRefWithTracking<R extends Ref<unknown>>(
ref: R,
name: string,
options?: Partial<{
writesOnly: boolean
readsOnly: boolean
}>
): R {
const { writesOnly, readsOnly } = options || {}
const getTrace = () => (new Error('Trace:').stack || '').substring(7)
const logger = useLogger()
return computed({
get: () => {
if (!writesOnly) {
logger.debug(`debugging: '${name}' read`, ref.value, getTrace())
}
return ref.value
},
set: (newVal) => {
if (!readsOnly) {
logger.debug(`debugging: '${name}' written to`, newVal, getTrace())
}
ref.value = newVal
}
// hiding the real type so that you don't have to re-type everything that relies on the ref being a ref
}) as unknown as R
}
/**
* Use this to render a stack trace with clickable links to the source code in the browser console
*/
export class StackTrace extends Error {
constructor() {
super('')
this.name = 'Stack trace:'
}
}
export function getCurrentTrace() {
return (new Error('Trace:').stack || '').substring(7)
}