return oldDone err, data
oldDone err, data
cb.apply @, args
@_WrapParams = (...types) ->
(...args) ->
_cb = polyparams.apply @, types
_cb.apply @, args
@_WrapCache = (name, cb) ->
if not cache?
cache = require(\./Cache)(@N.config)
if not @N.config?.cache
return (...args) ->
cb.apply @, args
fullName = name
(...args) ->
Resource = @
name = @name + fullName
doneIdx = @_FindDone args
_oldDone = args[doneIdx]
first = true
oldDone = (err, res) ->
if first
first := false
_oldDone err, res
else
0
if is-type \Array args[0] or is-type \Object args[0]
name += JSON.stringify args[0]
else if is-type \Number args[0]
name += args[0]
else
name += '{}'
cache.Get name, (err, cached) ~>
if not err? and cached?
cached = JSON.parse cached
if is-type \Array cached
cached = cached |> map -> Resource.Hydrate it
else
cached = Resource.Hydrate cached
return oldDone null, cached
return oldDone err if err?
args[doneIdx] = (err, res) ~>
if err?
return oldDone err
if is-type \Array res
toStore = res
else
toStore = obj-to-pairs res |> filter (.0.0 isnt \_) |> pairs-to-obj
cache.Set name, JSON.stringify(toStore), (err, status) ~>
return oldDone err if err?
oldDone null, res
watchers.push @N.Watch ~>
cb.apply @, args
@Reset = ->
watchers |> each (.Stop!)
watchers := []
module.exports = Wrappers