Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions dist/js/entity/set/InMemoryEntityInSetMixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,12 @@ import type { Constructor } from "../../utils/types";
import { type InMemoryEntity } from "../in_memory";
export type SystemInSet = Required<SystemInSetSchema>;
export type InSet = SystemInSet["inSet"][0];
export declare function inMemoryEntityInSetMixin<E extends InMemoryEntity>(item: E): {
getInSetFilteredByCls(cls: string): {
_id: string;
cls?: string;
slug?: string;
type?: string;
index?: number;
}[];
parentEntitySetReference: {
_id: string;
cls?: string;
slug?: string;
type?: string;
index?: number;
} | undefined;
inSet: InSet[];
export declare function inMemoryEntityInSetMixin<E extends InMemoryEntity>(item: E): void;
export type InSetPropertiesInMemoryEntity = {
getInSetFilteredByCls: (cls: string) => InSet[];
parentEntitySetReference: InSet | undefined;
};
export type InMemoryEntityInSet = ReturnType<typeof inMemoryEntityInSetMixin>;
export type InMemoryEntityInSet = SystemInSet & InSetPropertiesInMemoryEntity;
export type InMemoryEntityInSetConstructor = Constructor<InMemoryEntityInSet>;
type Base = Constructor<InMemoryEntity>;
export default function InMemoryEntityInSetMixin<S extends Base = Base>(superclass: S): S & InMemoryEntityInSetConstructor;
Expand Down
31 changes: 8 additions & 23 deletions dist/js/entity/set/InMemoryEntityInSetMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,29 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.inMemoryEntityInSetMixin = inMemoryEntityInSetMixin;
exports.default = InMemoryEntityInSetMixin;
function schemaMixin(item) {
const schema = {
function inMemoryEntityInSetMixin(item) {
// @ts-expect-error
const properties = {
get inSet() {
return item.prop("inSet", []);
return this.prop("inSet", []);
},
set inSet(inSet) {
item.setProp("inSet", inSet);
this.setProp("inSet", inSet);
},
};
Object.defineProperties(item, Object.getOwnPropertyDescriptors(schema));
return schema;
}
function propertiesMixin(item) {
const properties = {
getInSetFilteredByCls(cls) {
return item.inSet.filter((ref) => ref.cls === cls);
return this.inSet.filter((ref) => ref.cls === cls);
},
// finds a parent entity set of the same cls (hence `cls` field is absent)
// NOTE: assumes that only one entry of this kind is present => gets the first one
get parentEntitySetReference() {
return item.inSet.find((item) => item._id && !item.cls);
return this.inSet.find((inSetItem) => inSetItem._id && !inSetItem.cls);
},
};
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
return properties;
}
function inMemoryEntityInSetMixin(item) {
return {
...schemaMixin(item),
...propertiesMixin(item),
};
}
function InMemoryEntityInSetMixin(superclass) {
class InMemoryEntityInSetMixin extends superclass {
constructor(...args) {
super(...args);
inMemoryEntityInSetMixin(this);
}
}
inMemoryEntityInSetMixin(InMemoryEntityInSetMixin.prototype);
return InMemoryEntityInSetMixin;
}
46 changes: 15 additions & 31 deletions src/js/entity/set/InMemoryEntityInSetMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,45 @@ import { type InMemoryEntity } from "../in_memory";
export type SystemInSet = Required<SystemInSetSchema>;
export type InSet = SystemInSet["inSet"][0];

type InMemoryEntityInSetSchema = ReturnType<typeof schemaMixin>;

function schemaMixin<E extends InMemoryEntity>(item: E) {
const schema = {
export function inMemoryEntityInSetMixin<E extends InMemoryEntity>(item: E) {
// @ts-expect-error
const properties: SystemInSet & InSetPropertiesInMemoryEntity & E = {
get inSet() {
return item.prop<InSet[]>("inSet", []);
return this.prop<InSet[]>("inSet", []);
},

set inSet(inSet: InSet[]) {
item.setProp("inSet", inSet);
this.setProp("inSet", inSet);
},
} satisfies SystemInSetSchema;

Object.defineProperties(item, Object.getOwnPropertyDescriptors(schema));

return schema;
}

function propertiesMixin<E extends InMemoryEntity>(item: E & InMemoryEntityInSetSchema) {
const properties = {
getInSetFilteredByCls(cls: string) {
return item.inSet.filter((ref) => ref.cls === cls);
return this.inSet.filter((ref) => ref.cls === cls);
},

// finds a parent entity set of the same cls (hence `cls` field is absent)
// NOTE: assumes that only one entry of this kind is present => gets the first one
get parentEntitySetReference() {
return item.inSet.find((item) => item._id && !item.cls);
return this.inSet.find((inSetItem) => inSetItem._id && !inSetItem.cls);
},
};

Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));

return properties;
}

export function inMemoryEntityInSetMixin<E extends InMemoryEntity>(item: E) {
return {
...schemaMixin(item),
...propertiesMixin(item as E & InMemoryEntityInSetSchema),
};
}
export type InSetPropertiesInMemoryEntity = {
getInSetFilteredByCls: (cls: string) => InSet[];
parentEntitySetReference: InSet | undefined;
};

export type InMemoryEntityInSet = ReturnType<typeof inMemoryEntityInSetMixin>;
export type InMemoryEntityInSet = SystemInSet & InSetPropertiesInMemoryEntity;
export type InMemoryEntityInSetConstructor = Constructor<InMemoryEntityInSet>;

type Base = Constructor<InMemoryEntity>;

export default function InMemoryEntityInSetMixin<S extends Base = Base>(superclass: S) {
class InMemoryEntityInSetMixin extends superclass {
constructor(...args: any[]) {
super(...args);
inMemoryEntityInSetMixin(this);
}
}
class InMemoryEntityInSetMixin extends superclass {}

inMemoryEntityInSetMixin(InMemoryEntityInSetMixin.prototype);

return InMemoryEntityInSetMixin as S & InMemoryEntityInSetConstructor;
}
Loading