Skip to content

Include metadata that can be emitted along with events #81

@lukekarrys

Description

@lukekarrys

proc-log is designed to be Just an Event Emitter ™️ with some well known event names.

This means it passes along all arguments when emitting to consumers. This is a good approach because there are rarely changes that proc-log needs to make outside of adding new methods. If a producer sends arguments then it is the consumers responsibility to consume them somehow.

Our primary use case is displaying things in a terminal. So all arguments are passed to util.format which can take anything as input. We might be displaying a string, number, plain function or custom class with a [util.inspect.custom] method.

BUT most of the examples I wrote out in npm/statusboard#810 rely on some kind of well-known metadata to be useful. This is why I think proc-log should have some way to indicate "this event contains an argument that is special and belongs to proc-log. It will have data in it that you should look at differently than the rest"

Possible Solutions

Producers

proc-log itself should have some sort of ergonomic API to send this metadata.

meta() function on each method

const { output, log } = require('proc-log')

// Each function gets an additional `meta` function which indicates
// the first parameter is special
output.standard.meta(
  { colors: false, template: './arbitrary' },
  'anything', 'else', new You(), { want: new Set() }
)

// could also be the last parameter
output.standard.meta('anything', 'else', new You(), { want: new Set() }, {
  colors: false,
  template: './arbitrary'
})

top level meta() function

const { output, log, meta } = require('proc-log')

// Wrap an object in the meta function which will somehow mark it as special
output.standard(
  meta({ colors: false, template: './arbitrary' }),
  'anything', 'else', new You(), { want: new Set() }
)

// Can't enforce which location its used in, which seems bad
output.standard('anything', 'else', new You(), { want: new Set() }, meta({
  colors: false,
  template: './arbitrary'
}))

use a symbol to set a key

const { output, log, META } = require('proc-log')

// Still need to figure out which position the argument goes in
output.standard('anything', 'else', new You(), { want: new Set() }, {
  [META]: true,
  colors: false,
  template: './arbitrary'
}))

Consumers

Consumers listening with process.on(eventName) should have some ergonomic way to know that an argument is metadata.

level argument is metadata too

const { output, meta } = require('proc-log')

// This would be a breaking change
process.on('output', ({ level, ...metadata }, ...args) => 
  if (level === 'standard' && metadata.templates) {
    // It is a template
  }
})

helper to find metadata in args

const { output, meta } = require('proc-log')

process.on('output', (level, ...rawArgs) => {
  const [args, metadata] = meta.getMeta(rawArgs)
  if (level === 'standard' && metadata.templates) {
    // It is a template
  }
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions