Skip to content

Commit

Permalink
Merge pull request #158 from wentout/proto
Browse files Browse the repository at this point in the history
constructor opstions type
  • Loading branch information
wentout committed Nov 11, 2023
2 parents 259da39 + 822f337 commit c8d7c05
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 52 deletions.
12 changes: 10 additions & 2 deletions build/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ type hooksOpts = {
type hook = {
(opts: hooksOpts): void;
};
type constructorOptions = {
useOldStyle: boolean;
strictChain: boolean;
blockErrors: boolean;
submitStack: boolean;
awaitReturn: boolean;
bindedProto: boolean;
};
type Proto<P, T> = Pick<P, Exclude<keyof P, keyof T>> & T;
type SN = Record<string, new () => unknown>;
interface IDefinitorInstance<N extends object, S> {
Expand All @@ -21,9 +29,9 @@ interface IDefinitorInstance<N extends object, S> {
registerHook: (hookType: hooksTypes, cb: hook) => void;
}
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>;
<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?: constructorOptions): 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 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?: constructorOptions) => 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 build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports.defaultTypes = descriptors_1.descriptors.defaultTypes;
function checkThis(pointer) {
return pointer === exports.mnemonica || pointer === exports;
}
const define = function (TypeName, constructHandler, proto, config = {}) {
const define = function (TypeName, constructHandler, proto, config) {
const types = checkThis(this) ? exports.defaultTypes : this || exports.defaultTypes;
return types.define(TypeName, constructHandler, proto, config);
};
Expand Down
82 changes: 41 additions & 41 deletions 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.975",
"version": "0.9.977",
"description": "abstract technique that aids information retention : instance inheritance system",
"type": "commonjs",
"main": "./build/index.js",
Expand Down
43 changes: 41 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,45 @@ type hook = {
(opts: hooksOpts): void
}

type constructorOptions = {

// explicit declaration we wish use
// an old style based constructors
// e.g. with prototype described with:
// createInstanceModificator200XthWay
// or more general with: createInstanceModificator
useOldStyle : boolean,

// shall or not we use strict checking
// for creation sub-instances Only from current type
// or we might use up-nested sub-instances from chain
strictChain : boolean,

// should we use forced errors checking
// to make all inherited types errored
// if there is an error somewhere in chain
// disallow instance construction
// if there is an error in prototype chain
blockErrors : boolean,

// if it is necessary to collect stack
// as a __stack__ prototype property
// during the process of instance creation
submitStack : boolean,

// await new Constructor()
// must return value
// optional ./issues/106
awaitReturn : boolean,

// instance methods calls
// are binded by default
// with instance itself
bindedProto : boolean,

}


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

// type Narrowable =
Expand All @@ -51,7 +90,7 @@ interface IDefinitor<P extends object, SubTypeName extends string> {
TypeName: SubTypeName,
constructHandler: IDEF<T>,
proto?: PP,
config?: object,
config?: constructorOptions,
): IDefinitorInstance<M, S>
}

Expand All @@ -71,7 +110,7 @@ export const define = function <
TypeName?: string,
constructHandler?: IDEF<T>,
proto?: P,
config = {},
config?: constructorOptions,
): R {
const types = checkThis(this) ? defaultTypes : this || defaultTypes;
// if (typeof constructHandler !== 'function') {
Expand Down
11 changes: 11 additions & 0 deletions test/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const tests = (opts) => {
SubOfSomeADTCTypePre,
SubOfSomeADTCTypePost,
subOfSomeADTCInstanceA,
backSub,
subOfSomeADTCInstanceANoArgs,
subOfSomeADTCInstanceC,
subOfSomeADTCInstanceB,
Expand Down Expand Up @@ -368,6 +369,16 @@ const tests = (opts) => {

});

it('non strict chain works correctly', () => {

expect(backSub.sub_test).equal(321);
expect(backSub.constructor.name).equal('SubOfSomeADTCType');
expect(backSub.__parent__.constructor.name).equal('SubOfSomeADTCType');
expect(backSub.__parent__.__parent__.constructor.name).equal('SomeADTCType');
expect(backSub.__parent__.__parent__.__parent__.constructor.name).equal('Mnemonica');

});

describe('should create type from Proxy.set()', () => {
it('type creation from Proxy.set()', () => {
const userProxyTyped = user.ProxyTyped('aha');
Expand Down
2 changes: 2 additions & 0 deletions test/hookSamples.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const namespaceFlowCheckerSample = [
'SubOfSomeADTCType',
'SubOfSomeADTCType',
'SubOfSomeADTCType',
'SubOfSomeADTCType',
'SubOfSomeADTCType',
'UserType',
'UserType',
'UserTypePL1',
Expand Down
6 changes: 3 additions & 3 deletions test/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const tests = (opts) => {
assert.equal(8, userTypeHooksInvocations.length);
debugger;
// +2
assert.equal(179, namespaceFlowCheckerInvocations.length);
assert.equal(181, namespaceFlowCheckerInvocations.length);
// +2
assert.equal(169, typesFlowCheckerInvocations.length);
// +1
Expand All @@ -48,10 +48,10 @@ const tests = (opts) => {
// +1
assert.equal(77, typesPostCreationInvocations.length);
// +1
assert.equal(97, namespacePreCreationInvocations.length);
assert.equal(98, namespacePreCreationInvocations.length);
// there are two registered Hooks, that is why
// +2
assert.equal(164, namespacePostCreationInvocations.length);
assert.equal(166, namespacePostCreationInvocations.length);
});
});

Expand Down
9 changes: 7 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ const {

const SomeADTCType = adtcDefine('SomeADTCType', function () {
this.test = 123;
});
}, {}, { strictChain : false });

const someADTCInstance = new SomeADTCType();

Expand All @@ -239,7 +239,7 @@ let SubOfSomeADTCTypePost = null;
const SubOfSomeADTCType = SomeADTCType.define('SubOfSomeADTCType', function (...args) {
this.sub_test = 321;
this.args = args;
});
}, {}, { strictChain : false });

SubOfSomeADTCType.registerHook('preCreation', (opts) => {
SubOfSomeADTCTypePre = opts;
Expand All @@ -248,10 +248,14 @@ SubOfSomeADTCType.registerHook('postCreation', (opts) => {
SubOfSomeADTCTypePost = opts;
});


// debugger;
const subOfSomeADTCInstanceANoArgs = apply(someADTCInstance, SubOfSomeADTCType);
const subOfSomeADTCInstanceA = apply(someADTCInstance, SubOfSomeADTCType, [ 1, 2, 3 ]);

// debugger;
const backSub = new subOfSomeADTCInstanceA.SubOfSomeADTCType;

// debugger;
const subOfSomeADTCInstanceC = call(someADTCInstance, SubOfSomeADTCType, 1, 2, 3);

Expand Down Expand Up @@ -611,6 +615,7 @@ describe('Main Test', () => {
SubOfSomeADTCTypePost,
subOfSomeADTCInstanceANoArgs,
subOfSomeADTCInstanceA,
backSub,
subOfSomeADTCInstanceC,
subOfSomeADTCInstanceB,
anotherNamespace,
Expand Down

0 comments on commit c8d7c05

Please sign in to comment.