Skip to content

Commit

Permalink
outward modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrodger committed Sep 26, 2016
1 parent 2ab65c3 commit 0a584ea
Show file tree
Hide file tree
Showing 6 changed files with 412 additions and 256 deletions.
138 changes: 129 additions & 9 deletions lib/actions.js
Expand Up @@ -8,6 +8,7 @@ var Assert = require('assert')

var _ = require('lodash')
var Jsonic = require('jsonic')
var Lrucache = require('lru-cache')


var Common = require('./common')
Expand Down Expand Up @@ -54,12 +55,63 @@ exports.inward = {
act_not_found: inward_act_not_found,
validate_msg: inward_validate_msg,
warnings: inward_warnings,
msg_meta: inward_msg_meta
msg_meta: inward_msg_meta,
act_stats: inward_act_stats
}


function inward_act_default (ctxt, msg) {
exports.outward = {
act_cache: outward_act_cache,
res_object: outward_res_object,
act_stats: outward_act_stats
}


function inward_act_stats (ctxt, data) {
if (!ctxt.actmeta) {
return
}

var private$ = ctxt.seneca.private$
++private$.stats.act.calls

var pattern = ctxt.actmeta.pattern

var actstats = (private$.stats.actmap[pattern] =
private$.stats.actmap[pattern] || {})


++actstats.calls
}


function outward_act_stats (ctxt, data) {
if (!ctxt.actmeta || ctxt.cached$) {
return
}

var private$ = ctxt.seneca.private$
++private$.stats.act.done

var pattern = ctxt.actmeta.pattern

var actstats = (private$.stats.actmap[pattern] =
private$.stats.actmap[pattern] || {})


if (data.err) {
private$.stats.act.fails++
++actstats.fails
}
else {
++actstats.done
}
}


function inward_act_default (ctxt, data) {
var so = ctxt.options
var msg = data.msg

// TODO: existence of pattern action needs own indicator flag
if (!ctxt.actmeta) {
Expand Down Expand Up @@ -93,8 +145,9 @@ function inward_act_default (ctxt, msg) {
}


function inward_act_not_found (ctxt, msg) {
function inward_act_not_found (ctxt, data) {
var so = ctxt.options
var msg = data.msg

if (!ctxt.actmeta) {
return {
Expand All @@ -113,8 +166,10 @@ function inward_act_not_found (ctxt, msg) {
}


function inward_validate_msg (ctxt, msg) {
function inward_validate_msg (ctxt, data) {
var so = ctxt.options
var msg = data.msg

Assert(ctxt.actmeta)

if (!_.isFunction(ctxt.actmeta.validate)) {
Expand Down Expand Up @@ -153,15 +208,20 @@ function inward_validate_msg (ctxt, msg) {

// Check if actid has already been seen, and if action cache is active,
// then provide cached result, if any. Return true in this case.
function inward_act_cache (ctxt, msg) {
function inward_act_cache (ctxt, data) {
var so = ctxt.options
var msg = data.msg

var actid = msg.id$ || msg.actid$
var private$ = ctxt.seneca.private$

if (actid != null && so.actcache.active) {
var actdetails = ctxt.seneca.private$.actcache.get(actid)
private$.actcache = private$.actcache || Lrucache({ max: so.actcache.size })

var actdetails = private$.actcache.get(actid)

if (actdetails) {
ctxt.seneca.private$.stats.act.cache++
private$.stats.act.cache++

var err = actdetails.result[0]
var res = actdetails.result[1]
Expand All @@ -179,14 +239,17 @@ function inward_act_cache (ctxt, msg) {
}
}

ctxt.cached$ = true

return out
}
}
}


function inward_warnings (ctxt, msg) {
function inward_warnings (ctxt, data) {
var so = ctxt.options

Assert(ctxt.actmeta)

if (so.debug.deprecation && ctxt.actmeta.deprecate) {
Expand All @@ -201,7 +264,9 @@ function inward_warnings (ctxt, msg) {
}


function inward_msg_meta (ctxt, msg) {
function inward_msg_meta (ctxt, data) {
var msg = data.msg

Assert(ctxt.actmeta)

msg.meta$.pattern = ctxt.actmeta.pattern
Expand All @@ -210,3 +275,58 @@ function inward_msg_meta (ctxt, msg) {
msg.meta$.plugin_tag = ctxt.actmeta.plugin_tag
}


// Store result in action cache
function outward_act_cache (ctxt, data) {
var so = ctxt.options
var msg = data.msg
var res = data.res
var err = data.err

var actid = msg.meta$.id
var private$ = ctxt.seneca.private$

if (actid != null && so.actcache.active) {
private$.actcache = private$.actcache || Lrucache({ max: so.actcache.size })

private$.actcache.set(actid, {
result: [err, res],
actmeta: ctxt.actmeta,
when: Date.now()
})
}
}


function outward_res_object (ctxt, data) {
var so = ctxt.options
var msg = data.msg
var res = data.res
var err = data.err

var not_object =
err == null &&
res != null &&
!(_.isPlainObject(res) ||
_.isArray(res) ||
!!res.entity$ ||
!!res.force$)

var not_legacy =
!(msg.cmd === 'generate_id' ||
msg.note === true ||
msg.cmd === 'native' ||
msg.cmd === 'quickcode')

if (so.strict.result && not_legacy && not_object) {
return {
kind: 'error',
code: 'result_not_objarr',
info: {
pattern: ctxt.actmeta.pattern,
args: Util.inspect(Common.clean(msg)).replace(/\n/g, ''),
result: res
}
}
}
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -105,7 +105,7 @@
"eslint-config-seneca": "3.x.x",
"eslint-plugin-hapi": "4.x.x",
"eslint-plugin-standard": "2.x.x",
"json-stringify-safe": "^5.0.1",
"json-stringify-safe": "5.x.x",
"lab": "11.0.x",
"seneca-entity": "1.3.x",
"seneca-error-test": "0.2.x"
Expand Down

0 comments on commit 0a584ea

Please sign in to comment.