source: trip-planner-front/node_modules/karma/lib/launchers/base.js@ 84d0fbb

Last change on this file since 84d0fbb was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.2 KB
Line 
1const KarmaEventEmitter = require('../events').EventEmitter
2const EventEmitter = require('events').EventEmitter
3
4const log = require('../logger').create('launcher')
5const helper = require('../helper')
6
7const BEING_CAPTURED = 'BEING_CAPTURED'
8const CAPTURED = 'CAPTURED'
9const BEING_KILLED = 'BEING_KILLED'
10const FINISHED = 'FINISHED'
11const RESTARTING = 'RESTARTING'
12const BEING_FORCE_KILLED = 'BEING_FORCE_KILLED'
13
14/**
15 * Base launcher that any custom launcher extends.
16 */
17function BaseLauncher (id, emitter) {
18 if (this.start) {
19 return
20 }
21
22 // TODO(vojta): figure out how to do inheritance with DI
23 Object.keys(EventEmitter.prototype).forEach(function (method) {
24 this[method] = EventEmitter.prototype[method]
25 }, this)
26
27 this.bind = KarmaEventEmitter.prototype.bind.bind(this)
28 this.emitAsync = KarmaEventEmitter.prototype.emitAsync.bind(this)
29
30 this.id = id
31 this._state = null
32 Object.defineProperty(this, 'state', {
33 get: () => {
34 return this._state
35 },
36 set: (toState) => {
37 log.debug(`${this._state} -> ${toState}`)
38 this._state = toState
39 }
40 })
41
42 this.error = null
43
44 let killingPromise
45 let previousUrl
46
47 this.start = function (url) {
48 previousUrl = url
49
50 this.error = null
51 this.state = BEING_CAPTURED
52 this.emit('start', url + '?id=' + this.id + (helper.isDefined(this.displayName) ? '&displayName=' + encodeURIComponent(this.displayName) : ''))
53 }
54
55 this.kill = function () {
56 // Already killed, or being killed.
57 if (killingPromise) {
58 return killingPromise
59 }
60
61 killingPromise = this.emitAsync('kill').then(() => {
62 this.state = FINISHED
63 })
64
65 this.state = BEING_KILLED
66
67 return killingPromise
68 }
69
70 this.forceKill = function () {
71 this.kill()
72 this.state = BEING_FORCE_KILLED
73
74 return killingPromise
75 }
76
77 this.restart = function () {
78 if (this.state === BEING_FORCE_KILLED) {
79 return
80 }
81
82 if (!killingPromise) {
83 killingPromise = this.emitAsync('kill')
84 }
85
86 killingPromise.then(() => {
87 if (this.state === BEING_FORCE_KILLED) {
88 this.state = FINISHED
89 } else {
90 killingPromise = null
91 log.debug(`Restarting ${this.name}`)
92 this.start(previousUrl)
93 }
94 })
95
96 this.state = RESTARTING
97 }
98
99 this.markCaptured = function () {
100 if (this.state === BEING_CAPTURED) {
101 this.state = CAPTURED
102 }
103 }
104
105 this.isCaptured = function () {
106 return this.state === CAPTURED
107 }
108
109 this.toString = function () {
110 return this.name
111 }
112
113 this._done = function (error) {
114 killingPromise = killingPromise || Promise.resolve()
115
116 this.error = this.error || error
117 this.emit('done')
118
119 if (this.error && this.state !== BEING_FORCE_KILLED && this.state !== RESTARTING) {
120 emitter.emit('browser_process_failure', this)
121 }
122
123 this.state = FINISHED
124 }
125
126 this.STATE_BEING_CAPTURED = BEING_CAPTURED
127 this.STATE_CAPTURED = CAPTURED
128 this.STATE_BEING_KILLED = BEING_KILLED
129 this.STATE_FINISHED = FINISHED
130 this.STATE_RESTARTING = RESTARTING
131 this.STATE_BEING_FORCE_KILLED = BEING_FORCE_KILLED
132}
133
134BaseLauncher.decoratorFactory = function (id, emitter) {
135 return function (launcher) {
136 BaseLauncher.call(launcher, id, emitter)
137 }
138}
139
140module.exports = BaseLauncher
Note: See TracBrowser for help on using the repository browser.