bookManager.js 6.2 KB

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