|
1 | 1 | import { gt } from "semver";
|
2 | 2 | import type AutoImport from "./auto-import";
|
3 |
| -import { Project, AddonInstance } from './ember-cli-models'; |
| 3 | +import { Project, AddonInstance } from "./ember-cli-models"; |
| 4 | +import { Node } from "broccoli-node-api"; |
4 | 5 |
|
5 | 6 | const protocolV1 = "__ember_auto_import_protocol_v1__";
|
6 | 7 | const protocolV2 = "__ember_auto_import_protocol_v2__";
|
@@ -49,33 +50,48 @@ export class LeaderChooser {
|
49 | 50 | this.locked = this.tentative.create();
|
50 | 51 | let v1 = g[protocolV1];
|
51 | 52 | if (v1?.isV1Placeholder) {
|
52 |
| - g[protocolV1] = this.locked; |
| 53 | + v1.leader = this.locked; |
53 | 54 | }
|
54 | 55 | }
|
55 | 56 | return this.locked;
|
56 | 57 | }
|
57 | 58 | }
|
58 | 59 |
|
| 60 | +class V1Placeholder { |
| 61 | + isV1Placeholder = true; |
| 62 | + leader: AutoImport | undefined; |
| 63 | + |
| 64 | + // we never want v1-speaking copies of ember-auto-import to consider |
| 65 | + // themselves primary, so if they're asking here, the answer is no. |
| 66 | + isPrimary() { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + // this is the only method that is called after isPrimary returns false. So we |
| 71 | + // need to implement this one and don't need to implement the other public API |
| 72 | + // of AutoImport. |
| 73 | + analyze(tree: Node, addon: AddonInstance) { |
| 74 | + if (!this.leader) { |
| 75 | + throw new Error( |
| 76 | + `bug: expected some protcol v2 copy of ember-auto-import to take charge before any v1 copy started trying to analyze trees` |
| 77 | + ); |
| 78 | + } |
| 79 | + return this.leader.analyze(tree, addon); |
| 80 | + } |
| 81 | +} |
| 82 | + |
59 | 83 | // at module load time, preempt all earlier versions of ember-auto-import that
|
60 |
| -// don't use our v2 protocol for deciding which copy is in charge. This ensures |
61 |
| -// that the v2 protocol will pick which version is in charge (and it can't pick |
62 |
| -// a v1-speaking copy). |
| 84 | +// don't use our v2 leadership protocol. This ensures that the v2 protocol will |
| 85 | +// pick which version is in charge (and v1-speaking copies won't be eligible). |
63 | 86 | (function v1ProtocolCompat() {
|
64 | 87 | let v1 = g[protocolV1];
|
65 |
| - if (v1 && !v1.isV1Placeholder) { |
66 |
| - throw new Error( |
67 |
| - `bug: an old version of ember-auto-import has already taken over. This is unexpected.` |
68 |
| - ); |
69 |
| - } |
70 |
| - g[protocolV1] = { |
71 |
| - isV1Placeholder: true, |
72 |
| - analyze() { |
| 88 | + if (v1) { |
| 89 | + if (!v1.isV1Placeholder) { |
73 | 90 | throw new Error(
|
74 |
| - `bug: expected some copy of ember-auto-import to take charge before anybody started trying to analyze trees` |
| 91 | + `bug: an old version of ember-auto-import has already taken over. This is unexpected.` |
75 | 92 | );
|
76 |
| - }, |
77 |
| - isPrimary() { |
78 |
| - return false; |
79 |
| - }, |
80 |
| - }; |
| 93 | + } |
| 94 | + } else { |
| 95 | + g[protocolV1] = new V1Placeholder; |
| 96 | + } |
81 | 97 | })();
|
0 commit comments