sveltish
  • Svelte Stores in Python

stores

  • Welcome
    • Sveltish
  • API
    • stores
    • utils

Contents

  • The Svelte Store contract
    • Writable
    • StoreProtocol
  • Implementation
    • Store
    • writable
    • ReadableStore
    • readable
    • DerivedStore
    • derived
    • show_doc

stores

The Svelte Store contract

  1. A store must contain a .subscribe method, which must accept as its argument a subscription function(aka Subscriber or Callback). This subscription function must be immediately and synchronously called with the store’s current value upon calling subscribe. All of a store’s active subscription functions must later be synchronously called whenever the store’s value changes.

  2. The .subscribe method must return an unsubscribe function(aka Unsubscriber). Calling an unsubscribe function must stop its subscription, and its corresponding subscription function must not be called again by the store.

  3. A store may optionally contain a .set method, which must accept as its argument a new value for the store, and which synchronously calls all of the store’s active subscription functions. Such a store is called a writable store.

For interoperability with RxJS Observables, the .subscribe method is also allowed to return an object with an .unsubscribe method, rather than return the unsubscription function directly. Note however that unless .subscribe synchronously calls the subscription (which is not required by the Observable spec), Svelte will see the value of the store as undefined until it does.

Store Contract Documentation

Types Definition

T = TypeVar("T")
covT = TypeVar("covT", covariant=True)
Subscriber = Callable[[T], None] # a callback
Unsubscriber = Callable[[], None] # a callback to be used upon termination of the subscription
Updater = Callable[[T], T]
Notifier = Callable[[Subscriber], Union[Unsubscriber, None]]

class StoreProtocol(Protocol, Generic[covT]):
    ''' The Svelte Store ~~contract~~ protocol. '''
    def subscribe(self, subscriber: Subscriber[T]) -> Unsubscriber: ...

Readable: TypeAlias = StoreProtocol[T]

class Writable(Readable[T]):
    ''' Writable protocol'''
    def set(self, value: T) -> None: ...
    def update(self, updater: Updater[T]) -> None: ...

source

Writable

 Writable (*args, **kwargs)

Writable protocol


source

StoreProtocol

 StoreProtocol (*args, **kwargs)

The Svelte Store contract protocol.

Implementation

Writable Store


source

Store

 Store (initial_value:Any=None, start:Notifier=<function noop>)

A Writable Store.

Type Default Details
initial_value Any None initial value of the store
start Notifier noop A Notifier (Optional)
Returns None

Writable Factory


source

writable

 writable (value:~T=None, start:Callable[[Callable[[~T],NoneType]],Optiona
           l[Callable[[],NoneType]]]=<function noop>)

Creates a new Writable Store (A Writable factory).

Type Default Details
value T None initial value of the store
start Notifier noop Optional Notifier, a function called when the first subscriber is added
Returns Writable[T] Writable Store

Readable Store


source

ReadableStore

 ReadableStore (initial_value:T, start:Notifier)

A Readable Store.

Type Details
initial_value T initial value of the store
start Notifier function called when the first subscriber is added
Returns None

Readable Factory


source

readable

 readable (value:~T, start:Callable[[Callable[[~T],NoneType]],Optional[Cal
           lable[[],NoneType]]])

Creates a new Readable Store (A Readable factory).

Type Details
value T initial value of the store
start Notifier function called when the first subscriber is added
Returns Readable[T] Readable Store

Derived Store


source

DerivedStore

 DerivedStore (s:Union[Store,list[Store]], *functions:Callable)

A Derived Store.

Type Details
s Union[Store, listStore] source store(s)
functions Callable
Returns None a callback that takes the source store(s) values and returns the derived value

Derived Factory


source

derived

Creates a new Derived Store (A Derived factory).

d = derived(a, lambda a: f"{a}")
d
r<0> $str: 'fonzie'

Pipe Operator


source

show_doc

 show_doc (sym, renderer=None, name:Optional[str]=None, title_level:int=3)

Show signature and docstring for sym

Type Default Details
sym Symbol to document
renderer NoneType None Optional renderer (defaults to markdown)
name str | None None Optionally override displayed name of sym
title_level int 3 Heading level to use for symbol name
Copyright 2023, Fred Guth
  • Built with nbdev