Skip to content

Commit

Permalink
fix bug cache does not be updated
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Feb 26, 2017
1 parent 7c445a1 commit 3e8123c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 42 deletions.
8 changes: 8 additions & 0 deletions todoist/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import "fmt"

type IntBool bool

func (i IntBool) Bool() bool {
if i {
return true
} else {
return false
}
}

func (i IntBool) MarshalJSON() ([]byte, error) {
if i {
return []byte("1"), nil
Expand Down
28 changes: 16 additions & 12 deletions todoist/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,29 @@ func (c *filterCache) resolve(id ID) *Filter {
}

func (c *filterCache) store(filter Filter) {
old := c.resolve(filter.ID)
if old == nil {
if !filter.IsDeleted {
*c.cache = append(*c.cache, filter)
}
} else {
if filter.IsDeleted {
c.remove(filter)
var res []Filter
isNew := true
for _, f := range *c.cache {
if f.Equal(filter) {
if !filter.IsDeleted {
res = append(res, filter)
}
isNew = false
} else {
old = &filter
res = append(res, f)
}
}
if isNew && !filter.IsDeleted.Bool() {
res = append(res, filter)
}
c.cache = &res
}

func (c *filterCache) remove(filter Filter) {
var res []Filter
for _, p := range *c.cache {
if !p.Equal(filter) {
res = append(res, p)
for _, f := range *c.cache {
if !f.Equal(filter) {
res = append(res, f)
}
}
c.cache = &res
Expand Down
22 changes: 13 additions & 9 deletions todoist/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,20 +236,24 @@ func (c *itemCache) resolve(id ID) *Item {
}

func (c *itemCache) store(item Item) {
old := c.resolve(item.ID)
// sync api do not returns deleted items.
// so remove deleted items from cache too.
if old == nil {
if !item.IsDeleted {
*c.cache = append(*c.cache, item)
}
} else {
if item.IsDeleted {
c.remove(item)
var res []Item
isNew := true
for _, i := range *c.cache {
if i.Equal(item) {
if !item.IsDeleted {
res = append(res, item)
}
isNew = false
} else {
old = &item
res = append(res, i)
}
}
if isNew && !item.IsDeleted.Bool() {
res = append(res, item)
}
c.cache = &res
}

func (c *itemCache) remove(item Item) {
Expand Down
28 changes: 16 additions & 12 deletions todoist/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,29 @@ func (c *labelCache) resolve(id ID) *Label {
}

func (c *labelCache) store(label Label) {
old := c.resolve(label.ID)
if old == nil {
if !label.IsDeleted {
*c.cache = append(*c.cache, label)
}
} else {
if label.IsDeleted {
c.remove(label)
var res []Label
isNew := true
for _, l := range *c.cache {
if l.Equal(label) {
if !label.IsDeleted {
res = append(res, label)
}
isNew = false
} else {
old = &label
res = append(res, l)
}
}
if isNew && !label.IsDeleted.Bool() {
res = append(res, label)
}
c.cache = &res
}

func (c *labelCache) remove(label Label) {
var res []Label
for _, p := range *c.cache {
if !p.Equal(label) {
res = append(res, p)
for _, l := range *c.cache {
if !l.Equal(label) {
res = append(res, l)
}
}
c.cache = &res
Expand Down
22 changes: 13 additions & 9 deletions todoist/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,22 @@ func (c *projectCache) resolve(id ID) *Project {
}

func (c *projectCache) store(project Project) {
old := c.resolve(project.ID)
if old == nil {
if !project.IsDeleted {
*c.cache = append(*c.cache, project)
}
} else {
if project.IsDeleted {
c.remove(project)
var res []Project
isNew := true
for _, p := range *c.cache {
if p.Equal(project) {
if !project.IsDeleted {
res = append(res, project)
}
isNew = false
} else {
old = &project
res = append(res, p)
}
}
if isNew && !project.IsDeleted.Bool() {
res = append(res, project)
}
c.cache = &res
}

func (c *projectCache) remove(project Project) {
Expand Down

0 comments on commit 3e8123c

Please sign in to comment.