|
3 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
|
5 | 5 | // @flow |
6 | | - |
| 6 | +import type { Store } from "../types"; |
7 | 7 | const {WAIT_UNTIL_TYPE} = require("../../shared/redux/middleware/waitUntilService"); |
8 | 8 |
|
9 | 9 | /* |
@@ -67,7 +67,83 @@ function waitForDispatch(store: Object, type: string) { |
67 | 67 | }); |
68 | 68 | } |
69 | 69 |
|
| 70 | +/** |
| 71 | + * Wait until the condition evaluates to something truethy |
| 72 | + * @param {function} condition: function that we need for returning something truthy. |
| 73 | + * @param {int} interval: Time to wait before trying to evaluate condition again |
| 74 | + * @param {int} maxTries: Number of evaluation to try. |
| 75 | + */ |
| 76 | +async function waitFor( |
| 77 | + condition: (any) => any, |
| 78 | + interval: number = 50, |
| 79 | + maxTries: number = 100 |
| 80 | +) { |
| 81 | + let res = condition(); |
| 82 | + while (!res) { |
| 83 | + await new Promise(done => setTimeout(done, interval)); |
| 84 | + maxTries--; |
| 85 | + |
| 86 | + if (maxTries <= 0) { |
| 87 | + throw new Error("waitFor - maxTries limit hit"); |
| 88 | + } |
| 89 | + |
| 90 | + res = condition(); |
| 91 | + } |
| 92 | + return res; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Wait until the state has all the expected keys for the loadedProperties state prop. |
| 97 | + * @param {Redux Store} store: function that we need for returning something truthy. |
| 98 | + * @param {Array} expectedKeys: Array of stringified keys. |
| 99 | + * @param {int} interval: Time to wait before trying to evaluate condition again |
| 100 | + * @param {int} maxTries: Number of evaluation to try. |
| 101 | + */ |
| 102 | +function waitForLoadedProperties( |
| 103 | + store: Store, |
| 104 | + expectedKeys: Array<string>, |
| 105 | + interval: number, |
| 106 | + maxTries: number |
| 107 | +) : Promise<any> { |
| 108 | + return waitFor(() => |
| 109 | + storeHasLoadedPropertiesKeys(store, expectedKeys), interval, maxTries); |
| 110 | +} |
| 111 | + |
| 112 | +function storeHasLoadedPropertiesKeys(store: Store, expectedKeys: Array<string>) { |
| 113 | + return expectedKeys.every(key => storeHasLoadedProperty(store, key)); |
| 114 | +} |
| 115 | + |
| 116 | +function storeHasLoadedProperty(store: Store, key: string) : boolean { |
| 117 | + return [...store.getState().loadedProperties.keys()].some(k => k.toString() === key); |
| 118 | +} |
| 119 | + |
| 120 | +function storeHasExactLoadedProperties(store: Store, expectedKeys: Array<string>) { |
| 121 | + return expectedKeys.length === store.getState().loadedProperties.size |
| 122 | + && expectedKeys.every(key => storeHasLoadedProperty(store, key)); |
| 123 | +} |
| 124 | + |
| 125 | +function storeHasExpandedPaths(store: Store, expectedKeys: Array<string>) { |
| 126 | + return expectedKeys.every(key => storeHasExpandedPath(store, key)); |
| 127 | +} |
| 128 | + |
| 129 | +function storeHasExpandedPath(store: Store, key: string) : boolean { |
| 130 | + return [...store.getState().expandedPaths.keys()].some(k => k.toString() === key); |
| 131 | +} |
| 132 | + |
| 133 | +function storeHasExactExpandedPaths(store: Store, expectedKeys: Array<string>) { |
| 134 | + return expectedKeys.length === store.getState().expandedPaths.size |
| 135 | + && expectedKeys.every(key => storeHasExpandedPath(store, key)); |
| 136 | +} |
| 137 | + |
70 | 138 | module.exports = { |
71 | 139 | formatObjectInspector, |
| 140 | + storeHasExpandedPaths, |
| 141 | + storeHasExpandedPath, |
| 142 | + storeHasExactExpandedPaths, |
| 143 | + storeHasLoadedPropertiesKeys, |
| 144 | + storeHasLoadedProperty, |
| 145 | + storeHasExactLoadedProperties, |
| 146 | + waitFor, |
72 | 147 | waitForDispatch, |
| 148 | + waitForLoadedProperties, |
73 | 149 | }; |
0 commit comments