Skip to content

Commit

Permalink
(fix): ensure new chapter list is merged properly
Browse files Browse the repository at this point in the history
- previously, the new chapter list would just overwrite the old,
  persisted one
  - but the read/unread & new tracking meant the two lists had to be
    merged, and the previous merge logic was causing bugs that are
    fixed here

- for any manga that were persisted, new chapters wouldn't appear
  in the chapter list
  - it would only show the ones that were persisted
  - this is apparently bc MST was swallowing an error when a node is
    used more than once in the tree
    - instead of updating each entry in the chapter list as we go,
      just update the whole list at once with tne new one
      - and do merging out of tree before updating

- fix the incorrect logic that merged via indices
  - for one thing, chapters are in reverse chronological order, so when
    a new chapter is released old[0] will be equal to new[1], as new[0]
    is the new chapter
    - the previous logic merged old[0] with new[0], which is super
      broken (woops)
  - instead merge via `link`, which is the MST identifier of a chapter
    and therefore must be unique
    - currently assuming that the chapters are still ordered the same
      way, so we don't have to search the entire list to see if the
      chapter exists and instead move an offset as needed
      - this is somewhat of an over-optimization, and it falls
        relatively hard if a chapter were released in between
        old chapters like a 0.5, an uncensored, diff translation, etc
        - TODO: optimal fix is to have a map of chapters, update the
          map entries, and then the list is just the ordering of refs
          - would be somewhat similar to how mangas are stored
          - this breaks the stored model data though, so it requires
            a migration, and those aren't yet implemented in
            mst-persist, and nor are transforms to workaround the
            lack of it
  • Loading branch information
agilgur5 committed Nov 22, 2019
1 parent b4c712e commit 8d568fc
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions models/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,24 @@ const Manga = types.model('Manga', {
}).actions((self) => ({
loadChapters: flow(function * loadChapters () {
const { chapters, tags, summary } = yield getChapters(self.link)
if (self.chapters.length === 0) {
self.chapters = chapters // just an optimization compared to below
} else {
// merge chapter data if some already persisted
chapters.forEach((chapter, index) => {
self.chapters[index] = { ...self.chapters[index], ...chapter }
})

// merge chapter data if some already persisted
let offset = 0
for (let index = 0; index < self.chapters.length; index++) {
const oldChap = self.chapters[index]
// new set of chapters might be a little ahead, get the offset
for ( ; index + offset < chapters.length; offset++) {
// found the offset, break out
if (oldChap.link === chapters[index + offset].link) break
}
// if no matches, break out
if (index + offset >= chapters.length) break

// merge into the new list with new overriding old
chapters[index + offset] = {...oldChap, ...chapters[index + offset]}
}

self.chapters = chapters // set to the new, merged list
self.tags = tags
self.summary = summary
})
Expand Down

0 comments on commit 8d568fc

Please sign in to comment.