hyper icon indicating copy to clipboard operation
hyper copied to clipboard

Build data cache extension on hyper connect

Open tripott opened this issue 3 years ago • 1 comments

transferred from hyper63/hyper-cloud#485

tripott avatar Feb 03 '22 17:02 tripott

Tom made some progress on a utility cache counter increment method

import { connect } from 'hyper-connect'
const hyper = connect(process.env.HYPER)

const increment = result => result.count ? result.count + 1 : 1
const update = name => count => hyper.cache.set(name, { count })
const resolve = x => result => result.ok ? x : Promise.reject({ status: 500, error: 'Could not find cache' })
const decrement = result => result.count ? result.count - 1 : 0

export function incrementCounter(type) {
  return function (x) {
    return hyper.cache.get(`${type}-counter`)
      .then(increment)
      .then(update(`${type}-counter`))
      .then(resolve(x))
  }

}

export function decrementCounter(type) {
  return function (x) {
    return hyper.cache.get(`${type}-counter`)
      .then(decrement)
      .then(update(`${type}-counter`))
      .then(resolve(x))
  }

}

tripott avatar Feb 03 '22 18:02 tripott