Changeset View
Changeset View
Standalone View
Standalone View
lib/app/run-action-curry.js
"use strict"; | |||||
const assert = require("assert"); | |||||
const locreq = require("locreq")(__dirname); | |||||
const ActionResultCache = new WeakMap(); | const ActionResultCache = new WeakMap(); | ||||
function hash_call(subject_path, action_name, params) { | function hash_call(subject_path, action_name, params) { | ||||
const to_map = [subject_path, action_name, params]; | const to_map = [subject_path, action_name, params]; | ||||
return to_map.map(e => JSON.stringify(e)).join(", "); | return to_map.map(e => JSON.stringify(e)).join(", "); | ||||
} | } | ||||
function run_action_curry(app) { | function run_action_curry(app) { | ||||
Show All 15 Lines | ) { | ||||
original_context.total = original_context.total + 1 || 1; | original_context.total = original_context.total + 1 || 1; | ||||
if (ActionResultCache.get(original_context).has(hash)) { | if (ActionResultCache.get(original_context).has(hash)) { | ||||
original_context.from_cache = original_context.from_cache + 1 || 1; | original_context.from_cache = original_context.from_cache + 1 || 1; | ||||
return ActionResultCache.get(original_context).get(hash); | return ActionResultCache.get(original_context).get(hash); | ||||
} | } | ||||
let subject = null; | let subject = null; | ||||
const promise = app.RootSubject.get_subject(subject_path) | const promise = app.RootSubject.get_subject(subject_path) | ||||
.then(function(_subject) { | .then(_subject => { | ||||
subject = _subject; | subject = _subject; | ||||
return app.emit( | return app.emit( | ||||
{ | { | ||||
when: "before", | when: "before", | ||||
subject_path: subject_path.join("."), | subject_path: subject_path.join("."), | ||||
action: action_name, | action: action_name, | ||||
metadata: { | metadata: { | ||||
context, | context, | ||||
}, | }, | ||||
}, | }, | ||||
params | params | ||||
); | ); | ||||
}) | }) | ||||
.then(params => | .then(_params => | ||||
subject.perform_action(context, action_name, params) | subject.perform_action(context, action_name, _params) | ||||
) | ) | ||||
.then(response => | .then(response => | ||||
app.emit( | app.emit( | ||||
{ | { | ||||
when: "after", | when: "after", | ||||
subject_path: subject_path.join("."), | subject_path: subject_path.join("."), | ||||
action: action_name, | action: action_name, | ||||
metadata: { | metadata: { | ||||
context, | context, | ||||
params, | params, | ||||
}, | }, | ||||
}, | }, | ||||
response | response | ||||
) | ) | ||||
); | ); | ||||
if (action_name === "show") { | if (action_name === "show") { | ||||
ActionResultCache.get(original_context).set(hash, promise); | ActionResultCache.get(original_context).set(hash, promise); | ||||
} else { | } else { | ||||
ActionResultCache.delete(original_context); | ActionResultCache.delete(original_context); | ||||
} | } | ||||
return promise; | return promise; | ||||
}; | }; | ||||
} | } | ||||
module.exports = run_action_curry; | module.exports = run_action_curry; |