Skip to content

Commit

Permalink
fix: remove lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Oct 9, 2024
1 parent b496dc3 commit 65251ef
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 255 deletions.
18 changes: 5 additions & 13 deletions packages/rspack/src/ChunkGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,40 @@ import {
import { Chunk } from "./Chunk";
import type { Compilation } from "./Compilation";
import { Module } from "./Module";
import type { Lifecycle } from "./util/lifecycle";

export class ChunkGraph {
#compilation: Compilation;
#lifecycle: Lifecycle;

constructor(compilation: Compilation, lifecycle: Lifecycle) {
constructor(compilation: Compilation) {
this.#compilation = compilation;
this.#lifecycle = lifecycle;
}

getChunkModules(chunk: Chunk): Readonly<Module[]> {
this.#lifecycle.ensureActive();
return __chunk_graph_inner_get_chunk_modules(
chunk.__internal__innerUkey(),
this.#compilation.__internal_getInner()
).map(m => Module.__from_binding(m, this.#lifecycle, this.#compilation));
).map(m => Module.__from_binding(m, this.#compilation));
}

getChunkModulesIterable(chunk: Chunk): Iterable<Module> {
this.#lifecycle.ensureActive();
return new Set(
__chunk_graph_inner_get_chunk_modules(
chunk.__internal__innerUkey(),
this.#compilation.__internal_getInner()
).map(m => Module.__from_binding(m, this.#lifecycle, this.#compilation))
).map(m => Module.__from_binding(m, this.#compilation))
);
}

getChunkEntryModulesIterable(chunk: Chunk): Iterable<Module> {
this.#lifecycle.ensureActive();
return new Set(
__chunk_graph_inner_get_chunk_entry_modules(
chunk.__internal__innerUkey(),
this.#compilation.__internal_getInner()
).map(m => Module.__from_binding(m, this.#lifecycle, this.#compilation))
).map(m => Module.__from_binding(m, this.#compilation))
);
}

getChunkEntryDependentChunksIterable(chunk: Chunk): Iterable<Chunk> {
this.#lifecycle.ensureActive();
return new Set(
__chunk_graph_inner_get_chunk_entry_dependent_chunks_iterable(
chunk.__internal__innerUkey(),
Expand All @@ -63,13 +56,12 @@ export class ChunkGraph {
chunk: Chunk,
sourceType: string
): Iterable<Module> {
this.#lifecycle.ensureActive();
return new Set(
__chunk_graph_inner_get_chunk_modules_iterable_by_source_type(
chunk.__internal__innerUkey(),
sourceType,
this.#compilation.__internal_getInner()
).map(m => Module.__from_binding(m, this.#lifecycle, this.#compilation))
).map(m => Module.__from_binding(m, this.#compilation))
);
}
}
6 changes: 1 addition & 5 deletions packages/rspack/src/Compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import { createReadonlyMap } from "./util/createReadonlyMap";
import { createFakeCompilationDependencies } from "./util/fake";
import type { InputFileSystem } from "./util/fs";
import type Hash from "./util/hash";
import type { Lifecycle } from "./util/lifecycle";
import { memoizeValue } from "./util/memoize";
import { JsSource } from "./util/source";
export { type AssetInfo } from "./util/AssetInfo";
Expand Down Expand Up @@ -253,9 +252,7 @@ export class Compilation {
}
>;

#lifecycle: Lifecycle;

constructor(compiler: Compiler, inner: JsCompilation, lifecycle: Lifecycle) {
constructor(compiler: Compiler, inner: JsCompilation) {
this.#inner = inner;
this.#customModules = {};

Expand Down Expand Up @@ -368,7 +365,6 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
this.childrenCounters = {};
this.children = [];
this.chunkGraph = new ChunkGraph(this);
this.#lifecycle = lifecycle;
}

get hash(): Readonly<string | null> {
Expand Down
24 changes: 2 additions & 22 deletions packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ import type {
OutputFileSystem,
WatchFileSystem
} from "./util/fs";
import { Lifecycle } from "./util/lifecycle";

export interface AssetEmittedInfo {
content: Buffer;
Expand All @@ -86,7 +85,6 @@ class Compiler {
#instance?: binding.Rspack;
#initial: boolean;

#compilationLifecycle?: Lifecycle;
#compilation?: Compilation;
#compilationParams?: CompilationParams;

Expand Down Expand Up @@ -708,23 +706,13 @@ class Compiler {
}

#createCompilation(native: binding.JsCompilation): Compilation {
this.#compilationLifecycle = new Lifecycle(
"The associated Compilation has exceeded the lifetime of its internal Rust instance, making this object inaccessible"
);
const compilation = new Compilation(
this,
native,
this.#compilationLifecycle
);
const compilation = new Compilation(this, native);
compilation.name = this.name;
this.#compilation = compilation;
return compilation;
}

#resetThisCompilation() {
if (this.#compilationLifecycle) {
this.#compilationLifecycle.drop();
}
// reassign new compilation in thisCompilation
this.#compilation = undefined;
// ensure thisCompilation must call
Expand Down Expand Up @@ -1178,9 +1166,6 @@ class Compiler {
)
: false;
const result = await queried.promise(data);
if (data) {
ContextModuleFactoryBeforeResolveData.__drop(data);
}
return result
? ContextModuleFactoryBeforeResolveData.__to_binding(result)
: false;
Expand All @@ -1197,17 +1182,12 @@ class Compiler {
| false
| binding.JsContextModuleFactoryAfterResolveData
) => {
const lifecycle = new Lifecycle(
"The current object can only be used during the ContextModuleFactory afterResolve phase and is inaccessible beyond this lifecycle"
);
const data = bindingData
? ContextModuleFactoryAfterResolveData.__from_binding(
bindingData,
lifecycle
bindingData
)
: false;
const result = await queried.promise(data);
lifecycle.drop();
return result
? ContextModuleFactoryAfterResolveData.__to_binding(result)
: false;
Expand Down
15 changes: 3 additions & 12 deletions packages/rspack/src/DependenciesBlock.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import type { DependenciesBlockDTO } from "@rspack/binding";
import { Dependency } from "./Dependency";
import type { Lifecycle } from "./util/lifecycle";

export class DependenciesBlock {
#binding: DependenciesBlockDTO;
#lifecycle: Lifecycle;

constructor(binding: DependenciesBlockDTO, lifecycle: Lifecycle) {
constructor(binding: DependenciesBlockDTO) {
this.#binding = binding;
this.#lifecycle = lifecycle;
}

get dependencies(): Dependency[] {
this.#lifecycle.ensureActive();
return this.#binding.dependencies.map(d =>
Dependency.__from_binding(d, this.#lifecycle)
);
return this.#binding.dependencies.map(d => Dependency.__from_binding(d));
}

get blocks(): DependenciesBlock[] {
this.#lifecycle.ensureActive();
return this.#binding.blocks.map(
b => new DependenciesBlock(b, this.#lifecycle)
);
return this.#binding.blocks.map(b => new DependenciesBlock(b));
}
}
20 changes: 3 additions & 17 deletions packages/rspack/src/Dependency.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,33 @@
import { type JsDependency, JsDependencyMut } from "@rspack/binding";
import type { Lifecycle } from "./util/lifecycle";

export class Dependency {
#binding: JsDependencyMut | JsDependency;
#lifecycle: Lifecycle;

static __from_binding(
binding: JsDependencyMut | JsDependency,
lifecycle: Lifecycle
): Dependency {
return new Dependency(binding, lifecycle);
static __from_binding(binding: JsDependencyMut | JsDependency): Dependency {
return new Dependency(binding);
}

private constructor(
binding: JsDependencyMut | JsDependency,
lifecycle: Lifecycle
) {
private constructor(binding: JsDependencyMut | JsDependency) {
this.#binding = binding;
this.#lifecycle = lifecycle;
}

get type(): string {
this.#lifecycle.ensureActive();
return this.#binding.type;
}

get category(): string {
this.#lifecycle.ensureActive();
return this.#binding.category;
}

get request(): string | undefined {
this.#lifecycle.ensureActive();
return this.#binding.request;
}

get critital(): boolean {
this.#lifecycle.ensureActive();
return this.#binding.critical;
}

set critital(critital: boolean) {
this.#lifecycle.ensureActive();
if (
typeof critital === "boolean" &&
this.#binding instanceof JsDependencyMut
Expand Down
Loading

0 comments on commit 65251ef

Please sign in to comment.