Skip to content

Commit 22225fc

Browse files
committed
more examples
1 parent 83f4645 commit 22225fc

2 files changed

Lines changed: 33 additions & 109 deletions

File tree

README.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ A storage wrapper that adds expiration functionality to `localStorage` or any ot
1010
npm install expiring-storage
1111
```
1212

13-
## Usage
13+
## Examples
14+
15+
### Basic Usage
1416

1517
```javascript
1618
import { wrapStorage } from "expirix";
@@ -25,6 +27,31 @@ storage.setItem("key", "value");
2527
const value = storage.getItem("key");
2628
```
2729

30+
### Creating a reusable storage module
31+
32+
```javascript
33+
// app-storage.js
34+
import { wrapStorage } from "expirix";
35+
36+
const wrappedLocalStorage = wrapStorage(localStorage, {
37+
expiresInSeconds: 3600,
38+
});
39+
40+
export default wrappedLocalStorage;
41+
```
42+
43+
```javascript
44+
// Using in your app
45+
import storage from "./app-storage.js";
46+
47+
// Store user preferences that expire in 1 hour
48+
storage.setItem("theme", "dark");
49+
storage.setItem("language", "en");
50+
51+
// Later...
52+
const theme = storage.getItem("theme"); // null if expired
53+
```
54+
2855
## API
2956

3057
### `wrapStorage(originalStorage, options?)`
@@ -41,7 +68,8 @@ Wraps a Storage object to add expiration functionality.
4168

4269
### `cleanupFactory(originalStorage, options?)`
4370

44-
Creates a cleanup function to wrap existing values and remove expired ones.
71+
Creates a cleanup function to remove expired values and optionally wrap unwrapped values so that they can be cleaned up later on as well.
72+
Useful to cleanup keys that might not be requested by the app anymore.
4573

4674
**Parameters:**
4775

@@ -50,6 +78,7 @@ Creates a cleanup function to wrap existing values and remove expired ones.
5078
- `expiresInSeconds` (number, optional): Time in seconds after which stored items expire
5179
- `runWhenBrowserIsIdle` (boolean, optional): Whether to run cleanup when browser is idle (default: true)
5280
- `wrapUnwrappedItems` (boolean, optional): Whether to wrap non-wrapped items with expiration metadata (default: false)
81+
If turned on, this will wrap existing values in a JSON structure. Only use it if you are sure that whatever code reads these values can handle this.
5382

5483
**Returns:** An object with a `runCleanup()` method
5584

@@ -73,7 +102,7 @@ const immediateCleanup = cleanupFactory(localStorage, {
73102
});
74103
```
75104

76-
## Wrapping Behavior Control
105+
#### Wrapping Behavior Control
77106

78107
By default, cleanup operations only remove expired items but do not modify existing unwrapped values. You can control whether cleanup should wrap existing unwrapped items with the `wrapUnwrappedItems` option.
79108

@@ -92,7 +121,7 @@ const cleanupAndWrap = cleanupFactory(localStorage, {
92121

93122
This gives you fine-grained control over when existing data gets wrapped with expiration metadata.
94123

95-
## Browser Idle Cleanup
124+
#### Browser Idle Cleanup
96125

97126
By default, the library runs cleanup operations when the browser is idle, using the `requestIdleCallback` API when available, with a simple polyfill fallback for older browsers.
98127

@@ -115,7 +144,6 @@ const immediateCleanup = cleanupFactory(localStorage, {
115144

116145
- Uses native `requestIdleCallback` when available
117146
- Falls back to `setTimeout` with 1ms delay in older browsers
118-
- Provides deadline object with `didTimeout` and `timeRemaining()` methods
119147

120148
## Features
121149

examples/idle-cleanup.js

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)