Skip to content

Commit

Permalink
perf: improve load time
Browse files Browse the repository at this point in the history
  • Loading branch information
Delusoire committed Jul 8, 2024
1 parent 120fecb commit 061572a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
6 changes: 6 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import {
enableAllLoadableMixins,
INTERNAL_MIXIN_LOADER,
INTERNAL_TRANSFORMER,
loadLocalModules,
loadRemoteModules,
} from "./module.js";

await loadLocalModules();

await mixin(INTERNAL_TRANSFORMER);
console.time("onSpotifyPreInit");
await enableAllLoadableMixins();
Expand All @@ -29,3 +33,5 @@ console.time("onSpotifyPostInit");
await Promise.all(INTERNAL_MIXIN_LOADER.awaitedMixins);
await enableAllLoadable();
console.timeEnd("onSpotifyPostInit");

requestIdleCallback(loadRemoteModules);
63 changes: 33 additions & 30 deletions module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@ export interface Metadata {
dependencies: Record<string, string>;
}

const getLoadableChildrenInstances = () =>
RootModule.INSTANCE.getChildren().map((module) => module.getEnabledInstance()).filter(
(instance) => instance?.canLoad(),
) as ModuleInstance[];

export const enableAllLoadableMixins = () =>
Promise.all(getLoadableChildrenInstances().map((instance) => instance._loadMixins()));

export const enableAllLoadable = () =>
Promise.all(getLoadableChildrenInstances().map((instance) => instance.load()));

export abstract class ModuleBase<
C extends ModuleBase<any>,
I extends ModuleInstanceBase<ModuleBase<C, I>> = ModuleInstanceBase<ModuleBase<C, any>>,
Expand Down Expand Up @@ -353,7 +342,7 @@ export class ModuleInstance extends ModuleInstanceBase<Module> implements MixinL
if (!this.installed) {
return null;
}
return `/modules/${this.getModuleIdentifier()}/metadata.json`;
return `/store/${this.getModuleIdentifier()}/${this.getVersion()}/metadata.json`;
}

public getMetadataURL() {
Expand Down Expand Up @@ -746,25 +735,39 @@ export const INTERNAL_MIXIN_LOADER: MixinLoader = {

export const INTERNAL_TRANSFORMER = createTransformer(INTERNAL_MIXIN_LOADER);

const localModules = [
await fetchJson<_Vault>("/modules/vault.json"),
await fetchJson<_Vault>("https://raw.githubusercontent.com/spicetify/modules/main/vault.json"),
].reduceRight<_Vault["modules"]>((acc, vault) => deepMerge(acc, vault.modules), {});

const remoteModules = [
await fetchJson<_Vault>("https://raw.githubusercontent.com/spicetify/pkgs/main/vault.json"),
].reduceRight<_Vault["modules"]>((acc, vault) => deepMerge(acc, vault.modules), {});

await Promise.all(
Object.keys(localModules).map((identifier) =>
RootModule.INSTANCE.newChild(identifier, localModules[identifier], true)
),
);
requestIdleCallback(() =>
Promise.all(
export async function loadLocalModules() {
const localModules = [
await fetchJson<_Vault>("/modules/vault.json"),
].reduceRight<_Vault["modules"]>((acc, vault) => deepMerge(acc, vault.modules), {});

return Promise.all(
Object.keys(localModules).map((identifier) =>
RootModule.INSTANCE.newChild(identifier, localModules[identifier], true)
),
);
}

export async function loadRemoteModules() {
const remoteModules = [
await fetchJson<_Vault>("https://raw.githubusercontent.com/spicetify/modules/main/vault.json"),
await fetchJson<_Vault>("https://raw.githubusercontent.com/spicetify/pkgs/main/vault.json"),
].reduceRight<_Vault["modules"]>((acc, vault) => deepMerge(acc, vault.modules), {});

await Promise.all(
Object.keys(remoteModules).map(async (identifier) => {
const module = await RootModule.INSTANCE.getChildOrNew(identifier);
await module.init(remoteModules[identifier].v, false);
}),
)
);
);
}

const getLoadableChildrenInstances = () =>
RootModule.INSTANCE.getChildren().map((module) => module.getEnabledInstance()).filter(
(instance) => instance?.canLoad(),
) as ModuleInstance[];

export const enableAllLoadableMixins = () =>
Promise.all(getLoadableChildrenInstances().map((instance) => instance._loadMixins()));

export const enableAllLoadable = () =>
Promise.all(getLoadableChildrenInstances().map((instance) => instance.load()));

0 comments on commit 061572a

Please sign in to comment.