bookManager.js 7.5 KB

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