Skip to content
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

[Stable9.2] Cherry pick all of the pouch DB fixes to minecraft stable #9824

Merged
merged 4 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion pxtlib/browserutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,8 @@ namespace pxt.BrowserUtils {
private name: string,
private version: number,
private upgradeHandler?: IDBUpgradeHandler,
private quotaExceededHandler?: () => void) {
private quotaExceededHandler?: () => void,
private skipErrorLog = false) {
}

private throwIfNotOpened(): void {
Expand All @@ -845,6 +846,10 @@ namespace pxt.BrowserUtils {
}

private errorHandler(err: Error, op: string, reject: (err: Error) => void): void {
if (this.skipErrorLog) {
reject(err);
return;
}
console.error(new Error(`${this.name} IDBWrapper error for ${op}: ${err.message}`));
reject(err);
// special case for quota exceeded
Expand Down Expand Up @@ -945,6 +950,34 @@ namespace pxt.BrowserUtils {
request.onerror = () => this.errorHandler(request.error, "deleteAll", reject);
});
}

public getObjectStoreWrapper<T>(storeName: string): IDBObjectStoreWrapper<T> {
return new IDBObjectStoreWrapper(this, storeName);
}
}

export class IDBObjectStoreWrapper<T> {
constructor(protected db: IDBWrapper, protected storeName: string) {}

public getAsync(id: string): Promise<T> {
return this.db.getAsync(this.storeName, id);
}

public getAllAsync(): Promise<T[]> {
return this.db.getAllAsync(this.storeName);
}

public setAsync(data: T): Promise<void> {
return this.db.setAsync(this.storeName, data);
}

public async deleteAsync(id: string): Promise<void> {
await this.db.deleteAsync(this.storeName, id);
}

public async deleteAllAsync(): Promise<void> {
await this.db.deleteAllAsync(this.storeName);
}
}

class IndexedDbTranslationDb implements ITranslationDb {
Expand Down
3 changes: 3 additions & 0 deletions webapp/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import { CodeCardView } from "./codecard";
import { mergeProjectCode, appendTemporaryAssets } from "./mergeProjects";
import { Tour } from "./components/onboarding/Tour";
import { parseTourStepsAsync } from "./onboarding";
import { initGitHubDb } from "./idbworkspace";

pxsim.util.injectPolyphils();

Expand Down Expand Up @@ -5735,6 +5736,8 @@ document.addEventListener("DOMContentLoaded", async () => {
pxt.analytics.consoleTicks = pxt.analytics.ConsoleTickOptions.Short;
}

initGitHubDb();

pxt.perf.measureStart("setAppTarget");
pkg.setupAppTarget((window as any).pxtTargetBundle);

Expand Down
11 changes: 2 additions & 9 deletions webapp/src/browserworkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,13 @@ function setCoreAsync(headers: db.Table, texts: db.Table, h: Header, prevVer: an
return headerRes
}

export function copyProjectToLegacyEditor(h: Header, majorVersion: number): Promise<Header> {
export async function copyProjectToLegacyEditor(header: Header, script: pxt.workspace.ScriptText, majorVersion: number): Promise<void> {
const prefix = pxt.appTarget.appTheme.browserDbPrefixes && pxt.appTarget.appTheme.browserDbPrefixes[majorVersion];

const oldHeaders = new db.Table(prefix ? `${prefix}-header` : `header`);
const oldTexts = new db.Table(prefix ? `${prefix}-text` : `text`);

const header = pxt.Util.clone(h);
delete (header as any)._id;
delete header._rev;
header.id = pxt.Util.guidGen();

return getAsync(h)
.then(resp => setCoreAsync(oldHeaders, oldTexts, header, undefined, resp.text))
.then(rev => header);
await setCoreAsync(oldHeaders, oldTexts, header, undefined, script);
}

function deleteAsync(h: Header, prevVer: any) {
Expand Down
65 changes: 1 addition & 64 deletions webapp/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,67 +88,4 @@ export class Table {
obj._id = this.name + "--" + obj.id
return getDbAsync().then(db => db.put(obj)).then((resp: any) => resp.rev)
}
}

class GithubDb implements pxt.github.IGithubDb {
// in memory cache
private mem = new pxt.github.MemoryGithubDb();
private table = new Table("github");

latestVersionAsync(repopath: string, config: pxt.PackagesConfig): Promise<string> {
return this.mem.latestVersionAsync(repopath, config)
}

loadConfigAsync(repopath: string, tag: string): Promise<pxt.PackageConfig> {
// don't cache master
if (tag == "master")
return this.mem.loadConfigAsync(repopath, tag);

const id = `config-${repopath}-${tag}`;
return this.table.getAsync(id).then(
entry => {
pxt.debug(`github offline cache hit ${id}`);
return entry.config as pxt.PackageConfig;
},
e => {
pxt.debug(`github offline cache miss ${id}`);
return this.mem.loadConfigAsync(repopath, tag)
.then(config => {
return this.table.forceSetAsync({
id,
config
}).then(() => config, e => config);
})
} // not found
);
}
loadPackageAsync(repopath: string, tag: string): Promise<pxt.github.CachedPackage> {
if (!tag) {
pxt.debug(`dep: default to master`)
tag = "master"
}
// don't cache master
if (tag == "master")
return this.mem.loadPackageAsync(repopath, tag);

const id = `pkg-${repopath}-${tag}`;
return this.table.getAsync(id).then(
entry => {
pxt.debug(`github offline cache hit ${id}`);
return entry.package as pxt.github.CachedPackage;
},
e => {
pxt.debug(`github offline cache miss ${id}`);
return this.mem.loadPackageAsync(repopath, tag)
.then(p => {
return this.table.forceSetAsync({
id,
package: p
}).then(() => p, e => p);
})
} // not found
);
}
}

pxt.github.db = new GithubDb();
}
Loading