Skip to content

Commit

Permalink
amp-access onApplyAuthorizations callbacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmajoulet committed Jun 29, 2018
1 parent ea3987c commit a3b4f71
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
23 changes: 22 additions & 1 deletion extensions/amp-access/0.1/amp-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {AccessSource, AccessType} from './amp-access-source';
import {AccessVars} from './access-vars';
import {AmpEvents} from '../../../src/amp-events';
import {CSS} from '../../../build/amp-access-0.1.css';
import {Observable} from '../../../src/observable';
import {Services} from '../../../src/services';
import {cancellation} from '../../../src/error';
import {dev, user} from '../../../src/log';
Expand Down Expand Up @@ -119,6 +120,9 @@ export class AccessService {
/** @private {?Promise} */
this.reportViewPromise_ = null;

/** @private @const {!Observable} */
this.applyAuthorizationsObservable_ = new Observable();

// This will fire after the first received authorization, even if
// there are multiple sources.
this.lastAuthorizationPromises_.then(() => {
Expand Down Expand Up @@ -160,6 +164,21 @@ export class AccessService {
return this.readerIdPromise_;
}

/**
* @return {boolean}
*/
areFirstAuthorizationsCompleted() {
return this.firstAuthorizationsCompleted_;
}

/**
* Registers a callback to be triggered when the document gets (re)authorized.
* @param {!Function} callback
*/
onApplyAuthorizations(callback) {
this.applyAuthorizationsObservable_.add(callback);
}

/**
* @return {!Array<!AccessSource>}
* @private
Expand Down Expand Up @@ -399,7 +418,9 @@ export class AccessService {
for (let i = 0; i < elements.length; i++) {
promises.push(this.applyAuthorizationToElement_(elements[i], response));
}
return Promise.all(promises);
return Promise.all(promises).then(() => {
this.applyAuthorizationsObservable_.fire();
});
}

/**
Expand Down
15 changes: 15 additions & 0 deletions extensions/amp-access/0.1/test/test-amp-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,21 @@ describes.fakeWin('AccessService authorization', {
});
});

it('should execute the onApplyAuthorizations registered callbacks', () => {
expectGetReaderId('reader1');
adapterMock.expects('authorize')
.withExactArgs()
.returns(Promise.resolve({access: true}))
.once();

const applyAuthorizationsStub = sinon.stub();
service.onApplyAuthorizations(applyAuthorizationsStub);

return service.runAuthorization_().then(() => {
expect(applyAuthorizationsStub).to.have.been.calledOnce;
});
});

it('should run authorization for broadcast events on same origin', () => {
let broadcastHandler;
sandbox.stub(service.viewer_, 'onBroadcast').callsFake(handler => {
Expand Down

0 comments on commit a3b4f71

Please sign in to comment.