| 88 | const commands = new Map<string, QuickAccess.CommandHandler>() |
| 89 | |
| 90 | export const createQuickAccessInstance = () => { |
| 91 | const activeCommandListeners: ((command: string) => void)[] = [] |
| 92 | let activeCommandName: string | null = null |
| 93 | return { |
| 94 | asyncFeature: Feature.create<QuickAccess.Result>(), |
| 95 | onActiveCommandChange(lis) { |
| 96 | activeCommandListeners.push(lis) |
| 97 | return () => { |
| 98 | const index = activeCommandListeners.indexOf(lis) |
| 99 | if (index !== -1) { |
| 100 | activeCommandListeners.splice(index, 1) |
| 101 | } |
| 102 | } |
| 103 | }, |
| 104 | run(command) { |
| 105 | activeCommandName = command |
| 106 | this.asyncFeature = this.asyncFeature.reset() |
| 107 | activeCommandListeners.forEach(lis => lis(command)) |
| 108 | return this.asyncFeature.promise |
| 109 | }, |
| 110 | get activeCommandHandler() { |
| 111 | return activeCommandName |
| 112 | ? commands.get(activeCommandName) |
| 113 | : undefined |
| 114 | }, |
| 115 | register(command, handler) { |
| 116 | commands.set(command, handler) |
| 117 | return () => { |
| 118 | commands.delete(command) |
| 119 | } |
| 120 | } |
| 121 | } as QuickAccessContextValue |
| 122 | } |
| 123 | |
| 124 | export const QuickAccessContext = createContext<QuickAccessContextValue>(null!) |
| 125 | |