Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Fix issue with non-extraneous config and token aliasing (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
ganemone authored Jan 31, 2018
1 parent 5971c69 commit adc3614
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
55 changes: 55 additions & 0 deletions src/__tests__/dependency-resolution.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,61 @@ tape('dependency registration with aliases', t => {
t.end();
});

tape('dependency registration with aliasing non-plugins', t => {
const app = new App('el', el => el);
t.ok(app, 'creates an app');
const counters = {
a: 0,
b: 0,
c: 0,
d: 0,
};

const ValueA = 'some-value';
const AliasedValue = 'some-aliased-value';
const ValueTokenA: string = createToken('ValueA');
const AliasedTokenA: string = createToken('AliasedTokenA');
const PluginB: FusionPlugin<{a: string}, BType> = createPlugin({
deps: {
a: ValueTokenA,
},
provides: deps => {
counters.b++;
t.equal(deps.a, 'some-value');
t.equal(counters.b, 1, 'only instantiates once');
return {
b: 'PluginB',
};
},
});

type PluginCType = FusionPlugin<{a: string}, CType>;
const PluginC: PluginCType = createPlugin({
deps: {
a: ValueTokenA,
},
provides: deps => {
counters.c++;
t.equal(deps.a, 'some-aliased-value');
t.equal(counters.c, 1, 'only instantiates once');
return {
c: 'PluginC',
};
},
});

app.register(ValueTokenA, ValueA);
app.register(TokenB, PluginB);
app.register(TokenC, PluginC).alias(ValueTokenA, AliasedTokenA);
app.register(AliasedTokenA, AliasedValue);
t.equal(counters.b, 0, 'does not instantiate until resolve is called');
t.equal(counters.c, 0, 'does not instantiate until resolve is called');
app.resolve();
t.equal(counters.b, 1, 'only instantiates once');
t.equal(counters.c, 1, 'only instantiates once');
t.end();
});

tape('dependency registration with no token', t => {
const app = new App('el', el => el);
const PluginA: FusionPlugin<void, AType> = createPlugin({
Expand Down
12 changes: 11 additions & 1 deletion src/base-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ class FusionApp {
const resolving = new Set();
const registered = this.registered;
const resolvedPlugins = [];
const allAliases = new Set();
const resolveToken = (token, tokenAliases) => {
// if we have already resolved the type, return it
if (tokenAliases && tokenAliases.has(token)) {
token = tokenAliases.get(token);
const newToken = tokenAliases.get(token);
allAliases.add([token, newToken]);
token = newToken;
}

if (resolved.has(token)) {
return resolved.get(token);
}
Expand Down Expand Up @@ -116,6 +120,12 @@ class FusionApp {
for (let i = 0; i < this.plugins.length; i++) {
resolveToken(this.plugins[i]);
}
for (const aliasPair of allAliases) {
const [sourceToken, destToken] = aliasPair;
if (dependedOn.has(sourceToken)) {
dependedOn.add(destToken);
}
}
for (const token of nonPluginTokens) {
if (!dependedOn.has(token)) {
throw new Error(
Expand Down

0 comments on commit adc3614

Please sign in to comment.