Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm building an event recorder. So far the code looks clean, but I feel like I'm missing something that can make this code even better.

Is composition a better solution? If yes, how can I do it properly in this context. Do you know a better way to share the Recorder across all other components? Will it be better to initialize the recorder in app.js and then pass it to the other components (mouse, keyboard)?

Mouse.js and Keyboard.js look pretty much the same, except they record different event types. Is there a way to avoid repetition (stop and record method)? Just tell me what you think about the logic.

This needs to be flexible because I have no idea how many events I will need to record in the future.

app.js

export default (params) => {
  const container = document.getElementById(params.container),
        recorder = Recorder('session'),
        keyboard = Keyboard(),
        mouse = Mouse(container);

  const startRecording = () => {
    recorder.record('Start recording');
    keyboard.record();
    mouse.record();
  }

  const stopRecording = () => {
    keyboard.stop();
    mouse.stop();
    recorder.record('Stop recording');
  }

  const init = () => {
    document.getElementById('start').onclick = (e) => {
      e.preventDefault();
      startRecording();
    };
    document.getElementById('stop').onclick = (e) => {
      e.preventDefault();
      stopRecording();
    };
  }

  return {
    init
  }
}

recorder.js

import Data from '../db/eventsLog';
import moment from 'moment';

export default (type) => {
  const record = (input) => {
    const t = moment().unix();
    Data[type].push({t, event: input});
  }

  return {
    record
  }
}

keyboard.js

import Recorder from './recorder';

export default () => {
  const recorder = Recorder('keyboard');

  const initRecorder = (recording) => {
    document.onkeydown = (e) => {
      if (recording) {
        recorder.record(e);
      }
    };
  };

  const record = () => initRecorder(true);

  const stop = () => initRecorder(false);

  return {
    record,
    stop
  }
}

mouse.js

import Recorder from './recorder';

export default (container) => {
  const recorder = Recorder('mouse');

  const startRecording = (recording, e) => {
    if (recording) {
      recorder.record(e);
    }
  };

  const initRecorder = (recording) => {
    // Mouse mouve
    container.onmousemove = (e) => startRecording(recording, e);

    // Mouse Double click
    container.ondblclick = (e) => startRecording(recording, e);

    // Mouse Click
    container.onclick = (e) => startRecording(recording, e);
  };

  const record = () => initRecorder(true);

  const stop = () => initRecorder(false);

  return {
    record,
    stop
  }
}
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.