HoppExtension.ts 919 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { distinctUntilChanged, pluck } from "rxjs"
  2. import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
  3. export type ExtensionStatus = "available" | "unknown-origin" | "waiting"
  4. type InitialState = {
  5. extensionStatus: ExtensionStatus
  6. }
  7. const initialState: InitialState = {
  8. extensionStatus: "waiting",
  9. }
  10. const dispatchers = defineDispatchers({
  11. changeExtensionStatus(
  12. _,
  13. { extensionStatus }: { extensionStatus: ExtensionStatus }
  14. ) {
  15. return {
  16. extensionStatus,
  17. }
  18. },
  19. })
  20. export const hoppExtensionStore = new DispatchingStore(
  21. initialState,
  22. dispatchers
  23. )
  24. export const extensionStatus$ = hoppExtensionStore.subject$.pipe(
  25. pluck("extensionStatus"),
  26. distinctUntilChanged()
  27. )
  28. export function changeExtensionStatus(extensionStatus: ExtensionStatus) {
  29. hoppExtensionStore.dispatch({
  30. dispatcher: "changeExtensionStatus",
  31. payload: { extensionStatus },
  32. })
  33. }