Svelte store with a websocket backend
import websocketStore from "svelte-websocket-store";
const initialValue = { };
export const myStore = websocketStore("wss://mydomain.com/ws1", initialValue, ['option 1', 'option 2']);
// send JSON to websocket server
$myStore = { content: "to be saved", other_values: "all" };
// receive JSON from server (push)
let response = $myStore;
Keeps the node active state in sync.
node Element router Router Keys also act as svelte stores and can be subscribed.
export const article = derived(
[articles, router.keys.article],
([$articles, $id], set) => {
set($articles.find(a => a.id === $id));
return () => {};
}
);
Type: Object
Extends BaseTransition
key subscriptions:
const aKey = router.keys.aKey;
$aKey // fired if value of aKey changes
routes Array<Route> all managed routesbase string url (optional, default new URL("../",import.meta.url).pathname)linkNodes Set<Node> nodes having their active state updatedroutes Array<Route> keys Object collected keys of all routesparams Object value mapping from keys (from current route)route Route currentnested Transition ongoing nested transitionbase string urlCurrent component. Either from a nested transition or from the current route
Returns SvelteComponent
Value if the current route
Returns any
Returns string url path with fragment & query
Replace current route.
path string Replace current route without updating the state.
path string Returns Object former state
Leave current route and enter route for given path. The work is done by a Transition.
path string where to goReturns Transition running transition
Called from a Transition to manifest the new destination. If path is undefined the Transition has been aborderd.
path string Continue a transition to its original destination. Shortcut for this.transition.continue(). If there is no transition ongoing and a fallbackPath is present, it will be entered. Otherwise does nothing.
fallbackPath string Abort a transition. Shortcut for this.transition.abort() If there is no transition ongoing and a fallbackPath is present it will be entered. Otherwise does nothing.
fallbackPath string Router subscription. Changes in the current route will trigger a update
subscription Function Update the active state of a node. A node is considered active if it shared the path prefix with the current route.
node Element Add a new Route.
route Route Find Route for a given value.
value any Returns Route able to support given value
Find path for a given value.
value any suffix string to be appendedReturns string path + suffix
Deliver url search params form the current location.
Returns URLSearchParams as extracted from the path
Replaces the search part of the path with the given searchParams.
searchParams (URLSearchParams | Object) Add another transition nesting level. Starts a transition from the given factory.
path string factory Transition Continue a nested route to its original destination. Does nothing if the transition has not been nested.
Abort the transition.
error Returns Promise<boolean> truen in case there was a nesten transition
Extends ValueStoreRoute
Route to represent a slice of the masters list of values.
master Route route holding the master recordsRoute holding the list ov values
Returns Route our master
Returns Promise<any> 1st. entry
Returns any last entry
Returns any previous value
Returns Promise<any> next value
Enforces conditions of routes Like the presents of values in the context
Called while entering a route (current outlet is not yet set)
transition Transition Called before leaving a route
transition Transition Redirects to a given path if condition is met.
Execute guards in a sequence.
children Iterable<Guard> Execute guards in a parallel.
children Iterable<Guard> Create a writable store based on a web-socket. Data is transferred as JSON. Keeps socket open (reopens if closed) as long as there are subscriptions.
url string the WebSocket urlinitialValue any store value used before 1st. response from server is presentsocketOptions Array<string> transparently passed to the WebSocket constructorReturns Store
Bring session into the valid state by calling the authorization endpoint and asking for a access_token. Executes a POST on the endpoint url expecting username, and password as json
session Session to be openedendpoint string authorization urlusername string id of the userpassword string user credentialstokenmap Object token names in response to internal known values (optional, default defaultTokenMap)Returns Promise<(string | undefined)> error message in case of failure or undefined on success
Extract error description from response.
response Response Extends SkeletonRoute
Route holding a ordered collection of values.
path options value Array<any> Default empty guard does nothing.
Route at the root of the tree. This route has no parent. All other routes are below of this one.
Are there parameters in the path.
Returns boolean true if route has parameters (:key)
Returns string empty as we are the root
Returns Object empty object
Returns Guard empty guard which does nothing
Extends RootRoute
Route Subscriptions on Routes fire when the route value changes.
path options (optional, default {})path string full path of the Route including all parentscomponent SvelteComponent target to showlinkComponent SvelteComponent content for ObjectLinkpropertyMapping Object Map properties to object attributes
Keys are the property names and values are the keys in the resulting object.priority number keys Array<string> as found in the pathregex RegEx value any Enter the route from a former one. All parent routes up to the common ancestor are entered first.
transition Transition untilRoute Route the common ancestor with the former routeLeave the route to a new one. All parent routes up to the common ancestor are left.
transition Transition untilRoute Route the common ancestor with the future routeCheck if value and properties are acceptable for the route.
value any to be placed into routeproperties Object as presented in the routeReturns boolean true if value can be accepted
Extract properties from a value. All property values must be strings.
value any source of the propertiesReturns (Object | undefined) properties extracted from given value
Find common ancestor with an other Route.
other Route Returns (Route | undefined) common ancestor Route between receiver and other
Deliver value for a given set of properties of the transition. Default implemantation asks the parent route.
transition Transition Returns any for matching properties
Deliver route value. Default implemantation asks the parent route.
Returns any
Deliver property mapping. Default implemantation asks the parent route.
Returns Object for matching properties
Default implemantation asks the parent route.
Extends SkeletonRoute
Route holding a single value
Data as preserved in the backing store.
Type: Object
Time required to execute a refresh
Type: number
User session. To create as session backed by browser local storage.
let session = new Session(localStorage);
or by browser session storage
let session = new Session(sessionStorage);
store SessionData (optional, default localStorage)entitlements Set<string> subscriptions Set<Object> store subscriptionsexpirationDate Date when the access token expiresaccess_token string token itselfrefresh_token string refresh tokenConsume auth response data and reflect internal state.
data Object Refresh with refresh_token.
Returns Promise<boolean> true if refresh was succcessfull false otherwise
Http header suitable for fetch.
Returns Object header The http header.
Returns string header.Authorization The Bearer access token.
As long as the expirationTimer is running we must be valid.
Returns boolean true if session is valid (not expired)
Remove all tokens from the session and the backing store.
Check presence of an entitlement.
name string of the entitlementReturns boolean true if the named entitlement is present
Fired when the session changes.
subscription Function Extract and decode the payload.
token string Returns Object payload object
Extends BaseTransition
Transition between routes.
router Router path string new destinationrouter Router path string new destinationStart the transition
Cleanup transition. Update Nodes active state
Halt current transition and go to another route. To proceed with the original route call continue() The original transition will keept in place and can be continued afterwards.
path string new route to enter temporaryBring back the router into the state before the transition has started. All nested transitions also will be termniated.
e (Exception | undefined) Walks up the dom tree parents unti a node with a given attribute is found or the root node is reached.
Dispatches a NAVIGATION_EVENT with pathname and hash
event Event Create a named object which also acts as a store.
name string value any initial valuevalue any Returns Store
Extends Guard
Shows a component during transition.
component SvelteComponent to show up during th transitionrampUpTime number initial delay for the componnt to show up (optional, default 300)With npm do:
npm install svelte-websocket-store
With yarn do:
yarn add svelte-websocket-store
export BROWSER=safari|chrome|...
npm|yarn test
BSD-2-Clause