Skip to content

Commit 7c4969c

Browse files
committed
feat: semi
1 parent fd726a7 commit 7c4969c

6 files changed

Lines changed: 30 additions & 23 deletions

File tree

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"js/ts.format.insertSpaceAfterCommaDelimiter": true,
1414
"js/ts.preferences.quoteStyle": "single",
1515
"js/ts.format.semicolons": "remove",
16+
"js/ts.preferences.preferTypeOnlyAutoImports": true,
1617
"js/ts.format.insertSpaceBeforeFunctionParenthesis": false,
1718
"files.eol": "\n",
1819
"cSpell.words": [

src/background/limit-processor.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,21 @@ function initDailyBroadcast() {
2626
)
2727
}
2828

29-
function initCountdownBroadcast() {
29+
function anyChanged(
30+
newVal: tt4b.option.LimitOption,
31+
oldVal: tt4b.option.LimitOption,
32+
...keys: (keyof tt4b.option.LimitOption)[]
33+
) {
34+
return keys.some(key => newVal[key] !== oldVal[key])
35+
}
36+
37+
function initOptionBroadcast() {
3038
optionHolder.addChangeListener(async (newVal, oldVal) => {
31-
if (newVal.limitCountdown === oldVal.limitCountdown) return
39+
if (!anyChanged(newVal, oldVal, 'limitCountdown', 'limitDelayDuration')) return
3240
const tabs = await listTabs()
3341
for (const { id: tabId } of tabs) {
3442
try {
35-
tabId && await sendMsg2Tab(tabId, 'limitCountdownChanged')
43+
tabId && await sendMsg2Tab(tabId, 'limitOptionChanged', newVal)
3644
} catch { }
3745
}
3846
})
@@ -68,7 +76,7 @@ async function querySummary(): Promise<tt4b.limit.Summary | undefined> {
6876

6977
export default function init(dispatcher: MessageDispatcher) {
7078
initDailyBroadcast()
71-
initCountdownBroadcast()
79+
initOptionBroadcast()
7280

7381
dispatcher
7482
.register('limit.list', selectLimit)

src/content-script/limit/countdown/index.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { trySendMsg2Runtime } from '@api/sw/common'
2-
import { getOption } from '@api/sw/option'
32
import LocationWatcher from '@cs/location-watcher'
43
import DelayCoordinator from '../manager/delay-coordinator'
54
import LimitState from '../manager/state'
@@ -12,17 +11,17 @@ const TIMER_INTERVAL = 1000
1211
export default class Countdown {
1312
#enabled: boolean = false
1413
#visible: boolean = false
15-
#state = new CountdownState()
16-
#component = new CountdownComponent()
1714
#interval?: ReturnType<typeof setInterval>
18-
#onVisibleChange = () => document.visibilityState === 'visible' && this.#sync()
15+
readonly #state = new CountdownState()
16+
readonly #component = new CountdownComponent()
17+
readonly #onVisibleChange = () => document.visibilityState === 'visible' && this.#sync()
1918

2019
get isEffective() {
2120
return this.#enabled && !this.#visible && !this.location.isWhite
2221
}
2322

2423
constructor(private readonly location: LocationWatcher, initialOption: tt4b.option.LimitOption) {
25-
this.#applyOption(initialOption)
24+
this.applyOption(initialOption)
2625
}
2726

2827
async init(state: LimitState, visit: VisitProcessor, delayCoord: DelayCoordinator) {
@@ -57,15 +56,10 @@ export default class Countdown {
5756
document.removeEventListener('visibilitychange', this.#onVisibleChange)
5857
}
5958

60-
async fetchOption() {
61-
const option = await getOption()
62-
this.#applyOption(option)
63-
this.#sync()
64-
}
65-
66-
#applyOption(option: tt4b.option.LimitOption) {
59+
applyOption(option: tt4b.option.LimitOption) {
6760
this.#state.delayDuration = option.limitDelayDuration
6861
this.#enabled = option.limitCountdown
62+
this.#sync()
6963
}
7064

7165
async #sync() {
@@ -91,7 +85,7 @@ export default class Countdown {
9185
this.#interval = undefined
9286
}
9387

94-
async #render() {
88+
#render() {
9589
const data = this.#state.data
9690
this.#component.render(data)
9791
data ? this.#startTimer() : this.#stopTimer()

src/content-script/limit/countdown/state.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class CountdownState {
1313
/**
1414
* Calculate the milliseconds of each dimension
1515
*/
16-
#dimStrategy: Record<Dimension, (item: tt4b.limit.Item) => [total: number, used: number] | undefined> = {
16+
readonly #dimStrategy: Record<Dimension, (item: tt4b.limit.Item) => [total: number, used: number] | undefined> = {
1717
daily: ({ time, waste, delayCount }) => {
1818
if (!time) return undefined
1919
const total = time * MILL_PER_SECOND
@@ -26,7 +26,11 @@ export class CountdownState {
2626
const used = waste + this.#activeMills - delayCount * this.delayDuration * MILL_PER_MINUTE
2727
return [total, used]
2828
},
29-
visit: ({ visitTime }) => visitTime ? [visitTime * MILL_PER_SECOND, this.#visitMills] : undefined,
29+
visit: ({ visitTime }) => {
30+
if (!visitTime) return undefined
31+
const used = Math.max(0, this.#visitMills - this.#delayCount * this.delayDuration * MILL_PER_MINUTE)
32+
return [visitTime * MILL_PER_SECOND, used]
33+
},
3034
}
3135

3236
set rules(rules: tt4b.limit.Item[]) {

src/content-script/limit/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default async function processLimit(state: LimitState, location: Location
2020

2121
const processors = [dailyWeeklyPsr, visitPsr, periodPsr, focusPsr]
2222
await Promise.all(processors.map(p => p.init()))
23-
location.onCurrChange(() => void processors.forEach(p => void p.reset()))
23+
location.onCurrChange(() => processors.forEach(p => void p.reset()))
2424

2525
new ModalManager(location).init(state, delayCoord, visitPsr)
2626

@@ -32,7 +32,7 @@ export default async function processLimit(state: LimitState, location: Location
3232
.register('limitChanged', () => processors.forEach(p => void p.reset()))
3333
.register('limitTimeMeet', items => dailyWeeklyPsr.onTimeMeet(items))
3434
.register('limitReminder', data => reminder.show(data))
35-
.register('limitCountdownChanged', () => countdown.fetchOption())
35+
.register('limitOptionChanged', val => countdown.applyOption(val))
3636
.register('askVisitHit', ruleId => state.reasons.some(r => r.type === 'VISIT' && ruleId === r.id))
3737
.register('focusChanged', session => focusPsr.onFocusChanged(session))
3838
}

types/tt4b.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ declare namespace tt4b {
958958
limitReminderDuration?: number
959959
/**
960960
* Whether to display the countdown
961-
*
961+
*
962962
* @since 4.5.1
963963
*/
964964
limitCountdown: boolean
@@ -1214,7 +1214,7 @@ declare namespace tt4b {
12141214
& mq._MakeRegistry<'limitReminder', limit.ReminderInfo>
12151215
& mq._MakeRegistry<'askVisitHit', number, boolean>
12161216
& mq._MakeRegistry<'focusChanged', focus.Session | undefined>
1217-
& mq._MakeRegistry<'limitCountdownChanged'>
1217+
& mq._MakeRegistry<'limitOptionChanged', tt4b.option.LimitOption>
12181218

12191219
type ReqCode = keyof _HandlerRegistry
12201220

0 commit comments

Comments
 (0)