@cleanweb/react 1.1.1-beta.7 → 1.1.1-beta.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/base/methods.d.ts +2 -2
- package/build/base/methods.js +0 -1
- package/build/base/state.d.ts +9 -7
- package/build/base/state.js +3 -7
- package/build/classy/class.d.ts +5 -3
- package/build/classy/class.js +27 -12
- package/build/classy/instance.d.ts +7 -3
- package/build/classy/instance.js +14 -13
- package/build/classy/logic.d.ts +11 -8
- package/build/classy/logic.js +12 -8
- package/build/globals.d.ts +1 -3
- package/package.json +1 -1
package/build/base/methods.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import type { TCleanState } from './state';
|
2
|
-
export declare class ComponentMethods<TProps extends object, TState extends
|
1
|
+
import type { TCleanState, TStateData } from './state';
|
2
|
+
export declare class ComponentMethods<TProps extends object, TState extends TStateData> {
|
3
3
|
props: TProps;
|
4
4
|
state: TCleanState<TState>;
|
5
5
|
}
|
package/build/base/methods.js
CHANGED
@@ -66,7 +66,6 @@ var useMethods = function (Methods, props, state) {
|
|
66
66
|
// But useRef and useState values appear to always be preserved whenever this happens.
|
67
67
|
// So those two are the only cross-render-persistence methods we can consider safe.
|
68
68
|
var methods = (0, react_1.useRef)((0, react_1.useMemo)(function () {
|
69
|
-
// See useLogic implementation for a discussion of this type assertion.
|
70
69
|
return new Methods();
|
71
70
|
}, [])).current;
|
72
71
|
methods.props = props;
|
package/build/base/state.d.ts
CHANGED
@@ -7,8 +7,8 @@ type PutState<TState extends object> = {
|
|
7
7
|
[Key in keyof TState]: React.Dispatch<React.SetStateAction<TState[Key]>>;
|
8
8
|
};
|
9
9
|
declare class CleanStateBase<TState extends Record<string, any>> {
|
10
|
-
reservedKeys: string[];
|
11
|
-
valueKeys: string[];
|
10
|
+
readonly reservedKeys: string[];
|
11
|
+
readonly valueKeys: string[];
|
12
12
|
private _values_;
|
13
13
|
private _initialValues_;
|
14
14
|
private _setters_;
|
@@ -16,14 +16,16 @@ declare class CleanStateBase<TState extends Record<string, any>> {
|
|
16
16
|
static update: <TState_1 extends object>(this: CleanStateBase<TState_1>) => void;
|
17
17
|
get put(): PutState<TState>;
|
18
18
|
get initialState(): TState;
|
19
|
-
putMany: (newValues: Partial<TState>) => void;
|
19
|
+
readonly putMany: (newValues: Partial<TState>) => void;
|
20
20
|
}
|
21
|
-
type
|
22
|
-
|
23
|
-
|
21
|
+
export type TStateData = object & {
|
22
|
+
[Key in keyof CleanStateBase<{}>]?: never;
|
23
|
+
};
|
24
|
+
export type TCleanState<TState extends TStateData> = (CleanStateBase<TState> & Omit<TState, keyof CleanStateBase<{}>>);
|
25
|
+
export type ExtractCleanStateData<YourCleanState extends CleanStateBase<{}>> = Omit<YourCleanState, keyof CleanStateBase<{}>>;
|
24
26
|
type StateInitFunction = (...args: any[]) => object;
|
25
27
|
type StateInit = object | StateInitFunction;
|
26
28
|
type TInitialState<Initializer extends StateInit> = Initializer extends (...args: any[]) => (infer TState extends object) ? TState : Initializer;
|
27
|
-
type TUseCleanState = <TInit extends StateInit>(_initialState: TInit, ...props: TInit extends (...args: infer TProps extends any[]) => (infer TState extends object) ? TProps : []) =>
|
29
|
+
type TUseCleanState = <TInit extends StateInit>(_initialState: TInit, ...props: TInit extends (...args: infer TProps extends any[]) => (infer TState extends object) ? TProps : []) => TCleanState<TInitialState<TInit>>;
|
28
30
|
export declare const useCleanState: TUseCleanState;
|
29
31
|
export {};
|
package/build/base/state.js
CHANGED
@@ -122,17 +122,13 @@ var useCleanState = function (_initialState) {
|
|
122
122
|
for (var _i = 1; _i < arguments.length; _i++) {
|
123
123
|
props[_i - 1] = arguments[_i];
|
124
124
|
}
|
125
|
-
var mounted = (0, exports.useMountState)();
|
126
125
|
var initialState = typeof _initialState === 'function'
|
127
126
|
? (0, react_1.useMemo)(function () { return _initialState.apply(void 0, props); }, [])
|
128
127
|
: _initialState;
|
129
128
|
;
|
130
|
-
var
|
131
|
-
|
132
|
-
|
133
|
-
if (!freshInstance.put)
|
134
|
-
throw new Error('useCleanState failed to initialized a state instance.');
|
135
|
-
var cleanState = (0, react_1.useRef)(freshInstance).current;
|
129
|
+
var cleanState = (0, react_1.useRef)((0, react_1.useMemo)(function () {
|
130
|
+
return new CleanState(initialState);
|
131
|
+
}, [])).current;
|
136
132
|
CleanState.update.call(cleanState);
|
137
133
|
return cleanState;
|
138
134
|
};
|
package/build/classy/class.d.ts
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
import type { VoidFunctionComponent } from 'react';
|
2
2
|
import type { IComponentInstanceClass } from './instance';
|
3
|
+
import type { TStateData } from '../base';
|
3
4
|
import { ComponentInstance } from './instance';
|
4
5
|
type ComponentClassParams = ConstructorParameters<typeof ClassComponent>;
|
5
6
|
type o = object;
|
6
7
|
export interface IComponentClass<Instance extends ClassComponent<o, o, o> = ClassComponent, Params extends ComponentClassParams = ComponentClassParams> extends IComponentInstanceClass<Instance, Params> {
|
7
8
|
}
|
8
9
|
type Extractor = <TComponent extends typeof ClassComponent<o, o, o>>(this: NonNullable<typeof _Component>, _Component?: TComponent & IComponentClass<InstanceType<TComponent>>) => VoidFunctionComponent<InstanceType<TComponent>['props']>;
|
9
|
-
|
10
|
-
|
10
|
+
type ReactTemplate = React.JSX.Element | null;
|
11
|
+
export declare class ClassComponent<TProps extends o = EmptyObject, TState extends TStateData = EmptyObject, THooks extends o = EmptyObject> extends ComponentInstance<TProps, TState, THooks> {
|
12
|
+
Render?: VoidFunctionComponent<{}>;
|
13
|
+
template?: ReactTemplate | (() => ReactTemplate);
|
11
14
|
readonly forceUpdate: VoidFunction;
|
12
|
-
static renderAs: 'component' | 'template';
|
13
15
|
static readonly FC: Extractor;
|
14
16
|
}
|
15
17
|
interface HookWrapperProps<THookFunction extends AnyFunction> {
|
package/build/classy/class.js
CHANGED
@@ -47,21 +47,25 @@ var ClassComponent = /** @class */ (function (_super) {
|
|
47
47
|
function ClassComponent() {
|
48
48
|
return _super !== null && _super.apply(this, arguments) || this;
|
49
49
|
}
|
50
|
-
ClassComponent.renderAs = 'component';
|
51
50
|
ClassComponent.FC = function FC(_Component) {
|
52
51
|
var Component = _Component !== null && _Component !== void 0 ? _Component : this;
|
53
52
|
var isClassComponentType = Component.prototype instanceof ClassComponent;
|
54
53
|
if (!Component.getInitialState || !isClassComponentType)
|
55
54
|
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()`).');
|
56
|
-
var Wrapper = function (props
|
55
|
+
var Wrapper = function (props) {
|
57
56
|
var instance = (0, instance_1.useInstance)(Component, props);
|
58
|
-
var Render = instance.Render;
|
57
|
+
var Render = instance.Render, template = instance.template;
|
59
58
|
var _forceUpdate;
|
60
59
|
// @ts-expect-error (Cannot assign to 'forceUpdate' because it is a read-only property.ts(2540))
|
61
60
|
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
61
|
);
|
63
62
|
// Add calling component name to Render function name in stack traces.
|
64
|
-
(0, react_1.useMemo)(function () {
|
63
|
+
(0, react_1.useMemo)(function () {
|
64
|
+
if (typeof template === 'function')
|
65
|
+
setFunctionName(template, "".concat(Component.name, ".template"));
|
66
|
+
else if (typeof Render === 'function')
|
67
|
+
setFunctionName(Render, "".concat(Component.name, ".Render"));
|
68
|
+
}, [Render, template]);
|
65
69
|
/**
|
66
70
|
* Normally a component can update it's own state in the "before-render" stage to
|
67
71
|
* skip DOM updates and trigger and immediate rerun of the rendering with the new state.
|
@@ -80,15 +84,25 @@ var ClassComponent = /** @class */ (function (_super) {
|
|
80
84
|
* of doing it directly in `Render`. This might mean cleaner Render functions,
|
81
85
|
* so there's probably no real value lost if we keep the component boundary.
|
82
86
|
**/
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
87
|
+
switch (typeof template) {
|
88
|
+
case 'undefined':
|
89
|
+
if (typeof Render === 'function')
|
90
|
+
return (0, jsx_runtime_1.jsx)(Render, {});
|
91
|
+
else
|
92
|
+
throw new Error([
|
93
|
+
'A ClassComponent must have either a `template` or a `Render` property. But neither was found.',
|
94
|
+
'Add a `template` member to your class and assign a valid (JSX.Element | null) to it. (or a function that returns that).',
|
95
|
+
'Alternatively, add a `Render` method and assign a FunctionComponent to it.',
|
96
|
+
'\n\n',
|
97
|
+
'Expected `Render` to be a Function Component because `template` was `undefined`.',
|
98
|
+
"Instead got the following '".concat(typeof Render, "': $o"),
|
99
|
+
].join(' '), Render);
|
100
|
+
case 'function': return template();
|
101
|
+
default: return template;
|
102
|
+
}
|
89
103
|
};
|
90
104
|
// Include calling component name in wrapper function name on stack traces.
|
91
|
-
setFunctionName(Wrapper, "".concat(Component.name, "
|
105
|
+
setFunctionName(Wrapper, "$".concat(Component.name, "$"));
|
92
106
|
return Wrapper;
|
93
107
|
};
|
94
108
|
return ClassComponent;
|
@@ -110,8 +124,9 @@ testing: {
|
|
110
124
|
function MyComponentLogic() {
|
111
125
|
return _super !== null && _super.apply(this, arguments) || this;
|
112
126
|
}
|
113
|
-
MyComponentLogic.getInitialState = function () { return ({}); };
|
127
|
+
MyComponentLogic.getInitialState = function () { return ({ a: '' }); };
|
114
128
|
return MyComponentLogic;
|
115
129
|
}(ClassComponent));
|
116
130
|
;
|
131
|
+
var Template = MyComponentLogic.FC();
|
117
132
|
}
|
@@ -1,9 +1,10 @@
|
|
1
|
+
import { TStateData } from '../base/state';
|
1
2
|
import { ComponentLogic, IComponentLogicClass } from './logic';
|
2
3
|
type AsyncAllowedEffectCallback = () => Awaitable<IVoidFunction>;
|
3
4
|
type UseMountCallbacks = <TInstance extends ComponentInstance<any, any, any>>(instance: TInstance) => void;
|
4
5
|
export declare const useMountCallbacks: UseMountCallbacks;
|
5
6
|
export declare const noOp: () => void;
|
6
|
-
export declare class ComponentInstance<TProps extends o = EmptyObject, TState extends
|
7
|
+
export declare class ComponentInstance<TProps extends o = EmptyObject, TState extends TStateData = EmptyObject, THooks extends o = EmptyObject> extends ComponentLogic<TProps, TState, THooks> {
|
7
8
|
/**
|
8
9
|
* Runs only _before_ first render, i.e before the component instance is mounted.
|
9
10
|
* Useful for logic that is involved in determining what to render.
|
@@ -52,6 +53,9 @@ type o = object;
|
|
52
53
|
type InstanceClassParams = ConstructorParameters<typeof ComponentInstance<o, o, o>>;
|
53
54
|
export interface IComponentInstanceClass<Instance extends ComponentInstance<o, o, o> = ComponentInstance, Params extends InstanceClassParams = InstanceClassParams> extends IComponentLogicClass<Instance, Params> {
|
54
55
|
}
|
55
|
-
type
|
56
|
-
|
56
|
+
type UseInstance2 = {
|
57
|
+
<Class extends typeof ComponentInstance<EmptyObject, o, o>>(Methods: Class & IComponentInstanceClass<InstanceType<Class>>, ...props: ([] | [EmptyObject])): InstanceType<Class>;
|
58
|
+
<Class extends typeof ComponentInstance<o, o, o>>(Methods: Class & IComponentInstanceClass<InstanceType<Class>>, ...props: [InstanceType<Class>['props']]): InstanceType<Class>;
|
59
|
+
};
|
60
|
+
export declare const useInstance: UseInstance2;
|
57
61
|
export {};
|
package/build/classy/instance.js
CHANGED
@@ -14,15 +14,6 @@ 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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
18
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
19
|
-
if (ar || !(i in from)) {
|
20
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
21
|
-
ar[i] = from[i];
|
22
|
-
}
|
23
|
-
}
|
24
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
25
|
-
};
|
26
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
27
18
|
exports.useInstance = exports.ComponentInstance = exports.noOp = exports.useMountCallbacks = void 0;
|
28
19
|
var react_1 = require("react");
|
@@ -118,14 +109,15 @@ exports.ComponentInstance = ComponentInstance;
|
|
118
109
|
* the second param is given `{}` as a default follow to account for the empty tuple case. TypeScript
|
119
110
|
* just wants us to use the rest parameter explicitly by force.
|
120
111
|
*/
|
121
|
-
var useInstance = function (
|
112
|
+
var useInstance = function () {
|
122
113
|
var _a;
|
123
114
|
var args = [];
|
124
|
-
for (var _i =
|
125
|
-
args[_i
|
115
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
116
|
+
args[_i] = arguments[_i];
|
126
117
|
}
|
118
|
+
var Component = args[0], _b = args[1], props = _b === void 0 ? {} : _b;
|
127
119
|
// useHooks.
|
128
|
-
var instance =
|
120
|
+
var instance = (0, logic_1.useLogic)(Component, props); // Must spread rest parameter, rather than passing a single `props` argument directly.
|
129
121
|
/**
|
130
122
|
* Argument of type '
|
131
123
|
* [
|
@@ -158,3 +150,12 @@ var useInstance = function (Component) {
|
|
158
150
|
return instance;
|
159
151
|
};
|
160
152
|
exports.useInstance = useInstance;
|
153
|
+
testing: {
|
154
|
+
var A = /** @class */ (function (_super) {
|
155
|
+
__extends(A, _super);
|
156
|
+
function A() {
|
157
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
158
|
+
}
|
159
|
+
return A;
|
160
|
+
}(ComponentInstance));
|
161
|
+
}
|
package/build/classy/logic.d.ts
CHANGED
@@ -1,17 +1,20 @@
|
|
1
|
-
import type { TCleanState,
|
1
|
+
import type { TCleanState, ExtractCleanStateData, TStateData } from '../base/state';
|
2
2
|
export type Empty = EmptyObject;
|
3
3
|
type o = object;
|
4
|
-
export declare class ComponentLogic<TProps extends o = Empty, TState extends
|
4
|
+
export declare class ComponentLogic<TProps extends o = Empty, TState extends TStateData = Empty, THooks extends o = Empty> {
|
5
5
|
state: TCleanState<TState>;
|
6
6
|
props: TProps;
|
7
7
|
hooks: THooks;
|
8
|
-
static getInitialState: (p?:
|
9
|
-
useHooks
|
8
|
+
static getInitialState: (p?: object) => {};
|
9
|
+
useHooks: THooks extends Empty ? undefined | (() => Empty | undefined) : () => 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
|
-
getInitialState: (props?: Instance['props']) =>
|
13
|
+
getInitialState: (props?: Instance['props']) => ExtractCleanStateData<Instance['state']>;
|
14
14
|
}
|
15
|
-
type
|
16
|
-
|
17
|
-
|
15
|
+
type UseLogic2 = {
|
16
|
+
<Class extends typeof ComponentLogic<Empty, o, o>>(Methods: Class & IComponentLogicClass<InstanceType<Class>>, ...props: ([] | [EmptyObject])): InstanceType<Class>;
|
17
|
+
<Class extends typeof ComponentLogic<o, o, o>>(Methods: Class & IComponentLogicClass<InstanceType<Class>>, ...props: [InstanceType<Class>['props']]): InstanceType<Class>;
|
18
|
+
};
|
19
|
+
declare const useLogic: UseLogic2;
|
20
|
+
export { useLogic };
|
package/build/classy/logic.js
CHANGED
@@ -14,6 +14,7 @@ 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;
|
17
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
18
19
|
exports.useLogic = exports.ComponentLogic = void 0;
|
19
20
|
var react_1 = require("react");
|
@@ -26,9 +27,13 @@ var ComponentLogic = /** @class */ (function () {
|
|
26
27
|
}());
|
27
28
|
exports.ComponentLogic = ComponentLogic;
|
28
29
|
;
|
29
|
-
var useLogic = function (
|
30
|
+
var useLogic = function () {
|
30
31
|
var _a, _b;
|
31
|
-
|
32
|
+
var args = [];
|
33
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
34
|
+
args[_i] = arguments[_i];
|
35
|
+
}
|
36
|
+
var Methods = args[0], _c = args[1], props = _c === void 0 ? {} : _c;
|
32
37
|
var state = (0, state_1.useCleanState)(Methods.getInitialState, props);
|
33
38
|
var methods = (0, react_1.useRef)((0, react_1.useMemo)(function () {
|
34
39
|
return new Methods();
|
@@ -53,21 +58,20 @@ testing: {
|
|
53
58
|
}(ComponentLogic));
|
54
59
|
;
|
55
60
|
MyComponentLogic.getInitialState;
|
56
|
-
var self_1 =
|
61
|
+
var self_1 = useLogic(MyComponentLogic);
|
57
62
|
}
|
58
63
|
testing: {
|
59
64
|
var A = /** @class */ (function (_super) {
|
60
65
|
__extends(C, _super);
|
61
66
|
function C() {
|
62
|
-
|
63
|
-
// static getInitialState = () => ({a: 'l'});
|
64
|
-
_this.b = _this.state.a;
|
65
|
-
return _this;
|
67
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
66
68
|
}
|
67
69
|
return C;
|
68
70
|
}(ComponentLogic));
|
69
71
|
A.getInitialState();
|
70
|
-
|
72
|
+
// const oa = {['a' as unknown as symbol]: 'boo'};
|
73
|
+
var oa = (_a = {}, _a['a'] = 'boo', _a);
|
74
|
+
// const self = useLogic(A, oa);
|
71
75
|
}
|
72
76
|
// export type ComponentClassStatics<Instance extends ComponentLogic<object, object, object>> = {
|
73
77
|
// getInitialState: (props?: Instance['props']) => TState<Instance['state']>;
|
package/build/globals.d.ts
CHANGED
@@ -57,9 +57,7 @@ declare global {
|
|
57
57
|
''?: never;
|
58
58
|
};
|
59
59
|
type TEmptyObject2 = Record<symbol, never>;
|
60
|
-
type EmptyObject =
|
61
|
-
[key: keyof any]: never;
|
62
|
-
};
|
60
|
+
type EmptyObject = __FromPrivateHelpers['EmptyObject2'];
|
63
61
|
type EmptyObject2 = __FromPrivateHelpers['EmptyObject2'];
|
64
62
|
type EmptyObject3 = __FromPrivateHelpers['EmptyObject3'];
|
65
63
|
type valueof<TObject> = TObject[keyof TObject];
|