angular/devtools/projects/ng-devtools-backend/src/lib/utils.ts
Matthieu Riegler e227275087 refactor(devtools): Add support for signals. (#53269)
The devtools now support signals.
Writable signals of primitives are editable.
Object Signal and other non-writable signals (like computed) are not editable.

Co-authored-by: Tomasz Ducin <tomasz.ducin@gmail.com>

PR Close #53269
2024-01-17 16:47:17 -08:00

72 lines
1.6 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
export const ngDebug = () => (window as any).ng;
export const runOutsideAngular = (f: () => any): void => {
const w = window as any;
if (!w.Zone || !w.Zone.current) {
f();
return;
}
if (w.Zone.current._name !== 'angular') {
w.Zone.current.run(f);
return;
}
const parent = w.Zone.current._parent;
if (parent && parent.run) {
parent.run(f);
return;
}
f();
};
export const isCustomElement = (node: Node) => {
if (typeof customElements === 'undefined') {
return false;
}
if (!(node instanceof HTMLElement)) {
return false;
}
const tagName = node.tagName.toLowerCase();
return !!customElements.get(tagName);
};
export function hasDiDebugAPIs(): boolean {
if (!ngDebugApiIsSupported('ɵgetInjectorResolutionPath')) {
return false;
}
if (!ngDebugApiIsSupported('ɵgetDependenciesFromInjectable')) {
return false;
}
if (!ngDebugApiIsSupported('ɵgetInjectorProviders')) {
return false;
}
if (!ngDebugApiIsSupported('ɵgetInjectorMetadata')) {
return false;
}
return true;
}
export function ngDebugApiIsSupported(api: string): boolean {
const ng = ngDebug();
return typeof ng[api] === 'function';
}
export function isSignal(prop: unknown): prop is() => unknown {
if (!ngDebugApiIsSupported('isSignal')) {
return false;
}
return (window as any).ng.isSignal(prop);
}
export function unwrapSignal(s: any): any {
return isSignal(s) ? s() : s;
}