https://github.com/vaadin-component-factory/browserstorage
Allows access to the browser's LocalStorage and SessionStorage components from Vaadin server-side code
https://github.com/vaadin-component-factory/browserstorage
Last synced: 3 months ago
JSON representation
Allows access to the browser's LocalStorage and SessionStorage components from Vaadin server-side code
- Host: GitHub
- URL: https://github.com/vaadin-component-factory/browserstorage
- Owner: vaadin-component-factory
- License: apache-2.0
- Created: 2022-04-19T10:42:31.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-04-19T10:49:18.000Z (about 3 years ago)
- Last Synced: 2025-01-10T00:36:31.506Z (4 months ago)
- Language: Java
- Size: 217 KB
- Stars: 0
- Watchers: 7
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Vaadin BrowserStorage Add-on
Adds server-side logic and a JavaScript file to allow usage of local and session storage on the client's browser from server-side Vaadin code.
This add-on should be forward-compatible up to Vaadin 23.
## Usage
The API for SessionStorage and LocalStorage are identical, and have been mapped to the Storage object API as returning completable futures, with the addition of a "contains key" function to potentially save on bandwidth, as follows:
```java
CompletableFuture containsKey(String key);
CompletableFuture getItem(String key);
CompletableFuture setItem(String key, String value);
CompletableFuture removeItem(String key);
CompletableFuture clear();
```The returned boolean for `setItem`, `removeItem` and `clear` can safely be ignored for now; it is reserved for future use.
To access `SessionStorage` or `LocalStorage`, create an instance of them:
```java
LocalStorage localStorage = new LocalStorage();
localStorage.setItem("foo", "bar");
```Because the objects need to bind to a specific UI. In complex codebases, you may want to use the explicit constructor:
```java
UI myUI = UI.getCurrent();
LocalStorage localStorage = new LocalStorage(myUI);
localStorage.setItem("foo", "bar");
```The above examples are equivalent - the default constructor uses UI.getCurrent() internally to store a UI reference.
### Demo/test UI deployment
Starting the demo/test server:
```
mvn jetty:run
```This deploys demo at http://localhost:8080