Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Crisfole/svelte-readonly
https://github.com/Crisfole/svelte-readonly
Last synced: 27 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/Crisfole/svelte-readonly
- Owner: Crisfole
- License: mit
- Created: 2021-01-09T00:37:46.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-01-09T00:44:25.000Z (almost 4 years ago)
- Last Synced: 2024-11-09T12:36:17.321Z (about 1 month ago)
- Language: TypeScript
- Size: 8.79 KB
- Stars: 10
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-svelte-stores - svelte-readonly
README
# Svelte Readonly
Not all writable stores should be writable outside their module. This enables that. It has no dependencies. It also has no tests, but that's ok. It's not that interesting.
## Installation
Install, as expected, with `npm install svelte-readonly`.
## Usage
```
import readonly from 'svelte-readonly';
// or
import {readonly} from 'svelte-readonly';
```Readonly takes a writable store and returns a readable store. That's it.
## Example
This example lets you subscribe to user changes, but doesn't let you set it outside the logIn/logOut functions. This gets more handy the less you want to accidentally set something incorrectly outside a module. (Adding session timeouts, warnings, etc).
```
// session.js
import { writable } from 'svelte/store';
import readonly from 'svelte-readonly';const writableUser = writable(null);
export const user = readonly(writableUser);
export async function logIn(username, password) {
try {
writableUser.set(await api.logIn(username, password));
} catch (err) {
// handle login error
}
}export async function logOut() {
writableUser.set(null);
}
```