MCPcopy
hub / github.com/ampproject/amphtml / registerExtension

Method registerExtension

src/service/extensions-impl.js:129–183  ·  view source on GitHub ↗

* Register and process the specified extension. The factory is called * immediately, which in turn is expected to register elements, templates, * services and document factories. This method is called by the extension's * script itself when it's loaded using the regular `AMP.push()` callbac

(extensionId, version, latest, factory, arg)

Source from the content-addressed store, hash-verified

127 * @restricted
128 */
129 registerExtension(extensionId, version, latest, factory, arg) {
130 const latestHolder = latest
131 ? this.extensions_[extensionKey(extensionId, LATEST_VERSION)]
132 : null;
133 const holder = this.getExtensionHolder_(
134 extensionId,
135 version,
136 // Inherit the `auto` (auto-install) flag from the "latest" version
137 // when available. If the "latest" has been added as a non-auto-install
138 // then this registration should not auto-install either. If the numeric
139 // version was independently added to the document, then it's auto-install
140 // will be preserved.
141 latestHolder?.auto ?? true
142 );
143 holder.latest = latest;
144
145 if (holder.loaded) {
146 // This extension has already been registered. This could be a
147 // a "latest" script requested for a previously loaded numeric
148 // version or vice versa.
149 return;
150 }
151
152 // Replace the "latest": both numerical and "latest" will be pointing to
153 // the same record.
154 if (latest) {
155 this.extensions_[extensionKey(extensionId, LATEST_VERSION)] = holder;
156 }
157
158 try {
159 this.currentExtensionId_ = extensionId;
160 this.currentExtensionVersion_ = version;
161 this.currentExtensionLatest_ = latest;
162 factory(arg, arg['_']);
163 if (getMode(this.win).localDev || getMode(this.win).test) {
164 if (Object.freeze) {
165 const m = holder.extension;
166 m.elements = Object.freeze(m.elements);
167 holder.extension = Object.freeze(m);
168 }
169 }
170 holder.loaded = true;
171 holder.resolve?.(holder.extension);
172 latestHolder?.resolve?.(holder.extension);
173 } catch (e) {
174 holder.error = e;
175 holder.reject?.(e);
176 latestHolder?.reject?.(e);
177 throw e;
178 } finally {
179 this.currentExtensionId_ = null;
180 this.currentExtensionVersion_ = null;
181 this.currentExtensionLatest_ = null;
182 }
183 }
184
185 /**
186 * Waits for the previously included extension to complete

Callers 5

test-extensions.jsFile · 0.80
registerFunction · 0.80
test-amp-ad.jsFile · 0.80
setupMethod · 0.80

Calls 5

getExtensionHolder_Method · 0.95
getModeFunction · 0.90
extensionKeyFunction · 0.85
resolveMethod · 0.80
factoryFunction · 0.50

Tested by

no test coverage detected