-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add beforeRequest decorator for ui sdk (#1983)
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
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,66 @@ | ||
import type {SdkConfig} from '@gravity-ui/sdk'; | ||
import {CancellablePromise} from '@gravity-ui/sdk'; | ||
|
||
const CANCEL_REQUEST_EVENT = 'sdk_cancel_request'; | ||
|
||
type ConcurrentId = string | undefined; | ||
|
||
class SdkEvent extends EventTarget { | ||
cancelRequest(concurrentId: ConcurrentId) { | ||
this.dispatchEvent(new CustomEvent(CANCEL_REQUEST_EVENT, {detail: {concurrentId}})); | ||
} | ||
} | ||
const instance = new SdkEvent(); | ||
|
||
function subscribeCancelRequest(cb: (concurrentId: ConcurrentId) => void) { | ||
function subscribe(event: Event) { | ||
const customEvent = event as CustomEvent<{concurrentId: ConcurrentId}>; | ||
cb(customEvent.detail.concurrentId); | ||
} | ||
instance.addEventListener(CANCEL_REQUEST_EVENT, subscribe); | ||
function unsubscribe() { | ||
instance.removeEventListener(CANCEL_REQUEST_EVENT, subscribe); | ||
} | ||
return unsubscribe; | ||
} | ||
|
||
export function emitCancelRequest(concurrentId: ConcurrentId) { | ||
instance.cancelRequest(concurrentId); | ||
} | ||
|
||
export function initBeforeRequestDecorator( | ||
beforeRequest: () => Promise<unknown>, | ||
): SdkConfig['decorator'] { | ||
return (sdkActionFunc) => | ||
(...sdkActionFuncArgs) => { | ||
const requestOptions = sdkActionFuncArgs[1]; | ||
const concurrentId = requestOptions?.concurrentId; | ||
let cancelled = false; | ||
let actionPromise: ReturnType<typeof sdkActionFunc>; | ||
let unsubscribe: ReturnType<typeof subscribeCancelRequest>; | ||
if (concurrentId) { | ||
unsubscribe = subscribeCancelRequest((emittedConcurrentId) => { | ||
if (emittedConcurrentId === concurrentId) { | ||
cancelled = true; | ||
} | ||
}); | ||
} | ||
return new CancellablePromise( | ||
beforeRequest().then(() => { | ||
unsubscribe?.(); | ||
actionPromise = sdkActionFunc(...sdkActionFuncArgs); | ||
if (cancelled) { | ||
actionPromise.cancel(); | ||
} | ||
return actionPromise; | ||
}), | ||
() => { | ||
unsubscribe?.(); | ||
actionPromise?.cancel(); | ||
cancelled = true; | ||
}, | ||
).finally(() => { | ||
unsubscribe?.(); | ||
}); | ||
}; | ||
} |
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