layout-helpers.ts 962 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import type { Accessor } from "solid-js"
  2. export function ensureSessionKey(key: string, touch: (key: string) => void, seed: (key: string) => void) {
  3. touch(key)
  4. seed(key)
  5. return key
  6. }
  7. export function createSessionKeyReader(sessionKey: string | Accessor<string>, ensure: (key: string) => void) {
  8. const key = typeof sessionKey === "function" ? sessionKey : () => sessionKey
  9. return () => {
  10. const value = key()
  11. ensure(value)
  12. return value
  13. }
  14. }
  15. export function pruneSessionKeys(input: {
  16. keep?: string
  17. max: number
  18. used: Map<string, number>
  19. view: string[]
  20. tabs: string[]
  21. }) {
  22. if (!input.keep) return []
  23. const keys = new Set<string>([...input.view, ...input.tabs])
  24. if (keys.size <= input.max) return []
  25. const score = (key: string) => {
  26. if (key === input.keep) return Number.MAX_SAFE_INTEGER
  27. return input.used.get(key) ?? 0
  28. }
  29. return Array.from(keys)
  30. .sort((a, b) => score(b) - score(a))
  31. .slice(input.max)
  32. }