Skip to content

Commit

Permalink
Added narrowing
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Jul 11, 2023
1 parent 6f8ad05 commit 9ee80b4
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/model/events/EntityEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,35 @@ import ChangeEntry from "../changes/ChangeEntry.js";

const done = Promise.resolve() as Promise<void>;

export class ForeignKeyFilter<T = any> {
export class ForeignKeyFilter<T = any, TE = any> {

public type: EntityType;
public name: string;
public fkName: string;

private events: EntityEvents<any>;
private events: EntityEvents<TE>;
private context: EntityContext;

constructor(p: Partial<ForeignKeyFilter> & { context: EntityContext, events: EntityEvents<any> }) {
Object.setPrototypeOf(p, ForeignKeyFilter.prototype);
return p as any as ForeignKeyFilter;
}

public is<TR>(fx: (x: T) => TR): boolean {
public is<TR>(fx: (x: T) => TR): this is ForeignKeyFilter<T, TR> & boolean {
const name = NameParser.parseMember(fx);
return name === this.fkName || name === this.name;
}

public read() {
public read(): IEntityQuery<TE> {
const read = this.context.query(this.type.typeClass);
return this.events.filter(read);
}

public modify() {
public unfiltered(): IEntityQuery<TE> {
return this.context.query(this.type.typeClass);
}

public modify(): IEntityQuery<TE> {
const read = this.context.query(this.type.typeClass);
return this.events.modify(read);
}
Expand Down

0 comments on commit 9ee80b4

Please sign in to comment.