mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 03:31:45 +08:00
Caching class names
This commit is contained in:
22
utils/types.d.ts
vendored
22
utils/types.d.ts
vendored
@ -61,4 +61,26 @@
|
|||||||
* Return an array of strings with the name of all classes.
|
* Return an array of strings with the name of all classes.
|
||||||
*/
|
*/
|
||||||
export function getBaseClasses(object): Array<string>;
|
export function getBaseClasses(object): Array<string>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function that gets the ClassInfo for an object.
|
||||||
|
* @param object The object for which the ClassInfo will be get.
|
||||||
|
* Returns a ClassInfo for the object.
|
||||||
|
*/
|
||||||
|
export function getClassInfo(object: Object): ClassInfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Class holding information about a class
|
||||||
|
*/
|
||||||
|
export class ClassInfo {
|
||||||
|
/**
|
||||||
|
* Gets the name of the class.
|
||||||
|
*/
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the ClassInfo for the base class of the current info.
|
||||||
|
*/
|
||||||
|
baseClassInfo: ClassInfo;
|
||||||
|
}
|
||||||
}
|
}
|
@ -31,27 +31,63 @@ export function verifyCallback(value: any) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var classInfosMap = new Map<Function, ClassInfo>();
|
||||||
var funcNameRegex = /function (.{1,})\(/;
|
var funcNameRegex = /function (.{1,})\(/;
|
||||||
export function getClass(object): string {
|
export function getClass(object: Object): string {
|
||||||
var results = (funcNameRegex).exec((object).constructor.toString());
|
return getClassInfo(object).name;
|
||||||
return (results && results.length > 1) ? results[1] : "";
|
}
|
||||||
|
|
||||||
|
export function getClassInfo(object: Object): ClassInfo {
|
||||||
|
var constructor = object.constructor;
|
||||||
|
|
||||||
|
var result = classInfosMap.get(constructor);
|
||||||
|
if (!result) {
|
||||||
|
result = new ClassInfo(constructor);
|
||||||
|
classInfosMap.set(constructor, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getBaseClasses(object): Array<string> {
|
export function getBaseClasses(object): Array<string> {
|
||||||
var baseProto = object.__proto__;
|
|
||||||
var result = [];
|
var result = [];
|
||||||
result.push(getClass(object));
|
var info = getClassInfo(object);
|
||||||
|
while (info) {
|
||||||
while (baseProto !== Object.prototype) {
|
result.push(info.name);
|
||||||
var baseProtoString = baseProto.toString();
|
info = info.baseClassInfo;
|
||||||
// while extending some classes for platform specific versions results in duplicate class types in hierarchy
|
}
|
||||||
if (result.indexOf(baseProtoString) === -1) {
|
return result;
|
||||||
result.push(baseProtoString);
|
}
|
||||||
}
|
|
||||||
baseProto = baseProto.__proto__;
|
export class ClassInfo {
|
||||||
|
private _typeCosntructor: Function;
|
||||||
|
private _name: string;
|
||||||
|
private _baseClassInfo: ClassInfo;
|
||||||
|
|
||||||
|
constructor(typeCosntructor: Function) {
|
||||||
|
this._typeCosntructor = typeCosntructor;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.push("Object");
|
get name(): string {
|
||||||
|
if (!this._name) {
|
||||||
|
var results = (funcNameRegex).exec(this._typeCosntructor.toString());
|
||||||
|
this._name = (results && results.length > 1) ? results[1] : "";
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return this._name;
|
||||||
|
}
|
||||||
|
|
||||||
|
get baseClassInfo(): ClassInfo {
|
||||||
|
if (isUndefined(this._baseClassInfo)) {
|
||||||
|
var constructorProto = this._typeCosntructor.prototype;
|
||||||
|
if (constructorProto.__proto__) {
|
||||||
|
this._baseClassInfo = getClassInfo(constructorProto.__proto__);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this._baseClassInfo = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this._baseClassInfo;
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user