Skip to content

Commit

Permalink
feat: make "test" a modifier of Environment
Browse files Browse the repository at this point in the history
  • Loading branch information
TylorS committed Mar 3, 2024
1 parent d8ff1e2 commit 6d69d94
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 320 deletions.
7 changes: 7 additions & 0 deletions .changeset/purple-rivers-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@typed/environment": patch
"@typed/template": patch
"@typed/router": patch
---

Make "test" a modifier of the environment rather than an individual environment
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"effect": "^2.4.1",
"eslint": "^8.57.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-codegen": "0.23.0",
"eslint-plugin-codegen": "0.24.1",
"eslint-plugin-deprecation": "^2.0.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-simple-import-sort": "^12.0.0",
Expand Down
53 changes: 38 additions & 15 deletions packages/environment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,45 @@ import type * as Effect from "effect/Effect"
* @since 1.0.0
*/
export type Environment =
| "dom"
| "server"
| "serviceWorker"
| "static"
| "test"
| "webWorker"
| Environment.Value
| `test:${Environment.Value}`

export namespace Environment {
export type Value =
| "dom"
| "server"
| "serviceWorker"
| "static"
| "webWorker"
}

class EnvironmentValueImpl<T extends Environment.Value> extends String {
readonly test: string
constructor(value: T) {
super(value)

this.test = `test:${value}` as `test:${T}`
}
}

export type EnvironmentValue<T extends Environment.Value> = T & {
readonly test: `test:${T}`
}

function EnvironmentValue<const T extends Environment.Value>(value: T): EnvironmentValue<T> {
return new EnvironmentValueImpl(value) as any
}

/**
* @since 1.0.0
*/
export const Environment: { readonly [_ in Environment]: _ } = {
dom: "dom",
server: "server",
serviceWorker: "serviceWorker",
static: "static",
test: "test",
webWorker: "webWorker"
}
export const Environment = {
dom: EnvironmentValue("dom"),
server: EnvironmentValue("server"),
serviceWorker: EnvironmentValue("serviceWorker"),
static: EnvironmentValue("static"),
webWorker: EnvironmentValue("webWorker")
} satisfies { readonly [_ in Environment.Value]: EnvironmentValue<_> }

/**
* @since 1.0.0
Expand Down Expand Up @@ -62,7 +83,9 @@ export const isStatic: Effect.Effect<boolean, never, CurrentEnvironment> = Curre
/**
* @since 1.0.0
*/
export const isTest: Effect.Effect<boolean, never, CurrentEnvironment> = CurrentEnvironment.with((e) => e === "test")
export const isTest: Effect.Effect<boolean, never, CurrentEnvironment> = CurrentEnvironment.with((e) =>
e.startsWith("test:")
)

/**
* @since 1.0.0
Expand Down
2 changes: 1 addition & 1 deletion packages/router/src/Matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class RouteMatcherImpl<A, E, R> implements RouteMatcher<A, E, R> {
let matcher: Match.ValueMatcher<string, A | B, E | E2, R | R2 | Navigation.Navigation | Scope.Scope> = Match
.value(
// Only if we're rendering in a DOM-based environment should we allow for routing to last indefinitely
env === "dom" ? Navigation.CurrentPath : Fx.take(Navigation.CurrentPath, 1)
env === "dom" || env === "test:dom" ? Navigation.CurrentPath : Fx.take(Navigation.CurrentPath, 1)
)

for (const { guard, match, route } of this.guards) {
Expand Down
4 changes: 2 additions & 2 deletions packages/router/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CurrentEnvironment } from "@typed/environment"
import { CurrentEnvironment, Environment } from "@typed/environment"
import * as Fx from "@typed/fx"
import * as Navigation from "@typed/navigation"
import * as Router from "@typed/router"
Expand Down Expand Up @@ -44,5 +44,5 @@ describe("Router", () => {
const resources = (url: string) =>
Navigation.initialMemory({ url }).pipe(
Layer.provideMerge(Router.layer("/")),
Layer.provideMerge(CurrentEnvironment.layer("test"))
Layer.provideMerge(CurrentEnvironment.layer(Environment.dom.test))
)
2 changes: 1 addition & 1 deletion packages/template/src/Html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function renderHtmlTemplate(ctx: RenderContext.RenderContext) {
Placeholder.Error<Values[number]>,
Scope.Scope | Placeholder.Context<readonly [] extends Values ? never : Values[number]>
> => {
const isStatic = ctx.environment === "static"
const isStatic = ctx.environment === "static" || ctx.environment === "test:static"
const entry = getServerEntry(templateStrings, ctx.templateCache, isStatic)

if (values.length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/template/src/Many.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function many<A, E, R, B extends PropertyKey, R2, E2>(
const ctx = get(context, RenderContext)
const hydrateContext = getOption(context, HydrateContext)

if (ctx.environment === "dom") {
if (ctx.environment === "dom" || ctx.environment === "test:dom") {
// If we're hydrating, attempt to provide the correct HydrateContext to rendering Fx
if (isSome(hydrateContext) && hydrateContext.value.hydrate) {
return Fx.keyed(values, {
Expand Down
1 change: 0 additions & 1 deletion packages/template/src/internal/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,6 @@ export const renderTemplate: (document: Document, renderContext: RenderContext)
values: Values
) => {
const entry = getBrowserEntry(document, renderContext, templateStrings)
console.log("entry", ...entry.template.nodes)
if (values.length === 0) {
return Fx.sync(() => DomRenderEvent(persistent(document.importNode(entry.content, true))))
}
Expand Down
Loading

0 comments on commit 6d69d94

Please sign in to comment.