-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[form-builder] Rewrite how we limit concurrent uploads (#245)
- Loading branch information
Showing
5 changed files
with
76 additions
and
65 deletions.
There are no files selected for viewing
17 changes: 10 additions & 7 deletions
17
packages/@sanity/form-builder/src/sanity/inputs/client-adapters/assets.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 0 additions & 51 deletions
51
packages/@sanity/form-builder/src/sanity/uploads/uploadQueue.js
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
packages/@sanity/form-builder/src/sanity/utils/withMaxConcurrency.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// @flow | ||
|
||
// Takes a observable-returning function and returns a new function that limits on the number of | ||
// concurrent observables. | ||
|
||
import Multicast from '@sanity/observable/multicast' | ||
import Observable from '@sanity/observable' | ||
import type {ObservableI, Subscription} from '../../typedefs/observable' | ||
|
||
const DEFAULT_CONCURRENCY = 4 | ||
|
||
function remove<T>(array: Array<T>, item: T): Array<T> { | ||
const index = array.indexOf(item) | ||
if (index > -1) { | ||
array.splice(index, 1) | ||
} | ||
return array | ||
} | ||
|
||
export function withMaxConcurrency(func: (any) => ObservableI<*>, concurrency: number = DEFAULT_CONCURRENCY) { | ||
const throttler = createThrottler(concurrency) | ||
return (...args: Array<any>) => Observable.from(throttler(func(...args))) | ||
} | ||
|
||
export function createThrottler(concurrency: number = DEFAULT_CONCURRENCY) { | ||
const currentSubscriptions: Array<Subscription> = [] | ||
const pendingObservables: Array<ObservableI<*>> = [] | ||
const ready$ = new Multicast() | ||
|
||
return request | ||
|
||
function request(observable: ObservableI<*>) { | ||
return new Observable(observer => { | ||
if (currentSubscriptions.length >= concurrency) { | ||
return scheduleAndWait(observable) | ||
.mergeMap(request) | ||
.subscribe(observer) | ||
} | ||
const subscription = observable.subscribe(observer) | ||
currentSubscriptions.push(subscription) | ||
return () => { | ||
remove(currentSubscriptions, subscription) | ||
remove(pendingObservables, observable) | ||
subscription.unsubscribe() | ||
check() | ||
} | ||
}) | ||
} | ||
|
||
function scheduleAndWait(observable) { | ||
pendingObservables.push(observable) | ||
return ready$.asObservable() | ||
.first(obs => obs === observable) | ||
} | ||
|
||
function check() { | ||
while (pendingObservables.length > 0 && currentSubscriptions.length < concurrency) { | ||
ready$.next(pendingObservables.shift()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters