This commit is contained in:
Liang Ding 2022-12-23 17:02:25 +08:00
parent dc79d218ba
commit 72e3cf5135
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
2 changed files with 62 additions and 18 deletions

View file

@ -17,6 +17,7 @@
package model
import (
"errors"
"os"
"path/filepath"
"strings"
@ -59,9 +60,14 @@ func RenderFlashcard(blockID string) (content string, err error) {
func ReviewFlashcard(deckID string, blockID string, rating riff.Rating) (err error) {
deckLock.Lock()
deck := Decks[deckID]
deckLock.Unlock()
defer deckLock.Unlock()
if syncingStorages {
err = errors.New(Conf.Language(81))
return
}
deck := Decks[deckID]
deck.Review(blockID, rating)
err = deck.Save()
if nil != err {
@ -77,14 +83,19 @@ type Flashcard struct {
}
func GetDueFlashcards(deckID string) (ret []*Flashcard, err error) {
deckLock.Lock()
defer deckLock.Unlock()
if syncingStorages {
err = errors.New(Conf.Language(81))
return
}
if "" == deckID {
return getAllDueFlashcards()
}
deckLock.Lock()
deck := Decks[deckID]
deckLock.Unlock()
cards := deck.Dues()
for _, card := range cards {
blockID := card.BlockID()
@ -132,9 +143,14 @@ func getAllDueFlashcards() (ret []*Flashcard, err error) {
func RemoveFlashcards(deckID string, blockIDs []string) (err error) {
deckLock.Lock()
deck := Decks[deckID]
deckLock.Unlock()
defer deckLock.Unlock()
if syncingStorages {
err = errors.New(Conf.Language(81))
return
}
deck := Decks[deckID]
var rootIDs []string
blockRoots := map[string]string{}
for _, blockID := range blockIDs {
@ -214,9 +230,14 @@ func RemoveFlashcards(deckID string, blockIDs []string) (err error) {
func AddFlashcards(deckID string, blockIDs []string) (err error) {
deckLock.Lock()
deck := Decks[deckID]
deckLock.Unlock()
defer deckLock.Unlock()
if syncingStorages {
err = errors.New(Conf.Language(81))
return
}
deck := Decks[deckID]
var rootIDs []string
blockRoots := map[string]string{}
for _, blockID := range blockIDs {
@ -324,9 +345,14 @@ func InitFlashcards() {
func RenameDeck(deckID, name string) (err error) {
deckLock.Lock()
deck := Decks[deckID]
deckLock.Unlock()
defer deckLock.Unlock()
if syncingStorages {
err = errors.New(Conf.Language(81))
return
}
deck := Decks[deckID]
deck.Name = name
err = deck.Save()
if nil != err {
@ -337,6 +363,14 @@ func RenameDeck(deckID, name string) (err error) {
}
func RemoveDeck(deckID string) (err error) {
deckLock.Lock()
defer deckLock.Unlock()
if syncingStorages {
err = errors.New(Conf.Language(81))
return
}
riffSavePath := getRiffDir()
deckPath := filepath.Join(riffSavePath, deckID+".deck")
if err = os.Remove(deckPath); nil != err {
@ -352,6 +386,14 @@ func RemoveDeck(deckID string) (err error) {
}
func CreateDeck(name string) (deck *riff.Deck, err error) {
deckLock.Lock()
defer deckLock.Unlock()
if syncingStorages {
err = errors.New(Conf.Language(81))
return
}
riffSavePath := getRiffDir()
deckID := ast.NewNodeID()
deck, err = riff.LoadDeck(riffSavePath, deckID)
@ -360,10 +402,7 @@ func CreateDeck(name string) (deck *riff.Deck, err error) {
return
}
deck.Name = name
deckLock.Lock()
Decks[deckID] = deck
deckLock.Unlock()
return
}

View file

@ -755,6 +755,7 @@ func IndexRepo(memo string) (err error) {
}
var syncingFiles = sync.Map{}
var syncingStorages = false
func IsSyncingFile(rootID string) (ret bool) {
_, ret = syncingFiles.Load(rootID)
@ -803,14 +804,17 @@ func bootSyncRepo() (err error) {
}
syncingFiles = sync.Map{}
syncingStorages = false
for _, fetchedFile := range fetchedFiles {
name := path.Base(fetchedFile.Path)
if !(strings.HasSuffix(name, ".sy")) {
if strings.HasSuffix(name, ".sy") {
id := name[:len(name)-3]
syncingFiles.Store(id, true)
continue
}
id := name[:len(name)-3]
syncingFiles.Store(id, true)
if strings.HasPrefix(name, "/storage/") {
syncingStorages = true
}
}
elapsed := time.Since(start)
@ -843,6 +847,7 @@ func bootSyncRepo() (err error) {
return
}
syncingFiles = sync.Map{}
syncingStorages = false
}()
}
return