@cleanweb/react 1.1.1-beta.4 → 1.1.1-beta.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -70,7 +70,9 @@ var MergedState = /** @class */ (function () {
70
70
  return MergedState;
71
71
  }());
72
72
  var useMergedState = function (initialState) {
73
- var cleanState = (0, react_1.useMemo)(function () { return new MergedState(initialState); }, []);
73
+ var cleanState = (0, react_1.useMemo)(function () {
74
+ return new MergedState(initialState);
75
+ }, []);
74
76
  MergedState.useRefresh.call(cleanState);
75
77
  return cleanState;
76
78
  };
@@ -1,9 +1,8 @@
1
1
  import type { TCleanState } from './state';
2
- export declare class ComponentMethods<TState extends object, TProps extends object> {
3
- state: TCleanState<TState>;
2
+ export declare class ComponentMethods<TProps extends object, TState extends object> {
4
3
  props: TProps;
4
+ state: TCleanState<TState>;
5
5
  }
6
- type UseMethods = <Class extends TypeofClass & Constructor<InstanceType<TypeofClass>>, // = TMethods & Cons,
7
- TypeofClass extends typeof ComponentMethods<object, object>>(Methods: Class, state: InstanceType<Class>['state'], props: InstanceType<Class>['props']) => InstanceType<Class>;
6
+ type UseMethods = <Class extends typeof ComponentMethods<object, object>>(Methods: Class & Constructor<InstanceType<Class>>, props: InstanceType<Class>['props'], state: InstanceType<Class>['state']) => InstanceType<Class>;
8
7
  export declare const useMethods: UseMethods;
9
8
  export {};
@@ -60,7 +60,7 @@ var ComponentMethods = /** @class */ (function () {
60
60
  }());
61
61
  exports.ComponentMethods = ComponentMethods;
62
62
  ;
63
- var useMethods = function (Methods, state, props) {
63
+ var useMethods = function (Methods, props, state) {
64
64
  // @todo Switch to useRef. Vite HMR seems to sometimes reinitialize useMemo calls after a hot update,
65
65
  // causing the instance to be unexpectedly recreated in the middle of the components lifecycle.
66
66
  // But useRef and useState values appear to always be preserved whenever this happens.
@@ -69,8 +69,8 @@ var useMethods = function (Methods, state, props) {
69
69
  // See useLogic implementation for a discussion of this type assertion.
70
70
  return new Methods();
71
71
  }, [])).current;
72
- methods.state = state;
73
72
  methods.props = props;
73
+ methods.state = state;
74
74
  return methods;
75
75
  };
76
76
  exports.useMethods = useMethods;
@@ -92,7 +92,7 @@ testing: {
92
92
  return [4 /*yield*/, import('./state.js')];
93
93
  case 1:
94
94
  useCleanState = (_a.sent()).useCleanState;
95
- self = (0, exports.useMethods)(MyMethods, useCleanState({}), {});
95
+ self = (0, exports.useMethods)(MyMethods, {}, useCleanState({}));
96
96
  return [2 /*return*/];
97
97
  }
98
98
  });
@@ -6,8 +6,9 @@ type o = object;
6
6
  export interface IComponentClass<Instance extends ClassComponent<o, o, o> = ClassComponent, Params extends ComponentClassParams = ComponentClassParams> extends IComponentInstanceClass<Instance, Params> {
7
7
  }
8
8
  type Extractor = <TComponent extends typeof ClassComponent<o, o, o>>(this: NonNullable<typeof _Component>, _Component?: TComponent & IComponentClass<InstanceType<TComponent>>) => VoidFunctionComponent<InstanceType<TComponent>['props']>;
9
- export declare class ClassComponent<TState extends object = EmptyObject, TProps extends object = EmptyObject, THooks extends object = EmptyObject> extends ComponentInstance<TState, TProps, THooks> {
9
+ export declare class ClassComponent<TProps extends o = EmptyObject, TState extends o = EmptyObject, THooks extends o = EmptyObject> extends ComponentInstance<TState, TProps, THooks> {
10
10
  Render: VoidFunctionComponent<{}>;
11
+ readonly forceUpdate: VoidFunction;
11
12
  static renderAs: 'component' | 'template';
12
13
  static readonly FC: Extractor;
13
14
  }
@@ -32,6 +32,15 @@ var setFunctionName = function (func, newName) {
32
32
  console.warn(error);
33
33
  }
34
34
  };
35
+ var useRerender = function () {
36
+ /*
37
+ * Skip the value, we don't need it.
38
+ * Grab just the setter function.
39
+ */
40
+ var _a = (0, react_1.useState)(Date.now()), _forceRerender = _a[1];
41
+ var rerender = function () { return _forceRerender(Date.now()); };
42
+ return rerender;
43
+ };
35
44
  ;
36
45
  var ClassComponent = /** @class */ (function (_super) {
37
46
  __extends(ClassComponent, _super);
@@ -45,7 +54,12 @@ var ClassComponent = /** @class */ (function (_super) {
45
54
  if (!Component.getInitialState || !isClassComponentType)
46
55
  throw new Error('Attempted to initialize ClassComponent with invalid Class type. Either pass a class that extends ClassComponent to FC (e.g `export FC(MyComponent);`), or ensure it is called as a method on a ClassComponent constructor type (e.g `export MyComponent.FC()`).');
47
56
  var Wrapper = function (props, context) {
48
- var Render = (0, instance_1.useInstance)(Component, props).Render;
57
+ var instance = (0, instance_1.useInstance)(Component, props);
58
+ var Render = instance.Render;
59
+ var _forceUpdate;
60
+ // @ts-expect-error (Cannot assign to 'forceUpdate' because it is a read-only property.ts(2540))
61
+ instance.forceUpdate = (_forceUpdate = useRerender() // Moved this to separate line to allow TS errors. Use proxy local variable to regain some type checking for the assignment.
62
+ );
49
63
  // Add calling component name to Render function name in stack traces.
50
64
  (0, react_1.useMemo)(function () { return setFunctionName(Render, "".concat(Component.name, ".Render")); }, [Render]);
51
65
  /**
@@ -3,7 +3,7 @@ type AsyncAllowedEffectCallback = () => Awaitable<IVoidFunction>;
3
3
  type UseMountCallbacks = <TInstance extends ComponentInstance<any, any, any>>(instance: TInstance) => void;
4
4
  export declare const useMountCallbacks: UseMountCallbacks;
5
5
  export declare const noOp: () => void;
6
- export declare class ComponentInstance<TState extends object = EmptyObject, TProps extends object = EmptyObject, THooks extends object = EmptyObject> extends ComponentLogic<TState, TProps, THooks> {
6
+ export declare class ComponentInstance<TProps extends o = EmptyObject, TState extends o = EmptyObject, THooks extends o = EmptyObject> extends ComponentLogic<TState, TProps, THooks> {
7
7
  /**
8
8
  * Runs only _before_ first render, i.e before the component instance is mounted.
9
9
  * Useful for logic that is involved in determining what to render.
@@ -1,18 +1,17 @@
1
1
  import type { TCleanState, TState } from '../base/state';
2
2
  export type Empty = EmptyObject;
3
3
  type o = object;
4
- export declare class ComponentLogic<TState extends o = Empty, TProps extends o = Empty, THooks extends o = Empty> {
4
+ export declare class ComponentLogic<TProps extends o = Empty, TState extends o = Empty, THooks extends o = Empty> {
5
5
  state: TCleanState<TState>;
6
6
  props: TProps;
7
7
  hooks: THooks;
8
- static getInitialState: TBaseComponentLogic['getInitialState'];
8
+ static getInitialState: (p?: o) => {};
9
9
  useHooks?: () => THooks;
10
10
  }
11
11
  type LogicClassParams = ConstructorParameters<typeof ComponentLogic>;
12
12
  export interface IComponentLogicClass<Instance extends ComponentLogic<o, o, o> = ComponentLogic, Params extends LogicClassParams = LogicClassParams> extends Constructor<Instance, Params> {
13
13
  getInitialState: (props?: Instance['props']) => TState<Instance['state']>;
14
14
  }
15
- export type TBaseComponentLogic = IComponentLogicClass<ComponentLogic<any, any, any>>;
16
- type UseLogic = <Class extends TypeofClass & IComponentLogicClass<InstanceType<TypeofClass>>, TypeofClass extends typeof ComponentLogic<o, o, o>>(Methods: Class, ...props: valueof<InstanceType<Class>['props']> extends never ? ([] | [EmptyObject] | [InstanceType<Class>['props']]) : [InstanceType<Class>['props']]) => InstanceType<Class>;
15
+ 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>;
17
16
  export declare const useLogic: UseLogic;
18
17
  export {};
@@ -14,21 +14,20 @@ var __extends = (this && this.__extends) || (function () {
14
14
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
15
  };
16
16
  })();
17
- var _a;
18
17
  Object.defineProperty(exports, "__esModule", { value: true });
19
18
  exports.useLogic = exports.ComponentLogic = void 0;
20
19
  var react_1 = require("react");
21
20
  var state_1 = require("../base/state");
22
- // React.Component
23
21
  var ComponentLogic = /** @class */ (function () {
24
22
  function ComponentLogic() {
25
23
  }
24
+ ComponentLogic.getInitialState = function (p) { return ({}); };
26
25
  return ComponentLogic;
27
26
  }());
28
27
  exports.ComponentLogic = ComponentLogic;
29
28
  ;
30
29
  var useLogic = function (Methods, props) {
31
- var _b, _c;
30
+ var _a, _b;
32
31
  if (props === void 0) { props = {}; }
33
32
  var state = (0, state_1.useCleanState)(Methods.getInitialState, props);
34
33
  var methods = (0, react_1.useRef)((0, react_1.useMemo)(function () {
@@ -36,7 +35,7 @@ var useLogic = function (Methods, props) {
36
35
  }, [])).current;
37
36
  methods.state = state;
38
37
  methods.props = props;
39
- methods.hooks = (_c = (_b = methods.useHooks) === null || _b === void 0 ? void 0 : _b.call(methods)) !== null && _c !== void 0 ? _c : {};
38
+ methods.hooks = (_b = (_a = methods.useHooks) === null || _a === void 0 ? void 0 : _a.call(methods)) !== null && _b !== void 0 ? _b : {};
40
39
  return methods;
41
40
  };
42
41
  exports.useLogic = useLogic;
@@ -45,28 +44,30 @@ testing: {
45
44
  var MyComponentLogic = /** @class */ (function (_super) {
46
45
  __extends(MyComponentLogic, _super);
47
46
  function MyComponentLogic() {
48
- return _super !== null && _super.apply(this, arguments) || this;
47
+ var _this = _super !== null && _super.apply(this, arguments) || this;
48
+ _this.b = _this.state.a;
49
+ return _this;
49
50
  }
50
- MyComponentLogic.getInitialState = function () { return ({}); };
51
+ MyComponentLogic.getInitialState = function (p) { return ({ a: '' }); };
51
52
  return MyComponentLogic;
52
53
  }(ComponentLogic));
53
54
  ;
54
- var self_1 = (0, exports.useLogic)(MyComponentLogic);
55
+ MyComponentLogic.getInitialState;
56
+ var self_1 = (0, exports.useLogic)(MyComponentLogic, {});
55
57
  }
56
58
  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);
59
+ var A = /** @class */ (function (_super) {
60
+ __extends(C, _super);
61
+ function C() {
62
+ var _this = _super !== null && _super.apply(this, arguments) || this;
63
+ // static getInitialState = () => ({a: 'l'});
64
+ _this.b = _this.state.a;
65
+ return _this;
66
+ }
67
+ return C;
68
+ }(ComponentLogic));
68
69
  A.getInitialState();
69
- // const self = useLogic(A);
70
+ var self_2 = (0, exports.useLogic)(A);
70
71
  }
71
72
  // export type ComponentClassStatics<Instance extends ComponentLogic<object, object, object>> = {
72
73
  // getInitialState: (props?: Instance['props']) => TState<Instance['state']>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleanweb/react",
3
- "version": "1.1.1-beta.4",
3
+ "version": "1.1.1-beta.6",
4
4
  "description": "A suite of helpers for writing cleaner React function components.",
5
5
  "engines": {
6
6
  "node": ">=18"