-
Notifications
You must be signed in to change notification settings - Fork 0
/
blox-store.js
93 lines (87 loc) · 1.93 KB
/
blox-store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';
/**
* `blox-store`
* get and set JSON data from local storage
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class BloxStore extends PolymerElement {
static get template() {
return html`
<style>
:host {
display: block;
}
</style>
<template is="dom-if" if="{{debug}}">
<p>[[key]]</p>
<p>[[value]]</p>
<p>[[operation]]</p>
<p>[[result]]</p>
</template>
`;
}
static get properties() {
return {
key: {
type: String,
},
value: {
type: String,
},
debug: {
type: Boolean,
value: false,
},
operation: {
type: String,
observer: "_start"
},
result: {
type: String,
notify: true,
reflectToAttribute: true,
},
overwrite: {
type: Boolean,
value: false,
},
error: {
type: String,
notify: true,
reflectToAttribute: true,
},
};
}
_start(){
if(this.operation == 'set' && this.key && this.value){
this.set(this.key, this.value)
} else if (this.operation == 'get' && this.key){
this.get(this.key)
} else if (this.operation == 'delete' && this.key){
this.delete(this.key)
} else {
this.error = "wrong arguments"
}
}
set(key, value){
return new Promise((resolve, reject) => {
this.result = localStorage.setItem(key, JSON.stringify(value));
resolve(this.result)
})
}
get(key){
return new Promise((resolve, reject) => {
this.result = JSON.parse(localStorage.getItem(key));
resolve(this.result)
})
}
delete(key){
return new Promise((resolve, reject) => {
this.result = localStorage.removeItem(key)
resolve(this.result)
})
}
} window.customElements.define('blox-store', BloxStore);