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 c1f8f8e commit 8caa426
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 158 deletions.
6 changes: 0 additions & 6 deletions packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1166,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 @@ -1191,9 +1188,6 @@ class Compiler {
)
: false;
const result = await queried.promise(data);
if (data) {
ContextModuleFactoryAfterResolveData.__drop(data);
}
return result
? ContextModuleFactoryAfterResolveData.__to_binding(result)
: false;
Expand Down
281 changes: 129 additions & 152 deletions packages/rspack/src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export type ResolveData = {

export class ContextModuleFactoryBeforeResolveData {
#inner: JsContextModuleFactoryBeforeResolveData;
#dropped = false;

declare context: string;
declare request: string;
declare regExp: RegExp | undefined;
declare recursive: boolean;

static __from_binding(binding: JsContextModuleFactoryBeforeResolveData) {
return new ContextModuleFactoryBeforeResolveData(binding);
Expand All @@ -51,70 +55,58 @@ export class ContextModuleFactoryBeforeResolveData {
return data.#inner;
}

static __drop(data: ContextModuleFactoryBeforeResolveData) {
data.#dropped = true;
}

private constructor(binding: JsContextModuleFactoryBeforeResolveData) {
this.#inner = binding;
}

private ensureValidLifecycle() {
if (this.#dropped) {
throw new Error(
"The ContextModuleFactoryBeforeResolveData has exceeded its lifecycle and has been dropped by Rust."
);
}
}

get context(): string {
this.ensureValidLifecycle();
return this.#inner.context;
}

set context(val: string) {
this.ensureValidLifecycle();
this.#inner.context = val;
}

get request(): string {
this.ensureValidLifecycle();
return this.#inner.request;
}

set request(val: string) {
this.#inner.request = val;
}

get regExp(): RegExp | undefined {
this.ensureValidLifecycle();
if (!this.#inner.regExp) {
return undefined;
}
const { source, flags } = this.#inner.regExp;
return new RegExp(source, flags);
}

set regExp(val: RegExp | undefined) {
this.ensureValidLifecycle();
if (!val) {
this.#inner.regExp = undefined;
return;
}
this.#inner.regExp = {
source: val.source,
flags: val.flags
};
}

get recursive(): boolean {
this.ensureValidLifecycle();
return this.#inner.recursive;
}

set recursive(val: boolean) {
this.ensureValidLifecycle();
this.#inner.recursive = val;
Object.defineProperties(this, {
context: {
enumerable: true,
get(): string {
return binding.context;
},
set(val: string) {
binding.context = val;
}
},
request: {
enumerable: true,
get(): string {
return binding.request;
},
set(val: string) {
binding.request = val;
}
},
regExp: {
enumerable: true,
get(): RegExp | undefined {
if (!binding.regExp) {
return undefined;
}
const { source, flags } = binding.regExp;
return new RegExp(source, flags);
},
set(val: RegExp | undefined) {
if (!val) {
binding.regExp = undefined;
return;
}
binding.regExp = {
source: val.source,
flags: val.flags
};
}
},
recursive: {
enumerable: true,
get(this: ContextModuleFactoryAfterResolveData): boolean {
return binding.recursive;
},
set(val: boolean) {
binding.recursive = val;
}
}
});
}
}

Expand All @@ -124,8 +116,13 @@ export type ContextModuleFactoryBeforeResolveResult =

export class ContextModuleFactoryAfterResolveData {
#inner: JsContextModuleFactoryAfterResolveData;
#resolvedDependencies?: Dependency[];
#dropped = false;

declare resource: number;
declare context: string;
declare request: string;
declare regExp: RegExp | undefined;
declare recursive: boolean;
declare readonly dependencies: Dependency[];

static __from_binding(binding: JsContextModuleFactoryAfterResolveData) {
return new ContextModuleFactoryAfterResolveData(binding);
Expand All @@ -137,95 +134,75 @@ export class ContextModuleFactoryAfterResolveData {
return data.#inner;
}

static __drop(data: ContextModuleFactoryAfterResolveData) {
data.#dropped = true;
if (data.#resolvedDependencies) {
for (const dependency of data.#resolvedDependencies) {
Dependency.__drop(dependency);
}
}
}

private ensureValidLifecycle() {
if (this.#dropped) {
throw new Error(
"The ContextModuleFactoryAfterResolveData has exceeded its lifecycle and has been dropped by Rust."
);
}
}

private constructor(data: JsContextModuleFactoryAfterResolveData) {
this.#inner = data;
}

get resource(): string {
this.ensureValidLifecycle();
return this.#inner.resource;
}

set resource(val: string) {
this.ensureValidLifecycle();
this.#inner.resource = val;
}

get context(): string {
this.ensureValidLifecycle();
return this.#inner.context;
}

set context(val: string) {
this.ensureValidLifecycle();
this.#inner.context = val;
}

get request(): string {
this.ensureValidLifecycle();
return this.#inner.request;
}

set request(val: string) {
this.#inner.request = val;
}

get regExp(): RegExp | undefined {
this.ensureValidLifecycle();
if (!this.#inner.regExp) {
return undefined;
}
const { source, flags } = this.#inner.regExp;
return new RegExp(source, flags);
}

set regExp(val: RegExp | undefined) {
this.ensureValidLifecycle();
if (!val) {
this.#inner.regExp = undefined;
return;
}
this.#inner.regExp = {
source: val.source,
flags: val.flags
};
}

get recursive(): boolean {
this.ensureValidLifecycle();
return this.#inner.recursive;
}

set recursive(val: boolean) {
this.ensureValidLifecycle();
this.#inner.recursive = val;
}
private constructor(binding: JsContextModuleFactoryAfterResolveData) {
this.#inner = binding;

get dependencies(): Dependency[] {
this.ensureValidLifecycle();
if (!this.#resolvedDependencies) {
this.#resolvedDependencies = this.#inner.dependencies.map(dep =>
Dependency.__from_binding(dep)
);
}
return this.#resolvedDependencies;
Object.defineProperties(this, {
resource: {
enumerable: true,
get(): string {
return binding.resource;
},
set(val: string) {
binding.resource = val;
}
},
context: {
enumerable: true,
get(): string {
return binding.context;
},
set(val: string) {
binding.context = val;
}
},
request: {
enumerable: true,
get(): string {
return binding.request;
},
set(val: string) {
binding.request = val;
}
},
regExp: {
enumerable: true,
get(): RegExp | undefined {
if (!binding.regExp) {
return undefined;
}
const { source, flags } = binding.regExp;
return new RegExp(source, flags);
},
set(val: RegExp | undefined) {
if (!val) {
binding.regExp = undefined;
return;
}
binding.regExp = {
source: val.source,
flags: val.flags
};
}
},
recursive: {
enumerable: true,
get(): boolean {
return binding.recursive;
},
set(val: boolean) {
binding.recursive = val;
}
},
dependencies: {
enumerable: true,
get(): Dependency[] {
return binding.dependencies.map(dep =>
Dependency.__from_binding(dep)
);
}
}
});
}
}

Expand Down

0 comments on commit 8caa426

Please sign in to comment.