Change JSONCache to JSONCacheRW, change internal_get_json() to JSONCache.get()
authorNick Downing <nick@ndcode.org>
Tue, 20 Nov 2018 13:30:04 +0000 (00:30 +1100)
committerNick Downing <nick@ndcode.org>
Tue, 20 Nov 2018 13:30:04 +0000 (00:30 +1100)
SiteRoot.js
package.json

index 2f0e33a..8b4f2e7 100644 (file)
@@ -1,5 +1,5 @@
-let BuildCache = require('@ndcode/build_cache')
 let JSONCache = require('@ndcode/json_cache')
+let JSONCacheRW = require('@ndcode/json_cache_rw')
 let JSTCache = require('@ndcode/jst_cache')
 let LessCSSCache = require('@ndcode/less_css_cache')
 let MinCSSCache = require('@ndcode/min_css_cache')
@@ -10,15 +10,12 @@ let Site = require('./Site')
 let TextCache = require('@ndcode/text_cache')
 let ZipCache = require('@ndcode/zip_cache')
 let assert = require('assert')
-let disk_build = require('@ndcode/disk_build')
 let fs = require('fs')
-let path = require('path')
 let util = require('util')
 
 let fs_mkdir = util.promisify(fs.mkdir)
 let fs_readFile = util.promisify(fs.readFile)
 let fs_stat = util.promisify(fs.stat)
-let fs_writeFile = util.promisify(fs.writeFile)
 
 let SiteRoot = function(server, root) {
   if (!this instanceof SiteRoot)
@@ -26,8 +23,8 @@ let SiteRoot = function(server, root) {
   Site.call(this, server)
   this.root = root
 
-  this.build_cache_json = new BuildCache()
   this.json_cache = new JSONCache(true)
+  this.json_cache_rw = new JSONCacheRW(true)
   this.jst_cache = new JSTCache(root, {_server: server, _site: this}, true)
   this.less_css_cache = new LessCSSCache(root, true)
   this.min_css_cache = new MinCSSCache(true)
@@ -40,19 +37,6 @@ let SiteRoot = function(server, root) {
 
 SiteRoot.prototype = Object.create(Site.prototype)
 
-// this is for read-only JSON files
-// they will be reloaded from disk if modified
-SiteRoot.prototype.internal_get_json = function(pathname) {
-  return /*await*/ this.build_cache_json.get(
-    pathname,
-    async result => {
-      let text = await fs_readFile(pathname, {encoding: 'utf-8'})
-      console.log('getting', pathname, 'as json')
-      result.value = JSON.parse(text)
-    }
-  )
-}
-
 SiteRoot.prototype.internal_ensure_dir = async function(pathname) {
   try {
     await fs_mkdir(pathname)
@@ -64,35 +48,8 @@ SiteRoot.prototype.internal_ensure_dir = async function(pathname) {
   }
 }
 
-// this is for read/write JSON files
-// they will not be reloaded from disk if modified
-SiteRoot.prototype.internal_read_json =
-  async function(pathname, default_value) {
-    return /*await*/ this.json_cache.read(
-      pathname,
-      default_value
-    )
-  }
-SiteRoot.prototype.internal_write_json =
-  async function(pathname, value, timeout) {
-    return /*await*/ this.json_cache.write(
-      pathname,
-      value,
-      timeout
-    )
-  }
-SiteRoot.prototype.internal_modify_json =
-  async function(pathname, default_value, modify_func, timeout) {
-    return /*await*/ this.json_cache.modify(
-      pathname,
-      default_value,
-      modify_func,
-      timeout
-    )
-  }
-
 SiteRoot.prototype.get_json = function(pathname) {
-  return /*await*/ this.internal_get_json(this.root + pathname)
+  return /*await*/ this.json_cache.get(this.root + pathname)
 }
 
 SiteRoot.prototype.get_jst = function(pathname) {
@@ -134,14 +91,14 @@ SiteRoot.prototype.ensure_dir = async function(pathname) {
 // this is for read/write JSON files
 // they will not be reloaded from disk if modified
 SiteRoot.prototype.read_json = async function(pathname, default_value) {
-  return /*await*/ this.internal_read_json(
+  return /*await*/ this.json_cache_rw.read(
     this.root + pathname,
     default_value
   )
 }
 
 SiteRoot.prototype.write_json = async function(pathname, value, timeout) {
-  return /*await*/ this.internal_write_json(
+  return /*await*/ this.json_cache_rw.write(
     this.root + pathname,
     value,
     timeout
@@ -150,7 +107,7 @@ SiteRoot.prototype.write_json = async function(pathname, value, timeout) {
 
 SiteRoot.prototype.modify_json =
   async function(pathname, default_value, modify_func, timeout) {
-    return /*await*/ this.internal_modify_json(
+    return /*await*/ this.json_cache_rw.modify(
       this.root + pathname,
       default_value,
       modify_func,
index f6d3961..9d868a1 100644 (file)
@@ -5,14 +5,13 @@
   "main": "index.js",
   "directories": {},
   "dependencies": {
-    "@ndcode/build_cache": "^0.1.0",
-    "@ndcode/disk_build": "^0.1.1",
     "@ndcode/less_css_cache": "^0.1.0",
     "@ndcode/min_css_cache": "^0.1.0",
     "@ndcode/min_js_cache": "^0.1.0",
     "@ndcode/min_html_cache": "^0.1.0",
     "@ndcode/min_svg_cache": "^0.1.0",
-    "@ndcode/json_cache": "^0.1.1",
+    "@ndcode/json_cache": "^0.1.2",
+    "@ndcode/json_cache_rw": "^0.1.0",
     "@ndcode/jst_cache": "^0.1.0",
     "commander": "^2.18.0",
     "fs": "^0.0.1-security",