|
8 | 8 | * http://polymer.github.io/PATENTS.txt
|
9 | 9 | */
|
10 | 10 | import {Runtime} from '../runtime/runtime.js';
|
11 |
| -import {Manifest} from '../runtime/manifest.js'; |
12 |
| -import {IsValidOptions, Recipe, RecipeComponent} from '../runtime/recipe/recipe.js'; |
13 |
| -import {CapabilitiesResolver, StorageKeyOptions} from '../runtime/capabilities-resolver.js'; |
14 |
| -import {RecipeResolver} from '../runtime/recipe/recipe-resolver.js'; |
15 |
| -import {Arc} from '../runtime/arc.js'; |
16 |
| -import {Loader} from '../platform/loader-web.js'; |
17 |
| -import {Store} from '../runtime/storageNG/store.js'; |
18 |
| -import {Exists} from '../runtime/storageNG/drivers/driver.js'; |
19 |
| -import {TestVolatileMemoryProvider} from '../runtime/testing/test-volatile-memory-provider.js'; |
20 |
| -import {RamDiskStorageDriverProvider, RamDiskStorageKey} from '../runtime/storageNG/drivers/ramdisk.js'; |
21 |
| -import {Capabilities} from '../runtime/capabilities.js'; |
22 |
| -import {ramDiskStorageKeyPrefixForTest, storageKeyPrefixForTest} from '../runtime/testing/handle-for-test.js'; |
| 11 | +import {Recipe} from '../runtime/recipe/recipe.js'; |
| 12 | +import {StorageKeyRecipeResolver} from './storage-key-recipe-resolver.js'; |
23 | 13 |
|
24 | 14 |
|
25 | 15 | /**
|
@@ -51,112 +41,3 @@ async function generatePlans(resolutions: AsyncIterator<Recipe>): Promise<string
|
51 | 41 | }
|
52 | 42 |
|
53 | 43 |
|
54 |
| -/** |
55 |
| - * Responsible for resolving recipes with storage keys. |
56 |
| - */ |
57 |
| -export class StorageKeyRecipeResolver { |
58 |
| - private readonly runtime: Runtime; |
59 |
| - |
60 |
| - constructor(context: Manifest) { |
61 |
| - const loader = new Loader(); |
62 |
| - this.runtime = new Runtime({loader, context}); |
63 |
| - } |
64 |
| - |
65 |
| - /** |
66 |
| - * Produces resolved recipes with storage keys. |
67 |
| - * |
68 |
| - * TODO(alxr): Apply to long-running recipes appropriately. |
69 |
| - * @throws Error if recipe fails to resolve on first or second pass. |
70 |
| - * @yields Resolved recipes with storage keys |
71 |
| - */ |
72 |
| - async* resolve(): AsyncIterator<Recipe> { |
73 |
| - for (const r of this.runtime.context.allRecipes) { |
74 |
| - const arc = this.runtime.newArc(this.getArcId(r), ramDiskStorageKeyPrefixForTest()); |
75 |
| - const opts = {errors: new Map<Recipe | RecipeComponent, string>()}; |
76 |
| - const resolved = await this.resolveOrNormalize(r, arc, opts); |
77 |
| - if (!resolved) { |
78 |
| - throw Error(`Recipe ${r.name} failed to resolve:\n${[...opts.errors.values()].join('\n')}`); |
79 |
| - } |
80 |
| - this.createStoresForCreateHandles(resolved, arc); |
81 |
| - resolved.normalize(); |
82 |
| - if (!resolved.isResolved()) { |
83 |
| - throw Error(`Recipe ${resolved.name} did not properly resolve!\n${resolved.toString({showUnresolved: true})}`); |
84 |
| - } |
85 |
| - yield resolved; |
86 |
| - } |
87 |
| - } |
88 |
| - |
89 |
| - /** |
90 |
| - * Resolves unresolved recipe or normalizes resolved recipe. |
91 |
| - * |
92 |
| - * @param recipe long-running or ephemeral recipe |
93 |
| - * @param arc Arc is associated with input recipe |
94 |
| - * @param opts contains `errors` map for reporting. |
95 |
| - */ |
96 |
| - async resolveOrNormalize(recipe: Recipe, arc: Arc, opts?: IsValidOptions): Promise<Recipe | null> { |
97 |
| - const normed = recipe.clone(); |
98 |
| - normed.normalize(); |
99 |
| - if (normed.isResolved()) return normed; |
100 |
| - |
101 |
| - return await (new RecipeResolver(arc).resolve(recipe, opts)); |
102 |
| - } |
103 |
| - |
104 |
| - /** @returns the arcId from annotations on the recipe when present. */ |
105 |
| - getArcId(recipe: Recipe): string | null { |
106 |
| - const triggers: [string, string][][] = recipe.triggers; |
107 |
| - for (const trigger of triggers) { |
108 |
| - for (const pair of trigger) { |
109 |
| - if (pair[0] === 'arcId') { |
110 |
| - return pair[1]; |
111 |
| - } |
112 |
| - } |
113 |
| - } |
114 |
| - return null; |
115 |
| - } |
116 |
| - |
117 |
| - /** |
118 |
| - * Create stores with keys for all create handles. |
119 |
| - * |
120 |
| - * @param recipe should be long running. |
121 |
| - * @param arc Arc is associated with current recipe. |
122 |
| - */ |
123 |
| - createStoresForCreateHandles(recipe: Recipe, arc: Arc) { |
124 |
| - const resolver = new CapabilitiesResolver({arcId: arc.id}); |
125 |
| - for (const ch of recipe.handles.filter(h => h.fate === 'create')) { |
126 |
| - const storageKey = ramDiskStorageKeyPrefixForTest()(arc.id); // TODO: actually create the storage keys. |
127 |
| - const store = new Store({storageKey, exists: Exists.MayExist, type: ch.type, id: ch.id}); |
128 |
| - arc.context.registerStore(store, ch.tags); |
129 |
| - } |
130 |
| - } |
131 |
| - |
132 |
| - /** |
133 |
| - * WIP method to match `map` and `copy` fated handles with storage keys from `create` handles. |
134 |
| - * |
135 |
| - * @throws when a mapped handle is associated with too many stores (ambiguous mapping). |
136 |
| - * @throws when a mapped handle isn't associated with any store (no matches found). |
137 |
| - * @throws when handle is mapped to a handle from an ephemeral recipe. |
138 |
| - * @param recipe long-running or ephemeral recipe |
139 |
| - */ |
140 |
| - matchKeysToHandles(recipe: Recipe) { |
141 |
| - recipe.handles |
142 |
| - .filter(h => h.fate === 'map' || h.fate === 'copy') |
143 |
| - .forEach(h => { |
144 |
| - const matches = this.runtime.context.findHandlesById(h.id) |
145 |
| - .filter(h => h.fate === 'create'); |
146 |
| - |
147 |
| - if (matches.length !== 1) { |
148 |
| - const extra = matches.length > 1 ? 'Ambiguous handles' : 'No matching handles found'; |
149 |
| - throw Error(`Handle ${h.localName} mapped improperly: ${extra}.`); |
150 |
| - } |
151 |
| - |
152 |
| - const match = matches[0]; |
153 |
| - if (!match.recipe.isLongRunning) { |
154 |
| - throw Error(`Handle ${h.localName} mapped to ephemeral handle ${match.localName}.`); |
155 |
| - } |
156 |
| - |
157 |
| - h.storageKey = match.storageKey; |
158 |
| - }); |
159 |
| - } |
160 |
| -} |
161 |
| - |
162 |
| - |
0 commit comments