Skip to content

Commit

Permalink
Make shared storage create on demand instead of eargerly
Browse files Browse the repository at this point in the history
  • Loading branch information
MacFJA committed Oct 15, 2023
1 parent cd88f35 commit 791db58
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.4.1]

### Fixed

- Shared storage eagerly created, nullify the purpose of `disableWarnings` ([Issue#56])

## [2.4.0]

### Added
Expand Down Expand Up @@ -127,6 +133,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `sessionStorage()` use `createSessionStorage()` instead
- `indexedDBStorage()` use `createIndexedDBStorage()` instead

## Versions 1.x
<details>

## [1.3.0]

### Added
Expand Down Expand Up @@ -187,7 +196,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

First version

[Unreleased]: https://github.com/MacFJA/svelte-persistent-store/compare/2.4.0...HEAD
</details>

[Unreleased]: https://github.com/MacFJA/svelte-persistent-store/compare/2.4.1...HEAD
[2.4.1]: https://github.com/MacFJA/svelte-persistent-store/compare/2.4.0...2.4.1
[2.4.0]: https://github.com/MacFJA/svelte-persistent-store/compare/2.3.2...2.4.0
[2.3.2]: https://github.com/MacFJA/svelte-persistent-store/compare/2.3.1...2.3.2
[2.3.1]: https://github.com/MacFJA/svelte-persistent-store/compare/2.3.0...2.3.1
Expand Down Expand Up @@ -220,6 +232,7 @@ First version
[Issue#48]: https://github.com/MacFJA/svelte-persistent-store/issues/48
[Issue#50]: https://github.com/MacFJA/svelte-persistent-store/issues/50
[Issue#52]: https://github.com/MacFJA/svelte-persistent-store/issues/52
[Issue#56]: https://github.com/MacFJA/svelte-persistent-store/issues/56
[PR#8]: https://github.com/MacFJA/svelte-persistent-store/pull/8
[PR#38]: https://github.com/MacFJA/svelte-persistent-store/pull/38
[PR#39]: https://github.com/MacFJA/svelte-persistent-store/pull/39
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@macfja/svelte-persistent-store",
"version": "2.4.0",
"version": "2.4.1",
"description": "A Svelte store that keep its value through pages and reloads",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
28 changes: 22 additions & 6 deletions src/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,49 @@ import type { Writable, StartStopNotifier } from "svelte/store"
import type { PersistentStore, StorageInterface } from "./core"
import { createCookieStorage, createLocalStorage, createSessionStorage, persist } from "./core"

const sharedCookieStorage = createCookieStorage(),
sharedLocalStorage: StorageInterface<any> = createLocalStorage(),
sharedSessionStorage: StorageInterface<any> = createSessionStorage()
type StorageType = "cookie" | "local" | "session"
const sharedStorage: { [type in StorageType]?: StorageInterface<any> } = {}

function getStorage(type: StorageType): StorageInterface<any> {
if (!Object.keys(sharedStorage).includes(type)) {
switch (type) {
case "cookie":
sharedStorage[type] = createCookieStorage()
break
case "local":
sharedStorage[type] = createLocalStorage()
break
case "session":
sharedStorage[type] = createSessionStorage()
break
}
}
return sharedStorage[type] as StorageInterface<any>
}

/**
* Persist a store into a cookie
* @param {Writable<*>} store The store to enhance
* @param {string} cookieName The name of the cookie
*/
export function persistCookie<T>(store: Writable<T>, cookieName: string): PersistentStore<T> {
return persist(store, sharedCookieStorage, cookieName)
return persist(store, getStorage("cookie"), cookieName)
}
/**
* Persist a store into the browser session storage
* @param {Writable<*>} store The store to enhance
* @param {string} key The name of the key in the browser session storage
*/
export function persistBrowserSession<T>(store: Writable<T>, key: string): PersistentStore<T> {
return persist(store, sharedSessionStorage, key)
return persist(store, getStorage("session"), key)
}
/**
* Persist a store into the browser local storage
* @param {Writable<*>} store The store to enhance
* @param {string} key The name of the key in the browser local storage
*/
export function persistBrowserLocal<T>(store: Writable<T>, key: string): PersistentStore<T> {
return persist(store, sharedLocalStorage, key)
return persist(store, getStorage("local"), key)
}

/**
Expand Down

0 comments on commit 791db58

Please sign in to comment.