Skip to content

add inspector debug #20775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions packages/@ember/debug/ember-inspector-support/container-debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import DebugPort from './debug-port';

export default class ContainerDebug extends DebugPort {
declare objectToConsole: any;
get objectInspector() {
return this.namespace?.objectInspector;
}

get container() {
return this.namespace?.owner?.__container__;
}

TYPES_TO_SKIP = [
'component-lookup',
'container-debug-adapter',
'resolver-for-debugging',
'event_dispatcher',
];

static {
this.prototype.portNamespace = 'container';
this.prototype.messages = {
getTypes(this: ContainerDebug) {
this.sendMessage('types', {
types: this.getTypes(),
});
},
getInstances(this: ContainerDebug, message: any) {
let instances = this.getInstances(message.containerType);
if (instances) {
this.sendMessage('instances', {
instances,
status: 200,
});
} else {
this.sendMessage('instances', {
status: 404,
});
}
},
sendInstanceToConsole(this: ContainerDebug, message: any) {
const instance = this.container.lookup(message.name);
this.objectToConsole.sendValueToConsole(instance);
},
};
}

typeFromKey(key: string) {
return key.split(':').shift()!;
}

nameFromKey(key: string) {
return key.split(':').pop();
}

shouldHide(type: string) {
return type[0] === '-' || this.TYPES_TO_SKIP.indexOf(type) !== -1;
}

instancesByType() {
let key;
let instancesByType: Record<string, any> = {};
let cache = this.container.cache;
// Detect if InheritingDict (from Ember < 1.8)
if (typeof cache.dict !== 'undefined' && typeof cache.eachLocal !== 'undefined') {
cache = cache.dict;
}
for (key in cache) {
const type = this.typeFromKey(key);
if (this.shouldHide(type)) {
continue;
}
if (instancesByType[type] === undefined) {
instancesByType[type] = [];
}
instancesByType[type].push({
fullName: key,
instance: cache[key],
});
}
return instancesByType;
}

getTypes() {
let key;
let types = [];
const instancesByType = this.instancesByType();
for (key in instancesByType) {
types.push({ name: key, count: instancesByType[key].length });
}
return types;
}

getInstances(type: any) {
const instances = this.instancesByType()[type];
if (!instances) {
return null;
}
return instances.map((item: any) => ({
name: this.nameFromKey(item.fullName),
fullName: item.fullName,
inspectable: this.objectInspector.canSend(item.instance),
}));
}
}
191 changes: 191 additions & 0 deletions packages/@ember/debug/ember-inspector-support/data-debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import DebugPort from './debug-port';
import { guidFor } from '@ember/debug/ember-inspector-support/utils/ember/object/internals';

export default class DataDebug extends DebugPort {
declare portNamespace: string;
declare sentTypes: Record<string, any>;
declare sentRecords: Record<string, any>;
init() {
super.init();
this.sentTypes = {};
this.sentRecords = {};
}

releaseTypesMethod: Function | null = null;
releaseRecordsMethod: Function | null = null;

get adapter() {
const owner = this.namespace?.owner;

// dataAdapter:main is deprecated
let adapter = this._resolve('data-adapter:main') && owner.lookup('data-adapter:main');
// column limit is now supported at the inspector level
if (adapter) {
adapter.attributeLimit = 100;
return adapter;
}

return null;
}

_resolve(name: string) {
const owner = this.namespace?.owner;

return owner.resolveRegistration(name);
}

get objectInspector() {
return this.namespace?.objectInspector;
}

modelTypesAdded(types: any[]) {
let typesToSend;
typesToSend = types.map((type) => this.wrapType(type));
this.sendMessage('modelTypesAdded', {
modelTypes: typesToSend,
});
}

modelTypesUpdated(types: any[]) {
let typesToSend = types.map((type) => this.wrapType(type));
this.sendMessage('modelTypesUpdated', {
modelTypes: typesToSend,
});
}

wrapType(type: any) {
const objectId = guidFor(type.object);
this.sentTypes[objectId] = type;

return {
columns: type.columns,
count: type.count,
name: type.name,
objectId,
};
}

recordsAdded(recordsReceived: any[]) {
let records = recordsReceived.map((record) => this.wrapRecord(record));
this.sendMessage('recordsAdded', { records });
}

recordsUpdated(recordsReceived: any[]) {
let records = recordsReceived.map((record) => this.wrapRecord(record));
this.sendMessage('recordsUpdated', { records });
}

recordsRemoved(index: number, count: number) {
this.sendMessage('recordsRemoved', { index, count });
}

wrapRecord(record: any) {
const objectId = guidFor(record.object);
let columnValues: Record<string, any> = {};
let searchKeywords: any[] = [];
this.sentRecords[objectId] = record;
// make objects clonable
for (let i in record.columnValues) {
columnValues[i] = this.objectInspector.inspect(record.columnValues[i]);
}
// make sure keywords can be searched and clonable
searchKeywords = record.searchKeywords.filter(
(keyword: any) => typeof keyword === 'string' || typeof keyword === 'number'
);
return {
columnValues,
searchKeywords,
filterValues: record.filterValues,
color: record.color,
objectId,
};
}

releaseTypes() {
if (this.releaseTypesMethod) {
this.releaseTypesMethod();
this.releaseTypesMethod = null;
this.sentTypes = {};
}
}

releaseRecords() {
if (this.releaseRecordsMethod) {
this.releaseRecordsMethod();
this.releaseRecordsMethod = null;
this.sentRecords = {};
}
}

willDestroy() {
super.willDestroy();
this.releaseRecords();
this.releaseTypes();
}

static {
this.prototype.portNamespace = 'data';
this.prototype.messages = {
checkAdapter(this: DataDebug) {
this.sendMessage('hasAdapter', { hasAdapter: Boolean(this.adapter) });
},

getModelTypes(this: DataDebug) {
this.modelTypesAdded([]);
this.releaseTypes();
this.releaseTypesMethod = this.adapter.watchModelTypes(
(types: any) => {
this.modelTypesAdded(types);
},
(types: any) => {
this.modelTypesUpdated(types);
}
);
},

releaseModelTypes(this: DataDebug) {
this.releaseTypes();
},

getRecords(this: DataDebug, message: any) {
const type = this.sentTypes[message.objectId];
this.releaseRecords();

let typeOrName;
if (this.adapter.acceptsModelName) {
// Ember >= 1.3
typeOrName = type.name;
}

this.recordsAdded([]);
let releaseMethod = this.adapter.watchRecords(
typeOrName,
(recordsReceived: any) => {
this.recordsAdded(recordsReceived);
},
(recordsUpdated: any) => {
this.recordsUpdated(recordsUpdated);
},
(index: number, count: number) => {
this.recordsRemoved(index, count);
}
);
this.releaseRecordsMethod = releaseMethod;
},

releaseRecords(this: DataDebug) {
this.releaseRecords();
},

inspectModel(this: DataDebug, message: any) {
this.objectInspector.sendObject(this.sentRecords[message.objectId].object);
},

getFilters(this: DataDebug) {
this.sendMessage('filters', {
filters: this.adapter.getFilters(),
});
},
};
}
}
48 changes: 48 additions & 0 deletions packages/@ember/debug/ember-inspector-support/debug-port.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import BaseObject from '@ember/debug/ember-inspector-support/utils/base-object';

Check failure on line 1 in packages/@ember/debug/ember-inspector-support/debug-port.ts

View workflow job for this annotation

GitHub Actions / Linting

Unable to resolve path to module '@ember/debug/ember-inspector-support/utils/base-object'

export default class DebugPort extends BaseObject {
declare port: any;
declare portNamespace: string;
declare messages: Record<string, Function>;
constructor(data: any) {
super(data);
if (!data) {
throw new Error('need to pass data');
}
this.port = this.namespace?.port;
this.setupOrRemovePortListeners('on');
}

willDestroy() {
super.willDestroy();
this.setupOrRemovePortListeners('off');
}

sendMessage(name: string, message?: any) {
if (this.isDestroyed) return;
this.port.send(this.messageName(name), message);
}

messageName(name: string) {
let messageName = name;
if (this.portNamespace) {
messageName = `${this.portNamespace}:${messageName}`;
}
return messageName;
}

/**
* Setup or tear down port listeners. Call on `init` and `willDestroy`
* @param {String} onOrOff 'on' or 'off' the functions to call i.e. port.on or port.off for adding or removing listeners
*/
setupOrRemovePortListeners(onOrOff: 'on' | 'off') {
let port = this.port;
let messages = this.messages;

for (let name in messages) {
if (Object.prototype.hasOwnProperty.call(messages, name)) {
port[onOrOff](this.messageName(name), this, messages[name]);
}
}
}
}
Loading
Loading