feat: 完成observer
This commit is contained in:
parent
61fcb5fa22
commit
02f2b34fe6
|
@ -10,11 +10,15 @@ struct Index {
|
|||
@State key1: string = ''
|
||||
@State value1: string = ''
|
||||
|
||||
async aboutToAppear() {
|
||||
aboutToAppear() {
|
||||
KOnlineParamsUtil.init(getContext())
|
||||
await KOnlineParamsUtil.requestAsync()
|
||||
|
||||
KOnlineParamsUtil.requestAsync()
|
||||
KOnlineParamsUtil.run()
|
||||
|
||||
let time = 1
|
||||
KOnlineParamsManager.getInstance().registerOnParamsChangeListener(() => {
|
||||
console.log(this.TAG, 'CombData is changed! times: ' + time++);
|
||||
})
|
||||
}
|
||||
|
||||
async getModule(id: string) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { CombConfigManager } from './comb/CombConfigManager'
|
||||
import { KParams } from './comb/KParams'
|
||||
import { HashMap } from '@kit.ArkTS'
|
||||
|
||||
export type OnParamsChangeListener = () => void
|
||||
|
||||
|
@ -9,7 +10,6 @@ export type OnParamsChangeListener = () => void
|
|||
*/
|
||||
export class KOnlineParamsManager {
|
||||
private static INSTANCE: KOnlineParamsManager
|
||||
private listeners: OnParamsChangeListener[] = []
|
||||
private combConfigManager?: CombConfigManager
|
||||
|
||||
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) {
|
||||
this.listeners = this.listeners.filter(it => it != onParamsChangeListener)
|
||||
unregisterOnParamsChangeListener(id: string) {
|
||||
this.combConfigManager?.unregisterOnDataChangeListener(id)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,22 +4,30 @@ import { KOnlineParamsManager } from './KOnlineParamsManager'
|
|||
export namespace KOnlineParamsUtil {
|
||||
const COMB_PROJECT_ID: number = 1
|
||||
|
||||
|
||||
export function init(ctx: Context) {
|
||||
const combManager = new CombManager(ctx)
|
||||
|
||||
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() {
|
||||
KOnlineParamsManager.getInstance().runPoll()
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止轮询嗅探
|
||||
*/
|
||||
export function stop() {
|
||||
KOnlineParamsManager.getInstance().stopPoll()
|
||||
}
|
||||
|
|
|
@ -5,23 +5,35 @@
|
|||
*/
|
||||
import { CombDataInquiry } from '../comb_base/CombDataInquiry'
|
||||
import { CombDataStore } from '../comb_store/CombDataStore'
|
||||
import { ICombUpdater } from '../comb_type/CombTypes'
|
||||
import { CombDataListener, ICombObserver, ICombUpdater } from '../comb_type/CombTypes'
|
||||
import { KParams } from './KParams'
|
||||
|
||||
|
||||
interface ICombConfigManagerProps {
|
||||
store: CombDataStore,
|
||||
updater: ICombUpdater,
|
||||
observer: ICombObserver,
|
||||
}
|
||||
|
||||
export class CombConfigManager {
|
||||
private combStore: CombDataStore
|
||||
private combUpdater: ICombUpdater
|
||||
private combObserver: ICombObserver
|
||||
private timer?: number
|
||||
|
||||
constructor(combStore: CombDataStore, combUpdater: ICombUpdater) {
|
||||
this.combStore = combStore
|
||||
this.combUpdater = combUpdater
|
||||
constructor(props: ICombConfigManagerProps) {
|
||||
this.combStore = props.store
|
||||
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) {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { CombDataChecker } from '../comb_base/CombDataChecker'
|
||||
import { CombDataObserver } from '../comb_base/CombDataObserver'
|
||||
import { CombDataParser } from '../comb_base/CombDataParser'
|
||||
import { CombDataUpdater } from '../comb_base/CombDataUpdater'
|
||||
import { CombDataStore } from '../comb_store/CombDataStore'
|
||||
|
@ -19,17 +20,21 @@ export class CombManager {
|
|||
getCombConfigManager(projectID: number = 0) {
|
||||
const storeCore = new PreferencesStoreCore(this.context)
|
||||
const combStore = new CombDataStore(this.context, storeCore)
|
||||
|
||||
const combParser = new CombDataParser()
|
||||
const combChecker = new CombDataChecker()
|
||||
|
||||
const combObserver = new CombDataObserver()
|
||||
const combUpdater = new CombDataUpdater({
|
||||
store: combStore,
|
||||
parser: combParser,
|
||||
checker: combChecker,
|
||||
observer: combObserver,
|
||||
})
|
||||
|
||||
const combConfigManager = new CombConfigManager(combStore, combUpdater)
|
||||
const combConfigManager = new CombConfigManager({
|
||||
store: combStore,
|
||||
updater: combUpdater,
|
||||
observer: combObserver,
|
||||
})
|
||||
|
||||
return combConfigManager
|
||||
}
|
||||
|
|
38
support_comb/src/main/ets/comb_base/CombDataObserver.ets
Normal file
38
support_comb/src/main/ets/comb_base/CombDataObserver.ets
Normal 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()
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
import { CombDataStore } from '../comb_store/CombDataStore';
|
||||
import {
|
||||
ICombChecker,
|
||||
ICombObserver,
|
||||
ICombParser,
|
||||
ICombUpdater,
|
||||
IDiffConfigFile,
|
||||
|
@ -21,18 +22,21 @@ interface ICombDataUpdaterProps {
|
|||
store: CombDataStore
|
||||
parser: ICombParser
|
||||
checker: ICombChecker
|
||||
observer?: ICombObserver
|
||||
}
|
||||
|
||||
export class CombDataUpdater implements ICombUpdater {
|
||||
private dataStore: CombDataStore
|
||||
private checker: ICombChecker
|
||||
private parser: ICombParser
|
||||
private observer?: ICombObserver
|
||||
private verData: IVerData | null = null
|
||||
|
||||
constructor(props: ICombDataUpdaterProps) {
|
||||
this.dataStore = props.store
|
||||
this.parser = props.parser
|
||||
this.checker = props.checker
|
||||
this.observer = props.observer
|
||||
}
|
||||
|
||||
async getVer() {
|
||||
|
@ -49,6 +53,8 @@ export class CombDataUpdater implements ICombUpdater {
|
|||
else await this.fullUpdate(verData.full_url)
|
||||
|
||||
await this.updateIds()
|
||||
|
||||
this.observer?.notify()
|
||||
}
|
||||
|
||||
// todo: implement it
|
||||
|
|
|
@ -199,6 +199,7 @@ export class CombDataStore {
|
|||
|
||||
async storeIds(ids: string[]) {
|
||||
// todo: 存储ids
|
||||
await this.storeCore.flush()
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -78,6 +78,15 @@ export interface ICombUpdater {
|
|||
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 {
|
||||
checkFullFileBytesCount: (file: IFullConfigFile) => boolean
|
||||
checkDiffFileBytesCount: (file: IDiffConfigFile) => boolean
|
||||
|
|
|
@ -5,9 +5,7 @@ import { CombDataUpdater } from '../main/ets/comb_base/CombDataUpdater';
|
|||
import { CombDataStore } from '../main/ets/comb_store/CombDataStore';
|
||||
import { HashMapStoreCore } from '../main/ets/comb_store/core/HashMapCoreStore';
|
||||
|
||||
export default function combStoreUnit() {
|
||||
describe('CombStore', () => {
|
||||
it('test_updater', 0, async () => {
|
||||
export default async function combStoreUnit() {
|
||||
const combStore = await new CombDataStore(getContext(), new HashMapStoreCore()).init()
|
||||
|
||||
const combUpdater = new CombDataUpdater({
|
||||
|
@ -16,12 +14,14 @@ export default function combStoreUnit() {
|
|||
checker: new CombDataChecker(),
|
||||
})
|
||||
|
||||
const verData = await combUpdater.getVer()
|
||||
console.log(`ver Data ${JSON.stringify(verData)}`)
|
||||
|
||||
await combUpdater.update()
|
||||
console.log(`updated comStore size: ${await combStore.getSize()}`);
|
||||
|
||||
describe('CombStore', () => {
|
||||
it('test_updater', 0, async () => {
|
||||
const verData = await combUpdater.getVer()
|
||||
console.log(`ver Data ${JSON.stringify(verData)}`)
|
||||
|
||||
for (let i = 10040; i < 10080; i++) {
|
||||
const module = await combStore.getModuleById(i + '')
|
||||
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, () => {
|
||||
const a: Object = true
|
||||
const b: string = a as string
|
||||
|
|
Loading…
Reference in New Issue
Block a user