Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Very basic logging
Browse files Browse the repository at this point in the history
ref #1
  • Loading branch information
mattwynne committed Sep 12, 2018
1 parent 8f54a4a commit 2d0e4a8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
21 changes: 18 additions & 3 deletions lib/feynman/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
21 changes: 20 additions & 1 deletion lib/feynman/core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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!')
})
})
})
13 changes: 6 additions & 7 deletions lib/feynman/definitions.js
Original file line number Diff line number Diff line change
@@ -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),
},
Expand Down Expand Up @@ -76,5 +75,5 @@ module.exports = {
tasks: raw => Definition(raw),
interactions: raw => Definition(raw),
task: (paramName, subTasks) => MakeTask(paramName, subTasks),
action: Interaction,
action: DefineInteraction,
}

0 comments on commit 2d0e4a8

Please sign in to comment.