Skip to content

Commit

Permalink
allow registering an onReady callback (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
alextremp authored Jun 23, 2020
1 parent bb5f728 commit 50fb5eb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The Boros TCF stub implements the [standard TCF v2 stub](https://github.com/Inte
- Stubs the `window.__tcfapi` responding immediately to the commands
- `ping` [See PingReturn in the stubbed __tcfapi](https://github.com/InteractiveAdvertisingBureau/GDPR-Transparency-and-Consent-Framework/blob/master/TCFv2/IAB%20Tech%20Lab%20-%20CMP%20API%20v2.md#requirements-for-the-cmp-stub-api-script)
- `pending` returns the pending calls accumulated while calling `window.__tcfapi` commands
- `onReady` returns the optional registered `onReady` callback

- Initializes the cross-framee communication via `postMessagee`, [see usage details](https://github.com/InteractiveAdvertisingBureau/GDPR-Transparency-and-Consent-Framework/blob/master/TCFv2/IAB%20Tech%20Lab%20-%20CMP%20API%20v2.md#how-can-vendors-that-use-iframes-call-the-cmp-api-from-an-iframe)

Expand All @@ -43,10 +44,23 @@ npm i @adv-ui/boros-tcf-stub --save
import registerStub from '../main'
// do your magic
registerStub()
```

**Register the Stub with an onReady callback**

This allows creating additional commands that can have access to the Boros TCF API facade.

```
import registerStub from '../main'
const onReady = api => initializeCustomCommands(api)
registerStub({onReady})
```

> The `onReady` callback will be called after Boros TCF initializes the `window.__tcfapi` and before processing any pending command in the stub's queue.
> Remember that the Stub **must** be registered before any script depending on the TCF is loaded
### As a standalone script
Expand All @@ -60,5 +74,7 @@ registerStub()
/>
```

> This does not accept registering an `onReady` callback. Import the `registerStub` and generate your own script if it's a need.
## License
Boros TCF Stub is [MIT licensed](./LICENSE).
7 changes: 6 additions & 1 deletion src/main/service/handler/TcfApiHandler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable standard/no-callback-literal */
export class TcfApiHandler {
constructor() {
constructor({onReady}) {
this._queue = []
this._onReady = onReady
}

handle({command, version, callback, parameter}) {
Expand All @@ -19,6 +20,9 @@ export class TcfApiHandler {
case PENDING_COMMAND: {
return this._queue
}
case ON_READY_COMMAND: {
return this._onReady
}
default: {
this._queue.push(() =>
window.__tcfapi(command, version, callback, parameter)
Expand All @@ -31,3 +35,4 @@ export class TcfApiHandler {

const PING_COMMAND = 'ping'
const PENDING_COMMAND = 'pending'
const ON_READY_COMMAND = 'onReady'
4 changes: 2 additions & 2 deletions src/main/service/registerStub.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import {registerTcfApiLocator} from './registerTcfApiLocator'
import {registerIframeMessageHandler} from './registerIframeMessageHandler'
import {registerTcfApiHandler} from './registerTcfApiHandler'

export const registerStub = () => {
export const registerStub = ({onReady} = {}) => {
if (typeof window === 'undefined') {
return
}
if (!registerTcfApiLocator()) {
return
}
registerIframeMessageHandler()
registerTcfApiHandler()
registerTcfApiHandler({onReady})
}
4 changes: 2 additions & 2 deletions src/main/service/registerTcfApiHandler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {TcfApiHandler} from './handler/TcfApiHandler'

const tcfApiHandler = new TcfApiHandler()
export const registerTcfApiHandler = () => {
export const registerTcfApiHandler = ({onReady}) => {
const tcfApiHandler = new TcfApiHandler({onReady})
window.__tcfapi = (command, version, callback, parameter) =>
tcfApiHandler.handle({command, version, callback, parameter})
}
9 changes: 8 additions & 1 deletion src/test/indexTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {waitUntil} from '../main/service/waitUntil'

describe('boros tcf stub', () => {
beforeEach(() => {
jsdom(null, {runScripts: 'dangerously'})
jsdom()
window.postMessage = message => {
const event = new window.MessageEvent('message', {
data: message,
Expand Down Expand Up @@ -36,6 +36,13 @@ describe('boros tcf stub', () => {
expect(pending.length).to.equal(1)
})

it('should register an onReady function', done => {
registerStub({onReady: () => done()})
const onReady = window.__tcfapi('onReady')
expect(onReady).to.be.a('function')
onReady()
})

it('should accept iframe communications', () => {
registerStub()

Expand Down

0 comments on commit 50fb5eb

Please sign in to comment.