( source: Source<TOptions, TValueBefore>, transformValue: (value: TValueBefore) => TValueAfter, )
| 165 | * This function is out of Semantic Versioning, i.e. can change unexpectedly. Usage is at your own risk. |
| 166 | */ |
| 167 | export function transformSource<TOptions, TValueBefore, TValueAfter>( |
| 168 | source: Source<TOptions, TValueBefore>, |
| 169 | transformValue: (value: TValueBefore) => TValueAfter, |
| 170 | ): Source<TOptions, TValueAfter> { |
| 171 | const transformLoadResult = (loadResult: TValueBefore | (() => MaybePromise<TValueBefore>)) => { |
| 172 | if (isFinalResultLoaded(loadResult)) { |
| 173 | return transformValue(loadResult) |
| 174 | } |
| 175 | |
| 176 | return () => { |
| 177 | const getResult = loadResult() |
| 178 | |
| 179 | if (isPromise(getResult)) { |
| 180 | return getResult.then(transformValue) |
| 181 | } |
| 182 | |
| 183 | return transformValue(getResult) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return (options) => { |
| 188 | const loadResult = source(options) |
| 189 | |
| 190 | if (isPromise(loadResult)) { |
| 191 | return loadResult.then(transformLoadResult) |
| 192 | } |
| 193 | |
| 194 | return transformLoadResult(loadResult) |
| 195 | } |
| 196 | } |
searching dependent graphs…