import { signal, type Signal } from '@angular/core'; // Общая часть сервисов разрешений всех Expo-пакетов, наследник передаёт пару методов // Сбой автозапроса попадает в `error`, а не в unhandled rejection: null-статус с непустым `error` // значит "запрос упал", а не "ещё не было" export abstract class PermissionsServiceBase { private readonly status = signal(null); private readonly fetchError = signal(null); private isAutoFetchStarted = false; readonly error: Signal = this.fetchError.asReadonly(); protected constructor( private readonly getMethod: (options?: TOptions) => Promise, private readonly requestMethod: ( options?: TOptions, ) => Promise, ) {} // Защёлка, а не проверка `status() === null`: упавший запрос оставляет null навсегда connect(): Signal { if (!this.isAutoFetchStarted) { this.isAutoFetchStarted = true; this.get().catch((cause: unknown) => { this.fetchError.set( cause instanceof Error ? cause : new Error(String(cause)), ); }); } return this.status.asReadonly(); } async get(options?: TOptions): Promise { return this.store(await this.getMethod(options)); } async request(options?: TOptions): Promise { return this.store(await this.requestMethod(options)); } private store(response: TPermission): TPermission { this.status.set(response); this.fetchError.set(null); return response; } }