Skip to content

Commit

Permalink
add periodic compaction
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed May 24, 2020
1 parent ec3399c commit 12b1838
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/Main.elm
Expand Up @@ -122,6 +122,7 @@ type Msg
| PouchDBPutSuccessfully Value
| TimerTriggeredSave
| TimerTriggeredSyncAll
| TimerTriggeredCompaction
| FocusedOnEditor
| UserClickedNewNote
| UserEditedNode String
Expand Down Expand Up @@ -157,6 +158,7 @@ type Effect
| Put Value
| FocusOnEditor
| SyncOnce Sync
| Compact


type UpdatedRevision
Expand Down Expand Up @@ -240,6 +242,9 @@ update msg model =
, Batch (List.map SyncOnce model.settings.syncs)
)

TimerTriggeredCompaction ->
( model, Compact )

FocusedOnEditor ->
( model, NoEffect )

Expand Down Expand Up @@ -632,13 +637,19 @@ perform model effect =
Nothing ->
Cmd.none

Compact ->
compact ()


port put : Value -> Cmd msg


port syncOnce : String -> Cmd msg


port compact : () -> Cmd msg


port putSuccessfully : (Value -> msg) -> Sub msg


Expand All @@ -647,7 +658,8 @@ subscriptions model =
Sub.batch
[ putSuccessfully PouchDBPutSuccessfully
, Time.every 1000 (\_ -> TimerTriggeredSave)
, Time.every 10000 (\_ -> TimerTriggeredSyncAll)
, Time.every 60000 (\_ -> TimerTriggeredSyncAll)
, Time.every 120000 (\_ -> TimerTriggeredCompaction)

-- Using a 1-second resolution is more than enough for most UI
-- concerns. If it needs to be more precise, crank this down! But
Expand Down
7 changes: 7 additions & 0 deletions src/index.js
Expand Up @@ -62,6 +62,13 @@ var db = new PouchDB("notes");
.catch((err) => console.error(err));
});

// compacting
app.ports.compact.subscribe(() => {
db.compact()
.then(() => console.log("compaction success"))
.catch(err) => console.error(err));
})

// syncing
app.ports.syncOnce.subscribe((url) => {
db.replicate.from(url).on("complete", (info) => {
Expand Down
5 changes: 4 additions & 1 deletion tests/MainTest.elm
Expand Up @@ -65,7 +65,10 @@ testPerform effect =
FocusOnEditor ->
SCmd.none

StartSyncing _ ->
SyncOnce _ ->
SCmd.none

Compact ->
SCmd.none


Expand Down

0 comments on commit 12b1838

Please sign in to comment.