Skip to content

Commit

Permalink
Change keyToId map from Record to Map
Browse files Browse the repository at this point in the history
  • Loading branch information
oamaok committed Jan 21, 2024
1 parent 27ef215 commit 8e94d83
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/kaiku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,17 @@ const dependeeToKeys: Map<DependeeId, Set<StateKey>> = new Map()
const keyToDependees: Map<StateKey, Set<DependeeId>> = new Map()

let updatedDependencies: StateKey[] = []
const keyToId: Record<any, number> = {}
const keyToId: Map<any, StateKey> = new Map()
const getKeyId = (key: any): StateKey => {
const existingKey = keyToId.get(key)
if (existingKey) {
return existingKey
}

const id = (++nextKeyId * 0x4000000) as StateKey
keyToId.set(key, id)
return id
}

let deferredUpdateQueued = false

Expand Down Expand Up @@ -443,9 +453,7 @@ const createState = <T extends object>(obj: T): State<T> => {
}

if (trackedDependencyStack.length) {
const dependencyKey = (id +
(keyToId[key] ||
(keyToId[key] = ++nextKeyId * 0x4000000))) as StateKey
const dependencyKey = (id + getKeyId(key)) as StateKey
trackedDependencyStack[trackedDependencyStack.length - 1].add(
dependencyKey
)
Expand Down Expand Up @@ -525,8 +533,7 @@ const createState = <T extends object>(obj: T): State<T> => {
updatedDependencies.push(id as StateKey)
}

const dependencyKey = (id +
(keyToId[key] || (keyToId[key] = ++nextKeyId * 0x4000000))) as StateKey
const dependencyKey = (id + getKeyId(key)) as StateKey
updatedDependencies.push(dependencyKey)
if (!deferredUpdateQueued) {
deferredUpdateQueued = true
Expand Down

0 comments on commit 8e94d83

Please sign in to comment.