Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

registerHook fix #153

Merged
merged 1 commit into from
Nov 9, 2023
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
27 changes: 12 additions & 15 deletions build/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import { TypeLookup, IDEF } from './types';
export type { IDEF } from './types';
export declare const defaultTypes: any;
type Proto<P, T> = Pick<P, Exclude<keyof P, keyof T>> & T;
type SN = Record<string, new () => unknown>;
interface IDefinitorInstance<N extends object, S> {
new (): {
[key in keyof S]: S[key];
};
define: IDefinitor<N, string>;
}
interface IDefinitor<P extends object, SubTypeName extends string> {
<PP extends object, T, M extends Proto<P, Proto<PP, T>>, S extends SN & M>(this: unknown, TypeName: SubTypeName, constructHandler: IDEF<T>, proto?: PP, config?: object): IDefinitorInstance<M, S>;
}
type hooksTypes = 'preCreation' | 'postCreation' | 'creationError';
type hooksOpts = {
TypeName: string;
Expand All @@ -22,11 +11,19 @@ type hooksOpts = {
type hook = {
(opts: hooksOpts): void;
};
export declare const define: <T, P extends object, N extends Proto<P, T>, SubTypeName extends string, S extends SN & N, R extends {
new (): { [key in keyof S]: S[key]; };
define: IDefinitor<N, SubTypeName>;
type Proto<P, T> = Pick<P, Exclude<keyof P, keyof T>> & T;
type SN = Record<string, new () => unknown>;
interface IDefinitorInstance<N extends object, S> {
new (): {
[key in keyof S]: S[key];
};
define: IDefinitor<N, string>;
registerHook: (hookType: hooksTypes, cb: hook) => void;
}>(this: unknown, TypeName?: string, constructHandler?: IDEF<T> | undefined, proto?: P | undefined, config?: {}) => R;
}
interface IDefinitor<P extends object, SubTypeName extends string> {
<PP extends object, T, M extends Proto<P, Proto<PP, T>>, S extends SN & M>(this: unknown, TypeName: SubTypeName, constructHandler: IDEF<T>, proto?: PP, config?: object): IDefinitorInstance<M, S>;
}
export declare const define: <T, P extends object, N extends Proto<P, T>, S extends SN & N, R extends IDefinitorInstance<N, S>>(this: unknown, TypeName?: string, constructHandler?: IDEF<T> | undefined, proto?: P | undefined, config?: {}) => R;
export declare const lookup: TypeLookup;
export declare const apply: <E extends object, T extends object, S extends Proto<E, T>>(entity: E, Constructor: IDEF<T>, args?: unknown[]) => { [key in keyof S]: S[key]; };
export declare const call: <E extends object, T extends object, S extends Proto<E, T>>(entity: E, Constructor: IDEF<T>, ...args: unknown[]) => { [key in keyof S]: S[key]; };
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mnemonica",
"version": "0.9.970",
"version": "0.9.971",
"description": "abstract technique that aids information retention : instance inheritance system",
"type": "commonjs",
"main": "./build/index.js",
Expand Down
32 changes: 13 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ function checkThis (pointer: typeof mnemonica | typeof exports | unknown): boole
return pointer === mnemonica || pointer === exports;
}

type hooksTypes = 'preCreation' | 'postCreation' | 'creationError'
type hooksOpts = {
TypeName: string,
args: unknown[],
existentInstance: object,
inheritedInstance: object,
}
type hook = {
(opts: hooksOpts): void
}

type Proto <P, T> = Pick<P, Exclude<keyof P, keyof T>> & T;

// type Narrowable =
Expand All @@ -30,6 +41,7 @@ interface IDefinitorInstance<N extends object, S> {
[key in keyof S]: S[key]
}
define: IDefinitor<N, string>
registerHook: (hookType: hooksTypes, cb: hook) => void
}

interface IDefinitor<P extends object, SubTypeName extends string> {
Expand All @@ -42,35 +54,17 @@ interface IDefinitor<P extends object, SubTypeName extends string> {
): IDefinitorInstance<M, S>
}

type hooksTypes = 'preCreation' | 'postCreation' | 'creationError'
type hooksOpts = {
TypeName: string,
args: unknown[],
existentInstance: object,
inheritedInstance: object,
}
type hook = {
(opts: hooksOpts): void
}

export const define = function <
T,
// K extends IDEF<T>,
// H extends ThisType<IDEF<T>>,
P extends object,
N extends Proto<P, T>,
SubTypeName extends string,
// so S it just basically allows nested constructors
// and gives extracted props from constructHandler & proto
// then it goes to new() keyword of define output
S extends SN & N,
R extends {
new (): {
[key in keyof S]: S[key]
}
define: IDefinitor<N, SubTypeName>
registerHook: (hookType: hooksTypes, cb: hook) => void
},
R extends IDefinitorInstance<N, S>
> (
this: unknown,
TypeName?: string,
Expand Down
3 changes: 2 additions & 1 deletion test-ts/test-no-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const SomeType = define( 'SomeType', function (this: {
}, {
l : 12345
});
SomeType.registerHook('preCreation', () => { console.log('SomeType'); });

const SomeSubType = SomeType.define( 'SomeSubType', function ( this: {
one: undefined,
Expand All @@ -28,7 +29,7 @@ const SomeSubType = SomeType.define( 'SomeSubType', function ( this: {
});

const first = new SomeType();

SomeSubType.registerHook('preCreation', () => { console.log('SomeSubType'); });

const x = first.one;
first.one = 123; // hinting is correct !
Expand Down
Loading