import type { Nullable } from "@libs/typing"
import { type _Permissions, type _PermissionsStatus, dispatch, illegal, internal } from "./_.ts"
export class Permissions implements _Permissions {
constructor(_?: { [internal]?: boolean }) {
illegal(arguments)
}
query({ name }: PermissionDescriptor): Promise<PermissionsStatus> {
if (!name) {
return Promise.reject(new TypeError(`'${name}' (value of 'name' member of PermissionDescriptor) is not a valid value for enumeration PermissionName.`))
}
const state = this.#state[name] ?? "denied"
return Promise.resolve(new PermissionsStatus({ [internal]: true, name, state }))
}
get [internal](): { state: Record<string, PermissionState> } {
return {
state: this.#state,
}
}
readonly #state = {} as Record<string, PermissionState>
}
export class PermissionsStatus extends EventTarget implements _PermissionsStatus {
constructor({ name, state } = {} as { [internal]?: boolean; name?: PermissionsStatus["name"]; state?: PermissionState }) {
illegal(arguments)
super()
this.#name = name!
this.#state = state!
}
get name(): string {
return this.#name
}
set name(_: string) {
return
}
readonly #name
get state(): PermissionState {
return this.#state
}
set state(_: PermissionState) {
return
}
#state
get [internal](): { state: (state: PermissionsStatus["state"]) => void } {
return {
state: (state: PermissionsStatus["state"]) => {
const changed = this.#state !== state
this.#state = state
if (changed) {
dispatch(this, new Event("change"))
}
},
}
}
onchange = null as Nullable<(this: _PermissionsStatus, event: Event) => any>
}
|