From 2d0e4a8e23cc7aebbd5b54d065e83f98b393ff86 Mon Sep 17 00:00:00 2001 From: Matt Wynne Date: Tue, 11 Sep 2018 22:10:14 -0400 Subject: [PATCH] Very basic logging ref #1 --- lib/feynman/core.js | 21 ++++++++++++++++++--- lib/feynman/core.spec.js | 21 ++++++++++++++++++++- lib/feynman/definitions.js | 13 ++++++------- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/lib/feynman/core.js b/lib/feynman/core.js index d1a1026..6df3c24 100644 --- a/lib/feynman/core.js +++ b/lib/feynman/core.js @@ -105,17 +105,32 @@ const Perspective = (name, definition) => { if (definition) definition(registerHandler) return perspective } -const Task = (id, params = {}) => + +const NoLog = () => {} + +const Task = (id, params = {}, description = Description()) => Object.assign( - ({ actor, perspective }) => - actor.attemptsTo(perspective.handlerFor({ id, params })), + ({ actor, perspective, log = NoLog }) => { + log(description.toString()) + return actor.attemptsTo(perspective.handlerFor({ id, params })) + }, { id } ) +const Interaction = (fn, description) => ({ + actor, + log = NoLog, + ...abilities +}) => { + log(description.toString()) + return fn({ actor, log, ...abilities }) +} + module.exports = { Actor, Perspective, Task, + Interaction, Description, Id, } diff --git a/lib/feynman/core.spec.js b/lib/feynman/core.spec.js index 0cff8c9..7d8cd3b 100644 --- a/lib/feynman/core.spec.js +++ b/lib/feynman/core.spec.js @@ -2,7 +2,7 @@ const assert = require('assert') const sinon = require('sinon') -const { Perspective, Actor, Task } = require('./core') +const { Perspective, Actor, Task, Description, Interaction } = require('./core') describe('feynman core objects', () => { context('handling tasks through perspectives', () => { @@ -142,4 +142,23 @@ describe('feynman core objects', () => { ) ) }) + + describe('logging', () => { + it('logs when a task is attempted', async () => { + const log = sinon.spy() + const doSomething = Task('doSomething', {}, Description('Do something!')) + const perspective = Perspective('Perspective', handle => { + handle({ id: 'doSomething' }, () => () => {}) + }) + await Actor({ log }, perspective).attemptsTo(doSomething) + sinon.assert.calledWith(log, 'Do something!') + }) + + it('logs when an interaction is attempted', async () => { + const log = sinon.spy() + const doSomething = Interaction(() => {}, Description('Do something!')) + await Actor({ log }, Perspective()).attemptsTo(doSomething) + sinon.assert.calledWith(log, 'Do something!') + }) + }) }) diff --git a/lib/feynman/definitions.js b/lib/feynman/definitions.js index 919fc83..9f8a5cc 100644 --- a/lib/feynman/definitions.js +++ b/lib/feynman/definitions.js @@ -1,19 +1,18 @@ 'use strict' -const { Description, Id, Task } = require('./core') +const { Description, Id, Task, Interaction } = require('./core') -const Interaction = (fn, description = Description()) => { +const DefineInteraction = (fn, description = Description()) => { if (typeof fn !== 'function') throw new Error('An action must be a function') - const handler = (...args) => fn(...args) - return Object.assign(handler, { + return Object.assign(Interaction(fn, description), { description, - withContext: ({ description }) => Interaction(fn, description), + withContext: ({ description }) => DefineInteraction(fn, description), }) } const DefineTask = (definition, context) => { return Object.assign( - Task(context.id, context.params), + Task(context.id, context.params, context.description), { withContext: context => DefineTask(definition, context), }, @@ -76,5 +75,5 @@ module.exports = { tasks: raw => Definition(raw), interactions: raw => Definition(raw), task: (paramName, subTasks) => MakeTask(paramName, subTasks), - action: Interaction, + action: DefineInteraction, }