|
1 | | -const elements = new Map<string, HTMLElement>() |
2 | | -const swipingIds = new Set<string>() |
3 | | -const listeners = new Set<() => void>() |
| 1 | +class DrawerRegistry { |
| 2 | + private readonly elements = new Map<string, HTMLElement>() |
| 3 | + private readonly swipingIds = new Set<string>() |
| 4 | + private readonly swipeProgress = new Map<string, number>() |
| 5 | + private readonly listeners = new Set<() => void>() |
4 | 6 |
|
5 | | -function notify() { |
6 | | - listeners.forEach((fn) => fn()) |
7 | | -} |
| 7 | + notify() { |
| 8 | + this.listeners.forEach((fn) => fn()) |
| 9 | + } |
8 | 10 |
|
9 | | -export const drawerRegistry = { |
10 | 11 | register(id: string, el: HTMLElement) { |
11 | | - elements.set(id, el) |
12 | | - notify() |
13 | | - }, |
| 12 | + this.elements.set(id, el) |
| 13 | + this.notify() |
| 14 | + } |
| 15 | + |
14 | 16 | unregister(id: string) { |
15 | | - swipingIds.delete(id) |
16 | | - if (!elements.delete(id)) return |
17 | | - notify() |
18 | | - }, |
| 17 | + this.swipingIds.delete(id) |
| 18 | + this.swipeProgress.delete(id) |
| 19 | + if (!this.elements.delete(id)) return |
| 20 | + this.notify() |
| 21 | + } |
| 22 | + |
19 | 23 | setSwiping(id: string, swiping: boolean) { |
20 | | - const changed = swiping ? !swipingIds.has(id) : swipingIds.has(id) |
21 | | - if (!changed) return |
22 | | - if (swiping) swipingIds.add(id) |
23 | | - else swipingIds.delete(id) |
24 | | - notify() |
25 | | - }, |
| 24 | + const changed = swiping ? !this.swipingIds.has(id) : this.swipingIds.has(id) |
| 25 | + if (!changed && swiping) return |
| 26 | + |
| 27 | + if (swiping) { |
| 28 | + this.swipingIds.add(id) |
| 29 | + } else { |
| 30 | + this.swipingIds.delete(id) |
| 31 | + this.swipeProgress.delete(id) |
| 32 | + } |
| 33 | + |
| 34 | + this.notify() |
| 35 | + } |
| 36 | + |
| 37 | + setSwipeProgress(id: string, progress: number) { |
| 38 | + this.swipeProgress.set(id, progress) |
| 39 | + this.notify() |
| 40 | + } |
| 41 | + |
| 42 | + getSwipeProgressAfter(id: string): number { |
| 43 | + const keys = [...this.elements.keys()] |
| 44 | + const myIndex = keys.indexOf(id) |
| 45 | + if (myIndex === -1) return 0 |
| 46 | + |
| 47 | + for (let i = keys.length - 1; i > myIndex; i -= 1) { |
| 48 | + if (this.swipingIds.has(keys[i])) { |
| 49 | + return this.swipeProgress.get(keys[i]) ?? 0 |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return 0 |
| 54 | + } |
| 55 | + |
26 | 56 | hasSwipingAfter(id: string) { |
27 | | - const keys = [...elements.keys()] |
| 57 | + const keys = [...this.elements.keys()] |
28 | 58 | const myIndex = keys.indexOf(id) |
29 | 59 | if (myIndex === -1) return false |
30 | | - return keys.slice(myIndex + 1).some((key) => swipingIds.has(key)) |
31 | | - }, |
32 | | - notify, |
| 60 | + return keys.slice(myIndex + 1).some((key) => this.swipingIds.has(key)) |
| 61 | + } |
| 62 | + |
33 | 63 | getEntries() { |
34 | | - return elements |
35 | | - }, |
| 64 | + return this.elements |
| 65 | + } |
| 66 | + |
36 | 67 | subscribe(fn: () => void) { |
37 | | - listeners.add(fn) |
| 68 | + this.listeners.add(fn) |
38 | 69 | return () => { |
39 | | - listeners.delete(fn) |
| 70 | + this.listeners.delete(fn) |
40 | 71 | } |
41 | | - }, |
| 72 | + } |
42 | 73 | } |
| 74 | + |
| 75 | +export const drawerRegistry = new DrawerRegistry() |
0 commit comments