Skip to content

Commit

Permalink
ConfigController minor internal renaming (#6145)
Browse files Browse the repository at this point in the history
* chore: use unusable<T> generic instead of casting

* chore: rename config controller instance properties with 'latest' prefix

* chore: remove unnecessary unwrapHook usage
  • Loading branch information
RamIdeas authored Jun 25, 2024
1 parent 6b09f65 commit abb21a9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe("Core", () => {

const config: StartDevWorkerOptions = {
name: "worker",
entrypoint: unusable() as FilePath,
entrypoint: unusable<FilePath>(),
compatibilityFlags: ["nodejs_compat"],
compatibilityDate: "2023-10-01",
};
Expand Down Expand Up @@ -271,7 +271,7 @@ describe("Core", () => {

const config: StartDevWorkerOptions = {
name: "worker",
entrypoint: unusable() as FilePath,
entrypoint: unusable<FilePath>(),
};
const bundle: Bundle = {
type: "commonjs",
Expand Down Expand Up @@ -358,7 +358,7 @@ describe("Core", () => {
function update(version: number) {
const config: StartDevWorkerOptions = {
name: "worker",
entrypoint: unusable() as FilePath,
entrypoint: unusable<FilePath>(),
bindings: {
VERSION: { type: "json", value: version },
},
Expand Down Expand Up @@ -406,7 +406,7 @@ describe("Core", () => {

const config: StartDevWorkerOptions = {
name: "worker",
entrypoint: unusable() as FilePath,
entrypoint: unusable<FilePath>(),
compatibilityDate: disabledDate,
};
const bundle = makeEsbuildBundle(dedent/*javascript*/ `
Expand Down Expand Up @@ -444,7 +444,7 @@ describe("Core", () => {

const config: StartDevWorkerOptions = {
name: "worker",
entrypoint: unusable() as FilePath,
entrypoint: unusable<FilePath>(),
};
const bundle = makeEsbuildBundle(dedent/*javascript*/ `
export default {
Expand Down Expand Up @@ -503,7 +503,7 @@ describe("Bindings", () => {

const config: StartDevWorkerOptions = {
name: "worker",
entrypoint: unusable() as FilePath,
entrypoint: unusable<FilePath>(),
bindings: {
TEXT: { type: "plain_text", value: "text" },
OBJECT: { type: "json", value: { a: { b: 1 } } },
Expand Down Expand Up @@ -543,7 +543,7 @@ describe("Bindings", () => {

const config: StartDevWorkerOptions = {
name: "worker",
entrypoint: unusable() as FilePath,
entrypoint: unusable<FilePath>(),
bindings: {
// `wasm-module` bindings aren't allowed in modules workers
WASM: { type: "wasm_module", source: { contents: WASM_ADD_MODULE } },
Expand Down Expand Up @@ -573,7 +573,7 @@ describe("Bindings", () => {

const config: StartDevWorkerOptions = {
name: "worker",
entrypoint: unusable() as FilePath,
entrypoint: unusable<FilePath>(),
dev: { persist: { path: persist } },
};
const bundle = makeEsbuildBundle(`export default {
Expand Down Expand Up @@ -621,7 +621,7 @@ describe("Bindings", () => {

const config: StartDevWorkerOptions = {
name: "worker",
entrypoint: unusable() as FilePath,
entrypoint: unusable<FilePath>(),
bindings: { NAMESPACE: { type: "kv_namespace", id: "ns" } },
dev: { persist: { path: persist } },
};
Expand Down Expand Up @@ -667,7 +667,7 @@ describe("Bindings", () => {

let config: StartDevWorkerOptions = {
name: "worker",
entrypoint: unusable() as FilePath,
entrypoint: unusable<FilePath>(),
legacy: { site: { bucket: tmp, include: ["*.txt"] } },
};
const bundle = makeEsbuildBundle(`
Expand Down Expand Up @@ -722,7 +722,7 @@ describe("Bindings", () => {

const config: StartDevWorkerOptions = {
name: "worker",
entrypoint: unusable() as FilePath,
entrypoint: unusable<FilePath>(),
bindings: { BUCKET: { type: "r2_bucket", bucket_name: "bucket" } },
dev: { persist: { path: persist } },
};
Expand Down Expand Up @@ -766,7 +766,7 @@ describe("Bindings", () => {

const config: StartDevWorkerOptions = {
name: "worker",
entrypoint: unusable() as FilePath,
entrypoint: unusable<FilePath>(),
bindings: {
DB: { type: "d1", database_name: "db-name", database_id: "db" },
},
Expand Down Expand Up @@ -816,7 +816,7 @@ describe("Bindings", () => {
const reportPromise = new DeferredPromise<unknown>();
const config: StartDevWorkerOptions = {
name: "worker",
entrypoint: unusable() as FilePath,
entrypoint: unusable<FilePath>(),
bindings: {
QUEUE: { type: "queue", queue_name: "queue" },
BATCH_REPORT: {
Expand Down Expand Up @@ -872,7 +872,7 @@ describe("Bindings", () => {
const localConnectionString = `postgres://username:password@127.0.0.1:${port}/db`;
const config: StartDevWorkerOptions = {
name: "worker",
entrypoint: unusable() as FilePath,
entrypoint: unusable<FilePath>(),
bindings: { DB: { type: "hyperdrive", id: "db", localConnectionString } },
};
const bundle = makeEsbuildBundle(`export default {
Expand Down
19 changes: 8 additions & 11 deletions packages/wrangler/src/api/startDevWorker/ConfigController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import assert from "node:assert";
import { Controller } from "./BaseController";
import { unwrapHook } from "./utils";
import type { ControllerEventMap } from "./BaseController";
import type { ConfigUpdateEvent } from "./events";
import type { StartDevWorkerOptions } from "./types";
Expand All @@ -11,32 +10,30 @@ export type ConfigControllerEventMap = ControllerEventMap & {

type Options = StartDevWorkerOptions;
export class ConfigController extends Controller<ConfigControllerEventMap> {
config?: Options;
latestInput?: Options;
latestConfig?: Options;

public set(input: Options) {
const config = unwrapHook(input, this.latest);

this.#updateConfig(config);
this.#updateConfig(input);
}
public patch(input: Partial<Options>) {
assert(
this.latest,
this.latestInput,
"Cannot call updateConfig without previously calling setConfig"
);

const config: Options = {
...this.latest,
...this.latestInput,
...input,
};

this.#updateConfig(config);
}

latest?: Options;
#updateConfig(input: Options) {
const directory = input.directory;

this.config = {
this.latestConfig = {
directory,
build: {
moduleRules: [],
Expand All @@ -48,8 +45,8 @@ export class ConfigController extends Controller<ConfigControllerEventMap> {
},
...input,
};
this.latest = input;
this.emitConfigUpdateEvent(this.config);
this.latestInput = input;
this.emitConfigUpdateEvent(this.latestConfig);
}

// ******************
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/api/startDevWorker/DevEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export function createWorkerObject(devEnv: DevEnv): DevWorker {
return devEnv.proxy.ready.promise.then(() => undefined);
},
get config() {
return devEnv.config.config;
return devEnv.config.latestConfig;
},
setConfig(config) {
return devEnv.config.set(config);
Expand Down

0 comments on commit abb21a9

Please sign in to comment.