|
| 1 | +type Listener = (...args: any[]) => void |
| 2 | + |
| 3 | +class EventEmitterLike { |
| 4 | + private listeners: Record<string, Listener[]> = {} |
| 5 | + |
| 6 | + on (event: string, listener: Listener): void { |
| 7 | + const list = this.listeners[event] || [] |
| 8 | + list.push(listener) |
| 9 | + this.listeners[event] = list |
| 10 | + } |
| 11 | + |
| 12 | + off (event: string, listener: Listener): void { |
| 13 | + const list = this.listeners[event] || [] |
| 14 | + this.listeners[event] = list.filter(item => item !== listener) |
| 15 | + } |
| 16 | + |
| 17 | + emit (event: string, ...args: any[]): void { |
| 18 | + const list = this.listeners[event] || [] |
| 19 | + list.forEach(listener => listener(...args)) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +export class Session { |
| 24 | + info: { webId?: string, isLoggedIn: boolean } = { isLoggedIn: false } |
| 25 | + webId?: string |
| 26 | + isActive = false |
| 27 | + events = new EventEmitterLike() |
| 28 | + |
| 29 | + private eventTarget = new EventTarget() |
| 30 | + |
| 31 | + addEventListener (type: string, listener: EventListenerOrEventListenerObject | null): void { |
| 32 | + if (!listener) return |
| 33 | + this.eventTarget.addEventListener(type, listener) |
| 34 | + } |
| 35 | + |
| 36 | + removeEventListener (type: string, listener: EventListenerOrEventListenerObject | null): void { |
| 37 | + if (!listener) return |
| 38 | + this.eventTarget.removeEventListener(type, listener) |
| 39 | + } |
| 40 | + |
| 41 | + dispatchEvent (event: Event): boolean { |
| 42 | + return this.eventTarget.dispatchEvent(event) |
| 43 | + } |
| 44 | + |
| 45 | + async handleIncomingRedirect (): Promise<void> { |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + async handleRedirectFromLogin (): Promise<void> { |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + async restore (): Promise<void> { |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + async login (_idp?: string, _redirectUri?: string): Promise<void> { |
| 58 | + } |
| 59 | + |
| 60 | + async logout (): Promise<void> { |
| 61 | + this.info = { isLoggedIn: false } |
| 62 | + this.webId = undefined |
| 63 | + this.isActive = false |
| 64 | + } |
| 65 | + |
| 66 | + fetch (input: RequestInfo | URL, init?: RequestInit): Promise<Response> { |
| 67 | + return globalThis.fetch(input, init) |
| 68 | + } |
| 69 | + |
| 70 | + authFetch (input: RequestInfo | URL, init?: RequestInit): Promise<Response> { |
| 71 | + return globalThis.fetch(input, init) |
| 72 | + } |
| 73 | +} |
0 commit comments