Commit eadddc08 authored by nanahira's avatar nanahira

fix subscribe

parent bf361ba9
......@@ -14,7 +14,7 @@ import {
} from './utility';
import { DoRegister } from './registry';
import _ from 'lodash';
import { isObservable, Observable } from 'rxjs';
import { isObservable, Observable, of } from 'rxjs';
export interface DoRegisterResult<T> extends DoRegister.Config {
key: keyof T & string;
......@@ -82,11 +82,15 @@ export class Registrar<T = any> {
ctx: Context,
cb: ContextFunction<R>,
layers: ContextCallbackLayer[],
) {
): Observable<R extends Observable<infer U> ? U : Awaited<R>> {
const rest = [...layers];
const layer = rest.pop();
return new Observable<R extends Observable<infer U> ? U : Awaited<R>>(
(subscriber) => {
if (!layer) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return of(cb(ctx));
}
return new Observable((subscriber) => {
layer(ctx, async (nextCtx) => {
if (!rest.length) {
const result = cb(nextCtx);
......@@ -108,8 +112,7 @@ export class Registrar<T = any> {
this.runLayersWith(nextCtx, cb, rest).subscribe(subscriber);
}
});
},
);
});
}
runLayers<R>(ctx: Context, cb: ContextFunction<R>, key?: keyof T) {
......
......@@ -149,11 +149,13 @@ describe('Register', () => {
});
it('should work with layers', () => {
registrar.runLayers(
registrar
.runLayers(
app,
(ctx) => registrar.register(app, 'unUnderwear'),
'unUnderwear',
);
)
.subscribe();
expect(app['foo']).toBe(1);
expect(app['bar']).toBe(2);
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment