-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(stash): clean up mutations, emit updates as a list #3376
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4b08147
pull out stash changes from entrykit
alvrs d4cd627
remove unrelated changes
alvrs 0a849d8
fix tests
alvrs 9452fd9
update types
alvrs b3ec3a4
update registerTable
alvrs fe4481a
fix type issues
alvrs 368d3f1
fix subscribeStash test
alvrs 8818ee0
fix subscribeTable test
alvrs 3d02aa2
update createStash test
alvrs 6a5be07
update defaultActions test
alvrs f7525bb
update getTable test
alvrs be7cd4b
update query type
alvrs 5cc3324
improve query result and params
alvrs 799ba48
fix query subscriber updates
alvrs e1c6f2e
add test for multiple updates
alvrs 6dff518
Create popular-lies-teach.md
holic 539f105
Apply suggestions from code review
holic e081b49
Merge remote-tracking branch 'origin/main' into alvrs/stash-improvements
holic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@latticexyz/stash": patch | ||
--- | ||
|
||
Consolidated how state changes are applied and subscribers notified. Stash subscribers now receive an ordered list of state updates rather than an object. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { schemaAbiTypeToDefaultValue } from "@latticexyz/schema-type/internal"; | ||
import { Key, Stash, TableRecord, TableUpdates } from "../common"; | ||
import { encodeKey } from "./encodeKey"; | ||
import { Table } from "@latticexyz/config"; | ||
import { registerTable } from "./registerTable"; | ||
|
||
export type PendingStashUpdate<table extends Table = Table> = { | ||
table: table; | ||
key: Key<table>; | ||
value: undefined | Partial<TableRecord<table>>; | ||
}; | ||
|
||
export type ApplyUpdatesArgs = { | ||
stash: Stash; | ||
updates: PendingStashUpdate[]; | ||
}; | ||
|
||
type PendingUpdates = { | ||
[namespaceLabel: string]: { | ||
[tableLabel: string]: TableUpdates; | ||
}; | ||
}; | ||
|
||
const pendingStashUpdates = new Map<Stash, PendingUpdates>(); | ||
|
||
export function applyUpdates({ stash, updates }: ApplyUpdatesArgs): void { | ||
const pendingUpdates = pendingStashUpdates.get(stash) ?? {}; | ||
if (!pendingStashUpdates.has(stash)) pendingStashUpdates.set(stash, pendingUpdates); | ||
|
||
for (const { table, key, value } of updates) { | ||
if (stash.get().config[table.namespaceLabel]?.[table.label] == null) { | ||
registerTable({ stash, table }); | ||
} | ||
const tableState = ((stash._.state.records[table.namespaceLabel] ??= {})[table.label] ??= {}); | ||
const encodedKey = encodeKey({ table, key }); | ||
const prevRecord = tableState[encodedKey]; | ||
|
||
// create new record, preserving field order | ||
const nextRecord = | ||
value == null | ||
? undefined | ||
: Object.fromEntries( | ||
Object.entries(table.schema).map(([fieldName, { type }]) => [ | ||
fieldName, | ||
key[fieldName] ?? // Use provided key fields | ||
value[fieldName] ?? // Or provided value fields | ||
prevRecord?.[fieldName] ?? // Keep existing non-overridden fields | ||
schemaAbiTypeToDefaultValue[type], // Default values for new fields | ||
]), | ||
); | ||
|
||
// apply update to state | ||
if (nextRecord != null) { | ||
tableState[encodedKey] = nextRecord; | ||
} else { | ||
delete tableState[encodedKey]; | ||
} | ||
|
||
// add update to pending updates for notifying subscribers | ||
const tableUpdates = ((pendingUpdates[table.namespaceLabel] ??= {})[table.label] ??= []); | ||
tableUpdates.push({ | ||
table, | ||
key, | ||
previous: prevRecord, | ||
current: nextRecord, | ||
}); | ||
} | ||
|
||
queueMicrotask(() => { | ||
notifySubscribers(stash); | ||
}); | ||
} | ||
|
||
function notifySubscribers(stash: Stash) { | ||
const pendingUpdates = pendingStashUpdates.get(stash); | ||
if (!pendingUpdates) return; | ||
|
||
// Notify table subscribers | ||
for (const [namespaceLabel, namespaceUpdates] of Object.entries(pendingUpdates)) { | ||
for (const [tableLabel, tableUpdates] of Object.entries(namespaceUpdates)) { | ||
stash._.tableSubscribers[namespaceLabel]?.[tableLabel]?.forEach((subscriber) => subscriber(tableUpdates)); | ||
} | ||
} | ||
// Notify stash subscribers | ||
const updates = Object.values(pendingUpdates) | ||
.map((namespaceUpdates) => Object.values(namespaceUpdates)) | ||
.flat(2); | ||
stash._.storeSubscribers.forEach((subscriber) => subscriber({ type: "records", updates })); | ||
|
||
pendingStashUpdates.delete(stash); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in my entrykit branch, I had removed queuing because it caused something in the live records sync to fail and fallback to the slower sync methods and was unclear why
do you know if this is an issue here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tested locally with a fresh template and everything seems fine
making a mental note to come back to this if we see weird issues with syncing, esp large chunks of data