aboutsummaryrefslogtreecommitdiff
path: root/internal/imap/mailboxes.go
blob: f1dc6c63758853784a59f8e44a0c6ce791f2c634 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package imap

import (
	"sync"

	"github.com/emersion/go-imap"
)

type mailboxes struct {
	mb          map[string]*imap.MailboxInfo
	mu          sync.RWMutex
	changeLocks map[string]chan struct{}
}

func (mbs *mailboxes) unlocking(elem Folder) {
	mbs.mu.Lock()
	defer mbs.mu.Unlock()

	ch, ok := mbs.changeLocks[elem.str]
	if !ok {
		panic("Unlocking where nothing is locked")
	}
	close(ch)
	delete(mbs.changeLocks, elem.str)
}

func (mbs *mailboxes) locking(elem Folder) bool {
	mbs.mu.Lock()
	ch, ok := mbs.changeLocks[elem.str]
	if !ok {
		ch = make(chan struct{})
		mbs.changeLocks[elem.str] = ch
		mbs.mu.Unlock()
		// we created the lock, we are in charge and done here
		return false
	} else {
		// someone else is working, we wait till he's done
		mbs.mu.Unlock() // we are not doing anything...
		<-ch
		return true
	}
}

func (mbs *mailboxes) contains(elem Folder) bool {
	mbs.mu.RLock()
	defer mbs.mu.RUnlock()

	_, ok := mbs.mb[elem.str]
	return ok
}

func (mbs *mailboxes) add(elem *imap.MailboxInfo) {
	mbs.mu.Lock()
	defer mbs.mu.Unlock()

	mbs.mb[elem.Name] = elem
}

func NewMailboxes() *mailboxes {
	return &mailboxes{
		mb:          map[string]*imap.MailboxInfo{},
		changeLocks: map[string]chan struct{}{},
		mu:          sync.RWMutex{},
	}
}
>-1/+1 2010-07-26Added addition and modification of constant stuffRené 'Necoro' Neumann3-8/+106 2010-07-05Only show right nav arrow, if the following month is not in the futureRené 'Necoro' Neumann2-14/+20 2010-07-05Add month navigationRené 'Necoro' Neumann6-5/+35 2010-07-05Add iconRené 'Necoro' Neumann2-1/+2 2010-07-05change cursorRené 'Necoro' Neumann1-0/+5 2010-07-05Some restructuringRené 'Necoro' Neumann3-28/+45 2010-07-05Closed/Open imagesRené 'Necoro' Neumann4-0/+146 2010-05-25Move page templates into their own folderRené 'Necoro' Neumann5-5/+7 2010-05-25Added the ability to edit an expenseRené 'Necoro' Neumann3-9/+34 2010-05-12FixRené 'Necoro' Neumann1-2/+2 2010-05-12Show more detailsRené 'Necoro' Neumann4-19/+42 2010-05-12Create new form each timeRené 'Necoro' Neumann1-24/+26 2010-05-10Fix redirect in AddRené 'Necoro' Neumann1-1/+1 2010-05-10Added the 'add expense' stuffRené 'Necoro' Neumann4-3/+69