Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

818 B
Raw Blame
import { useSyncExternalStore } from "react";

import { emptyFormatState, type EditorFormatState } from "./editor-commands";

export type FormatStore = {
  get: () => EditorFormatState;
  set: (state: EditorFormatState) => void;
  subscribe: (listener: () => void) => () => void;
};

export function createFormatStore(): FormatStore {
  let state = emptyFormatState;
  const listeners = new Set<() => void>();
  return {
    get: () => state,
    set: (next) => {
      state = next;
      listeners.forEach((listener) => listener());
    },
    subscribe: (listener) => {
      listeners.add(listener);
      return () => {
        listeners.delete(listener);
      };
    },
  };
}

export function useFormatState(store: FormatStore): EditorFormatState {
  return useSyncExternalStore(store.subscribe, store.get);
}