Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/api-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,27 @@ export function decorateApi<A>(
) {
// read existing total and local
let current = scope.get(api.context) ?? { total: {}, local: {} };
let hasOwnContext = scope.hasOwn(api.context);

let local = decorate(current.local, {
let total = hasOwnContext
? current.total
: decorate(current.total, current.local);

let local = decorate(hasOwnContext ? current.local : {}, {
[options?.at ?? "max"]: decorator,
});

if (!scope.hasOwn(api.context)) {
if (!hasOwnContext) {
scope.set(api.context, {
local,
total: current.total,
total,
handle: api.core,
});
} else {
current.local = local;
}

install(scope, api, current.total);
install(scope, api, total);
}

function decorate<A>(base: Decorator<A>, next: Decorator<A>): Decorator<A> {
Expand Down
53 changes: 53 additions & 0 deletions test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,59 @@ describe("api", () => {
});
});

it("does not duplicate existing ancestor middleware when propagating later additions", async () => {
let api = createApi("test", {
*test(order: string[]): Operation<string[]> {
return order;
},
});

await run(function* () {
yield* api.around({
*test(args, next) {
let [input] = args;
let output = yield* next(input.concat("parent-before"));
return output.concat("/parent-before");
},
});

let tester = yield* resource<{ test(): Operation<string[]> }>(
function* (provide) {
let scope = yield* useScope();
yield* api.around({
*test(args, next) {
let [input] = args;
let output = yield* next(input.concat("child"));
return output.concat("/child");
},
});
yield* provide({
*test() {
return yield* scope.run(() => api.operations.test([]));
},
});
},
);

yield* api.around({
*test(args, next) {
let [input] = args;
let output = yield* next(input.concat("parent-after"));
return output.concat("/parent-after");
},
});

expect(yield* tester.test()).toEqual([
"parent-before",
"parent-after",
"child",
"/child",
"/parent-after",
"/parent-before",
]);
});
});

it("isolates sibling scopes from each other's middleware", async () => {
let api = createApi("test", {
*test(order: string[]): Operation<string[]> {
Expand Down
Loading