Skip to content

Commit

Permalink
Feat DescShardManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Folyd committed Jun 20, 2024
1 parent 4253618 commit f8c725d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
4 changes: 3 additions & 1 deletion content-script-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import storage from "./extension/core/storage.js";
import settings from "./extension/settings.js";
import IndexSetter from "./extension/index-setter.js";
import CrateDocManager from "./extension/crate-manager.js";
import { DescShardManager } from "./extension/search/docs/desc-shard.js";

export {
storage,
settings,
IndexSetter,
CrateDocManager
CrateDocManager,
DescShardManager,
}
2 changes: 1 addition & 1 deletion extension/content-script-bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions extension/index-setter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ export default class IndexSetter {
storage.setItem('index-std-nightly', index);
}

static setDescShards(crate, shards) {
storage.setItem(`index-desc-shards-${crate}`, shards);
}

static setBookIndex(index) {
storage.setItem('index-book', index);
}
Expand Down
4 changes: 2 additions & 2 deletions extension/script/doc-rust-lang-org.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ window.addEventListener("message", function (event) {
if (TARGET === 'stable') {
rse.IndexSetter.setStdStableIndex(searchIndex);
if (descShards) {
rse.IndexSetter.setDescShards("std-stable", descShards);
rse.DescShardManager.setDescShards("std-stable", descShards);
}
} else {
rse.IndexSetter.setStdNightlyIndex(searchIndex);
if (descShards) {
rse.IndexSetter.setDescShards("std-nightly", descShards);
rse.DescShardManager.setDescShards("std-nightly", descShards);
}
}
let now = new Date();
Expand Down
17 changes: 14 additions & 3 deletions extension/search/docs/desc-shard.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import CrateDocManager from "../../crate-manager.js";
import IndexManager from "../../index-manager.js";
import storage from "../../core/storage.js";

class DescShardManager {
export class DescShardManager {
constructor() {
// A crate -> desc shard map.
this.descShards = new Map();
this.initDescShards();
}

async initDescShards() {
const stdDescShards = await DescShardManager.getDescShards('std-stable');
this.descShards = new Map(Object.entries(stdDescShards));
for (const crate of Object.keys(await CrateDocManager.getCrates())) {
const descShards = await IndexManager.getDescShards(crate);
const descShards = await DescShardManager.getDescShards(crate);
this.descShards.set(crate, descShards);
}
}
Expand All @@ -24,6 +27,14 @@ class DescShardManager {
}
return crateDescShard[descShard.shard][descIndex];
}

static setDescShards(crate, shards) {
storage.setItem(`desc-shards-${crate}`, shards);
}

static async getDescShards(crate) {
return await storage.getItem(`desc-shards-${crate}`) || {};
}
}

const searchState = new DescShardManager();
Expand Down

0 comments on commit f8c725d

Please sign in to comment.