Skip to content

Commit

Permalink
✨ add ability for viewer to notify subscriptions that the state has c…
Browse files Browse the repository at this point in the history
…hanged (#21720)

* Add a reset message to the viewer interface

* lint

* add tests + fixes

* fix merge
  • Loading branch information
jpettitt committed Apr 5, 2019
1 parent 3c0847a commit 22efc43
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions extensions/amp-subscriptions/0.1/test/test-viewer-platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {Dialog} from '../dialog';
import {Entitlement, GrantReason} from '../entitlement';
import {PageConfig} from '../../../../third_party/subscriptions-project/config';
import {ServiceAdapter} from '../service-adapter';
import {Services} from '../../../../src/services';
import {SubscriptionAnalytics} from '../analytics';
import {ViewerSubscriptionPlatform} from '../viewer-subscription-platform';
import {getWinOrigin} from '../../../../src/url';
Expand All @@ -27,6 +28,7 @@ describes.fakeWin('ViewerSubscriptionPlatform', {amp: true}, env => {
let ampdoc, win;
let viewerPlatform;
let serviceAdapter, sendAuthTokenStub;
let resetPlatformsStub, messageCallback;
const currentProductId = 'example.org:basic';
const origin = 'origin';
const entitlementData = {source: 'local', raw: 'raw',
Expand Down Expand Up @@ -60,6 +62,9 @@ describes.fakeWin('ViewerSubscriptionPlatform', {amp: true}, env => {
.callsFake(() => new Dialog(ampdoc));
sandbox.stub(serviceAdapter, 'getReaderId')
.callsFake(() => Promise.resolve('reader1'));
resetPlatformsStub = sandbox.stub(serviceAdapter, 'resetPlatforms');
sandbox.stub(Services.viewerForDoc(ampdoc),'onMessage')
.callsFake((message, cb) => { messageCallback = cb; });
viewerPlatform = new ViewerSubscriptionPlatform(
ampdoc, serviceConfig, serviceAdapter, origin);
sandbox.stub(viewerPlatform.viewer_,
Expand Down Expand Up @@ -89,6 +94,13 @@ describes.fakeWin('ViewerSubscriptionPlatform', {amp: true}, env => {
});
});

describe('subscriptionchange message', () => {
it('should call resetPlatforms() on a subscriptionchange message', () => {
(messageCallback)();
expect(resetPlatformsStub).to.be.called;
});
});

it('Should allow prerender', () => {
expect(viewerPlatform.isPrerenderSafe()).to.be.true;
});
Expand Down
14 changes: 14 additions & 0 deletions extensions/amp-subscriptions/0.1/viewer-subscription-platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export class ViewerSubscriptionPlatform {
/** @private @const */
this.ampdoc_ = ampdoc;

/** @private @const */
this.serviceAdapter_ = serviceAdapter;

/** @private @const {!PageConfig} */
this.pageConfig_ = serviceAdapter.getPageConfig();

Expand All @@ -50,6 +53,8 @@ export class ViewerSubscriptionPlatform {

/** @const @private {!../../../src/service/viewer-impl.Viewer} */
this.viewer_ = Services.viewerForDoc(this.ampdoc_);
this.viewer_.onMessage('subscriptionchange',
this.subscriptionChange_.bind(this));

/** @private @const {!JwtHelper} */
this.jwtHelper_ = new JwtHelper(ampdoc.win);
Expand Down Expand Up @@ -212,6 +217,15 @@ export class ViewerSubscriptionPlatform {
decorateUI(element, action, options) {
return this.platform_.decorateUI(element, action, options);
}

/**
* Handles a reset message from the viewer which indicates
* subscription state changed. Eventually that will trigger
* a new getEntitlements() message exchange.
*/
subscriptionChange_() {
this.serviceAdapter_.resetPlatforms();
}
}

/**
Expand Down

0 comments on commit 22efc43

Please sign in to comment.