-
Notifications
You must be signed in to change notification settings - Fork 580
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
Store Rubric in IndexedDB #9840
Merged
thsparks
merged 16 commits into
master
from
thsparks/teacher_tool/store_rubric_in_indexedDb
Feb 1, 2024
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6a63633
Validator service improvements
thsparks eb39f55
Untested code to write / read rubrics from indexeddb
thsparks ac06f95
Create Rubric object, which also contains a field for name
thsparks f07434f
Some saving and loading from indexeddb. May want to rework so saving …
thsparks db4440d
Save rubric when removing criteria
thsparks 3d531a4
Validate criteria when loading
thsparks 5068f1c
Move save code into transform called by reducer
thsparks 1dfc266
Update schema for indexeddb to separate metadata. I anticipate we may…
thsparks 8d0e79c
Prettier
thsparks de5d7b5
Merge branch 'master' of https://github.com/microsoft/pxt into thspar…
thsparks 05267c0
Aria label and preserveValueOnBlur for rubric name input
thsparks 0619155
Use a global promise that initializes the db to ensure initialization…
thsparks d3a95cb
Apply suggestions from code review
thsparks 8f86a03
Merge branch 'thsparks/teacher_tool/store_rubric_in_indexedDb' of htt…
thsparks cc03024
Create & use a debounced input to save the rubric name rather than re…
thsparks 3f6314c
DebouncedInput updates from PR (send event if pending and unmounting …
thsparks 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useEffect, useRef } from "react"; | ||
import { Input, InputProps } from "react-common/components/controls/Input"; | ||
|
||
export interface DebouncedInputProps extends InputProps { | ||
intervalMs?: number; // Default 500 ms | ||
} | ||
|
||
// This functions like the React Common Input, but debounces onChange calls, | ||
// so if onChange is called multiple times in quick succession, it will only | ||
// be executed once after a pause of the specified `interval` in milliseconds. | ||
export const DebouncedInput: React.FC<DebouncedInputProps> = ({ intervalMs = 500, ...props }) => { | ||
const timerId = useRef<NodeJS.Timeout | undefined>(undefined); | ||
const latestValue = useRef<string>(""); | ||
|
||
const sendChange = () => { | ||
if (props.onChange) { | ||
props.onChange(latestValue.current); | ||
} | ||
}; | ||
|
||
// If the timer is pending and the component unmounts, | ||
// clear the timer and fire the onChange event immediately. | ||
useEffect(() => { | ||
return () => { | ||
if (timerId.current) { | ||
clearTimeout(timerId.current); | ||
sendChange(); | ||
} | ||
}; | ||
}, []); | ||
|
||
const onChangeDebounce = (newValue: string) => { | ||
latestValue.current = newValue; | ||
|
||
if (timerId.current) { | ||
clearTimeout(timerId.current); | ||
} | ||
|
||
timerId.current = setTimeout(sendChange, intervalMs); | ||
}; | ||
|
||
return <Input {...props} onChange={onChangeDebounce} />; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { openDB, IDBPDatabase } from "idb"; | ||
import { ErrorCode } from "../types/errorCode"; | ||
import { logError } from "./loggingService"; | ||
import { Rubric } from "../types/rubric"; | ||
|
||
const teacherToolDbName = "makecode-project-insights"; | ||
const dbVersion = 1; | ||
const rubricsStoreName = "rubrics"; | ||
const metadataStoreName = "metadata"; | ||
const metadataKeys = { | ||
lastActiveRubricKey: "lastActiveRubricName", | ||
}; | ||
|
||
type MetadataEntry = { key: string; value: any }; | ||
|
||
class TeacherToolDb { | ||
db: IDBPDatabase | undefined; | ||
|
||
public async initializeAsync() { | ||
if (this.db) return; | ||
this.db = await openDB(teacherToolDbName, dbVersion, { | ||
upgrade(db) { | ||
db.createObjectStore(rubricsStoreName, { keyPath: "name" }); | ||
db.createObjectStore(metadataStoreName, { keyPath: "key" }); | ||
}, | ||
}); | ||
} | ||
|
||
private async getAsync<T>(storeName: string, key: string): Promise<T | undefined> { | ||
if (!this.db) { | ||
throw new Error("IndexedDb not initialized."); | ||
} | ||
|
||
try { | ||
return await this.db.get(storeName, key); | ||
} catch (e) { | ||
// Not recording key, as it could contain user-input with sensitive information. | ||
logError(ErrorCode.unableToGetIndexedDbRecord, e); | ||
} | ||
} | ||
|
||
private async setAsync<T>(storeName: string, value: T): Promise<void> { | ||
if (!this.db) { | ||
throw new Error("IndexedDb not initialized."); | ||
} | ||
|
||
try { | ||
await this.db.put(storeName, value); | ||
} catch (e) { | ||
// Not recording key, as it could contain user-input with sensitive information. | ||
logError(ErrorCode.unableToSetIndexedDbRecord, e); | ||
} | ||
} | ||
|
||
private async deleteAsync(storeName: string, key: string): Promise<void> { | ||
if (!this.db) { | ||
throw new Error("IndexedDb not initialized."); | ||
} | ||
try { | ||
await this.db.delete(storeName, key); | ||
} catch (e) { | ||
// Not recording key, as it could contain user-input with sensitive information. | ||
logError(ErrorCode.unableToDeleteIndexedDbRecord, e); | ||
} | ||
} | ||
|
||
private async getMetadataEntryAsync(key: string): Promise<MetadataEntry | undefined> { | ||
return this.getAsync<MetadataEntry>(metadataStoreName, key); | ||
} | ||
|
||
private async setMetadataEntryAsync(key: string, value: any): Promise<void> { | ||
return this.setAsync<MetadataEntry>(metadataStoreName, { key, value }); | ||
} | ||
|
||
private async deleteMetadataEntryAsync(key: string): Promise<void> { | ||
return this.deleteAsync(metadataStoreName, key); | ||
} | ||
|
||
public async getLastActiveRubricNameAsync(): Promise<string | undefined> { | ||
const metadataEntry = await this.getMetadataEntryAsync(metadataKeys.lastActiveRubricKey); | ||
return metadataEntry?.value; | ||
} | ||
|
||
public saveLastActiveRubricNameAsync(name: string): Promise<void> { | ||
return this.setMetadataEntryAsync(metadataKeys.lastActiveRubricKey, name); | ||
} | ||
|
||
public getRubric(name: string): Promise<Rubric | undefined> { | ||
return this.getAsync<Rubric>(rubricsStoreName, name); | ||
} | ||
|
||
public saveRubric(rubric: Rubric): Promise<void> { | ||
return this.setAsync(rubricsStoreName, rubric); | ||
} | ||
|
||
public deleteRubric(name: string): Promise<void> { | ||
return this.deleteAsync(rubricsStoreName, name); | ||
} | ||
} | ||
|
||
const getDb = (async () => { | ||
const db = new TeacherToolDb(); | ||
await db.initializeAsync(); | ||
return db; | ||
})(); | ||
|
||
export async function getLastActiveRubricAsync(): Promise<Rubric | undefined> { | ||
const db = await getDb; | ||
|
||
let rubric: Rubric | undefined = undefined; | ||
const name = await db.getLastActiveRubricNameAsync(); | ||
if (name) { | ||
rubric = await db.getRubric(name); | ||
} | ||
|
||
return rubric; | ||
} | ||
|
||
export async function saveRubricAsync(rubric: Rubric) { | ||
const db = await getDb; | ||
await db.saveRubric(rubric); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to await the db init here. |
||
await db.saveLastActiveRubricNameAsync(rubric.name); | ||
} | ||
|
||
export async function deleteRubricAsync(name: string) { | ||
const db = await getDb; | ||
await db.deleteRubric(name); | ||
} |
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.
@srietkerk The changes in this file are responding to your comments in #9835. They're not related to the indexed db change, I actually forgot I'd made them in this branch, but I suppose here they are 😅