bookManager.js 7.8 KB

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