bookManager.js 6.6 KB

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