bookManager.js 8.3 KB

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