From b09b325f0689281e37df80b928d9ef4d4359c1f3 Mon Sep 17 00:00:00 2001 From: wentout Date: Thu, 9 Nov 2023 14:11:36 +0300 Subject: [PATCH] registerHook fix --- build/index.d.ts | 27 ++++++++++++--------------- package-lock.json | 2 +- package.json | 2 +- src/index.ts | 32 +++++++++++++------------------- test-ts/test-no-types.ts | 3 ++- 5 files changed, 29 insertions(+), 37 deletions(-) diff --git a/build/index.d.ts b/build/index.d.ts index 912d914..8ddda58 100644 --- a/build/index.d.ts +++ b/build/index.d.ts @@ -1,17 +1,6 @@ import { TypeLookup, IDEF } from './types'; export type { IDEF } from './types'; export declare const defaultTypes: any; -type Proto = Pick> & T; -type SN = Record unknown>; -interface IDefinitorInstance { - new (): { - [key in keyof S]: S[key]; - }; - define: IDefinitor; -} -interface IDefinitor

{ - >, S extends SN & M>(this: unknown, TypeName: SubTypeName, constructHandler: IDEF, proto?: PP, config?: object): IDefinitorInstance; -} type hooksTypes = 'preCreation' | 'postCreation' | 'creationError'; type hooksOpts = { TypeName: string; @@ -22,11 +11,19 @@ type hooksOpts = { type hook = { (opts: hooksOpts): void; }; -export declare const define: , SubTypeName extends string, S extends SN & N, R extends { - new (): { [key in keyof S]: S[key]; }; - define: IDefinitor; +type Proto = Pick> & T; +type SN = Record unknown>; +interface IDefinitorInstance { + new (): { + [key in keyof S]: S[key]; + }; + define: IDefinitor; registerHook: (hookType: hooksTypes, cb: hook) => void; -}>(this: unknown, TypeName?: string, constructHandler?: IDEF | undefined, proto?: P | undefined, config?: {}) => R; +} +interface IDefinitor

{ + >, S extends SN & M>(this: unknown, TypeName: SubTypeName, constructHandler: IDEF, proto?: PP, config?: object): IDefinitorInstance; +} +export declare const define: , S extends SN & N, R extends IDefinitorInstance>(this: unknown, TypeName?: string, constructHandler?: IDEF | undefined, proto?: P | undefined, config?: {}) => R; export declare const lookup: TypeLookup; export declare const apply: >(entity: E, Constructor: IDEF, args?: unknown[]) => { [key in keyof S]: S[key]; }; export declare const call: >(entity: E, Constructor: IDEF, ...args: unknown[]) => { [key in keyof S]: S[key]; }; diff --git a/package-lock.json b/package-lock.json index 53c1eb3..a54cc04 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "mnemonica", - "version": "0.9.970", + "version": "0.9.971", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/package.json b/package.json index 74d9bf4..145ab81 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 13bcc6b..eeeb40d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 = Pick> & T; // type Narrowable = @@ -30,6 +41,7 @@ interface IDefinitorInstance { [key in keyof S]: S[key] } define: IDefinitor + registerHook: (hookType: hooksTypes, cb: hook) => void } interface IDefinitor

{ @@ -42,35 +54,17 @@ interface IDefinitor

{ ): IDefinitorInstance } -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, // H extends ThisType>, P extends object, N extends Proto, - 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 - registerHook: (hookType: hooksTypes, cb: hook) => void - }, + R extends IDefinitorInstance > ( this: unknown, TypeName?: string, diff --git a/test-ts/test-no-types.ts b/test-ts/test-no-types.ts index 50750c0..abd7542 100644 --- a/test-ts/test-no-types.ts +++ b/test-ts/test-no-types.ts @@ -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, @@ -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 !