bookManager.js 8.4 KB

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