Skip to content

Commit

Permalink
Fix issues with incorrect Root/Child model types
Browse files Browse the repository at this point in the history
  • Loading branch information
craigbeck committed Mar 18, 2024
1 parent 13941e8 commit 8de6f6d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export abstract class AppBase extends EventEmitter {
tracksRoutes: Routes;
model: Model;
page: Page;
protected _pendingComponentMap: Record<string, ComponentConstructor>;
protected _pendingComponentMap: Record<string, ComponentConstructor | SingletonComponentConstructor>;
protected _waitForAttach: boolean;
protected _cancelAttach: boolean;

Expand Down
8 changes: 4 additions & 4 deletions src/Controller.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { EventEmitter } from 'events';

import { type Model } from 'racer';
import { DefualtType, type ChildModel } from 'racer';

import { type AppBase } from './App';
import { type ComponentModelData } from './components';
import { Dom } from './Dom';
import { Page } from './Page';

export class Controller extends EventEmitter {
export class Controller<T = DefualtType> extends EventEmitter {
dom: Dom;
app: AppBase;
page: Page;
model: Model;
model: ChildModel<T>;
markerNode: Node;

constructor(app: AppBase, page: Page, model: Model) {
constructor(app: AppBase, page: Page, model: ChildModel<T>) {
super();
this.dom = new Dom(this);
this.app = app;
Expand Down
27 changes: 11 additions & 16 deletions src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
*/

import { type ChildModel, util } from 'racer';
import { type ChildModel, util, DefualtType } from 'racer';

import { Controller } from './Controller';
import { Page } from './Page';
Expand All @@ -27,30 +27,25 @@ type AnyVoidFunction = (...args: any[]) => void;
export interface ComponentConstructor {
new(context: Context, data: Record<string, unknown>): Component;
DataConstructor?: DataConstructor;
singleton?: true | undefined,
singleton?: undefined,
view?: ComponentViewDefinition,
}

export interface SingletonComponentConstructor {
new(): Component
new(): object;
singleton: true;
view?: {
is: string,
dependencies?: ComponentConstructor[],
source?: string,
file?: string,
}
view?: ComponentViewDefinition
}

export interface ComponentViewDefinition {
dependencies?: ComponentConstructor[],
dependencies?: Array<ComponentConstructor | SingletonComponentConstructor>,
file?: string,
is?: string,
source?: string,
viewPartialDependencies?: Array<string | { is: string }>,
}

export abstract class Component extends Controller {
export abstract class Component<T = DefualtType> extends Controller<T> {
context: Context;
id: string;
isDestroyed: boolean;
Expand All @@ -66,13 +61,13 @@ export abstract class Component extends Controller {
const parent = context.controller;
const id = context.id();
const scope = ['$components', id];
const model = parent.model.root.eventContext(id);
const model = parent.model.root.eventContext(id) as ChildModel<T>;
model._at = scope.join('.');
data.id = id;
model._set(scope, data);
// Store a reference to the component's scope such that the expression
// getters are relative to the component
model.data = data;
model.data = data as T;
// IMPORTANT: call super _after_ model created
super(context.controller.app, context.controller.page, model);

Expand Down Expand Up @@ -427,10 +422,10 @@ function setModelAttribute(context: Context, model: ChildModel, key: string, val
model.set(key, value);
}

export function createFactory(constructor: ComponentConstructor) {
export function createFactory(constructor: ComponentConstructor | SingletonComponentConstructor) {
// DEPRECATED: constructor.prototype.singleton is deprecated. "singleton"
// static property on the constructor is preferred
return (constructor.singleton || constructor.prototype.singleton) ?
return (constructor.singleton === true) ?
new SingletonComponentFactory(constructor) :
new ComponentFactory(constructor);
}
Expand Down Expand Up @@ -509,7 +504,7 @@ class SingletonComponentFactory{

init(context) {
// eslint-disable-next-line new-cap
if (!this.component) this.component = new this.constructorFn();
if (!this.component) this.component = new this.constructorFn() as Component;
return context.componentChild(this.component);
}

Expand Down
6 changes: 3 additions & 3 deletions src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Model } from 'racer';
import { RootModel, type Model } from 'racer';
import tracks = require('tracks');

import { type AppBase } from './App';
Expand Down Expand Up @@ -39,13 +39,13 @@ export interface RouteMethod {
}

export interface RouteHandler {
(page: Page, model: Model, params: PageParams, next: (err?: Error) => void): void;
(page: Page, model: RootModel, params: PageParams, next: (err?: Error) => void): void;
}

export interface TransitionalRouteHandler {
(
page: Page,
model: Model,
model: RootModel,
params: PageParams,
next: (err?: Error) => void,
done: () => void
Expand Down

0 comments on commit 8de6f6d

Please sign in to comment.