Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amp-list: Data-transform proof of concept #26079

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions examples/amp-list-data-transform.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!doctype html>
<html ⚡>
<head>
<title>amp-list data transform example</title>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
<link rel="canonical" href="https://example.com/">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<style amp-custom>
body {
padding: 8px;
}
</style>
</head>
<body>
<h2>amp-list data transform example</h2>
<amp-list layout="responsive" width="300" height="300"
src="amp-list-data-transform.json"
transform="amp-list-data-transform.js">
<template type="amp-mustache">
<p>{{key}}: {{val}}</p>
</template>
</amp-list>
</body>
</html>
13 changes: 13 additions & 0 deletions examples/amp-list-data-transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
onmessage = function(e) {
console.log('amp-list transformer, input:\n', e.data);
const finaldata = {
items: Object.keys(e.data).map(key => {
return {
key: key,
val: e.data[key]
};
})
};
console.log('amp-list transformer, output:\n', finaldata);
postMessage(finaldata);
};
5 changes: 5 additions & 0 deletions examples/amp-list-data-transform.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"a": "123",
"b": "456",
"c": "789"
}
48 changes: 37 additions & 11 deletions extensions/amp-list/0.1/amp-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ export class AmpList extends AMP.BaseElement {

/** @private {?../../../src/ssr-template-helper.SsrTemplateHelper} */
this.ssrTemplateHelper_ = null;

/** @private {Worker} */
this.dataTransformer_ = null;
}

/** @override */
Expand All @@ -192,6 +195,16 @@ export class AmpList extends AMP.BaseElement {
// is missing attributes in the constructor.
this.initialSrc_ = this.element.getAttribute('src');

// Remember, this is a proof of concept. No validation or error
// handling is performed.
if (this.element.hasAttribute('transform')) {
try {
this.dataTransformer_ = new Worker(
this.element.getAttribute('transform')
);
} catch (ex) {}
}

if (this.element.hasAttribute('diffable')) {
// Set container to the initial content, if it exists. This allows
// us to DOM diff with the rendered result.
Expand Down Expand Up @@ -552,17 +565,30 @@ export class AmpList extends AMP.BaseElement {
if (this.ssrTemplateHelper_.isEnabled()) {
fetch = this.ssrTemplate_(opt_refresh);
} else {
fetch = this.prepareAndSendFetch_(opt_refresh).then(data => {
const items = this.computeListItems_(data);
if (this.loadMoreEnabled_) {
this.updateLoadMoreSrc_(/** @type {!JsonObject} */ (data));
}
return this.scheduleRender_(
items,
/*opt_append*/ false,
data
).then(() => this.maybeSetLoadMore_());
});
fetch = this.prepareAndSendFetch_(opt_refresh)
.then(data => {
// Remember, this is a proof of concept. No validation or error
// handling is performed.
if (this.dataTransformer_) {
this.dataTransformer_.postMessage(data);
return new Promise(resolve => {
this.dataTransformer_.onmessage = e => resolve(e.data);
});
} else {
return Promise.resolve(data);
}
})
.then(data => {
const items = this.computeListItems_(data);
if (this.loadMoreEnabled_) {
this.updateLoadMoreSrc_(/** @type {!JsonObject} */ (data));
}
return this.scheduleRender_(
items,
/*opt_append*/ false,
data
).then(() => this.maybeSetLoadMore_());
});
}

return fetch.catch(error => {
Expand Down