Skip to content

Commit

Permalink
remove LevelContext types
Browse files Browse the repository at this point in the history
  • Loading branch information
KermanX committed Jan 18, 2024
1 parent 123b134 commit ec8e7b0
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 52 deletions.
15 changes: 1 addition & 14 deletions packages/core/src/component/component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { App, RefTreeNode } from "../app";
import {
Context,
ContextState,
InitialContextState,
LowlevelContext,
} from "../context";
import { Context, ContextState, InitialContextState } from "../context";
import { DOMElementComponent } from "../dom";

/**
Expand Down Expand Up @@ -139,14 +134,6 @@ export type ComponentContext<
CS extends ContextState = InitialContextState,
> = Context<CS> & ComponentOnlyContextFuncs<N>;

/**
* The full component context type, with context funcs and lowlevel APIs.
*/
export type LowlevelComponentContext<
N extends keyof Components,
CS extends ContextState = InitialContextState,
> = LowlevelContext<CS> & ComponentOnlyContextFuncs<N>;

export interface ComponentRefTypeRawMap {}

type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (
Expand Down
30 changes: 11 additions & 19 deletions packages/core/src/context/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ComponentMainFunc,
} from "../component";
import { AppState } from "../constants";
import { Model, Ref } from "../data";
import { Ref } from "../data";
import type { RecvContext } from "./recv";
import type { UpdateContext } from "./update";

Expand Down Expand Up @@ -97,7 +97,10 @@ export interface InitialContextState extends ContextState {
* }
* ```
*/
export interface ContextFuncs<C extends ContextState> {}
export interface ContextFuncs<C extends ContextState> {
<V>(callee: ContextDirectCallee<V>): V;
<K extends keyof this>(name: K): this[K];
}

/**
* Get the real context function type from the transformed context function type.
Expand All @@ -108,7 +111,7 @@ export interface ContextFuncs<C extends ContextState> {}
*/
export type ToRealContextFunc<
N extends keyof ContextFuncs<any>,
Ctx = LowlevelContext,
Ctx = IntrinsicBaseContext,
> = N extends `$${string}`
? never
: ContextFuncs<any>[N] extends (...args: infer Args) => infer RetVal
Expand Down Expand Up @@ -178,7 +181,7 @@ export interface IntrinsicBaseContext<
/**
* The lowlevel context.
*/
$lowlevel: LowlevelContext;
$lowlevel: IntrinsicBaseContext;

/**
* If the context is in `UPDATE` state, it is the update context.
Expand Down Expand Up @@ -470,25 +473,13 @@ export type ContextDirectCallee<V> = (
ckey: string,
) => V;

interface ContextDirectCall {
<V>(callee: ContextDirectCallee<V>): V;
<K extends keyof this>(name: K): this[K];
}

/**
* The full context type, with context funcs.
*/
export type Context<CS extends ContextState = InitialContextState> = Readonly<
Omit<IntrinsicBaseContext<CS>, `$$${string}`>
> &
ContextFuncs<CS> &
ContextDirectCall;

/**
* The full context type, with context funcs and lowlevel APIs.
*/
export type LowlevelContext<CS extends ContextState = InitialContextState> =
IntrinsicBaseContext<CS> & ContextFuncs<CS>;
ContextFuncs<CS>;

/**
* Initialize a context.
Expand All @@ -499,7 +490,7 @@ export function initializeBaseContext(context: IntrinsicBaseContext, app: App) {
context._ = context as unknown as Context;
context.$app = app;
context.$appState = app.state;
context.$lowlevel = context as unknown as LowlevelContext;
context.$lowlevel = context;
context.$update = app.update;
context.$updateModel = app.updateModel;
context.$permanentData = app.permanentData;
Expand Down Expand Up @@ -534,7 +525,8 @@ export function initializeBaseContext(context: IntrinsicBaseContext, app: App) {
context.$$d = (
ckey: string,
nameOrCallee:
| keyof LowlevelContext
| keyof IntrinsicBaseContext
| keyof ContextFuncs<InitialContextState>
| ((this: IntrinsicBaseContext, ckey: string) => unknown),
) => {
if (typeof nameOrCallee === "function") {
Expand Down
8 changes: 1 addition & 7 deletions packages/core/src/context/recv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
export interface IntrinsicRecvContext<
CS extends ContextState = InitialContextState,
> extends IntrinsicBaseContext<CS> {
$lowlevel: LowlevelRecvContext;
$lowlevel: IntrinsicRecvContext;

/**
* The receiver of the event.
Expand All @@ -45,12 +45,6 @@ export interface IntrinsicRecvContext<
export type RecvContext<CS extends ContextState = InitialContextState> =
Readonly<Omit<IntrinsicRecvContext<CS>, `$$${string}`>> & ContextFuncs<CS>;

/**
* The full context type in `RECV` state, with context funcs and lowlevel APIs.
*/
export type LowlevelRecvContext<CS extends ContextState = InitialContextState> =
IntrinsicRecvContext<CS> & ContextFuncs<CS>;

/**
* Initialize a context in `RECV` state.
* @param context The context to initialize.
Expand Down
9 changes: 1 addition & 8 deletions packages/core/src/context/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
export interface IntrinsicUpdateContext<
CS extends ContextState = InitialContextState,
> extends IntrinsicBaseContext<CS> {
$lowlevel: LowlevelUpdateContext;
$lowlevel: IntrinsicUpdateContext;

/**
* The current parent DOM element.
Expand Down Expand Up @@ -149,13 +149,6 @@ export interface IntrinsicUpdateContext<
export type UpdateContext<CS extends ContextState = InitialContextState> =
Readonly<Omit<IntrinsicUpdateContext<CS>, `$$${string}`>> & ContextFuncs<CS>;

/**
* The full context type in `UPDATE` state, with context funcs and lowlevel APIs.
*/
export type LowlevelUpdateContext<
CS extends ContextState = InitialContextState,
> = IntrinsicUpdateContext<CS> & ContextFuncs<CS>;

/**
* Intialize the context in `UPDATE` state.
* @param context The context to initialize.
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/loop.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RefTreeNode } from "../app";
import { Prelude } from "../constants";
import { LowlevelContext } from "../context";
import type { IntrinsicBaseContext } from "../context";

type AvaliableKeyImpl<T, K extends keyof T> = K extends any
? T[K] extends string | number
Expand Down Expand Up @@ -35,7 +35,7 @@ type LoopRefTreeNodeMap = Record<string, RefTreeNode>;

Prelude.registerFunc("for", function <
T,
>(this: LowlevelContext, ckey: string, iterable: Iterable<T>, key: LoopKey<T>, body: (item: T, index: number) => void) {
>(this: IntrinsicBaseContext, ckey: string, iterable: Iterable<T>, key: LoopKey<T>, body: (item: T, index: number) => void) {
this.$$currentRefTreeNode[ckey] ??= {};
const refTreeNodes = this.$$currentRefTreeNode[ckey] as LoopRefTreeNodeMap;
const parentRefTreeNode = this.$$currentRefTreeNode;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/useModel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Prelude } from "../constants";
import { LowlevelContext } from "../context";
import type { IntrinsicBaseContext } from "../context";
import { JustModel, model } from "../data";

declare module "../context/base" {
Expand All @@ -15,7 +15,7 @@ declare module "../context/base" {

Prelude.registerFunc("useModel", function <
T,
>(this: LowlevelContext, ckey: string, init: T) {
>(this: IntrinsicBaseContext, ckey: string, init: T) {
const refTreeNode = this.$$currentRefTreeNode;
if (!refTreeNode[ckey]) {
refTreeNode[ckey] = model(init);
Expand Down

1 comment on commit ec8e7b0

@vercel
Copy link

@vercel vercel bot commented on ec8e7b0 Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deployment failed with the following error:

Resource is limited - try again in 55 minutes (more than 100, code: "api-deployments-free-per-day").

Please sign in to comment.