@rxap/ngx-utilities
Version:
46 lines (41 loc) • 1.49 kB
JavaScript
import { runInInjectionContext } from '@angular/core';
import { isPromise } from '@rxap/utilities';
function ToMethodWithInjectionContext(call, injector, metadata = { id: 'to-method' }) {
return {
call: (parameters, ...args) => new Promise((resolve, reject) => {
if (!injector) {
throw new Error('The injector is not defined. Can not run the method in the injection context.');
}
runInInjectionContext(injector, () => {
try {
const result = call(parameters, ...args);
if (isPromise(result)) {
result.then(resolve).catch(reject);
}
else {
resolve(result);
}
}
catch (e) {
reject(e);
}
});
}),
metadata
};
}
function ToMethodWithInjectionContextFactory(call, metadata = { id: 'to-method' }) {
return (injector) => {
if (!injector) {
throw new Error('The injector is not defined. Ensure the factory is used in conjunction with deps: [ INJECTOR ]');
}
return ToMethodWithInjectionContext(call, injector, metadata);
};
}
// region
// endregion
/**
* Generated bundle index. Do not edit.
*/
export { ToMethodWithInjectionContext, ToMethodWithInjectionContextFactory };
//# sourceMappingURL=rxap-ngx-utilities.mjs.map