feat: 完成observer

This commit is contained in:
Cheliangzhao 2024-04-25 11:14:33 +08:00
parent 61fcb5fa22
commit 02f2b34fe6
10 changed files with 123 additions and 33 deletions

View File

@ -10,11 +10,15 @@ struct Index {
@State key1: string = '' @State key1: string = ''
@State value1: string = '' @State value1: string = ''
async aboutToAppear() { aboutToAppear() {
KOnlineParamsUtil.init(getContext()) KOnlineParamsUtil.init(getContext())
await KOnlineParamsUtil.requestAsync() KOnlineParamsUtil.requestAsync()
KOnlineParamsUtil.run() KOnlineParamsUtil.run()
let time = 1
KOnlineParamsManager.getInstance().registerOnParamsChangeListener(() => {
console.log(this.TAG, 'CombData is changed! times: ' + time++);
})
} }
async getModule(id: string) { async getModule(id: string) {

View File

@ -1,5 +1,6 @@
import { CombConfigManager } from './comb/CombConfigManager' import { CombConfigManager } from './comb/CombConfigManager'
import { KParams } from './comb/KParams' import { KParams } from './comb/KParams'
import { HashMap } from '@kit.ArkTS'
export type OnParamsChangeListener = () => void export type OnParamsChangeListener = () => void
@ -9,7 +10,6 @@ export type OnParamsChangeListener = () => void
*/ */
export class KOnlineParamsManager { export class KOnlineParamsManager {
private static INSTANCE: KOnlineParamsManager private static INSTANCE: KOnlineParamsManager
private listeners: OnParamsChangeListener[] = []
private combConfigManager?: CombConfigManager private combConfigManager?: CombConfigManager
private constructor() { private constructor() {
@ -51,11 +51,14 @@ export class KOnlineParamsManager {
} }
registerOnParamsChangeListeners(onParamsChangeListener: OnParamsChangeListener) { /**
this.listeners.push(onParamsChangeListener) * 注册comb更新监听器
*/
registerOnParamsChangeListener(onParamsChangeListener: OnParamsChangeListener): string {
return this.combConfigManager?.registerOnDataChangeListener(onParamsChangeListener) ?? ''
} }
unregisterOnParamsChangeListeners(onParamsChangeListener: OnParamsChangeListener) { unregisterOnParamsChangeListener(id: string) {
this.listeners = this.listeners.filter(it => it != onParamsChangeListener) this.combConfigManager?.unregisterOnDataChangeListener(id)
} }
} }

View File

@ -4,22 +4,30 @@ import { KOnlineParamsManager } from './KOnlineParamsManager'
export namespace KOnlineParamsUtil { export namespace KOnlineParamsUtil {
const COMB_PROJECT_ID: number = 1 const COMB_PROJECT_ID: number = 1
export function init(ctx: Context) { export function init(ctx: Context) {
const combManager = new CombManager(ctx) const combManager = new CombManager(ctx)
KOnlineParamsManager.getInstance().init(combManager.getCombConfigManager(COMB_PROJECT_ID)) KOnlineParamsManager.getInstance().init(combManager.getCombConfigManager(COMB_PROJECT_ID))
} }
export async function requestAsync() { /**
await KOnlineParamsManager.getInstance().requestForce(false) * 拉取配置和ids
* @param isDiff 是否增量更新
*/
export async function requestAsync(isDiff: boolean = false) {
await KOnlineParamsManager.getInstance().requestForce(isDiff)
} }
/**
* 运行轮询嗅探
*/
export function run() { export function run() {
KOnlineParamsManager.getInstance().runPoll() KOnlineParamsManager.getInstance().runPoll()
} }
/**
* 停止轮询嗅探
*/
export function stop() { export function stop() {
KOnlineParamsManager.getInstance().stopPoll() KOnlineParamsManager.getInstance().stopPoll()
} }

View File

@ -5,23 +5,35 @@
*/ */
import { CombDataInquiry } from '../comb_base/CombDataInquiry' import { CombDataInquiry } from '../comb_base/CombDataInquiry'
import { CombDataStore } from '../comb_store/CombDataStore' import { CombDataStore } from '../comb_store/CombDataStore'
import { ICombUpdater } from '../comb_type/CombTypes' import { CombDataListener, ICombObserver, ICombUpdater } from '../comb_type/CombTypes'
import { KParams } from './KParams' import { KParams } from './KParams'
interface ICombConfigManagerProps {
store: CombDataStore,
updater: ICombUpdater,
observer: ICombObserver,
}
export class CombConfigManager { export class CombConfigManager {
private combStore: CombDataStore private combStore: CombDataStore
private combUpdater: ICombUpdater private combUpdater: ICombUpdater
private combObserver: ICombObserver
private timer?: number private timer?: number
constructor(combStore: CombDataStore, combUpdater: ICombUpdater) { constructor(props: ICombConfigManagerProps) {
this.combStore = combStore this.combStore = props.store
this.combUpdater = combUpdater this.combUpdater = props.updater
this.combObserver = props.observer
} }
registerOnDataChangeListeners() { registerOnDataChangeListener(listener: CombDataListener) {
const id = this.combObserver.register(listener)
return id
} }
unregisterOnDataChangeListeners() { unregisterOnDataChangeListener(id: string) {
this.combObserver.unregister(id)
} }
async requestUpdateAsync(isDiff: boolean = false) { async requestUpdateAsync(isDiff: boolean = false) {

View File

@ -1,4 +1,5 @@
import { CombDataChecker } from '../comb_base/CombDataChecker' import { CombDataChecker } from '../comb_base/CombDataChecker'
import { CombDataObserver } from '../comb_base/CombDataObserver'
import { CombDataParser } from '../comb_base/CombDataParser' import { CombDataParser } from '../comb_base/CombDataParser'
import { CombDataUpdater } from '../comb_base/CombDataUpdater' import { CombDataUpdater } from '../comb_base/CombDataUpdater'
import { CombDataStore } from '../comb_store/CombDataStore' import { CombDataStore } from '../comb_store/CombDataStore'
@ -19,17 +20,21 @@ export class CombManager {
getCombConfigManager(projectID: number = 0) { getCombConfigManager(projectID: number = 0) {
const storeCore = new PreferencesStoreCore(this.context) const storeCore = new PreferencesStoreCore(this.context)
const combStore = new CombDataStore(this.context, storeCore) const combStore = new CombDataStore(this.context, storeCore)
const combParser = new CombDataParser() const combParser = new CombDataParser()
const combChecker = new CombDataChecker() const combChecker = new CombDataChecker()
const combObserver = new CombDataObserver()
const combUpdater = new CombDataUpdater({ const combUpdater = new CombDataUpdater({
store: combStore, store: combStore,
parser: combParser, parser: combParser,
checker: combChecker, checker: combChecker,
observer: combObserver,
}) })
const combConfigManager = new CombConfigManager(combStore, combUpdater) const combConfigManager = new CombConfigManager({
store: combStore,
updater: combUpdater,
observer: combObserver,
})
return combConfigManager return combConfigManager
} }

View File

@ -0,0 +1,38 @@
import { HashMap } from '@kit.ArkTS'
import { ICombObserver, CombDataListener } from '../comb_type/CombTypes'
/**
* 发布订阅
*
* @author 车亮召
* @date 2024.4.25
*/
export class CombDataObserver implements ICombObserver {
private listeners: HashMap<string, CombDataListener>
constructor() {
this.listeners = new HashMap()
}
notify() {
for (const listener of this.listeners.values()) {
listener()
}
}
register(listener: CombDataListener) {
const id = String(Date.now())
this.listeners.set(id, listener)
return id
}
unregister(id: string) {
this.listeners.remove(id)
}
unregisterAll() {
this.listeners.clear()
}
}

View File

@ -1,6 +1,7 @@
import { CombDataStore } from '../comb_store/CombDataStore'; import { CombDataStore } from '../comb_store/CombDataStore';
import { import {
ICombChecker, ICombChecker,
ICombObserver,
ICombParser, ICombParser,
ICombUpdater, ICombUpdater,
IDiffConfigFile, IDiffConfigFile,
@ -21,18 +22,21 @@ interface ICombDataUpdaterProps {
store: CombDataStore store: CombDataStore
parser: ICombParser parser: ICombParser
checker: ICombChecker checker: ICombChecker
observer?: ICombObserver
} }
export class CombDataUpdater implements ICombUpdater { export class CombDataUpdater implements ICombUpdater {
private dataStore: CombDataStore private dataStore: CombDataStore
private checker: ICombChecker private checker: ICombChecker
private parser: ICombParser private parser: ICombParser
private observer?: ICombObserver
private verData: IVerData | null = null private verData: IVerData | null = null
constructor(props: ICombDataUpdaterProps) { constructor(props: ICombDataUpdaterProps) {
this.dataStore = props.store this.dataStore = props.store
this.parser = props.parser this.parser = props.parser
this.checker = props.checker this.checker = props.checker
this.observer = props.observer
} }
async getVer() { async getVer() {
@ -49,6 +53,8 @@ export class CombDataUpdater implements ICombUpdater {
else await this.fullUpdate(verData.full_url) else await this.fullUpdate(verData.full_url)
await this.updateIds() await this.updateIds()
this.observer?.notify()
} }
// todo: implement it // todo: implement it

View File

@ -199,6 +199,7 @@ export class CombDataStore {
async storeIds(ids: string[]) { async storeIds(ids: string[]) {
// todo: 存储ids // todo: 存储ids
await this.storeCore.flush()
} }
/** /**

View File

@ -78,6 +78,15 @@ export interface ICombUpdater {
getIds: () => Promise<string[]> getIds: () => Promise<string[]>
} }
export type CombDataListener = () => void | Promise<void>
export interface ICombObserver {
notify: () => void
register: (listener: CombDataListener) => string
unregister: (id: string) => void
unregisterAll: () => void
}
export interface ICombChecker { export interface ICombChecker {
checkFullFileBytesCount: (file: IFullConfigFile) => boolean checkFullFileBytesCount: (file: IFullConfigFile) => boolean
checkDiffFileBytesCount: (file: IDiffConfigFile) => boolean checkDiffFileBytesCount: (file: IDiffConfigFile) => boolean

View File

@ -5,23 +5,23 @@ import { CombDataUpdater } from '../main/ets/comb_base/CombDataUpdater';
import { CombDataStore } from '../main/ets/comb_store/CombDataStore'; import { CombDataStore } from '../main/ets/comb_store/CombDataStore';
import { HashMapStoreCore } from '../main/ets/comb_store/core/HashMapCoreStore'; import { HashMapStoreCore } from '../main/ets/comb_store/core/HashMapCoreStore';
export default function combStoreUnit() { export default async function combStoreUnit() {
const combStore = await new CombDataStore(getContext(), new HashMapStoreCore()).init()
const combUpdater = new CombDataUpdater({
store: combStore,
parser: new CombDataParser(),
checker: new CombDataChecker(),
})
await combUpdater.update()
console.log(`updated comStore size: ${await combStore.getSize()}`);
describe('CombStore', () => { describe('CombStore', () => {
it('test_updater', 0, async () => { it('test_updater', 0, async () => {
const combStore = await new CombDataStore(getContext(), new HashMapStoreCore()).init()
const combUpdater = new CombDataUpdater({
store: combStore,
parser: new CombDataParser(),
checker: new CombDataChecker(),
})
const verData = await combUpdater.getVer() const verData = await combUpdater.getVer()
console.log(`ver Data ${JSON.stringify(verData)}`) console.log(`ver Data ${JSON.stringify(verData)}`)
await combUpdater.update()
console.log(`updated comStore size: ${await combStore.getSize()}`);
for (let i = 10040; i < 10080; i++) { for (let i = 10040; i < 10080; i++) {
const module = await combStore.getModuleById(i + '') const module = await combStore.getModuleById(i + '')
module && console.log(module?.toString()); module && console.log(module?.toString());
@ -34,6 +34,10 @@ export default function combStoreUnit() {
} }
}) })
it('test_diff_update', 0, async () => {
await combUpdater.update(true)
})
it('test', 0, () => { it('test', 0, () => {
const a: Object = true const a: Object = true
const b: string = a as string const b: string = a as string