bookManager.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import localForage from 'localforage';
  2. import * as utils from '../../../share/utils';
  3. import BookParser from './BookParser';
  4. const maxDataSize = 100*1024*1024;//chars, not bytes
  5. const bmMetaStore = localForage.createInstance({
  6. name: 'bmMetaStore'
  7. });
  8. const bmDataStore = localForage.createInstance({
  9. name: 'bmDataStore'
  10. });
  11. class BookManager {
  12. async init() {
  13. this.books = {};
  14. const len = await bmMetaStore.length();
  15. for (let i = 0; i < len; i++) {
  16. const key = await bmMetaStore.key(i);
  17. const keySplit = key.split('-');
  18. if (keySplit.length == 2 && keySplit[0] == 'bmMeta') {
  19. let meta = await bmMetaStore.getItem(key);
  20. this.books[meta.key] = meta;
  21. }
  22. }
  23. await this.cleanBooks();
  24. }
  25. async cleanBooks() {
  26. while (1) {// eslint-disable-line no-constant-condition
  27. let size = 0;
  28. let min = Date.now();
  29. let toDel = null;
  30. for (let key in this.books) {
  31. let book = this.books[key];
  32. size += (book.length ? book.length : 0);
  33. if (book.addTime < min) {
  34. toDel = book;
  35. min = book.addTime;
  36. }
  37. }
  38. if (size > maxDataSize && toDel) {
  39. await this.delBook(toDel);
  40. } else {
  41. break;
  42. }
  43. }
  44. }
  45. async addBook(newBook, callback) {
  46. if (!this.books)
  47. await this.init();
  48. let meta = {url: newBook.url, path: newBook.path};
  49. meta.key = this.keyFromUrl(meta.url);
  50. meta.addTime = Date.now();
  51. const result = await this.parseBook(meta, newBook.data, callback);
  52. this.books[meta.key] = result;
  53. await bmMetaStore.setItem(`bmMeta-${meta.key}`, this.metaOnly(result));
  54. await bmDataStore.setItem(`bmData-${meta.key}`, result.data);
  55. return result;
  56. }
  57. hasBookParsed(meta) {
  58. if (!this.books)
  59. return false;
  60. if (!meta.url)
  61. return false;
  62. if (!meta.key)
  63. meta.key = this.keyFromUrl(meta.url);
  64. let book = this.books[meta.key];
  65. return (book && book.parsed);
  66. }
  67. async getBook(meta, callback) {
  68. if (!this.books)
  69. await this.init();
  70. let result = undefined;
  71. if (!meta.key)
  72. meta.key = this.keyFromUrl(meta.url);
  73. result = this.books[meta.key];
  74. if (result && !result.data) {
  75. result.data = await bmDataStore.getItem(`bmData-${meta.key}`);
  76. this.books[meta.key] = result;
  77. }
  78. if (result && !result.parsed) {
  79. result = await this.parseBook(result, result.data, callback);
  80. this.books[meta.key] = result;
  81. }
  82. return result;
  83. }
  84. async delBook(meta) {
  85. if (!this.books)
  86. await this.init();
  87. await bmMetaStore.removeItem(`bmMeta-${meta.key}`);
  88. await bmDataStore.removeItem(`bmData-${meta.key}`);
  89. delete this.books[meta.key];
  90. }
  91. async parseBook(meta, data, callback) {
  92. if (!this.books)
  93. await this.init();
  94. const parsed = new BookParser();
  95. const parsedMeta = await parsed.parse(data, callback);
  96. const result = Object.assign({}, meta, parsedMeta, {
  97. length: data.length,
  98. textLength: parsed.textLength,
  99. data,
  100. parsed
  101. });
  102. return result;
  103. }
  104. metaOnly(book) {
  105. let result = Object.assign({}, book);
  106. delete result.data;
  107. delete result.parsed;
  108. return result;
  109. }
  110. keyFromUrl(url) {
  111. return utils.stringToHex(url);
  112. }
  113. }
  114. export default new BookManager();