1
- # Persistent Client (Browser) Storage
2
-
3
- <a href =" https://www.patreon.com/bePatron?u=20396046 " >
4
- <img src =" https://c5.patreon.com/external/logo/become_a_patron_button@2x .png " width = " 160 " >
1
+ [ ![ support ] ( https://img.shields.io/badge/support-GitHub-white )] ( https://github.com/sponsors/dr-dimitru )
2
+ [ ![ support ] ( https://img.shields.io/badge/support-PayPal-white )] ( https://paypal.me/veliovgroup )
3
+ <a href =" https://ostr.io/info/built-by-developers-for-developers " >
4
+ <img src =" https://ostr.io/apple-touch-icon-60x60 .png " height = " 20 " >
5
5
</a >
6
6
7
+ # Persistent Browser (Client) Storage
8
+
7
9
- 👷 __ 100% Tests coverage__ ;
8
10
- 📦 No external dependencies;
9
11
- 💪 Bulletproof persistent Client storage;
@@ -35,85 +37,98 @@ meteor npm install --save ClientStorage
35
37
36
38
``` js
37
39
var ClientStorage = require (' ClientStorage' ).ClientStorage ;
40
+ var clientStorage = new ClientStorage ();
41
+ ```
42
+
43
+ ### ES6 Import:
44
+
45
+ ``` js
46
+ import { ClientStorage } from ' ClientStorage' ;
47
+ const clientStorage = new ClientStorage ();
38
48
```
39
49
40
50
### ES6 Import (Meteor):
41
51
42
52
``` js
43
53
import { ClientStorage } from ' meteor/ostrio:cstorage' ;
54
+ const clientStorage = new ClientStorage ();
44
55
```
45
56
46
57
## Usage:
47
58
48
- - ` ClientStorage .get('key')` - Read a record. If the key doesn't exist a * undefined* value will be returned;
59
+ - ` clientStorage .get('key')` - Read a record. If the key doesn't exist a * undefined* value will be returned;
49
60
- ` key ` - {* String* } - Record's key;
50
- - ` ClientStorage .set('key', value[, ttl])` - Create/overwrite a value in storage;
61
+ - ` clientStorage .set('key', value[, ttl])` - Create/overwrite a value in storage;
51
62
- ` key ` - {* String* } - Record's key;
52
63
- ` value ` - {* String* |[ * mix* ] |* Boolean* |* Object* } - Record's value (content);
53
64
- ` ttl ` - {* Number* } — [ Optional] Record's TTL in seconds;
54
- - ` ClientStorage .remove('key')` - Remove a record;
65
+ - ` clientStorage .remove('key')` - Remove a record;
55
66
- ` key ` - {* String* } - Record's key;
56
- - ` ClientStorage .has('key')` - Check whether a record exists, returns a boolean value;
67
+ - ` clientStorage .has('key')` - Check whether a record exists, returns a boolean value;
57
68
- ` key ` - {* String* } - Record's key;
58
- - ` ClientStorage .keys()` - Returns an array of all storage keys;
59
- - ` ClientStorage .empty()` - Empty storage (remove all key/value pairs). __ Use with caution! (* May remove cookies which weren't set by you* )__ .
69
+ - ` clientStorage .keys()` - Returns an array of all storage keys;
70
+ - ` clientStorage .empty()` - Empty storage (remove all key/value pairs). __ Use with caution! (* May remove cookies which weren't set by you* )__ .
60
71
61
72
## Alternate usage:
62
73
63
- ### Use ` cookies ` only :
74
+ By default ClientStorage package handle selecting storage driver in the next order (descending priority) :
64
75
65
- To use ` cookies ` as a driver for ` ClientStorage ` create new instance of ` clientStorage ` (* camel-case, first letter __ lower-case__ * ):
76
+ 1 . ` localStorage `
77
+ 2 . ` cookies `
78
+ 3 . ` js ` (* JS Object driven storage* )
66
79
67
- ``` js
68
- var clientStorage = require (' ClientStorage' ).clientStorage ;
69
- var csCookies = new clientStorage (' cookies' );
70
- csCookies .has (' locale' ); // false
71
- csCookies .set (' locale' , ' en_US' ); // true
72
- ```
80
+ To alter priority pass "preferred driver" to ` new ClientStorage(driverName) ` constructor.
81
+
82
+ ### Use ` cookies ` only:
73
83
74
- or in ES6 (Meteor) :
84
+ Pass ` cookies ` as an argument to new instance of ` ClientStorage ` :
75
85
76
86
``` js
77
- import { clientStorage } from ' meteor/ostrio:cstorage ' ;
78
- const csLocalStorage = new clientStorage (' cookies' );
79
- csLocalStorage .has (' locale' ); // false
80
- csLocalStorage .set (' locale' , ' en_US' ); // true
87
+ const { clientStorage } = require ( ' ClientStorage ' ) ;
88
+ var cookiesStorage = new ClientStorage (' cookies' );
89
+ cookiesStorage .has (' locale' ); // false
90
+ cookiesStorage .set (' locale' , ' en_US' ); // true
81
91
```
82
92
83
93
### Use ` localStorage ` only:
84
94
85
- To use ` localStorage ` as a driver for ` ClientStorage ` create new instance of ` clientStorage ` ( * camel-case, first letter __ lower-case __ * ) :
95
+ Pass ` localStorage ` as an argument to new instance of ` ClientStorage ` :
86
96
87
97
``` js
88
- var clientStorage = require (' ClientStorage' ). clientStorage ;
89
- var csLocalStorage = new clientStorage (' localStorage' );
90
- csLocalStorage .has (' locale' ); // false
91
- csLocalStorage .set (' locale' , ' en_US' ); // true
98
+ const { clientStorage } = require (' ClientStorage' );
99
+ var locStorage = new ClientStorage (' localStorage' );
100
+ locStorage .has (' locale' ); // false
101
+ locStorage .set (' locale' , ' en_US' ); // true
92
102
```
93
103
94
- or in ES6 (Meteor):
104
+ ### Use ` js ` only:
105
+
106
+ Pass ` js ` (* in-memory js object* ) as an argument to new instance of ` ClientStorage ` :
95
107
96
108
``` js
97
- import { clientStorage } from ' meteor/ostrio:cstorage ' ;
98
- const csLocalStorage = new clientStorage ( ' localStorage ' );
99
- csLocalStorage .has (' locale' ); // false
100
- csLocalStorage .set (' locale' , ' en_US' ); // true
109
+ const { clientStorage } = require ( ' ClientStorage ' ) ;
110
+ var jsStorage = new ClientStorage ( ' js ' );
111
+ jsStorage .has (' locale' ); // false
112
+ jsStorage .set (' locale' , ' en_US' ); // true
101
113
```
102
114
103
115
__ Note:__ * All instances are sharing same cookie and localStorage records!*
104
116
105
117
## [ Meteor] Add reactivity:
106
118
119
+ Persistent ` ReactiveVar ` implementation:
120
+
107
121
``` js
108
- import { ReactiveVar } from ' meteor/reactive-var' ;
122
+ import { ReactiveVar } from ' meteor/reactive-var' ;
109
123
import { ClientStorage } from ' meteor/ostrio:cstorage' ;
124
+ const clientStorage = new ClientStorage ();
110
125
111
- const persistentReactive = (name , initial = false ) => {
126
+ const persistentReactive = (name , initial = undefined ) => {
112
127
let reactive;
113
- if (ClientStorage .has (name)) {
114
- reactive = new ReactiveVar (ClientStorage .get (name));
128
+ if (clientStorage .has (name)) {
129
+ reactive = new ReactiveVar (clientStorage .get (name));
115
130
} else {
116
- ClientStorage .set (name, initial);
131
+ clientStorage .set (name, initial);
117
132
reactive = new ReactiveVar (initial);
118
133
}
119
134
@@ -123,43 +138,43 @@ const persistentReactive = (name, initial = false) => {
123
138
return ;
124
139
}
125
140
reactive .curValue = newValue;
126
- ClientStorage .set (name, newValue);
141
+ clientStorage .set (name, newValue);
127
142
reactive .dep .changed ();
128
143
};
129
144
130
145
return reactive;
131
146
};
132
147
133
- const UILayout = persistentReactive (' UILayout ' , ' two-columns' );
134
- UILayout .get (); // two-columns
135
- UILayout .set (' single-column' );
148
+ const layout = persistentReactive (' ui-layout ' , ' two-columns' );
149
+ layout .get (); // two-columns
150
+ layout .set (' single-column' );
136
151
```
137
152
138
153
## Examples:
139
154
140
155
``` js
141
- var ClientStorage = require (' ClientStorage' ).ClientStorage ;
156
+ const clientStorage = new ( require (' ClientStorage' ).ClientStorage ) ;
142
157
143
- ClientStorage .set (' locale' , ' en' ); // true
144
- ClientStorage .set (' country' , ' usa' ); // true
145
- ClientStorage .set (' gender' , ' male' ); // true
158
+ clientStorage .set (' locale' , ' en' ); // true
159
+ clientStorage .set (' country' , ' usa' ); // true
160
+ clientStorage .set (' gender' , ' male' ); // true
146
161
147
- ClientStorage .get (' gender' ); // male
162
+ clientStorage .get (' gender' ); // male
148
163
149
- ClientStorage .has (' locale' ); // true
150
- ClientStorage .has (' city' ); // false
164
+ clientStorage .has (' locale' ); // true
165
+ clientStorage .has (' city' ); // false
151
166
152
- ClientStorage .keys (); // ['locale', 'country', 'gender']
167
+ clientStorage .keys (); // ['locale', 'country', 'gender']
153
168
154
- ClientStorage .remove (' locale' ); // true
155
- ClientStorage .get (' locale' ); // undefined
169
+ clientStorage .remove (' locale' ); // true
170
+ clientStorage .get (' locale' ); // undefined
156
171
157
- ClientStorage .keys (); // ['country', 'gender']
172
+ clientStorage .keys (); // ['country', 'gender']
158
173
159
- ClientStorage .empty (); // true
160
- ClientStorage .keys (); // []
174
+ clientStorage .empty (); // true
175
+ clientStorage .keys (); // []
161
176
162
- ClientStorage .empty (); // false
177
+ clientStorage .empty (); // false
163
178
```
164
179
165
180
## Running Tests
@@ -183,5 +198,6 @@ MONGO_URL="mongodb://127.0.0.1:27017/client-storage-tests" meteor test-packages
183
198
184
199
## Support this project:
185
200
186
- - [ Become a patron] ( https://www.patreon.com/bePatron?u=20396046 ) — support my open source contributions with monthly donation
201
+ - [ Sponsor via GitHub] ( https://github.com/sponsors/dr-dimitru )
202
+ - [ Support via PayPal] ( https://paypal.me/veliovgroup )
187
203
- Use [ ostr.io] ( https://ostr.io ) — [ Monitoring] ( https://snmp-monitoring.com ) , [ Analytics] ( https://ostr.io/info/web-analytics ) , [ WebSec] ( https://domain-protection.info ) , [ Web-CRON] ( https://web-cron.info ) and [ Pre-rendering] ( https://prerendering.com ) for a website
0 commit comments