bookManager.js 5.9 KB

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