Skip to content

Commit 50fb5eb

Browse files
authored
allow registering an onReady callback (#2)
1 parent bb5f728 commit 50fb5eb

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The Boros TCF stub implements the [standard TCF v2 stub](https://github.com/Inte
2424
- Stubs the `window.__tcfapi` responding immediately to the commands
2525
- `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)
2626
- `pending` returns the pending calls accumulated while calling `window.__tcfapi` commands
27+
- `onReady` returns the optional registered `onReady` callback
2728

2829
- 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)
2930

@@ -43,10 +44,23 @@ npm i @adv-ui/boros-tcf-stub --save
4344
import registerStub from '../main'
4445
4546
// do your magic
46-
4747
registerStub()
4848
```
4949

50+
**Register the Stub with an onReady callback**
51+
52+
This allows creating additional commands that can have access to the Boros TCF API facade.
53+
54+
```
55+
import registerStub from '../main'
56+
57+
const onReady = api => initializeCustomCommands(api)
58+
59+
registerStub({onReady})
60+
```
61+
62+
> The `onReady` callback will be called after Boros TCF initializes the `window.__tcfapi` and before processing any pending command in the stub's queue.
63+
5064
> Remember that the Stub **must** be registered before any script depending on the TCF is loaded
5165
5266
### As a standalone script
@@ -60,5 +74,7 @@ registerStub()
6074
/>
6175
```
6276

77+
> This does not accept registering an `onReady` callback. Import the `registerStub` and generate your own script if it's a need.
78+
6379
## License
6480
Boros TCF Stub is [MIT licensed](./LICENSE).

src/main/service/handler/TcfApiHandler.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* eslint-disable standard/no-callback-literal */
22
export class TcfApiHandler {
3-
constructor() {
3+
constructor({onReady}) {
44
this._queue = []
5+
this._onReady = onReady
56
}
67

78
handle({command, version, callback, parameter}) {
@@ -19,6 +20,9 @@ export class TcfApiHandler {
1920
case PENDING_COMMAND: {
2021
return this._queue
2122
}
23+
case ON_READY_COMMAND: {
24+
return this._onReady
25+
}
2226
default: {
2327
this._queue.push(() =>
2428
window.__tcfapi(command, version, callback, parameter)
@@ -31,3 +35,4 @@ export class TcfApiHandler {
3135

3236
const PING_COMMAND = 'ping'
3337
const PENDING_COMMAND = 'pending'
38+
const ON_READY_COMMAND = 'onReady'

src/main/service/registerStub.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import {registerTcfApiLocator} from './registerTcfApiLocator'
22
import {registerIframeMessageHandler} from './registerIframeMessageHandler'
33
import {registerTcfApiHandler} from './registerTcfApiHandler'
44

5-
export const registerStub = () => {
5+
export const registerStub = ({onReady} = {}) => {
66
if (typeof window === 'undefined') {
77
return
88
}
99
if (!registerTcfApiLocator()) {
1010
return
1111
}
1212
registerIframeMessageHandler()
13-
registerTcfApiHandler()
13+
registerTcfApiHandler({onReady})
1414
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {TcfApiHandler} from './handler/TcfApiHandler'
22

3-
const tcfApiHandler = new TcfApiHandler()
4-
export const registerTcfApiHandler = () => {
3+
export const registerTcfApiHandler = ({onReady}) => {
4+
const tcfApiHandler = new TcfApiHandler({onReady})
55
window.__tcfapi = (command, version, callback, parameter) =>
66
tcfApiHandler.handle({command, version, callback, parameter})
77
}

src/test/indexTest.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {waitUntil} from '../main/service/waitUntil'
55

66
describe('boros tcf stub', () => {
77
beforeEach(() => {
8-
jsdom(null, {runScripts: 'dangerously'})
8+
jsdom()
99
window.postMessage = message => {
1010
const event = new window.MessageEvent('message', {
1111
data: message,
@@ -36,6 +36,13 @@ describe('boros tcf stub', () => {
3636
expect(pending.length).to.equal(1)
3737
})
3838

39+
it('should register an onReady function', done => {
40+
registerStub({onReady: () => done()})
41+
const onReady = window.__tcfapi('onReady')
42+
expect(onReady).to.be.a('function')
43+
onReady()
44+
})
45+
3946
it('should accept iframe communications', () => {
4047
registerStub()
4148

0 commit comments

Comments
 (0)