Skip to content

Commit 8d46f74

Browse files
authored
refactor(offerwall): don't expose private methods on element (#622)
1 parent ac5a38c commit 8d46f74

File tree

4 files changed

+25
-26
lines changed

4 files changed

+25
-26
lines changed

cdn/src/utils/offerwall/WebMonetizationCustomOfferwallChoice.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { OfferwallModal } from '@c/offerwall'
2-
import type { Controller } from '@c/offerwall/controller'
2+
import type { Actions } from '@c/offerwall/controller'
33
import { applyFontFamily } from '@c/utils'
44
import {
55
BORDER_RADIUS,
@@ -95,7 +95,9 @@ export class WebMonetizationCustomOfferwallChoice implements OfferwallCustomChoi
9595

9696
const abortController = new AbortController()
9797
const onDoneResolver = withResolvers<boolean>()
98-
const controller: Controller = {
98+
99+
const owElem = document.createElement(elementName) as OfferwallModal
100+
const actions = owElem.setController({
99101
onExtensionLinkClick() {
100102
// can start tracking
101103
},
@@ -106,10 +108,7 @@ export class WebMonetizationCustomOfferwallChoice implements OfferwallCustomChoi
106108
onDone() {
107109
onDoneResolver.resolve(true)
108110
},
109-
}
110-
111-
const owElem = document.createElement(elementName) as OfferwallModal
112-
owElem.setController(controller)
111+
})
113112

114113
// in case initialize() wasn't called
115114
this.#configPromise ??= fetchConfig(params)
@@ -119,7 +118,7 @@ export class WebMonetizationCustomOfferwallChoice implements OfferwallCustomChoi
119118
document.body.appendChild(owElem)
120119

121120
try {
122-
await this.#runBusinessLogic(owElem, abortController.signal)
121+
await this.#runBusinessLogic(actions, abortController.signal)
123122
onDoneResolver.resolve(true)
124123
} catch (error) {
125124
console.error(error)
@@ -157,7 +156,7 @@ export class WebMonetizationCustomOfferwallChoice implements OfferwallCustomChoi
157156
* to give access.
158157
* - If either of above was within allowed time, give access.
159158
*/
160-
#runBusinessLogic = async (elem: OfferwallModal, signal: AbortSignal) => {
159+
#runBusinessLogic = async (actions: Actions, signal: AbortSignal) => {
161160
const { linkElem } = this.#deps
162161
const wasExtensionInstalledAtStart = isExtensionInstalled()
163162

@@ -167,11 +166,11 @@ export class WebMonetizationCustomOfferwallChoice implements OfferwallCustomChoi
167166
this.#isWithinAllowedTime(lastEvent.timestamp) &&
168167
isExtensionInstalled()
169168
) {
170-
return elem.setScreen('all-set')
169+
return actions.setScreen('all-set')
171170
}
172171

173172
if (wasExtensionInstalledAtStart) {
174-
elem.setScreen('contribution-required')
173+
actions.setScreen('contribution-required')
175174
while (true) {
176175
if (!this.#monetizationEventResolver) {
177176
// if not initialized
@@ -189,7 +188,7 @@ export class WebMonetizationCustomOfferwallChoice implements OfferwallCustomChoi
189188
this.#isForSameWalletAddress(event.paymentPointer) &&
190189
(await isValidPayment(event.incomingPayment))
191190
) {
192-
elem.setScreen('all-set')
191+
actions.setScreen('all-set')
193192
this.#setLastEvent({
194193
type: 'monetization',
195194
timestamp: Date.now(),
@@ -211,7 +210,7 @@ export class WebMonetizationCustomOfferwallChoice implements OfferwallCustomChoi
211210
}
212211
} else {
213212
await this.#waitForExtensionInstall(signal)
214-
elem.setScreen('all-set')
213+
actions.setScreen('all-set')
215214
this.#setLastEvent({ type: 'install', timestamp: Date.now() })
216215
}
217216
}

components/src/offerwall/controller.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ export interface Controller {
99
* render it in a way so it can be embedded in editor's preview interface.
1010
*/
1111
isPreviewMode?: boolean
12+
}
1213

13-
// TODO: add all handlers to controller only, instead of using the element
14-
// instance. The element instance should have `setController` as the only
15-
// public method.
16-
//
17-
// requestScreenChange(screen: Screen): void
14+
export interface Actions {
15+
setScreen(screen: Screen): void
1816
}
1917

2018
export const NO_OP_CONTROLLER: Controller = {

components/src/offerwall/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
ContributionRequired,
77
InstallRequired,
88
} from './components/index.js'
9-
import { NO_OP_CONTROLLER, type Controller, type Screen } from './controller.js'
9+
import { NO_OP_CONTROLLER } from './controller.js'
10+
import type { Actions, Controller, Screen } from './controller.js'
1011
import styles from './styles.css?raw'
1112
import styleTokens from './vars.css?raw'
1213

@@ -30,15 +31,18 @@ export class OfferwallModal extends LitElement {
3031
}
3132

3233
#controller: Controller = NO_OP_CONTROLLER
33-
setController(controller: Controller) {
34+
setController(controller: Controller): Actions {
3435
if (this.#controller !== NO_OP_CONTROLLER) {
3536
throw new Error('Controller already set')
3637
}
3738
this.#controller = controller
39+
return {
40+
setScreen: (screen: Screen) => this.#setScreen(screen),
41+
}
3842
}
3943

4044
@state() _screen: Screen = 'install-required'
41-
setScreen(screen: Screen) {
45+
#setScreen(screen: Screen) {
4246
if (!ALLOWED_SCREENS.includes(screen)) {
4347
throw new Error('Invalid screen')
4448
}

frontend/app/components/offerwall/OfferwallPreview.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useEffect, useRef, useState } from 'react'
22
import { useSnapshot } from 'valtio'
33
import type { OfferwallModal } from '@c/index'
4-
import type { Controller } from '@c/offerwall/controller'
54
import { applyFontFamily } from '@c/utils'
65
import {
76
BORDER_RADIUS,
@@ -51,7 +50,7 @@ export default function OfferwallPreview() {
5150
}
5251

5352
const el = document.querySelector<OfferwallModal>('wm-offerwall')!
54-
const controller: Controller = {
53+
const actions = el.setController({
5554
onModalClose: (ev) => {
5655
ev.preventDefault()
5756
console.log('onModalClose')
@@ -61,17 +60,16 @@ export default function OfferwallPreview() {
6160
console.log('onExtensionLinkClick')
6261
ev.preventDefault()
6362
setTimeout(() => {
64-
el.setScreen('all-set')
63+
actions.setScreen('all-set')
6564
}, 500)
6665
},
6766
onDone(ev) {
6867
console.log('onDone')
6968
ev.preventDefault()
70-
el.setScreen('install-required')
69+
actions.setScreen('install-required')
7170
},
7271
isPreviewMode: true,
73-
}
74-
el.setController(controller)
72+
})
7573

7674
setIsLoaded(true)
7775
}

0 commit comments

Comments
 (0)