forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prebid core: add support for asynchronous access to consent data
This adds `gpdrDataHandler.promise` and `uspDataHandler.promise`, to enable access to USP/GDPR consent data from outside of an auction context (see use case: prebid#7803)
- Loading branch information
Showing
7 changed files
with
180 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
|
||
export class ConsentHandler { | ||
#enabled; | ||
#data; | ||
#promise; | ||
#resolve; | ||
#ready; | ||
|
||
constructor() { | ||
this.reset(); | ||
} | ||
|
||
/** | ||
* reset this handler (mainly for tests) | ||
*/ | ||
reset() { | ||
this.#promise = new Promise((resolve) => { | ||
this.#resolve = (data) => { | ||
this.#ready = true; | ||
this.#data = data; | ||
resolve(data); | ||
}; | ||
}); | ||
this.#enabled = false; | ||
this.#data = null; | ||
this.#ready = false; | ||
} | ||
|
||
/** | ||
* Enable this consent handler. This should be called by the relevant consent management module | ||
* on initialization. | ||
*/ | ||
enable() { | ||
this.#enabled = true; | ||
} | ||
|
||
/** | ||
* @returns {boolean} true if the related consent management module is enabled. | ||
*/ | ||
get enabled() { | ||
return this.#enabled; | ||
} | ||
|
||
/** | ||
* @returns {boolean} true if consent data has been resolved (it may be `null` if the resolution failed). | ||
*/ | ||
get ready() { | ||
return this.#ready; | ||
} | ||
|
||
/** | ||
* @returns a promise than resolves to the consent data, or null if no consent data is available | ||
*/ | ||
get promise() { | ||
if (!this.#enabled) { | ||
this.#resolve(null); | ||
} | ||
return this.#promise; | ||
} | ||
|
||
setConsentData(data) { | ||
this.#resolve(data); | ||
} | ||
|
||
getConsentData() { | ||
return this.#data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {ConsentHandler} from '../../../../src/consentHandler.js'; | ||
|
||
describe('Consent data handler', () => { | ||
let handler; | ||
beforeEach(() => { | ||
handler = new ConsentHandler(); | ||
}) | ||
|
||
it('should be disabled, return null data on init', () => { | ||
expect(handler.enabled).to.be.false; | ||
expect(handler.getConsentData()).to.equal(null); | ||
}) | ||
|
||
it('should resolve promise to null when disabled', () => { | ||
return handler.promise.then((data) => { | ||
expect(data).to.equal(null); | ||
}); | ||
}); | ||
|
||
it('should return data after setConsentData', () => { | ||
const data = {consent: 'string'}; | ||
handler.enable(); | ||
handler.setConsentData(data); | ||
expect(handler.getConsentData()).to.equal(data); | ||
}); | ||
|
||
it('should resolve .promise to data after setConsentData', (done) => { | ||
let actual = null; | ||
const data = {consent: 'string'}; | ||
handler.enable(); | ||
handler.promise.then((d) => actual = d); | ||
setTimeout(() => { | ||
expect(actual).to.equal(null); | ||
handler.setConsentData(data); | ||
setTimeout(() => { | ||
expect(actual).to.equal(data); | ||
done(); | ||
}) | ||
}) | ||
}); | ||
}) |