Commit eadddc08 authored by nanahira's avatar nanahira

fix subscribe

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