Change open()/load() to read(), and flush()/save() to write(), more consistent
authorNick Downing <downing.nick@gmail.com>
Sun, 21 Oct 2018 09:32:55 +0000 (20:32 +1100)
committerNick Downing <downing.nick@gmail.com>
Sun, 21 Oct 2018 09:50:51 +0000 (20:50 +1100)
JSONCache.js

index 11714be..ec5c14b 100644 (file)
@@ -11,9 +11,9 @@ let JSONCache = function(diag) {
   this.diag = diag || false
 }
 
-let load = (diag, key, default_value) => {
+let read = (diag, key, default_value) => {
   if (diag)
-    console.log('opening', key)
+    console.log('reading', key)
   let result = {dirty: false}
   result.done = (
     async () => {
@@ -32,10 +32,10 @@ let load = (diag, key, default_value) => {
   return result
 }
 
-JSONCache.prototype.open = async function(key, default_value) {
+JSONCache.prototype.read = async function(key, default_value) {
   let result = this.map.get(key)
   if (result === undefined) {
-    result = load(this.diag, key, default_value)
+    result = read(this.diag, key, default_value)
     this.map.set(key, result)
     await result.done
     delete result.done
@@ -46,13 +46,13 @@ JSONCache.prototype.open = async function(key, default_value) {
   return result.value
 }
 
-let save = (diag, key, result, timeout) => {
+let write = (diag, key, result, timeout) => {
   if (!result.dirty) {
     result.dirty = true
     setTimeout(
       async () => {
         if (diag)
-          console.log('flushing', key)
+          console.log('writing', key)
         result.dirty = false
         let text = JSON.stringify(result.value) + '\n'
         try {
@@ -67,7 +67,7 @@ let save = (diag, key, result, timeout) => {
   }
 }
 
-JSONCache.prototype.flush = async function(key, value, timeout) {
+JSONCache.prototype.write = async function(key, value, timeout) {
   let result = this.map.get(key)
   if (result === undefined) {
     assert(value !== undefined)
@@ -79,7 +79,7 @@ JSONCache.prototype.flush = async function(key, value, timeout) {
       await result.done
     result.value = value
   }
-  save(this.diag, key, result, timeout)
+  write(this.diag, key, result, timeout)
 }
 
 JSONCache.prototype.modify = async function(
@@ -92,7 +92,7 @@ JSONCache.prototype.modify = async function(
   // atomically check that result.done === undefined before the modification
   let result = this.map.get(key)
   if (result === undefined) {
-    result = load(this.diag, key, default_value)
+    result = read(this.diag, key, default_value)
     this.map.set(key, result)
     await result.done
     delete result.done
@@ -102,8 +102,8 @@ JSONCache.prototype.modify = async function(
       await result.done
   result.done = (
     async () => {
-      result.value = await modify_func(result.value)
-      save(this.diag, key, result, timeout)
+      await modify_func(result)
+      write(this.diag, key, result, timeout)
     }
   )()
   await result.done