-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathstability.ts
48 lines (39 loc) · 1.29 KB
/
stability.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import Promise from 'bluebird'
// eslint-disable-next-line @cypress/dev/arrow-body-multiline-braces
export const create = (Cypress, state) => ({
isStable: (stable: boolean = true, event: string) => {
if (state('isStable') === stable) {
return
}
const whenStable = state('whenStable')
// if we are going back to stable and we have
// a whenStable callback
if (stable && whenStable) {
// invoke it
whenStable()
}
state('isStable', stable)
// we notify the outside world because this is what the runner uses to
// show the 'loading spinner' during an app page loading transition event
return Cypress.action('cy:stability:changed', stable, event)
},
whenStable: (fn: () => any) => {
// if we are not stable
if (state('isStable') === false) {
return new Promise((resolve, reject) => {
// then when we become stable
return state('whenStable', () => {
// reset this callback function
state('whenStable', null)
// and invoke the original function
return Promise.try(fn)
.then(resolve)
.catch(reject)
})
})
}
// else invoke it right now
return Promise.try(fn)
},
})
export interface IStability extends ReturnType<typeof create> {}