#
LocalStorageSessionManager
A local storage session manager (strictly for development purpose).
#
Reference
const sessionManager = new LocalStorageSessionManager(options?: { key? })
#
Parameters
options
: An optional object literal of options:key
: An optional key identifying the session inlocalStorage
.
#
Usage
#
Create a manager instance
import { LocalStorageSessionManager } from "@squide/fakes";
import type { Session } from "@sample/share";
const sessionManager = new LocalStorageSessionManager<Session>();
#
Set a session
sessionManager.setSession({ username: "Foo" });
#
Get the current session
const session = sessionManager.getSession();
#
Clear the current session
sessionManager.clearSession();
#
Integrate with a runtime instance
host/src/session.ts
import type { SessionAccessorFunction } from "@squide/firefly";
import { LocalStorageSessionManager } from "@squide/fakes";
import type { Session } from "@sample/share";
export const sessionManager = new LocalStorageSessionManager<Session>();
export const sessionAccessor: SessionAccessorFunction = () => {
return sessionManager.getSession();
};
host/src/bootstrap.tsx
import { createRoot } from "react";
import { FireflyRuntime, RuntimeContext } from "@squide/firefly";
import { App } from "./App";
import { sessionAccessor } from "./session";
const runtime = new FireflyRuntime({
sessionAccessor
});
const root = createRoot(document.getElementById("root"));
root.render(
<RuntimeContext.Provider value={runtime}>
<App />
</RuntimeContext.Provider>
);
remote-module/src/UserProfile.tsx
import { useSession } from "@squide/firefly";
export function UserProfile() {
const session = useSession();
return (
<span>{session.userName}</span>
);
}