@cleanweb/react 1.1.1-beta.1 → 1.1.1-beta.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,6 @@ export declare class ComponentMethods<TState extends object, TProps extends obje
3
3
  state: TCleanState<TState>;
4
4
  props: TProps;
5
5
  }
6
- type UseMethods = <TMethods extends ComponentMethods<any, any>>(Methods: Constructor<TMethods>, state: TMethods['state'], props: TMethods['props']) => TMethods;
6
+ type UseMethods = <TMethods extends typeof ComponentMethods<any, any>>(Methods: TMethods & Constructor<InstanceType<TMethods>>, state: InstanceType<TMethods>['state'], props: InstanceType<TMethods>['props']) => InstanceType<TMethods>;
7
7
  export declare const useMethods: UseMethods;
8
8
  export {};
@@ -1,4 +1,55 @@
1
1
  "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
28
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
33
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
2
53
  Object.defineProperty(exports, "__esModule", { value: true });
3
54
  exports.useMethods = exports.ComponentMethods = void 0;
4
55
  var react_1 = require("react");
@@ -23,3 +74,27 @@ var useMethods = function (Methods, state, props) {
23
74
  return methods;
24
75
  };
25
76
  exports.useMethods = useMethods;
77
+ testing: {
78
+ var a = function () { return __awaiter(void 0, void 0, void 0, function () {
79
+ var a, MyMethods, useCleanState, self;
80
+ return __generator(this, function (_a) {
81
+ switch (_a.label) {
82
+ case 0:
83
+ a = { b: '' };
84
+ MyMethods = /** @class */ (function (_super) {
85
+ __extends(MyMethods, _super);
86
+ function MyMethods() {
87
+ return _super !== null && _super.apply(this, arguments) || this;
88
+ }
89
+ return MyMethods;
90
+ }(ComponentMethods));
91
+ ;
92
+ return [4 /*yield*/, import('./state.js')];
93
+ case 1:
94
+ useCleanState = (_a.sent()).useCleanState;
95
+ self = (0, exports.useMethods)(MyMethods, useCleanState({}), {});
96
+ return [2 /*return*/];
97
+ }
98
+ });
99
+ }); };
100
+ }
@@ -1,11 +1,15 @@
1
1
  import type { VoidFunctionComponent } from 'react';
2
- import type { TComponentClass } from './logic';
2
+ import type { IComponentInstanceClass } from './instance';
3
3
  import { ComponentInstance } from './instance';
4
- type Extractor = <TComponent extends ClassComponent<object, object, object>>(this: TComponentClass<TComponent, typeof ClassComponent>, _Component?: TComponentClass<TComponent, typeof ClassComponent>) => VoidFunctionComponent<TComponent['props']>;
4
+ type ComponentClassParams = ConstructorParameters<typeof ClassComponent>;
5
+ type o = object;
6
+ export interface IComponentClass<Instance extends ClassComponent<o, o, o> = ClassComponent, Params extends ComponentClassParams = ComponentClassParams> extends IComponentInstanceClass<Instance, Params> {
7
+ }
8
+ type Extractor = <TComponent extends typeof ClassComponent<o, o, o>>(this: NonNullable<typeof _Component>, _Component?: TComponent & IComponentClass<InstanceType<TComponent>>) => VoidFunctionComponent<InstanceType<TComponent>['props']>;
5
9
  export declare class ClassComponent<TState extends object = EmptyObject, TProps extends object = EmptyObject, THooks extends object = EmptyObject> extends ComponentInstance<TState, TProps, THooks> {
6
10
  Render: VoidFunctionComponent<{}>;
7
11
  static renderAs: 'component' | 'template';
8
- static FC: Extractor;
12
+ static readonly FC: Extractor;
9
13
  }
10
14
  interface HookWrapperProps<THookFunction extends AnyFunction> {
11
15
  hook: THookFunction;
@@ -32,7 +32,7 @@ var setFunctionName = function (func, newName) {
32
32
  console.warn(error);
33
33
  }
34
34
  };
35
- // eslint-enable no-use-before-define
35
+ ;
36
36
  var ClassComponent = /** @class */ (function (_super) {
37
37
  __extends(ClassComponent, _super);
38
38
  function ClassComponent() {
@@ -89,3 +89,15 @@ var Use = function (params) {
89
89
  return null;
90
90
  };
91
91
  exports.Use = Use;
92
+ testing: {
93
+ var a = { b: '' };
94
+ var MyComponentLogic = /** @class */ (function (_super) {
95
+ __extends(MyComponentLogic, _super);
96
+ function MyComponentLogic() {
97
+ return _super !== null && _super.apply(this, arguments) || this;
98
+ }
99
+ MyComponentLogic.getInitialState = function () { return ({}); };
100
+ return MyComponentLogic;
101
+ }(ClassComponent));
102
+ ;
103
+ }
@@ -1,6 +1,7 @@
1
- import type { TComponentClass } from './logic';
2
- import { ComponentLogic } from './logic';
1
+ import { ComponentLogic, IComponentLogicClass } from './logic';
3
2
  type AsyncAllowedEffectCallback = () => Awaitable<IVoidFunction>;
3
+ type UseMountCallbacks = <TInstance extends ComponentInstance<any, any, any>>(instance: TInstance) => void;
4
+ export declare const useMountCallbacks: UseMountCallbacks;
4
5
  export declare const noOp: () => void;
5
6
  export declare class ComponentInstance<TState extends object = EmptyObject, TProps extends object = EmptyObject, THooks extends object = EmptyObject> extends ComponentLogic<TState, TProps, THooks> {
6
7
  /**
@@ -47,17 +48,10 @@ export declare class ComponentInstance<TState extends object = EmptyObject, TPro
47
48
  */
48
49
  cleanUp: IVoidFunction;
49
50
  }
50
- type UseInstance = <TClass extends ComponentInstance<object, object, object>>(Class: TComponentClass<TClass>, ...props: valueof<TClass['props']> extends never ? ([] | [EmptyObject] | [TClass['props']]) : [TClass['props']]) => TClass;
51
- /**
52
- * To ensure successful type checking, the second parameter must be written with spread syntax.
53
- * Likely because of the `exactOptionalPropertyTypes` config option turned on,
54
- * and `UseInstance` using an empty tuple in its rest parameter type, attempting to simply
55
- * retrieve the second argument directly causes an error when that argument is passed on to `useLogic`.
56
- * But directly working with the rest array bypasses the problem. Also note that the issue persists even when
57
- * the second param is given `{}` as a default follow to account for the empty tuple case. TypeScript
58
- * just wants us to use the rest parameter explicitly by force.
59
- */
51
+ type o = object;
52
+ type InstanceClassParams = ConstructorParameters<typeof ComponentInstance<o, o, o>>;
53
+ export interface IComponentInstanceClass<Instance extends ComponentInstance<o, o, o> = ComponentInstance, Params extends InstanceClassParams = InstanceClassParams> extends IComponentLogicClass<Instance, Params> {
54
+ }
55
+ type UseInstance = <TClass extends typeof ComponentInstance<object, object, object>>(Class: TClass & IComponentInstanceClass<InstanceType<TClass>>, ...props: valueof<InstanceType<TClass>['props']> extends never ? ([] | [EmptyObject] | [InstanceType<TClass>['props']]) : [InstanceType<TClass>['props']]) => InstanceType<TClass>;
60
56
  export declare const useInstance: UseInstance;
61
- type UseMountCallbacks = <TInstance extends ComponentInstance<any, any, any>>(instance: TInstance) => void;
62
- export declare const useMountCallbacks: UseMountCallbacks;
63
57
  export {};
@@ -24,10 +24,35 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
24
24
  return to.concat(ar || Array.prototype.slice.call(from));
25
25
  };
26
26
  Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.useMountCallbacks = exports.useInstance = exports.ComponentInstance = exports.noOp = void 0;
27
+ exports.useInstance = exports.ComponentInstance = exports.noOp = exports.useMountCallbacks = void 0;
28
28
  var react_1 = require("react");
29
29
  var state_1 = require("../base/state");
30
30
  var logic_1 = require("./logic");
31
+ var useMountCallbacks = function (instance) {
32
+ var _a;
33
+ var mounted = (0, state_1.useMountState)();
34
+ if (!mounted)
35
+ (_a = instance.beforeMount) === null || _a === void 0 ? void 0 : _a.call(instance);
36
+ (0, react_1.useEffect)(function () {
37
+ var _a;
38
+ var mountHandlerCleanUp = (_a = instance.onMount) === null || _a === void 0 ? void 0 : _a.call(instance);
39
+ return function () {
40
+ var doCleanUp = function (runMountCleaners) {
41
+ var _a;
42
+ runMountCleaners === null || runMountCleaners === void 0 ? void 0 : runMountCleaners();
43
+ // onDismount? willUnmount?
44
+ (_a = instance.cleanUp) === null || _a === void 0 ? void 0 : _a.call(instance);
45
+ };
46
+ if (typeof mountHandlerCleanUp === 'function') {
47
+ doCleanUp(mountHandlerCleanUp);
48
+ }
49
+ else {
50
+ mountHandlerCleanUp === null || mountHandlerCleanUp === void 0 ? void 0 : mountHandlerCleanUp.then(doCleanUp);
51
+ }
52
+ };
53
+ }, []);
54
+ };
55
+ exports.useMountCallbacks = useMountCallbacks;
31
56
  var noOp = function () { };
32
57
  exports.noOp = noOp;
33
58
  var ComponentInstance = /** @class */ (function (_super) {
@@ -83,7 +108,8 @@ var ComponentInstance = /** @class */ (function (_super) {
83
108
  }(logic_1.ComponentLogic));
84
109
  exports.ComponentInstance = ComponentInstance;
85
110
  ;
86
- /**
111
+ ;
112
+ /*
87
113
  * To ensure successful type checking, the second parameter must be written with spread syntax.
88
114
  * Likely because of the `exactOptionalPropertyTypes` config option turned on,
89
115
  * and `UseInstance` using an empty tuple in its rest parameter type, attempting to simply
@@ -115,7 +141,6 @@ var useInstance = function (Component) {
115
141
  '
116
142
  */
117
143
  // beforeMount, onMount, cleanUp.
118
- // eslint-disable-next-line no-use-before-define
119
144
  (0, exports.useMountCallbacks)(instance);
120
145
  // beforeRender.
121
146
  (_a = instance.beforeRender) === null || _a === void 0 ? void 0 : _a.call(instance);
@@ -133,28 +158,3 @@ var useInstance = function (Component) {
133
158
  return instance;
134
159
  };
135
160
  exports.useInstance = useInstance;
136
- var useMountCallbacks = function (instance) {
137
- var _a;
138
- var mounted = (0, state_1.useMountState)();
139
- if (!mounted)
140
- (_a = instance.beforeMount) === null || _a === void 0 ? void 0 : _a.call(instance);
141
- (0, react_1.useEffect)(function () {
142
- var _a;
143
- var mountHandlerCleanUp = (_a = instance.onMount) === null || _a === void 0 ? void 0 : _a.call(instance);
144
- return function () {
145
- var doCleanUp = function (runMountCleaners) {
146
- var _a;
147
- runMountCleaners === null || runMountCleaners === void 0 ? void 0 : runMountCleaners();
148
- // onDismount? willUnmount?
149
- (_a = instance.cleanUp) === null || _a === void 0 ? void 0 : _a.call(instance);
150
- };
151
- if (typeof mountHandlerCleanUp === 'function') {
152
- doCleanUp(mountHandlerCleanUp);
153
- }
154
- else {
155
- mountHandlerCleanUp === null || mountHandlerCleanUp === void 0 ? void 0 : mountHandlerCleanUp.then(doCleanUp);
156
- }
157
- };
158
- }, []);
159
- };
160
- exports.useMountCallbacks = useMountCallbacks;
@@ -1,22 +1,18 @@
1
1
  import type { TCleanState, TState } from '../base/state';
2
- export declare class ComponentLogic<TState extends object = EmptyObject, TProps extends object = EmptyObject, THooks extends object = EmptyObject> {
2
+ export type Empty = EmptyObject;
3
+ type o = object;
4
+ export declare class ComponentLogic<TState extends o = Empty, TProps extends o = Empty, THooks extends o = Empty> {
3
5
  state: TCleanState<TState>;
4
6
  props: TProps;
5
7
  hooks: THooks;
6
- static getInitialState: IComponentClass['getInitialState'];
8
+ static getInitialState: TBaseComponentLogic['getInitialState'];
7
9
  useHooks?: () => THooks;
8
10
  }
9
- type CnstPrm = ConstructorParameters<typeof ComponentLogic>;
10
- export interface IComponentClass<Instance extends ComponentLogic = ComponentLogic> {
11
- new (...params: CnstPrm): Instance;
11
+ type LogicClassParams = ConstructorParameters<typeof ComponentLogic>;
12
+ export interface IComponentLogicClass<Instance extends ComponentLogic<o, o, o> = ComponentLogic, Params extends LogicClassParams = LogicClassParams> extends Constructor<Instance, Params> {
12
13
  getInitialState: (props?: Instance['props']) => TState<Instance['state']>;
13
14
  }
14
- export type ComponentClassStatics<Instance extends ComponentLogic<object, object, object>> = {
15
- getInitialState: (props?: Instance['props']) => TState<Instance['state']>;
16
- };
17
- export type TComponentClass<Instance extends ComponentLogic<object, object, object>, Statics extends ComponentClassStatics<Instance> = ComponentClassStatics<Instance>, Params extends CnstPrm = CnstPrm> = Statics & Constructor<Instance, Params>;
18
- export interface IEmpty extends EmptyObject {
19
- }
20
- type UseLogic = <CLogic extends ComponentLogic<object, object, object>>(Methods: TComponentClass<CLogic>, ...props: valueof<CLogic['props']> extends never ? ([] | [EmptyObject] | [CLogic['props']]) : [CLogic['props']]) => CLogic;
15
+ export type TBaseComponentLogic = IComponentLogicClass<ComponentLogic<o, o, o>>;
16
+ type UseLogic = <Class extends typeof ComponentLogic<o, o, o>>(Methods: Class & IComponentLogicClass<InstanceType<Class>>, ...props: valueof<InstanceType<Class>['props']> extends never ? ([] | [EmptyObject] | [InstanceType<Class>['props']]) : [InstanceType<Class>['props']]) => InstanceType<Class>;
21
17
  export declare const useLogic: UseLogic;
22
18
  export {};
@@ -19,6 +19,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
19
19
  exports.useLogic = exports.ComponentLogic = void 0;
20
20
  var react_1 = require("react");
21
21
  var state_1 = require("../base/state");
22
+ // React.Component
22
23
  var ComponentLogic = /** @class */ (function () {
23
24
  function ComponentLogic() {
24
25
  }
@@ -26,21 +27,8 @@ var ComponentLogic = /** @class */ (function () {
26
27
  }());
27
28
  exports.ComponentLogic = ComponentLogic;
28
29
  ;
29
- testing: {
30
- var A = (_a = /** @class */ (function (_super) {
31
- __extends(C, _super);
32
- function C() {
33
- return _super !== null && _super.apply(this, arguments) || this;
34
- }
35
- return C;
36
- }(ComponentLogic)),
37
- _a.getInitialState = function () { return ({}); },
38
- _a);
39
- A.getInitialState();
40
- }
41
- ;
42
30
  var useLogic = function (Methods, props) {
43
- var _b;
31
+ var _b, _c;
44
32
  if (props === void 0) { props = {}; }
45
33
  var state = (0, state_1.useCleanState)(Methods.getInitialState, props);
46
34
  var methods = (0, react_1.useRef)((0, react_1.useMemo)(function () {
@@ -48,7 +36,7 @@ var useLogic = function (Methods, props) {
48
36
  }, [])).current;
49
37
  methods.state = state;
50
38
  methods.props = props;
51
- methods.hooks = ((_b = methods.useHooks) === null || _b === void 0 ? void 0 : _b.call(methods)) || {};
39
+ methods.hooks = (_c = (_b = methods.useHooks) === null || _b === void 0 ? void 0 : _b.call(methods)) !== null && _c !== void 0 ? _c : {};
52
40
  return methods;
53
41
  };
54
42
  exports.useLogic = useLogic;
@@ -63,5 +51,27 @@ testing: {
63
51
  return MyComponentLogic;
64
52
  }(ComponentLogic));
65
53
  ;
66
- (0, exports.useLogic)(MyComponentLogic);
54
+ var self_1 = (0, exports.useLogic)(MyComponentLogic);
55
+ }
56
+ testing: {
57
+ var A = (_a = /** @class */ (function (_super) {
58
+ __extends(C, _super);
59
+ function C() {
60
+ var _this = _super !== null && _super.apply(this, arguments) || this;
61
+ _this.b = _this.state.a;
62
+ return _this;
63
+ }
64
+ return C;
65
+ }(ComponentLogic)),
66
+ _a.getInitialState = function () { return ({ a: 'l' }); },
67
+ _a);
68
+ A.getInitialState();
69
+ // const self = useLogic(A);
67
70
  }
71
+ // export type ComponentClassStatics<Instance extends ComponentLogic<object, object, object>> = {
72
+ // getInitialState: (props?: Instance['props']) => TState<Instance['state']>;
73
+ // }
74
+ // export type TComponentClass<
75
+ // Instance extends ComponentLogic<object, object, object>,
76
+ // Params extends CnstPrm = CnstPrm
77
+ // > = ComponentClassStatics<Instance> & Constructor<Instance, Params>;
@@ -57,7 +57,9 @@ declare global {
57
57
  ''?: never;
58
58
  };
59
59
  type TEmptyObject2 = Record<symbol, never>;
60
- type EmptyObject = __FromPrivateHelpers['EmptyObject'];
60
+ type EmptyObject = {
61
+ [key: keyof any]: never;
62
+ };
61
63
  type EmptyObject2 = __FromPrivateHelpers['EmptyObject2'];
62
64
  type EmptyObject3 = __FromPrivateHelpers['EmptyObject3'];
63
65
  type valueof<TObject> = TObject[keyof TObject];
@@ -7,9 +7,6 @@
7
7
  */
8
8
  /** */
9
9
  declare const UniqueSecretSymbolKey: unique symbol;
10
- declare class CEmptyObject {
11
- [key: keyof any]: never;
12
- }
13
10
  declare class CEmptyObject2 {
14
11
  [UniqueSecretSymbolKey]?: never;
15
12
  }
@@ -42,7 +39,6 @@ declare class CEmptyObject3 {
42
39
  */
43
40
  [key: symbol]: never;
44
41
  }
45
- export declare const EmptyObject: CEmptyObject;
46
42
  export declare const EmptyObject2: CEmptyObject2;
47
43
  export declare const EmptyObject3: CEmptyObject3;
48
44
  export {};
@@ -7,14 +7,9 @@
7
7
  * **You should not import this file directly.**
8
8
  */
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.EmptyObject3 = exports.EmptyObject2 = exports.EmptyObject = void 0;
10
+ exports.EmptyObject3 = exports.EmptyObject2 = void 0;
11
11
  /** */
12
12
  var UniqueSecretSymbolKey = Symbol('asdfghjkliuytrewqaxcvb,nb');
13
- var CEmptyObject = /** @class */ (function () {
14
- function CEmptyObject() {
15
- }
16
- return CEmptyObject;
17
- }());
18
13
  var CEmptyObject2 = /** @class */ (function () {
19
14
  function CEmptyObject2() {
20
15
  }
@@ -25,7 +20,6 @@ var CEmptyObject3 = /** @class */ (function () {
25
20
  }
26
21
  return CEmptyObject3;
27
22
  }());
28
- exports.EmptyObject = new CEmptyObject();
29
23
  exports.EmptyObject2 = new CEmptyObject2();
30
24
  exports.EmptyObject3 = new CEmptyObject3();
31
25
  testing: {
@@ -35,6 +29,6 @@ testing: {
35
29
  // [UniqueSecretSymbolKey]: '',
36
30
  // '': '',
37
31
  };
38
- var TT = new CEmptyObject();
32
+ var TT = new CEmptyObject2();
39
33
  TT = tt;
40
34
  }
@@ -32,7 +32,7 @@
32
32
  "noImplicitAny": true,
33
33
  "noUncheckedIndexedAccess": true,
34
34
  "strictBindCallApply": true,
35
- // "exactOptionalPropertyTypes": true
35
+ "exactOptionalPropertyTypes": true,
36
36
  },
37
37
  "include": [
38
38
  "**/*.ts",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleanweb/react",
3
- "version": "1.1.1-beta.1",
3
+ "version": "1.1.1-beta.3",
4
4
  "description": "A suite of helpers for writing cleaner React function components.",
5
5
  "engines": {
6
6
  "node": ">=18"