actions.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* An `action` is a unique verb that is associated with certain thing that can be done on Hoppscotch.
  2. * For example, sending a request.
  3. */
  4. import { onBeforeUnmount, onMounted } from "@nuxtjs/composition-api"
  5. import { BehaviorSubject } from "rxjs"
  6. export type HoppAction =
  7. | "request.send-cancel" // Send/Cancel a Hoppscotch Request
  8. | "request.reset" // Clear request data
  9. | "request.copy-link" // Copy Request Link
  10. | "request.save" // Save to Collections
  11. | "request.save-as" // Save As
  12. | "request.method.next" // Select Next Method
  13. | "request.method.prev" // Select Previous Method
  14. | "request.method.get" // Select GET Method
  15. | "request.method.head" // Select HEAD Method
  16. | "request.method.post" // Select POST Method
  17. | "request.method.put" // Select PUT Method
  18. | "request.method.delete" // Select DELETE Method
  19. | "flyouts.keybinds.toggle" // Shows the keybinds flyout
  20. | "modals.search.toggle" // Shows the search modal
  21. | "modals.support.toggle" // Shows the support modal
  22. | "modals.share.toggle" // Shows the share modal
  23. | "navigation.jump.rest" // Jump to REST page
  24. | "navigation.jump.graphql" // Jump to GraphQL page
  25. | "navigation.jump.realtime" // Jump to realtime page
  26. | "navigation.jump.documentation" // Jump to documentation page
  27. | "navigation.jump.settings" // Jump to settings page
  28. | "navigation.jump.profile" // Jump to profile page
  29. | "settings.theme.system" // Use system theme
  30. | "settings.theme.light" // Use light theme
  31. | "settings.theme.dark" // Use dark theme
  32. | "settings.theme.black" // Use black theme
  33. type BoundActionList = {
  34. // eslint-disable-next-line no-unused-vars
  35. [_ in HoppAction]?: Array<() => void>
  36. }
  37. const boundActions: BoundActionList = {}
  38. export const activeActions$ = new BehaviorSubject<HoppAction[]>([])
  39. export function bindAction(action: HoppAction, handler: () => void) {
  40. if (boundActions[action]) {
  41. boundActions[action]?.push(handler)
  42. } else {
  43. boundActions[action] = [handler]
  44. }
  45. activeActions$.next(Object.keys(boundActions) as HoppAction[])
  46. }
  47. export function invokeAction(action: HoppAction) {
  48. boundActions[action]?.forEach((handler) => handler())
  49. }
  50. export function unbindAction(action: HoppAction, handler: () => void) {
  51. boundActions[action] = boundActions[action]?.filter((x) => x !== handler)
  52. if (boundActions[action]?.length === 0) {
  53. delete boundActions[action]
  54. }
  55. activeActions$.next(Object.keys(boundActions) as HoppAction[])
  56. }
  57. export function defineActionHandler(action: HoppAction, handler: () => void) {
  58. onMounted(() => {
  59. bindAction(action, handler)
  60. })
  61. onBeforeUnmount(() => {
  62. unbindAction(action, handler)
  63. })
  64. }