Current section

Files

Jump to
wisp_inertia src internal@context.erl
Raw

src/internal@context.erl

-module(internal@context).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/internal/context.gleam").
-export([erase/1, get_orelse/2, add_entry/2, shared_add_entry/2, shared_get_orelse/2, shared_get_all/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/internal/context.gleam", 9).
?DOC(
" Erase the value referenced by `key`.\n"
"\n"
" Returns `True` if there was a value, `False` otherwise.\n"
).
-spec erase(binary()) -> boolean().
erase(Key) ->
context_ffi:ctx_erase(Key).
-file("src/internal/context.gleam", 28).
?DOC(" Retrieve the value referenced by `key` or `default` if not present.\n").
-spec get_orelse(binary(), REG) -> REG.
get_orelse(Key, Default) ->
case context_ffi:ctx_get(Key) of
{ok, A} ->
A;
{error, nil} ->
Default
end.
-file("src/internal/context.gleam", 39).
?DOC(
" Stores the `value` as referenced by `key` only if no value is already\n"
" present.\n"
"\n"
" This returns the stored value.\n"
).
-spec add_entry(binary(), any()) -> nil.
add_entry(Key, Value) ->
case context_ffi:ctx_get(Key) of
{error, nil} ->
context_ffi:ctx_add(Key, Value),
nil;
_ ->
nil
end.
-file("src/internal/context.gleam", 51).
?DOC(" Adds a value as a share property to Inertia context\n").
-spec shared_add_entry(binary(), any()) -> nil.
shared_add_entry(Key, Value) ->
case context_ffi:sharedctx_get(Key) of
{error, nil} ->
Ctx = maps:new(),
New_ctx = gleam@dict:insert(Ctx, Key, Value),
context_ffi:sharedctx_add(<<"inertia_shared"/utf8>>, New_ctx),
nil;
{ok, Shared_ctx} ->
case Shared_ctx of
{ok, Ctx@1} ->
New_ctx@1 = gleam@dict:insert(Ctx@1, Key, Value),
context_ffi:sharedctx_add(
<<"inertia_shared"/utf8>>,
New_ctx@1
),
nil;
_ ->
nil
end
end.
-file("src/internal/context.gleam", 75).
?DOC(
" Gets a value of share property from Inertia context, or return a default value\n"
" if the property does not exists\n"
).
-spec shared_get_orelse(binary(), REL) -> REL.
shared_get_orelse(Key, Default) ->
case context_ffi:sharedctx_get(<<"inertia_shared"/utf8>>) of
{ok, Ctx} ->
case gleam_stdlib:map_get(Ctx, Key) of
{ok, Val} ->
Val;
{error, _} ->
Default
end;
{error, nil} ->
Default
end.
-file("src/internal/context.gleam", 87).
-spec shared_get_all() -> gleam@dict:dict(binary(), binary()).
shared_get_all() ->
case context_ffi:sharedctx_get(<<"inertia_shared"/utf8>>) of
{ok, Ctx} ->
Ctx;
{error, nil} ->
maps:new()
end.