summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHubert Chathi <hubert@uhoreg.ca>2020-08-21 19:14:00 -0400
committerHubert Chathi <hubert@uhoreg.ca>2020-08-21 19:14:00 -0400
commit7a9741e3f6fd64ae492697560f61cad7ef9e0cb4 (patch)
tree63a1614529615b806224abf453a9b974aadd7bf7
parentautomatically start sync process (diff)
automatically start/stop sync when logging in/out
-rw-r--r--lib/polyjuice/client.ex16
-rw-r--r--lib/polyjuice/client/sync.ex2
2 files changed, 18 insertions, 0 deletions
diff --git a/lib/polyjuice/client.ex b/lib/polyjuice/client.ex
index 2a15d28..6cf1c84 100644
--- a/lib/polyjuice/client.ex
+++ b/lib/polyjuice/client.ex
@@ -49,6 +49,7 @@ defmodule Polyjuice.Client do
id: integer,
storage: Polyjuice.Client.Storage.t(),
handler: Polyjuice.Client.Handler.t(),
+ opts: list,
test: boolean
}
@@ -59,6 +60,7 @@ defmodule Polyjuice.Client do
:storage,
:handler,
sync: true,
+ opts: [],
test: false
]
@@ -126,6 +128,7 @@ defmodule Polyjuice.Client do
storage: storage,
handler: handler,
sync: sync,
+ opts: opts,
test: Keyword.get(opts, :test, false)
}
end
@@ -368,6 +371,15 @@ defmodule Polyjuice.Client do
)
end
+ if client.sync do
+ supervisor_name = process_name(client.id, :supervisor)
+
+ Supervisor.start_child(
+ supervisor_name,
+ sync_child_spec(client.base_url, client.id, client.opts)
+ )
+ end
+
ret
end
@@ -376,6 +388,10 @@ defmodule Polyjuice.Client do
"""
@spec log_out(client :: Polyjuice.Client.t()) :: {:ok} | any
def log_out(client) do
+ supervisor_name = process_name(client.id, :supervisor)
+ Supervisor.terminate_child(supervisor_name, Polyjuice.Client.Sync)
+ Supervisor.delete_child(supervisor_name, Polyjuice.Client.Sync)
+
{:ok} =
Polyjuice.Client.API.call(
client,
diff --git a/lib/polyjuice/client/sync.ex b/lib/polyjuice/client/sync.ex
index 1f6e95b..f6651e3 100644
--- a/lib/polyjuice/client/sync.ex
+++ b/lib/polyjuice/client/sync.ex
@@ -87,6 +87,8 @@ defmodule Polyjuice.Client.Sync do
uri = URI.merge(homeserver_url, @sync_path)
+ Registry.register(Polyjuice.Client, {id, :sync}, nil)
+
connect(%__MODULE__{
handler: handler,
client_state: Polyjuice.Client.process_name(id, :state),