event debouncing / throttling basically working now
This commit is contained in:
parent
8e7208ba08
commit
066f68ff4d
5 changed files with 33 additions and 22 deletions
|
|
@ -2,7 +2,8 @@ export namespace EventDataCache {
|
||||||
|
|
||||||
|
|
||||||
declare const global: {
|
declare const global: {
|
||||||
cache: LuaTable<CacheKey<FactorioEventUpdateType>, CacheEntry<FactorioEventUpdateType>>,
|
cache: LuaTable<string, CacheEntry<FactorioEventUpdateType>>,
|
||||||
|
// cache: Map<string, CacheEntry<FactorioEventUpdateType>>,
|
||||||
defaultCacheDurationTicks: Record<FactorioEventUpdateType | "default", uint>,
|
defaultCacheDurationTicks: Record<FactorioEventUpdateType | "default", uint>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -16,7 +17,8 @@ export namespace EventDataCache {
|
||||||
|
|
||||||
if (force == true || isAnythingUndefined) {
|
if (force == true || isAnythingUndefined) {
|
||||||
log("Initialising global.cache")
|
log("Initialising global.cache")
|
||||||
global.cache = new LuaTable<CacheKey<FactorioEventUpdateType>, CacheEntry<FactorioEventUpdateType>>()
|
// global.cache = new Map<CacheKey<FactorioEventUpdateType>, CacheEntry<FactorioEventUpdateType>>()
|
||||||
|
global.cache = new LuaTable<string, CacheEntry<FactorioEventUpdateType>>()
|
||||||
|
|
||||||
log("Initialising global.DEFAULT_CACHE_DURATION_TICKS")
|
log("Initialising global.DEFAULT_CACHE_DURATION_TICKS")
|
||||||
global.defaultCacheDurationTicks = {
|
global.defaultCacheDurationTicks = {
|
||||||
|
|
@ -83,13 +85,19 @@ export namespace EventDataCache {
|
||||||
|
|
||||||
|
|
||||||
export function extractExpired(): Array<FactorioEventUpdate> {
|
export function extractExpired(): Array<FactorioEventUpdate> {
|
||||||
|
let countExpired = 0
|
||||||
|
let countTotal = 0
|
||||||
const expiredData: Array<FactorioEventUpdate> = []
|
const expiredData: Array<FactorioEventUpdate> = []
|
||||||
for (let [key, entry] of global.cache) {
|
for (let [key, entry] of global.cache) {
|
||||||
|
countTotal++
|
||||||
if (isExpired(entry)) {
|
if (isExpired(entry)) {
|
||||||
|
countExpired++
|
||||||
expiredData.push(entry.data)
|
expiredData.push(entry.data)
|
||||||
global.cache.delete(key)
|
global.cache.delete(key)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log(`cache expired items count: ${countExpired}, total: ${countTotal}`)
|
||||||
return expiredData
|
return expiredData
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -97,10 +105,12 @@ export namespace EventDataCache {
|
||||||
function getCacheEntry<TYPE extends FactorioEventUpdateType>(
|
function getCacheEntry<TYPE extends FactorioEventUpdateType>(
|
||||||
key: CacheKey<TYPE>
|
key: CacheKey<TYPE>
|
||||||
): CacheEntry<TYPE> | undefined {
|
): CacheEntry<TYPE> | undefined {
|
||||||
if (!global.cache.has(key)) {
|
const hash = game.encode_string(game.table_to_json(key))!!
|
||||||
|
// const value: CacheEntry<any> | undefined = global.cache.get(hash)
|
||||||
|
if (!global.cache.has(hash)) {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
const value: CacheEntry<any> = global.cache.get(key)
|
const value: CacheEntry<any> = global.cache.get(hash)
|
||||||
if (isEntryInstanceOf(value, key.updateType)) {
|
if (isEntryInstanceOf(value, key.updateType)) {
|
||||||
return value
|
return value
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -114,10 +124,11 @@ export namespace EventDataCache {
|
||||||
key: CacheKey<TYPE>,
|
key: CacheKey<TYPE>,
|
||||||
mutate: CacheDataMutator<TYPE>,
|
mutate: CacheDataMutator<TYPE>,
|
||||||
): CacheEntry<TYPE> {
|
): CacheEntry<TYPE> {
|
||||||
const data: CacheData<TYPE> = <CacheData<TYPE>>{updateType: key.updateType}
|
const data: CacheData<TYPE> = <CacheData<TYPE>>{...key}
|
||||||
mutate(data)
|
mutate(data)
|
||||||
let entry: CacheEntry<TYPE> = new CacheEntry<TYPE>(data)
|
let entry: CacheEntry<TYPE> = new CacheEntry<TYPE>(data)
|
||||||
global.cache.set(key, entry)
|
const hash = game.encode_string(game.table_to_json(key))!!
|
||||||
|
global.cache.set(hash, entry)
|
||||||
return entry
|
return entry
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -151,7 +162,6 @@ export namespace EventDataCache {
|
||||||
export type CacheData<TYPE extends FactorioEventUpdateType> =
|
export type CacheData<TYPE extends FactorioEventUpdateType> =
|
||||||
CacheTyped<TYPE>
|
CacheTyped<TYPE>
|
||||||
& Omit<ConvertToUpdate<TYPE>, "updateType">
|
& Omit<ConvertToUpdate<TYPE>, "updateType">
|
||||||
// & NullablePartial<Omit<ConvertToUpdate<TYPE>, "updateType">>
|
|
||||||
|
|
||||||
|
|
||||||
export type CacheTyped<TYPE extends FactorioEventUpdateType> = {
|
export type CacheTyped<TYPE extends FactorioEventUpdateType> = {
|
||||||
|
|
@ -187,18 +197,26 @@ export namespace EventDataCache {
|
||||||
|
|
||||||
|
|
||||||
function isEntryInstanceOf<TYPE extends FactorioEventUpdateType>(
|
function isEntryInstanceOf<TYPE extends FactorioEventUpdateType>(
|
||||||
data: CacheEntry<any>,
|
entry: CacheEntry<any> | undefined,
|
||||||
updateType: TYPE,
|
updateType: TYPE,
|
||||||
): data is CacheEntry<TYPE> {
|
): entry is CacheEntry<TYPE> {
|
||||||
return isDataInstanceOf<TYPE>(data.data, updateType)
|
return isDataInstanceOf<TYPE>(entry?.data, updateType)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function isDataInstanceOf<TYPE extends FactorioEventUpdateType>(
|
function isDataInstanceOf<TYPE extends FactorioEventUpdateType>(
|
||||||
data: CacheData<any>,
|
data: CacheData<any> | undefined,
|
||||||
updateType: TYPE,
|
updateType: TYPE,
|
||||||
): data is CacheData<TYPE> {
|
): data is CacheData<TYPE> {
|
||||||
return data.updateType == updateType
|
return data != undefined && data.updateType == updateType
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function isKeyInstanceOf<TYPE extends FactorioEventUpdateType>(
|
||||||
|
key: CacheKey<any> | undefined,
|
||||||
|
updateType: TYPE,
|
||||||
|
): key is CacheKey<TYPE> {
|
||||||
|
return key != undefined && key.updateType == updateType
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,9 +20,7 @@ function playerUpdateThrottle(
|
||||||
if (data.events == null) {
|
if (data.events == null) {
|
||||||
data.events = []
|
data.events = []
|
||||||
}
|
}
|
||||||
if (eventName ! in data.events) {
|
data.events.push(eventName)
|
||||||
data.events.push(eventName)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,6 @@
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/node": {
|
|
||||||
"version": "12.20.33",
|
|
||||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-12.20.33.tgz",
|
|
||||||
"integrity": "sha512-5XmYX2GECSa+CxMYaFsr2mrql71Q4EvHjKS+ox/SiwSdaASMoBIWE6UmZqFO+VX1jIcsYLStI4FFoB6V7FeIYw=="
|
|
||||||
},
|
|
||||||
"enhanced-resolve": {
|
"enhanced-resolve": {
|
||||||
"version": "5.8.3",
|
"version": "5.8.3",
|
||||||
"resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.8.3.tgz",
|
"resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.8.3.tgz",
|
||||||
|
|
|
||||||
|
|
@ -13,11 +13,11 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"lua-types": "^2.11.0",
|
"lua-types": "^2.11.0",
|
||||||
"@types/node": "^12.0.8",
|
|
||||||
"typescript-to-lua": "1.3.3",
|
"typescript-to-lua": "1.3.3",
|
||||||
"typed-factorio": "0.18.1",
|
"typed-factorio": "0.18.1",
|
||||||
"typescript": "4.5.5"
|
"typescript": "4.5.5"
|
||||||
},
|
},
|
||||||
|
"// @types/node": "^12.0.8",
|
||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ fun splitFactorioServerPacketStream(
|
||||||
is FactorioEvent -> value.data.objectName.name
|
is FactorioEvent -> value.data.objectName.name
|
||||||
is FactorioConfigurationUpdate -> "FactorioConfigurationUpdate"
|
is FactorioConfigurationUpdate -> "FactorioConfigurationUpdate"
|
||||||
is FactorioPrototypes -> "all"
|
is FactorioPrototypes -> "all"
|
||||||
is FactorioEventUpdatePacket -> "all"
|
is FactorioEventUpdatePacket -> value.update.updateType.name
|
||||||
}
|
}
|
||||||
|
|
||||||
"$base.$type"
|
"$base.$type"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue