history.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { openDB, IndexNames, StoreNames, StoreValue } from "idb";
  2. import getDb, { NovelDb } from "./getDb";
  3. export async function putItem<N extends StoreNames<NovelDb>>(
  4. storeNames: N,
  5. item: StoreValue<NovelDb, N>
  6. ) {
  7. const db = await getDb();
  8. const tx = db.transaction(storeNames, "readwrite");
  9. const store = tx.store;
  10. store.put(item);
  11. return tx.done;
  12. }
  13. export async function delItem<N extends StoreNames<NovelDb>>(
  14. storeNames: N,
  15. key: NovelDb[N]["key"]
  16. ) {
  17. const db = await getDb();
  18. const tx = db.transaction(storeNames, "readwrite");
  19. const store = tx.store;
  20. store.delete(key);
  21. return tx.done;
  22. }
  23. export async function getItem<N extends StoreNames<NovelDb>>(
  24. storeNames: N,
  25. key: NovelDb[N]["key"]
  26. ) {
  27. const db = await getDb();
  28. const tx = db.transaction(storeNames, "readonly");
  29. const store = tx.store;
  30. return store.get(key);
  31. }
  32. export const putHistoryItem = (item: HistoryItem) => putItem("history", item);
  33. export const delHistoryItem = (key: string) => delItem("history", key);
  34. export const getHistoryItem = (key: string) => getItem("history", key);
  35. export async function getList<
  36. N extends StoreNames<NovelDb>,
  37. R extends [any, any, boolean?, boolean?]
  38. >(
  39. storeName: N,
  40. index?: IndexNames<NovelDb, N>,
  41. page = 0,
  42. pageSize = 20,
  43. direction: IDBCursorDirection = "next",
  44. rage?: R
  45. ): Promise<PageList<NovelDb[N]["value"]>> {
  46. const db = await getDb();
  47. const tx = db.transaction(storeName, "readonly");
  48. const store = tx.store;
  49. const keyRangeValue = rage ? IDBKeyRange.bound.apply(void 0, rage) : null;
  50. let [cursor, total] = await Promise.all([
  51. index
  52. ? store.index(index).openCursor(keyRangeValue, direction)
  53. : store.openCursor(),
  54. store.count(),
  55. ]);
  56. type ValueItem = StoreValue<NovelDb, N>;
  57. const list: ValueItem[] = [];
  58. const res: PageList<ValueItem> = {
  59. list,
  60. total,
  61. page,
  62. pageSize,
  63. };
  64. if (!cursor) return res;
  65. if (page > 0) {
  66. cursor = await cursor.advance(page * pageSize);
  67. }
  68. while (cursor) {
  69. list.push(cursor.value);
  70. if (pageSize === list.length) {
  71. break;
  72. }
  73. cursor = await cursor.continue();
  74. }
  75. return res;
  76. }
  77. function genGetList<
  78. N extends StoreNames<NovelDb>,
  79. R extends [any, any, boolean?, boolean?]
  80. >(
  81. storeName: N,
  82. index?: IndexNames<NovelDb, N>,
  83. direction: IDBCursorDirection = "next",
  84. defaultRange?: R
  85. ) {
  86. return async function (
  87. page = 0,
  88. pageSize = 20,
  89. rage = defaultRange
  90. ): Promise<PageList<NovelDb[N]["value"]>> {
  91. return getList(storeName, index, page, pageSize, direction, rage);
  92. };
  93. }
  94. export const getHistoryList = genGetList(
  95. "history",
  96. "historyIsReading",
  97. "prev",
  98. [
  99. [1, 0],
  100. [1, new Date()],
  101. ]
  102. );
  103. export const getFavoriteList = genGetList(
  104. "history",
  105. "historyIsFavorite",
  106. "prev",
  107. [1, 1]
  108. );