angular/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/file-api-service.ts
Kristiyan Kostadinov e301471342 fix(devtools): fix type checking issues (#60481)
Fixes type checking issues in the dev tools that weren't showing up, because we had `strictTemplates` turned off.

PR Close #60481
2025-03-20 11:55:52 -07:00

41 lines
1.2 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.dev/license
*/
import {Injectable} from '@angular/core';
import {Subject} from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class FileApiService {
uploadedData: Subject<any> = new Subject();
publishFileUpload(parentEvent: Event): void {
const reader = new FileReader();
reader.onload = (event) => {
try {
this.uploadedData.next(JSON.parse((event.target as any).result));
} catch (e) {
this.uploadedData.next({error: e});
}
(parentEvent.target as any).value = '';
};
reader.readAsText((parentEvent.target as any).files[0]);
}
saveObjectAsJSON(object: object): void {
const downloadLink = document.createElement('a');
const isoString = new Date().toISOString().slice(0, -5); // remove milliseconds
downloadLink.download = `NgDevTools-Profile-${isoString}.json`;
downloadLink.href = URL.createObjectURL(
new Blob([JSON.stringify(object)], {type: 'application/json'}),
);
downloadLink.click();
setTimeout(() => URL.revokeObjectURL(downloadLink.href));
}
}