Skip to content

Commit

Permalink
only sync periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed May 24, 2020
1 parent c7c3b71 commit ec3399c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
19 changes: 13 additions & 6 deletions src/Main.elm
Expand Up @@ -85,7 +85,7 @@ init flags url key =
( model
, Batch
[ routingEffects
, Batch (List.map StartSyncing settings.syncs)
, Batch (List.map SyncOnce settings.syncs)
]
)

Expand Down Expand Up @@ -121,6 +121,7 @@ type Msg
| GotCurrentTime Posix
| PouchDBPutSuccessfully Value
| TimerTriggeredSave
| TimerTriggeredSyncAll
| FocusedOnEditor
| UserClickedNewNote
| UserEditedNode String
Expand Down Expand Up @@ -155,7 +156,7 @@ type Effect
| ReplaceUrl Route
| Put Value
| FocusOnEditor
| StartSyncing Sync
| SyncOnce Sync


type UpdatedRevision
Expand Down Expand Up @@ -234,6 +235,11 @@ update msg model =
, Batch (List.map (Put << Database.encode) rows)
)

TimerTriggeredSyncAll ->
( model
, Batch (List.map SyncOnce model.settings.syncs)
)

FocusedOnEditor ->
( model, NoEffect )

Expand Down Expand Up @@ -549,7 +555,7 @@ update msg model =
( { model | draftSync = Nothing, settings = newSettings }
, Batch
[ Put (Settings.encode newSettings)
, StartSyncing draftSync
, SyncOnce draftSync
]
)

Expand Down Expand Up @@ -618,10 +624,10 @@ perform model effect =
(\_ -> FocusedOnEditor)
(Dom.focus "editor")

StartSyncing sync ->
SyncOnce sync ->
case Sync.toUrl sync of
Just url ->
startSyncing url
syncOnce url

Nothing ->
Cmd.none
Expand All @@ -630,7 +636,7 @@ perform model effect =
port put : Value -> Cmd msg


port startSyncing : String -> Cmd msg
port syncOnce : String -> Cmd msg


port putSuccessfully : (Value -> msg) -> Sub msg
Expand All @@ -641,6 +647,7 @@ subscriptions model =
Sub.batch
[ putSuccessfully PouchDBPutSuccessfully
, Time.every 1000 (\_ -> TimerTriggeredSave)
, Time.every 10000 (\_ -> TimerTriggeredSyncAll)

-- 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
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -63,9 +63,9 @@ var db = new PouchDB("notes");
});

// syncing
app.ports.startSyncing.subscribe((url) => {
app.ports.syncOnce.subscribe((url) => {
db.replicate.from(url).on("complete", (info) => {
db.sync(url, { live: true, retry: true })
db.sync(url, { retry: true })
.on("change", (info) => console.log("change", info))
.on("paused", (err) => console.log("paused", err))
.on("active", () => console.log("active"))
Expand Down

0 comments on commit ec3399c

Please sign in to comment.