@mblaney/holster 1.0.33 → 1.0.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mblaney/holster",
3
- "version": "1.0.33",
3
+ "version": "1.0.34",
4
4
  "description": "Holster is a service that allows synchronising data between devices using Node, Deno, Bun or the browser.",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/src/store.js CHANGED
@@ -64,35 +64,46 @@ const fileSystem = opt => {
64
64
 
65
65
  if (opt.indexedDB) {
66
66
  let db
67
- const o = indexedDB.open(dir, 1)
68
- o.onupgradeneeded = event => {
69
- event.target.result.createObjectStore(dir)
70
- }
71
- o.onerror = event => {
72
- console.log(event)
73
- }
74
- o.onsuccess = () => {
75
- db = o.result
76
- // Create the root node if it doesn't exist.
77
- if (db) {
78
- const tx = db.transaction([dir], "readonly")
79
- const req = tx.objectStore(dir).getKey("!")
80
- req.onerror = () => {
81
- console.log(`error getting key ${dir}/!`)
82
- }
83
- req.onsuccess = () => {
84
- if (!req.result) {
85
- const tx = db.transaction([dir], "readwrite")
86
- const req = tx.objectStore(dir).put(root, "!")
87
- req.onerror = () => {
88
- console.log(`error putting root on ${dir}/!`)
67
+ const dbReady = new Promise((resolve, reject) => {
68
+ const o = indexedDB.open(dir, 1)
69
+ o.onupgradeneeded = event => {
70
+ event.target.result.createObjectStore(dir)
71
+ }
72
+ o.onerror = event => {
73
+ console.log(event)
74
+ reject(event)
75
+ }
76
+ o.onsuccess = () => {
77
+ db = o.result
78
+ // Create the root node if it doesn't exist.
79
+ if (db) {
80
+ const tx = db.transaction([dir], "readonly")
81
+ const req = tx.objectStore(dir).getKey("!")
82
+ req.onerror = () => {
83
+ console.log(`error getting key ${dir}/!`)
84
+ reject(req.error)
85
+ }
86
+ req.onsuccess = () => {
87
+ if (!req.result) {
88
+ const tx = db.transaction([dir], "readwrite")
89
+ const req = tx.objectStore(dir).put(root, "!")
90
+ req.onerror = () => {
91
+ console.log(`error putting root on ${dir}/!`)
92
+ reject(req.error)
93
+ }
94
+ req.onsuccess = () => {
95
+ resolve()
96
+ }
97
+ } else {
98
+ resolve()
89
99
  }
90
100
  }
101
+ } else {
102
+ console.log("error indexedDB not available")
103
+ reject(new Error("indexedDB not available"))
91
104
  }
92
- } else {
93
- console.log("error indexedDB not available")
94
105
  }
95
- }
106
+ })
96
107
 
97
108
  return {
98
109
  get: (file, cb) => {
@@ -106,24 +117,14 @@ const fileSystem = opt => {
106
117
  cb(null, req.result)
107
118
  }
108
119
  }
109
- if (db) {
110
- _get(file, cb)
111
- return
112
- }
113
120
 
114
- let retry = 0
115
- const interval = setInterval(() => {
116
- if (db) {
117
- clearInterval(interval)
121
+ dbReady
122
+ .then(() => {
118
123
  _get(file, cb)
119
- return
120
- }
121
-
122
- if (retry++ > 5) {
123
- clearInterval(interval)
124
- cb("error indexedDB not available")
125
- }
126
- }, 1000)
124
+ })
125
+ .catch(err => {
126
+ cb(err)
127
+ })
127
128
  },
128
129
  put: (file, data, cb) => {
129
130
  const _put = (file, data, cb) => {
@@ -136,24 +137,14 @@ const fileSystem = opt => {
136
137
  cb(null)
137
138
  }
138
139
  }
139
- if (db) {
140
- _put(file, data, cb)
141
- return
142
- }
143
140
 
144
- let retry = 0
145
- const interval = setInterval(() => {
146
- if (db) {
147
- clearInterval(interval)
141
+ dbReady
142
+ .then(() => {
148
143
  _put(file, data, cb)
149
- return
150
- }
151
-
152
- if (retry++ > 5) {
153
- clearInterval(interval)
154
- cb("error indexedDB not available")
155
- }
156
- }, 1000)
144
+ })
145
+ .catch(err => {
146
+ cb(err)
147
+ })
157
148
  },
158
149
  list: cb => {
159
150
  const _list = cb => {
@@ -165,25 +156,15 @@ const fileSystem = opt => {
165
156
  cb()
166
157
  }
167
158
  }
168
- if (db) {
169
- _list(cb)
170
- return
171
- }
172
159
 
173
- let retry = 0
174
- const interval = setInterval(() => {
175
- if (db) {
176
- clearInterval(interval)
160
+ dbReady
161
+ .then(() => {
177
162
  _list(cb)
178
- return
179
- }
180
-
181
- if (retry++ > 5) {
182
- clearInterval(interval)
183
- console.log("error indexedDB not available")
163
+ })
164
+ .catch(err => {
165
+ console.log("error indexedDB not available:", err)
184
166
  cb()
185
- }
186
- }, 1000)
167
+ })
187
168
  },
188
169
  }
189
170
  }