bookManager.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import localForage from 'localforage';
  2. import * as utils from '../../../share/utils';
  3. import BookParser from './BookParser';
  4. const maxDataSize = 500*1024*1024;//chars, not bytes
  5. const bmMetaStore = localForage.createInstance({
  6. name: 'bmMetaStore'
  7. });
  8. const bmDataStore = localForage.createInstance({
  9. name: 'bmDataStore'
  10. });
  11. const bmRecentStore = localForage.createInstance({
  12. name: 'bmRecentStore'
  13. });
  14. class BookManager {
  15. async init() {
  16. this.books = {};
  17. this.recent = {};
  18. this.recentChanged = true;
  19. let len = await bmMetaStore.length();
  20. for (let i = 0; i < len; i++) {
  21. const key = await bmMetaStore.key(i);
  22. const keySplit = key.split('-');
  23. if (keySplit.length == 2 && keySplit[0] == 'bmMeta') {
  24. let meta = await bmMetaStore.getItem(key);
  25. this.books[meta.key] = meta;
  26. }
  27. }
  28. len = await bmRecentStore.length();
  29. for (let i = 0; i < len; i++) {
  30. const key = await bmRecentStore.key(i);
  31. let r = await bmRecentStore.getItem(key);
  32. this.recent[r.key] = r;
  33. }
  34. await this.cleanBooks();
  35. }
  36. async cleanBooks() {
  37. while (1) {// eslint-disable-line no-constant-condition
  38. let size = 0;
  39. let min = Date.now();
  40. let toDel = null;
  41. for (let key in this.books) {
  42. let book = this.books[key];
  43. size += (book.length ? book.length : 0);
  44. if (book.addTime < min) {
  45. toDel = book;
  46. min = book.addTime;
  47. }
  48. }
  49. if (size > maxDataSize && toDel) {
  50. await this.delBook(toDel);
  51. } else {
  52. break;
  53. }
  54. }
  55. }
  56. async addBook(newBook, callback) {
  57. if (!this.books)
  58. await this.init();
  59. let meta = {url: newBook.url, path: newBook.path};
  60. meta.key = this.keyFromUrl(meta.url);
  61. meta.addTime = Date.now();
  62. const result = await this.parseBook(meta, newBook.data, callback);
  63. this.books[meta.key] = result;
  64. await bmMetaStore.setItem(`bmMeta-${meta.key}`, this.metaOnly(result));
  65. await bmDataStore.setItem(`bmData-${meta.key}`, result.data);
  66. return result;
  67. }
  68. hasBookParsed(meta) {
  69. if (!this.books)
  70. return false;
  71. if (!meta.url)
  72. return false;
  73. if (!meta.key)
  74. meta.key = this.keyFromUrl(meta.url);
  75. let book = this.books[meta.key];
  76. return !!(book && book.parsed);
  77. }
  78. async getBook(meta, callback) {
  79. if (!this.books)
  80. await this.init();
  81. let result = undefined;
  82. if (!meta.key)
  83. meta.key = this.keyFromUrl(meta.url);
  84. result = this.books[meta.key];
  85. if (result && !result.data) {
  86. result.data = await bmDataStore.getItem(`bmData-${meta.key}`);
  87. this.books[meta.key] = result;
  88. }
  89. if (result && !result.parsed) {
  90. result = await this.parseBook(result, result.data, callback);
  91. this.books[meta.key] = result;
  92. }
  93. return result;
  94. }
  95. async delBook(meta) {
  96. if (!this.books)
  97. await this.init();
  98. await bmMetaStore.removeItem(`bmMeta-${meta.key}`);
  99. await bmDataStore.removeItem(`bmData-${meta.key}`);
  100. delete this.books[meta.key];
  101. }
  102. async parseBook(meta, data, callback) {
  103. if (!this.books)
  104. await this.init();
  105. const parsed = new BookParser();
  106. const parsedMeta = await parsed.parse(data, callback);
  107. const result = Object.assign({}, meta, parsedMeta, {
  108. length: data.length,
  109. textLength: parsed.textLength,
  110. data,
  111. parsed
  112. });
  113. return result;
  114. }
  115. metaOnly(book) {
  116. let result = Object.assign({}, book);
  117. delete result.data;
  118. delete result.parsed;
  119. return result;
  120. }
  121. keyFromUrl(url) {
  122. return utils.stringToHex(url);
  123. }
  124. async setRecentBook(value, noTouch) {
  125. if (!this.recent)
  126. await this.init();
  127. const result = Object.assign({}, value);
  128. if (!noTouch)
  129. Object.assign(result, {touchTime: Date.now()});
  130. if (result.textLength && !result.bookPos && result.bookPosPercent)
  131. result.bookPos = Math.round(result.bookPosPercent*result.textLength);
  132. this.recent[result.key] = result;
  133. await bmRecentStore.setItem(result.key, result);
  134. await this.cleanRecentBooks();
  135. this.recentChanged = true;
  136. return result;
  137. }
  138. async getRecentBook(value) {
  139. if (!this.recent)
  140. await this.init();
  141. return this.recent[value.key];
  142. }
  143. async delRecentBook(value) {
  144. if (!this.recent)
  145. await this.init();
  146. await bmRecentStore.removeItem(value.key);
  147. delete this.recent[value.key];
  148. this.recentChanged = true;
  149. }
  150. async cleanRecentBooks() {
  151. if (!this.recent)
  152. await this.init();
  153. if (Object.keys(this.recent).length > 100) {
  154. let min = Date.now();
  155. let found = null;
  156. for (let key in this.recent) {
  157. const book = this.recent[key];
  158. if (book.touchTime < min) {
  159. min = book.touchTime;
  160. found = book;
  161. }
  162. }
  163. if (found) {
  164. await this.delRecentBook(found);
  165. await this.cleanRecentBooks();
  166. }
  167. }
  168. }
  169. mostRecentBook() {
  170. if (!this.recentChanged && this.mostRecentCached) {
  171. return this.mostRecentCached;
  172. }
  173. let max = 0;
  174. let result = null;
  175. for (let key in this.recent) {
  176. const book = this.recent[key];
  177. if (book.touchTime > max) {
  178. max = book.touchTime;
  179. result = book;
  180. }
  181. }
  182. this.mostRecentCached = result;
  183. this.recentChanged = false;
  184. return result;
  185. }
  186. }
  187. export default new BookManager();