bookManager.js 7.6 KB

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