examples/webpack/src/script.js   A
last analyzed

Complexity

Total Complexity 12
Complexity/F 1

Size

Lines of Code 94
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 94
rs 10
wmc 12
mnd 0
bc 12
fnc 12
bpm 1
cpm 1
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A VoxEngine.addEventListener 0 72 1
1
/* global VoxEngine, AppEvents */
2
3
var SDK = require('@ama-team/voxengine-sdk')
4
var Slf4j = SDK.Logger.Slf4j
5
var LogLevel = SDK.Logger.Level
6
7
// mute rest client, but increase basic client verbosity
8
Slf4j.setLevel('ama-team.voxengine-sdk.http.basic', LogLevel.Debug)
9
Slf4j.setLevel('ama-team.voxengine-sdk.http.rest', LogLevel.Off)
10
// Let's receive every message about task queues
11
Slf4j.setLevel('ama-team.voxengine-sdk.concurrent.task-queue', LogLevel.All)
12
13
var logger = Slf4j.create('ama-team.voxengine-sdk.example', LogLevel.Info)
14
var http = new SDK.Http.Basic({
15
  url: 'https://ya.ru',
16
  retryOnClientError: true,
17
  throwOnClientError: true
18
})
19
var rest = new SDK.Http.Rest({
20
  // json serializer is bundled in by default
21
  url: 'https://api.github.com/repos/ama-team/voxengine-sdk'
22
})
23
var queue = SDK.Concurrent.TaskQueue.started({name: 'webpack-example'})
24
25
VoxEngine.addEventListener(AppEvents.Started, function (event) {
26
  logger.info('Ok, let\'s get started')
27
  logger.info('Incoming event was: {}', event)
28
  logger.error('Printing fake error:', new Error())
29
  logger.debug('Debug level has been hidden - minimum required level is INFO')
30
31
  logger.info('The first thing to do is to check https://ya.ru content')
32
  logger.info('I bet it hasn\'t changed yet')
33
  var simpleRequest = queue.push(function () { return http.get('/') })
34
  simpleRequest.then(function (response) {
35
    logger.info('ya.ru response: {}', response)
36
    logger.info(
37
      'Chances are, above output has been truncated - ' +
38
      'that\'s because VoxEngine\'s logger is still used under hood'
39
    )
40
  })
41
42
  logger.info('Next, let\'s check which tags are present for current repo')
43
  logger.info(
44
    'Most probably GitHub will return error 403, so HTTP client will spin' +
45
    'until it will spend all retries it has been configured with'
46
  )
47
  logger.info(
48
    'Please note that task will be registered but not executed until ' +
49
    'first request has finished'
50
  )
51
  var tags = queue.push(
52
    function () { return rest.get('/tags') },
53
    {name: 'Retrieving list of ama-team/voxengine-sdk tags'}
54
  )
55
  tags.then(function (tags) {
56
    tags = tags.map(function (entry) { return entry.name })
57
    logger.info('Found tags for current repo: {}', tags)
58
  }, function (error) {
59
    logger.error('GitHub didn\'t let me to query his API: ', error)
60
  })
61
62
  logger.info(
63
    'Finally, let\'s register task that will instantly time out ' +
64
    'and produce TimeoutException'
65
  )
66
  var failure = queue.push(function () {
67
    return rest.get('/contributors', {}, {}, 0)
68
  })
69
  failure
70
    .then(function () {
71
      logger.error(
72
        'Wow! The request has finished in less than one nodejs tick, guys, ' +
73
        'have you been mocking globals?'
74
      )
75
    }, function (error) {
76
      logger.warn('Impossible request expectedly ended with error {}',
77
        error.name)
78
    })
79
    .then(function () {
80
      logger.info(
81
        'Please note again that this task has started only after ' +
82
        'previous one has finished'
83
      )
84
    })
85
86
  logger.info(
87
    'All tasks have been registered at this moment. Let\'s close task queue, ' +
88
    'and, when last task is done, shut down VoxEngine'
89
  )
90
  queue
91
    .close()
92
    .then(function () {
93
      logger.notice('All tasks have finished, shutting down the engine')
94
    })
95
    .then(VoxEngine.terminate.bind(VoxEngine))
96
})
97