bookManager.js 6.6 KB

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