Skip to content

Commit

Permalink
Merge pull request #153 from wentout/proto
Browse files Browse the repository at this point in the history
registerHook fix
  • Loading branch information
wentout authored Nov 9, 2023
2 parents 28a5e7f + b09b325 commit 319b43c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 37 deletions.
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

0 comments on commit 319b43c

Please sign in to comment.