gocalstorage

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2022 License: MIT Imports: 4 Imported by: 0

README

gocalstorage

Go bindings for the JavaScript Storage APIs, using WASM.

tests

Tests use https://github.com/agnivade/wasmbrowsertest.

StorageEvent tests are currently fragile, using an iframe hack to simulate multiple pages on the same domain.

Documentation

Rendered for js/wasm

Index

Constants

This section is empty.

Variables

View Source
var ErrJSONNull = ErrNullJSON

ErrJSONNull exists for backward compatibility. TODO: remove in v0.3.0.

View Source
var ErrNullJSON = errors.New("null before json")

ErrNullJSON occurs when null is encountered before parsing JSON.

View Source
var ErrNullURL = errors.New("null before url")

ErrNullURL occurs when null is encountered before parsing a URL.

Functions

func Listen

func Listen() (<-chan *Event, func())

Listen creates a new channel of Events, similarly to OnStorage.

func ListenOn

func ListenOn(evs chan<- *Event) func()

ListenOn attaches to an existing channel of Events, similarly to OnStorage.

func OnStorage

func OnStorage(callback func(*Event)) func()

OnStorage adds an event listener for StorageEvents, invoking the provided callback.

The returned function will remove the callback and Release the associated js.Func.

Note: the storage event only occurs on other pages with access to the same Storage object.

Types

type Event

type Event struct {
	// contains filtered or unexported fields
}

Event represent a JavaScript StorageEvent.

StorageEvent properties are exposed in methods, but underlying Event properties may be accessed through JSValue as well.

func (*Event) JSValue

func (e *Event) JSValue() js.Value

JSValue implements js.Wrapper.

func (*Event) Key

func (e *Event) Key() (string, bool)

Key retrieves the key associated with the Event. If the underlying value is null, the second return value will be false.

func (*Event) New added in v0.2.0

func (e *Event) New() (string, bool)

New returns the new value associated with the Event. If the underlying value is null, the second return value will be false.

func (*Event) NewJSON added in v0.2.0

func (e *Event) NewJSON(val interface{}) error

NewJSON combines New with json.Unmarshal. The second argument should be a pointer, like with json.Unmarshal.

If the underlying property is null, ErrJSONNull is returned. Any other errors are from JSON parsing.

func (*Event) Old added in v0.2.0

func (e *Event) Old() (string, bool)

Old returns the old value associated with the Event. If the underlying value is null, the second return value will be false.

func (*Event) OldJSON added in v0.2.0

func (e *Event) OldJSON(val interface{}) error

OldJSON combines Old with json.Unmarshal. The second argument should be a pointer, like with json.Unmarshal.

If the underlying property is null, ErrJSONNull is returned. Any other errors are from JSON parsing.

func (*Event) ParseURL added in v0.2.0

func (e *Event) ParseURL() (*url.URL, error)

ParseURL returns the URL associated with the Event, parsing it into a *url.URL for convenience. If the underlying value is null, the first and second return values will be nil and ErrNullURL, respectively.

func (*Event) Storage

func (e *Event) Storage() *Storage

Storage returns the Storage object associated with the Event, wrapping it in a Storage struct for convenience.

func (*Event) URL

func (e *Event) URL() (string, bool)

URL returns the URL associated with the Event. If the underlying value is null, the second return value will be false.

type Storage

type Storage struct {
	// contains filtered or unexported fields
}

Storage represents a JavaScript Storage object.

func Local

func Local() *Storage

Local retrieves a Storage value from the global property localStorage, or nil if no such property exists.

func Session

func Session() *Storage

Session retrieves a Storage value from the global property sessionStorage, or nil if no such property exists.

func (*Storage) Clear

func (s *Storage) Clear()

Clear removes all keys from the Storage.

func (*Storage) Get added in v0.2.0

func (s *Storage) Get(key string) (string, bool)

Get retrieves the value associated with the given key in the Storage.

If the key does not exist in the Storage, the second return value is false.

func (*Storage) GetJSON added in v0.2.0

func (s *Storage) GetJSON(key string, val interface{}) error

GetJSON combines Get with json.Unmarshal. The second argument should be a pointer, like with json.Unmarshal.

If the key does not exist in the Storage, ErrNullJSON is returned. Any other errors are from JSON parsing.

func (*Storage) JSValue

func (s *Storage) JSValue() js.Value

JSValue implements js.Wrapper.

func (*Storage) Key

func (s *Storage) Key(n int) (string, bool)

Key retrieves the key of the Storage at the given index.

Key order is not guaranteed after mutations to the Storage.

func (*Storage) Length

func (s *Storage) Length() int

Length retrieves the underlying length property of the Storage.

func (*Storage) Listen

func (s *Storage) Listen() (<-chan *Event, func())

Listen acts similarly to the package-level Listen function, but it filters events from this Storage object.

func (*Storage) ListenOn

func (s *Storage) ListenOn(evs chan<- *Event) func()

ListenOn acts similarly to the package-level ListenOn function, but it filters events from this Storage object.

func (*Storage) OnStorage

func (s *Storage) OnStorage(callback func(*Event)) func()

OnStorage acts similarly to the package-level OnStorage function, but it filters events from this Storage object.

func (*Storage) Remove added in v0.2.0

func (s *Storage) Remove(key string)

Remove removes the given key from the Storage, if it exists.

func (*Storage) Set added in v0.2.0

func (s *Storage) Set(key, val string)

Set associates the given key with the given value in the Storage.

func (*Storage) SetJSON added in v0.2.0

func (s *Storage) SetJSON(key string, val interface{}) error

SetJSON combines Set with json.Marshal.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL