corebasic 1.0.183 → 1.0.185

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/libs/dip.js CHANGED
@@ -10,8 +10,8 @@ export const remove = Elabase.remove
10
10
 
11
11
  export const batchBegin = Elabase.batchBegin
12
12
  export const batchAbort = Elabase.batchAbort
13
- export const batchReset = Elabase.batchReset
14
13
  export const batchSubmit = Elabase.batchSubmit
14
+ export const batch = Elabase.batch
15
15
 
16
16
  export const shard_stats = Elabase.shard_stats
17
17
  export let config = Elabase.config
package/libs/elabase.js CHANGED
@@ -115,23 +115,33 @@ function generate_suffix(meta) {
115
115
  let BATCH_COUNTER = 0n;
116
116
  let batches = {}
117
117
 
118
- export function batchBegin() {
118
+ export function batchBegin() { // Manual Lifecycle. If you forget to batchSubmit() or batchAbort(), then memory leak due to dangling batch
119
119
  const uid = ++BATCH_COUNTER; // Safe. No skew in async ++BATCH_COUNTER as single thread and ++COUNTER is atomic in event loop sense. ⚠️ Only breaks on worker_threads or cluster (multi-process)
120
120
  batches[uid] = []
121
121
  return uid
122
122
  }
123
- export function batchAbort(batch_id) {
124
- delete batches[batch_id]
123
+ export function batchAbort(batch) { // Mandatory to prevent meory leak due to dangling batch if batchBegin() is called without batchSubmit()
124
+ delete batches[batch]
125
125
  }
126
- export function batchReset() {
127
- batchAbort()
126
+
127
+ export const batch = async (callback) => { // Convenience function with auto lifecyle management and auto submit when callback returns. abort is called appropriately.
128
+ const batch = batchBegin()
129
+
130
+ try {
131
+ await callback(batch)
132
+ return await batchSubmit(batch)
133
+ } catch (err) {
134
+ batchAbort(batch)
135
+ throw err
136
+ }
128
137
  }
129
- export function batchSubmit(batch_id, arg) {
130
- if (!batch_id || !batches[batch_id] || !batches[batch_id].length)
138
+
139
+ export function batchSubmit(batch, arg) {
140
+ if (!batch || !batches[batch] || !batches[batch].length)
131
141
  throw {message: `Error: Invalid batch in Dip.batchSubmit()`}
132
- let txns = batches[batch_id]
133
- let mode = batches[batch_id].some(txn => txn.insert || txn.update || txn.delete) ? "command" : "query"
134
- delete batches[batch_id]
142
+ let txns = batches[batch]
143
+ let mode = batches[batch].some(txn => txn.insert || txn.update || txn.delete) ? "command" : "query"
144
+ delete batches[batch]
135
145
  return new Promise((resolve, reject) => resolve())
136
146
  .then(() => execute({ "batch": txns, mode, version: arg?.version, compression: arg?.compression }))
137
147
  .then (result => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.183",
4
+ "version": "1.0.185",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "scripts": {