| 1 |
/*--------------------------------------------------------------------------------------------- |
| 2 |
* Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 |
* Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 |
*--------------------------------------------------------------------------------------------*/ |
| 5 |
|
| 6 |
export interface IDebugNameData { |
| 7 |
/** |
| 8 |
* The owner object of an observable. |
| 9 |
* Used for debugging only, such as computing a name for the observable by iterating over the fields of the owner. |
| 10 |
*/ |
| 11 |
readonly owner?: DebugOwner | undefined; |
| 12 |
|
| 13 |
/** |
| 14 |
* A string or function that returns a string that represents the name of the observable. |
| 15 |
* Used for debugging only. |
| 16 |
*/ |
| 17 |
readonly debugName?: DebugNameSource | undefined; |
| 18 |
|
| 19 |
/** |
| 20 |
* A function that points to the defining function of the object. |
| 21 |
* Used for debugging only. |
| 22 |
*/ |
| 23 |
readonly debugReferenceFn?: Function | undefined; |
| 24 |
} |
| 25 |
|
| 26 |
export class DebugNameData { |
| 27 |
constructor( |
| 28 |
public readonly owner: DebugOwner | undefined, |
| 29 |
public readonly debugNameSource: DebugNameSource | undefined, |
| 30 |
public readonly referenceFn: Function | undefined, |
| 31 |
) { } |
| 32 |
|
| 33 |
public getDebugName(target: object): string | undefined { |
| 34 |
return getDebugName(target, this); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* The owning object of an observable. |
| 40 |
* Is only used for debugging purposes, such as computing a name for the observable by iterating over the fields of the owner. |
| 41 |
*/ |
| 42 |
export type DebugOwner = object | undefined; |
| 43 |
export type DebugNameSource = string | (() => string | undefined); |
| 44 |
|
| 45 |
const countPerName = new Map<string, number>(); |
| 46 |
const cachedDebugName = new WeakMap<object, string>(); |
| 47 |
|
| 48 |
export function getDebugName(target: object, data: DebugNameData): string | undefined { |
| 49 |
const cached = cachedDebugName.get(target); |
| 50 |
if (cached) { |
| 51 |
return cached; |
| 52 |
} |
| 53 |
|
| 54 |
const dbgName = computeDebugName(target, data); |
| 55 |
if (dbgName) { |
| 56 |
let count = countPerName.get(dbgName) ?? 0; |
| 57 |
count++; |
| 58 |
countPerName.set(dbgName, count); |
| 59 |
const result = count === 1 ? dbgName : `${dbgName}#${count}`; |
| 60 |
cachedDebugName.set(target, result); |
| 61 |
return result; |
| 62 |
} |
| 63 |
return undefined; |
| 64 |
} |
| 65 |
|
| 66 |
function computeDebugName(self: object, data: DebugNameData): string | undefined { |
| 67 |
const cached = cachedDebugName.get(self); |
| 68 |
if (cached) { |
| 69 |
return cached; |
| 70 |
} |
| 71 |
|
| 72 |
const ownerStr = data.owner ? formatOwner(data.owner) + `.` : ''; |
| 73 |
|
| 74 |
let result: string | undefined; |
| 75 |
const debugNameSource = data.debugNameSource; |
| 76 |
if (debugNameSource !== undefined) { |
| 77 |
if (typeof debugNameSource === 'function') { |
| 78 |
result = debugNameSource(); |
| 79 |
if (result !== undefined) { |
| 80 |
return ownerStr + result; |
| 81 |
} |
| 82 |
} else { |
| 83 |
return ownerStr + debugNameSource; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
const referenceFn = data.referenceFn; |
| 88 |
if (referenceFn !== undefined) { |
| 89 |
result = getFunctionName(referenceFn); |
| 90 |
if (result !== undefined) { |
| 91 |
return ownerStr + result; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
if (data.owner !== undefined) { |
| 96 |
const key = findKey(data.owner, self); |
| 97 |
if (key !== undefined) { |
| 98 |
return ownerStr + key; |
| 99 |
} |
| 100 |
} |
| 101 |
return undefined; |
| 102 |
} |
| 103 |
|
| 104 |
function findKey(obj: object, value: object): string | undefined { |
| 105 |
for (const key in obj) { |
| 106 |
if ((obj as any)[key] === value) { |
| 107 |
return key; |
| 108 |
} |
| 109 |
} |
| 110 |
return undefined; |
| 111 |
} |
| 112 |
|
| 113 |
const countPerClassName = new Map<string, number>(); |
| 114 |
const ownerId = new WeakMap<object, string>(); |
| 115 |
|
| 116 |
function formatOwner(owner: object): string { |
| 117 |
const id = ownerId.get(owner); |
| 118 |
if (id) { |
| 119 |
return id; |
| 120 |
} |
| 121 |
const className = getClassName(owner); |
| 122 |
let count = countPerClassName.get(className) ?? 0; |
| 123 |
count++; |
| 124 |
countPerClassName.set(className, count); |
| 125 |
const result = count === 1 ? className : `${className}#${count}`; |
| 126 |
ownerId.set(owner, result); |
| 127 |
return result; |
| 128 |
} |
| 129 |
|
| 130 |
function getClassName(obj: object): string { |
| 131 |
const ctor = obj.constructor; |
| 132 |
if (ctor) { |
| 133 |
return ctor.name; |
| 134 |
} |
| 135 |
return 'Object'; |
| 136 |
} |
| 137 |
|
| 138 |
export function getFunctionName(fn: Function): string | undefined { |
| 139 |
const fnSrc = fn.toString(); |
| 140 |
// Pattern: /** @description ... */ |
| 141 |
const regexp = /\/\*\*\s*@description\s*([^*]*)\*\//; |
| 142 |
const match = regexp.exec(fnSrc); |
| 143 |
const result = match ? match[1] : undefined; |
| 144 |
return result?.trim(); |
| 145 |
} |