-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add support for Plug * Update Storex application supervisor children spec * Add separated test config * Update GitHub Actions workflow * Reorganize tests files, directories structure * Reorganize tests files, directories structure * Fix Plug handler stop result * Handlers tests
- Loading branch information
1 parent
147db84
commit ba8f44a
Showing
30 changed files
with
987 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# storex | ||
|
||
## 0.3.0 | ||
|
||
- **[BREAKING]** Rename Cowbow handler module from `Storex.Socket.Handler` to `Storex.Handler.Cowboy` | ||
- Add support for Plug based apps `plug Storex.Plug` | ||
- Update Storex application supervisor children spec | ||
|
||
## 0.2.5 | ||
|
||
- Fix diff of Date struct | ||
- Rewrite tests from Hound to Wallaby | ||
|
||
## 0.2.4 | ||
|
||
- Fix root state update | ||
- Remove optional from jason dependency | ||
|
||
## 0.2.3 | ||
|
||
- Fix reconnect of WebSocket on connection close | ||
|
||
## 0.2.2 | ||
|
||
- Fix reconnect of WebSocket on connection close | ||
|
||
## 0.2.1 | ||
|
||
- Typescript/Javascript improvements | ||
|
||
## 0.2.0 | ||
|
||
- Dynamic registry declaration | ||
- - Default registry on ETS | ||
- Fix issue with a restart of Store when stopped on disconnect | ||
- Update dependencies | ||
|
||
## 0.1.0 | ||
|
||
- The only diff of the store state is being sent on each mutation. | ||
- Subscriber of connection status | ||
- Fixes in library |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Config | ||
|
||
config :wallaby, | ||
otp_app: :storex, | ||
chromedriver: [ | ||
# headless: false | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
defmodule Storex.Handler.Plug do | ||
@moduledoc false | ||
|
||
alias Storex.Socket | ||
|
||
def init(_) do | ||
session = Application.get_env(:storex, :session_id_library, Nanoid).generate() | ||
pid = self() | ||
|
||
Storex.Registry.register_session(session, pid) | ||
|
||
{:ok, %{session: session, pid: pid}} | ||
end | ||
|
||
def terminate(_reason, _req, %{session: session}) do | ||
Storex.Registry.session_stores(session) | ||
|> Enum.each(fn {session, store, _} -> | ||
Storex.Supervisor.remove_store(session, store) | ||
end) | ||
|
||
Storex.Registry.unregister_session(session) | ||
|
||
:ok | ||
end | ||
|
||
def terminate(_, _) do | ||
:ok | ||
end | ||
|
||
def handle_in({message, [opcode: :text]}, state) do | ||
Jason.decode(message, keys: :atoms) | ||
|> case do | ||
{:ok, message} -> | ||
Socket.message_handle(message, state) | ||
|> map_response() | ||
|
||
{:error, _} -> | ||
{:stop, "Payload is malformed.", 1007, state} | ||
end | ||
end | ||
|
||
def handle_info({:mutate, store, mutation, data}, %{session: session} = state) do | ||
%{ | ||
type: "mutation", | ||
session: session, | ||
store: store, | ||
data: %{ | ||
data: data, | ||
name: mutation | ||
} | ||
} | ||
|> Socket.message_handle(state) | ||
|> map_response() | ||
end | ||
|
||
def handle_info(_info, state) do | ||
{:ok, state} | ||
end | ||
|
||
defp map_response({:text, message, state}) do | ||
{:push, {:text, message}, state} | ||
end | ||
|
||
defp map_response({:close, code, message, state}) do | ||
{:stop, :normal, {code, message}, state} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule Storex.Plug do | ||
import Plug.Conn | ||
|
||
@moduledoc """ | ||
Add Storex to your Plug application, to handle WebSocket connections. | ||
Example for Phoenix Endpoint: | ||
```elixir | ||
defmodule YourAppWeb.Endpoint do | ||
use Phoenix.Endpoint, otp_app: :your_app | ||
plug Storex.Plug | ||
# ... | ||
end | ||
``` | ||
## Options | ||
- `:path` - The path to mount the Storex handler. Default is `"/storex"`. | ||
""" | ||
|
||
@doc false | ||
def init(options \\ []) do | ||
[ | ||
path: Keyword.get(options, :path, "/storex") | ||
] | ||
end | ||
|
||
@doc false | ||
def call(%{method: "GET", request_path: path} = conn, path: path) do | ||
conn | ||
|> WebSockAdapter.upgrade(Storex.Handler.Plug, [], timeout: 60_000) | ||
|> halt() | ||
end | ||
|
||
@doc false | ||
def call(conn, _) do | ||
conn | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.