dojo / compose

**DEPRECATED** Dojo 2 - composition library.

Home Page:http://dojo.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with multiple advice being applied

kitsonk opened this issue · comments

Version and Environment:

Dojo 2 version: beta

Environment(s): all

Code

const createFoo = compose({
    foo(a: string): string {
        return a;
    }
});

const createAspectFoo = createFoo
    .mixin({
        aspectAdvice: {
            after: {
                foo(previousResult: string): string {
                    return previousResult + 'foo';
                }
            }
        }
    });

createAspectFoo
    .mixin({
        aspectAdvice: {
            after: {
                foo(previousResult: string): string {
                    return previousResult + 'bar';
                }
            }
        }
    });

const foo = createAspectFoo();
assert.strictEqual(foo.foo('baz'), 'bazfoo', 'should only apply advice in chain');

Expected behavior:

Only the advice in the constructor function used should be applied.

Actual behavior:

Additional advice downstream is applied upstream. The assert fails with 'bazfoobar' being returned.

Essentially this behaves surprisingly (essentially once you have a reference to a advised function, that function will continue to collect advice like a rolling stone, instead of creating a new reference):

function foo(a: string): string {
    return a;
}

function adviceAfter(origResult: string): string {
    return origResult + 'foo';
}

const fn = after(after(foo, adviceAfter), adviceAfter);
after(fn, adviceAfter);
assert.strictEqual(fn('bar'), 'barfoofoo', 'should only apply advice twice');