@expressive/react 0.70.0 → 0.72.0
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/dist/adapter.d.ts +60 -60
- package/dist/adapter.js +122 -106
- package/dist/adapter.js.map +1 -1
- package/dist/esm/adapter.js +66 -87
- package/dist/esm/adapter.js.map +1 -1
- package/dist/esm/chunk-4MR23TOU.js +45 -0
- package/dist/esm/chunk-4MR23TOU.js.map +1 -0
- package/dist/esm/chunk-UIHA4AT6.js +58 -0
- package/dist/esm/chunk-UIHA4AT6.js.map +1 -0
- package/dist/esm/index.js +20 -22
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/jsx-dev-runtime.js +2 -2
- package/dist/esm/jsx-dev-runtime.js.map +1 -1
- package/dist/esm/jsx-runtime.js +14 -24
- package/dist/esm/jsx-runtime.js.map +1 -1
- package/dist/index.d.ts +16 -10
- package/dist/index.js +23 -39
- package/dist/index.js.map +1 -1
- package/dist/jsx-dev-runtime.d.ts +2 -2
- package/dist/jsx-dev-runtime.js +1 -1
- package/dist/jsx-dev-runtime.js.map +1 -1
- package/dist/jsx-runtime.d.ts +9 -36
- package/dist/jsx-runtime.js +71 -34
- package/dist/jsx-runtime.js.map +1 -1
- package/package.json +8 -6
- package/dist/esm/context.js +0 -40
- package/dist/esm/context.js.map +0 -1
package/dist/adapter.d.ts
CHANGED
|
@@ -1,20 +1,59 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import React__default, { FunctionComponent } from 'react';
|
|
1
|
+
import { FunctionComponent, ReactNode } from 'react';
|
|
3
2
|
|
|
4
3
|
declare module '@expressive/mvc' {
|
|
5
|
-
namespace
|
|
6
|
-
|
|
4
|
+
namespace State {
|
|
5
|
+
type HasProps<T extends State> = {
|
|
6
|
+
[K in Exclude<keyof T, keyof State>]?: T[K];
|
|
7
|
+
};
|
|
8
|
+
type ComponentProps<T extends State> = HasProps<T> & {
|
|
9
|
+
/**
|
|
10
|
+
* Callback for newly created instance. Only called once.
|
|
11
|
+
* @returns Callback to run when instance is destroyed.
|
|
12
|
+
*/
|
|
13
|
+
is?: (instance: T) => void;
|
|
14
|
+
/**
|
|
15
|
+
* A fallback react tree to show when suspended.
|
|
16
|
+
* If not provided, `fallback` property of the State will be used.
|
|
17
|
+
*/
|
|
18
|
+
fallback?: React.ReactNode;
|
|
19
|
+
};
|
|
20
|
+
type Props<T extends State> = T extends {
|
|
21
|
+
render(props: infer P, self: any): any;
|
|
22
|
+
} ? ComponentProps<T> & Omit<P, keyof AsComponent> : ComponentProps<T> & {
|
|
23
|
+
children?: React.ReactNode;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Props which will not conflict with a State's use as a Component.
|
|
27
|
+
*
|
|
28
|
+
* Built-in properties must be optional, as they will always be omitted.
|
|
29
|
+
*/
|
|
30
|
+
type RenderProps<T extends State> = HasProps<T> & {
|
|
31
|
+
is?: never;
|
|
32
|
+
get?: never;
|
|
33
|
+
set?: never;
|
|
34
|
+
};
|
|
35
|
+
/** State which is not incompatable as Component in React. */
|
|
36
|
+
interface AsComponent extends State.Valid {
|
|
37
|
+
render?(props: RenderProps<this>, self: this): React.ReactNode;
|
|
38
|
+
fallback?: React.ReactNode;
|
|
39
|
+
}
|
|
40
|
+
interface FC<T extends State, P extends State.Assign<T>> extends FunctionComponent<P & State.Props<T>> {
|
|
7
41
|
displayName?: string;
|
|
8
|
-
|
|
42
|
+
State: State.Extends<T>;
|
|
9
43
|
}
|
|
10
|
-
function as<T extends
|
|
44
|
+
function as<T extends State, P extends State.Assign<T>>(this: State.New<T>, render: (props: P, self: T) => ReactNode): FC<T, P>;
|
|
11
45
|
}
|
|
12
46
|
}
|
|
13
47
|
|
|
14
48
|
declare module '@expressive/mvc' {
|
|
15
|
-
namespace
|
|
16
|
-
|
|
17
|
-
|
|
49
|
+
namespace State {
|
|
50
|
+
interface Valid {
|
|
51
|
+
use?(...props: any[]): Promise<void> | void;
|
|
52
|
+
}
|
|
53
|
+
type UseArgs<T extends State> = T extends {
|
|
54
|
+
use(...props: infer P): any;
|
|
55
|
+
} ? P : State.Args<T>;
|
|
56
|
+
function use<T extends State>(this: New<T>, ...args: UseArgs<T>): T;
|
|
18
57
|
}
|
|
19
58
|
}
|
|
20
59
|
|
|
@@ -42,64 +81,25 @@ type ForceRefresh = {
|
|
|
42
81
|
<T = void>(invoke: () => Promise<T>): Promise<T>;
|
|
43
82
|
};
|
|
44
83
|
declare module '@expressive/mvc' {
|
|
45
|
-
namespace
|
|
46
|
-
type GetFactory<T extends
|
|
47
|
-
type GetEffect<T extends
|
|
84
|
+
namespace State {
|
|
85
|
+
type GetFactory<T extends State, R> = (this: T, current: T, refresh: ForceRefresh) => R;
|
|
86
|
+
type GetEffect<T extends State> = (this: T, current: T, refresh: ForceRefresh) => null;
|
|
48
87
|
/** Fetch instance of this class from context. */
|
|
49
|
-
function get<T extends
|
|
88
|
+
function get<T extends State>(this: Extends<T>): T;
|
|
50
89
|
/** Fetch instance of this class optionally. */
|
|
51
|
-
function get<T extends
|
|
90
|
+
function get<T extends State>(this: Extends<T>, required: false): T | undefined;
|
|
52
91
|
/** Fetch instance of this class from context. */
|
|
53
|
-
function get<T extends
|
|
54
|
-
function get<T extends
|
|
55
|
-
function get<T extends
|
|
92
|
+
function get<T extends State>(this: Extends<T>, requireValues: true): Required<T>;
|
|
93
|
+
function get<T extends State, R>(this: Extends<T>, factory: GetFactory<T, Promise<R> | R>): NoVoid<R>;
|
|
94
|
+
function get<T extends State>(this: Extends<T>, factory: GetEffect<T>): null;
|
|
56
95
|
}
|
|
57
96
|
}
|
|
58
97
|
|
|
59
|
-
declare
|
|
60
|
-
namespace Model {
|
|
61
|
-
type HasProps<T extends Model> = {
|
|
62
|
-
[K in Exclude<keyof T, keyof Model>]?: T[K];
|
|
63
|
-
};
|
|
64
|
-
/**
|
|
65
|
-
* Props which will not conflict with a Model's use as a Component.
|
|
66
|
-
*
|
|
67
|
-
* Built-in properties must be optional, as they will always be omitted.
|
|
68
|
-
*/
|
|
69
|
-
type RenderProps<T extends Model> = HasProps<T> & {
|
|
70
|
-
is?: undefined;
|
|
71
|
-
get?: undefined;
|
|
72
|
-
set?: undefined;
|
|
73
|
-
};
|
|
74
|
-
/** Model which is not incompatable as Component in React. */
|
|
75
|
-
interface Compat extends Model {
|
|
76
|
-
render?(props: RenderProps<this>, self: this): React.ReactNode;
|
|
77
|
-
fallback?: React.ReactNode;
|
|
78
|
-
}
|
|
79
|
-
interface BaseProps<T extends Model> {
|
|
80
|
-
/**
|
|
81
|
-
* Callback for newly created instance. Only called once.
|
|
82
|
-
* @returns Callback to run when instance is destroyed.
|
|
83
|
-
*/
|
|
84
|
-
is?: (instance: T) => void | (() => void);
|
|
85
|
-
render?(props: HasProps<T>, self: T): React.ReactNode;
|
|
86
|
-
/**
|
|
87
|
-
* A fallback react tree to show when suspended.
|
|
88
|
-
* If not provided, `fallback` property of the Model will be used.
|
|
89
|
-
*/
|
|
90
|
-
fallback?: React.ReactNode;
|
|
91
|
-
}
|
|
92
|
-
type Props<T extends Model> = T extends {
|
|
93
|
-
render(props: infer P, self: any): any;
|
|
94
|
-
} ? BaseProps<T> & HasProps<T> & Omit<P, keyof Model> : BaseProps<T> & HasProps<T> & {
|
|
95
|
-
children?: React.ReactNode;
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
}
|
|
98
|
+
declare namespace Pragma { }
|
|
99
99
|
declare const Pragma: {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
100
|
+
useState<S>(initial: () => S): [S, (next: (previous: S) => S) => void];
|
|
101
|
+
useEffect(effect: () => () => void, deps?: any[]): void;
|
|
102
|
+
createElement(type: any, props?: any, ...children: any[]): any;
|
|
103
103
|
};
|
|
104
104
|
|
|
105
105
|
export { Pragma };
|
package/dist/adapter.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,144 +15,174 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
|
|
30
20
|
// src/adapter.ts
|
|
31
21
|
var adapter_exports = {};
|
|
32
22
|
__export(adapter_exports, {
|
|
33
|
-
Pragma: () =>
|
|
23
|
+
Pragma: () => Pragma4
|
|
34
24
|
});
|
|
35
25
|
module.exports = __toCommonJS(adapter_exports);
|
|
36
26
|
|
|
37
|
-
// src/
|
|
27
|
+
// src/state.as.ts
|
|
38
28
|
var import_mvc2 = require("@expressive/mvc");
|
|
39
29
|
|
|
40
30
|
// src/context.ts
|
|
41
|
-
var import_mvc =
|
|
31
|
+
var import_mvc = require("@expressive/mvc");
|
|
42
32
|
var import_react = require("react");
|
|
43
|
-
var
|
|
33
|
+
var Layers = (0, import_react.createContext)(new import_mvc.Context());
|
|
44
34
|
import_mvc.Context.use = (create) => {
|
|
45
|
-
const ambient = (0, import_react.useContext)(
|
|
46
|
-
return create ? (0, import_react.useMemo)(() => ambient.push(), []) : ambient;
|
|
35
|
+
const ambient = (0, import_react.useContext)(Layers);
|
|
36
|
+
return create ? (0, import_react.useMemo)(() => ambient.push(), [ambient]) : ambient;
|
|
47
37
|
};
|
|
48
|
-
function
|
|
49
|
-
if (
|
|
50
|
-
|
|
51
|
-
|
|
38
|
+
function provide(context, children, fallback, name) {
|
|
39
|
+
if (fallback !== void 0)
|
|
40
|
+
children = (0, import_react.createElement)(import_react.Suspense, { fallback, name }, children);
|
|
41
|
+
return (0, import_react.createElement)(Layers.Provider, {
|
|
52
42
|
key: context.id,
|
|
53
43
|
value: context,
|
|
54
44
|
children
|
|
55
45
|
});
|
|
56
|
-
return fallback !== void 0 ? (0, import_react.createElement)(import_react.Suspense, { fallback, name }, element) : element;
|
|
57
46
|
}
|
|
58
47
|
|
|
59
|
-
// src/
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return props.is(self);
|
|
68
|
-
});
|
|
69
|
-
local.set(props);
|
|
70
|
-
return createProvider(
|
|
71
|
-
local,
|
|
72
|
-
render(props, local),
|
|
73
|
-
props.fallback || local.fallback,
|
|
74
|
-
String(local)
|
|
75
|
-
);
|
|
76
|
-
};
|
|
77
|
-
Component.Model = this;
|
|
78
|
-
Component.displayName = this.name;
|
|
79
|
-
return Component;
|
|
48
|
+
// src/state.as.ts
|
|
49
|
+
var import_adapter = require("./adapter");
|
|
50
|
+
import_mvc2.State.as = function(render) {
|
|
51
|
+
const FC = Render.bind(this, { render });
|
|
52
|
+
return Object.assign(FC, {
|
|
53
|
+
displayName: this.name,
|
|
54
|
+
State: this
|
|
55
|
+
});
|
|
80
56
|
};
|
|
57
|
+
function Render(props, props2) {
|
|
58
|
+
const { is, ...rest } = { ...props, ...props2 };
|
|
59
|
+
const ambient = import_mvc2.Context.use();
|
|
60
|
+
const state = import_adapter.Pragma.useState(() => {
|
|
61
|
+
const instance = this.new(rest, is && ((x) => void is(x)));
|
|
62
|
+
const context = ambient.push(instance);
|
|
63
|
+
let ready;
|
|
64
|
+
let active;
|
|
65
|
+
(0, import_mvc2.watch)(instance, (current) => {
|
|
66
|
+
active = current;
|
|
67
|
+
if (ready) state[1]((x) => x.bind(null));
|
|
68
|
+
});
|
|
69
|
+
function didMount() {
|
|
70
|
+
ready = true;
|
|
71
|
+
return () => {
|
|
72
|
+
context.pop();
|
|
73
|
+
instance.set(null);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function Render2(props3) {
|
|
77
|
+
const render = import_mvc2.METHOD.get(active.render) || props3.render || active.render;
|
|
78
|
+
return render ? render.call(active, props3, active) : props3.children;
|
|
79
|
+
}
|
|
80
|
+
return (props3) => {
|
|
81
|
+
ready = false;
|
|
82
|
+
import_adapter.Pragma.useEffect(didMount, []);
|
|
83
|
+
Promise.resolve(instance.set(props3)).finally(() => {
|
|
84
|
+
ready = true;
|
|
85
|
+
});
|
|
86
|
+
return provide(
|
|
87
|
+
context,
|
|
88
|
+
import_adapter.Pragma.createElement(Render2, props3),
|
|
89
|
+
props3.fallback || active.fallback,
|
|
90
|
+
String(instance)
|
|
91
|
+
);
|
|
92
|
+
};
|
|
93
|
+
});
|
|
94
|
+
return state[0](rest);
|
|
95
|
+
}
|
|
81
96
|
|
|
82
|
-
// src/
|
|
97
|
+
// src/state.use.ts
|
|
83
98
|
var import_mvc3 = require("@expressive/mvc");
|
|
84
|
-
var
|
|
85
|
-
import_mvc3.
|
|
86
|
-
const
|
|
87
|
-
const
|
|
99
|
+
var import_adapter2 = require("./adapter");
|
|
100
|
+
import_mvc3.State.use = function(...args) {
|
|
101
|
+
const ambient = import_mvc3.Context.use();
|
|
102
|
+
const state = import_adapter2.Pragma.useState(() => {
|
|
88
103
|
let ready;
|
|
89
|
-
let
|
|
90
|
-
const instance = new
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
104
|
+
let active;
|
|
105
|
+
const instance = this.new(
|
|
106
|
+
(x) => x.use ? Promise.resolve(x.use(...args)) : args
|
|
107
|
+
);
|
|
108
|
+
const context = ambient.push(instance);
|
|
109
|
+
(0, import_mvc3.watch)(instance, (current) => {
|
|
110
|
+
active = current;
|
|
111
|
+
if (ready) state[1]((x) => x.bind(null));
|
|
96
112
|
});
|
|
97
113
|
function didMount() {
|
|
98
114
|
ready = true;
|
|
99
115
|
return () => {
|
|
100
|
-
unwatch();
|
|
101
116
|
context.pop();
|
|
102
117
|
instance.set(null);
|
|
103
118
|
};
|
|
104
119
|
}
|
|
105
|
-
return (
|
|
106
|
-
|
|
107
|
-
if (ready
|
|
120
|
+
return (...args2) => {
|
|
121
|
+
import_adapter2.Pragma.useEffect(didMount, []);
|
|
122
|
+
if (ready) {
|
|
108
123
|
ready = false;
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
else
|
|
124
|
+
Promise.resolve(
|
|
125
|
+
instance.use ? instance.use(...args2) : Promise.all(
|
|
126
|
+
args2.flat().map(
|
|
127
|
+
(arg) => typeof arg == "object" && instance.set(arg)
|
|
128
|
+
)
|
|
129
|
+
)
|
|
130
|
+
).finally(() => {
|
|
117
131
|
ready = true;
|
|
132
|
+
});
|
|
118
133
|
}
|
|
119
|
-
return
|
|
134
|
+
return active;
|
|
120
135
|
};
|
|
121
136
|
});
|
|
122
|
-
return
|
|
137
|
+
return state[0](...args);
|
|
123
138
|
};
|
|
124
139
|
|
|
125
|
-
// src/
|
|
140
|
+
// src/state.get.ts
|
|
126
141
|
var import_mvc4 = require("@expressive/mvc");
|
|
127
|
-
var
|
|
128
|
-
import_mvc4.
|
|
129
|
-
const context =
|
|
130
|
-
const
|
|
142
|
+
var import_adapter3 = require("./adapter");
|
|
143
|
+
import_mvc4.State.get = function(argument) {
|
|
144
|
+
const context = import_mvc4.Context.use();
|
|
145
|
+
const state = import_adapter3.Pragma.useState(() => {
|
|
131
146
|
const instance = context.get(this);
|
|
132
147
|
if (!instance)
|
|
133
|
-
if (argument === false)
|
|
134
|
-
|
|
135
|
-
else
|
|
136
|
-
throw new Error(`Could not find ${this} in context.`);
|
|
148
|
+
if (argument === false) return () => void 0;
|
|
149
|
+
else throw new Error(`Could not find ${this} in context.`);
|
|
137
150
|
let ready;
|
|
138
151
|
let value;
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
152
|
+
function render() {
|
|
153
|
+
state[1]((x) => x.bind(null));
|
|
154
|
+
}
|
|
155
|
+
function refresh(action) {
|
|
156
|
+
if (typeof action == "function") action = action();
|
|
157
|
+
render();
|
|
158
|
+
if (action) return action.finally(render);
|
|
159
|
+
}
|
|
160
|
+
function onMount() {
|
|
161
|
+
ready = true;
|
|
162
|
+
return unwatch;
|
|
163
|
+
}
|
|
164
|
+
const unwatch = (0, import_mvc4.watch)(
|
|
165
|
+
instance,
|
|
166
|
+
(current) => {
|
|
167
|
+
if (typeof argument === "function") {
|
|
168
|
+
const next = argument.call(current, current, refresh);
|
|
169
|
+
if (next === value) return;
|
|
170
|
+
value = next;
|
|
171
|
+
} else value = current;
|
|
172
|
+
if (ready) render();
|
|
173
|
+
},
|
|
174
|
+
argument === true
|
|
175
|
+
);
|
|
150
176
|
if (value instanceof Promise) {
|
|
151
177
|
let error;
|
|
152
178
|
unwatch();
|
|
153
|
-
value.then(
|
|
179
|
+
value.then(
|
|
180
|
+
(x) => value = x,
|
|
181
|
+
(e) => error = e
|
|
182
|
+
).finally(render);
|
|
154
183
|
value = null;
|
|
155
184
|
return () => {
|
|
156
|
-
if (error)
|
|
157
|
-
throw error;
|
|
185
|
+
if (error) throw error;
|
|
158
186
|
return value === void 0 ? null : value;
|
|
159
187
|
};
|
|
160
188
|
}
|
|
@@ -162,28 +190,16 @@ import_mvc4.Model.get = function(argument) {
|
|
|
162
190
|
unwatch();
|
|
163
191
|
return () => null;
|
|
164
192
|
}
|
|
165
|
-
;
|
|
166
|
-
function update(action) {
|
|
167
|
-
if (typeof action == "function")
|
|
168
|
-
action = action();
|
|
169
|
-
refresh();
|
|
170
|
-
if (action)
|
|
171
|
-
return action.finally(refresh);
|
|
172
|
-
}
|
|
173
|
-
function didMount() {
|
|
174
|
-
ready = true;
|
|
175
|
-
return unwatch;
|
|
176
|
-
}
|
|
177
193
|
return () => {
|
|
178
|
-
|
|
194
|
+
import_adapter3.Pragma.useEffect(onMount, []);
|
|
179
195
|
return value === void 0 ? null : value;
|
|
180
196
|
};
|
|
181
197
|
});
|
|
182
|
-
return
|
|
198
|
+
return state[0]();
|
|
183
199
|
};
|
|
184
200
|
|
|
185
201
|
// src/adapter.ts
|
|
186
|
-
var
|
|
202
|
+
var Pragma4 = {};
|
|
187
203
|
// Annotate the CommonJS export names for ESM import in node:
|
|
188
204
|
0 && (module.exports = {
|
|
189
205
|
Pragma
|
package/dist/adapter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/adapter.ts","../src/model.as.ts","../src/context.ts","../src/model.use.ts","../src/model.get.ts"],"sourcesContent":["import { Context } from '@expressive/mvc';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n type HasProps<T extends Model> = {\n [K in Exclude<keyof T, keyof Model>]?: T[K];\n }\n\n /**\n * Props which will not conflict with a Model's use as a Component.\n * \n * Built-in properties must be optional, as they will always be omitted.\n */\n type RenderProps<T extends Model> = HasProps<T> & {\n is?: undefined;\n get?: undefined;\n set?: undefined;\n };\n\n /** Model which is not incompatable as Component in React. */\n interface Compat extends Model {\n render?(props: RenderProps<this>, self: this): React.ReactNode;\n fallback?: React.ReactNode;\n }\n\n interface BaseProps<T extends Model> {\n /**\n * Callback for newly created instance. Only called once.\n * @returns Callback to run when instance is destroyed.\n */\n is?: (instance: T) => void | (() => void);\n\n render?(props: HasProps<T>, self: T): React.ReactNode;\n\n /**\n * A fallback react tree to show when suspended.\n * If not provided, `fallback` property of the Model will be used.\n */\n fallback?: React.ReactNode;\n }\n\n type Props<T extends Model> = \n T extends { render(props: infer P, self: any): any }\n ? BaseProps<T> & HasProps<T> & Omit<P, keyof Model>\n : BaseProps<T> & HasProps<T> & { children?: React.ReactNode };\n }\n}\n\nconst Pragma = {} as {\n useContext(): Context;\n\n useFactory<T extends Function>(\n factory: (refresh: () => void) => T\n ): T;\n\n useLifecycle(callback: () => () => void): void;\n};\n\nexport { Pragma };\n\nimport \"./model.as\";\nimport './model.use';\nimport './model.get';","import { Model } from '@expressive/mvc';\nimport React, { FunctionComponent } from 'react';\n\nimport { createProvider } from './context';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n interface Component<T extends Model, P extends Model.Assign<T>> extends FunctionComponent<P & Model.Props<T>> {\n displayName?: string;\n Model: Model.Type<T>;\n }\n\n function as <T extends Model, P extends Model.Assign<T>> (\n this: Model.Init<T>, render: (props: P, self: T) => React.ReactNode\n ): Component<T, P>;\n }\n}\n\nModel.as = function <T extends Model.Compat, P extends Model.Assign<T>> (\n this: Model.Init<T>,\n render: (props: P, self: T) => React.ReactNode\n){\n if(this === Model)\n throw new Error(\"Cannot create component from base Model.\");\n\n const Component: Model.Component<T, P> = (props) => {\n const local = this.use((self) => {\n self.set(props);\n\n if(props.is)\n return props.is(self);\n });\n\n local.set(props);\n\n return createProvider(\n local,\n render(props, local),\n props.fallback || local.fallback,\n String(local)\n );\n }\n\n Component.Model = this;\n Component.displayName = this.name;\n \n return Component;\n}","import Model, { Context } from '@expressive/mvc';\nimport { createContext, createElement, ReactNode, Suspense, useContext, useEffect, useMemo } from 'react';\n\nconst Lookup = createContext(new Context());\n\ndeclare module '@expressive/mvc' {\n namespace Context {\n function use(create?: true): Context;\n function use(create: boolean): Context | null | undefined;\n }\n}\n\nContext.use = (create?: boolean) => {\n const ambient = useContext(Lookup);\n\n return create ?\n useMemo(() => ambient.push(), []) :\n ambient;\n}\n\ndeclare namespace Consumer {\n type Props<T extends Model> = {\n /** Type of controller to fetch from context. */\n for: Model.Type<T>;\n\n /**\n * Render function, will receive instance of desired controller.\n *\n * Called every render of parent component.\n * Similar to `Model.get()`, updates to properties accessed in\n * this function will cause a refresh when they change.\n */\n children: (value: T) => ReactNode | void;\n }\n}\n\nfunction Consumer<T extends Model>(props: Consumer.Props<T>){\n return props.for.get(i => props.children(i));\n}\n\ndeclare namespace Provider {\n interface Props<T extends Model> {\n /** Model or group of Models to provide to descendant Consumers. */\n for: Context.Accept<T>;\n\n forEach?: Context.Expect<T>;\n children?: ReactNode;\n\n /** A fallback react tree to show when suspended. */\n fallback?: ReactNode;\n\n /**\n * A name for this Suspense boundary for instrumentation purposes.\n * The name will help identify this boundary in React DevTools.\n */\n name?: string | undefined;\n }\n}\n\nfunction Provider<T extends Model>(props: Provider.Props<T>){\n const context = Context.use(true);\n\n useEffect(() => () => context.pop(), [context]);\n\n context.include(props.for, (model) => {\n if(props.forEach){\n const cleanup = props.forEach(model);\n\n if(cleanup)\n model.set(cleanup, null);\n }\n });\n \n return createProvider(context, props.children, props.fallback, props.name);\n}\n\nexport function createProvider(\n context: Context | Model,\n children: ReactNode,\n fallback?: ReactNode,\n name?: string | undefined){\n\n if(context instanceof Model)\n context = Context.get(context)!;\n\n const element = createElement(Lookup.Provider, {\n key: context.id,\n value: context,\n children\n });\n\n return fallback !== undefined ?\n createElement(Suspense, { fallback, name }, element) :\n element;\n}\n\nexport { Consumer, Provider, Context }\n","import { Model, Context, createEffect } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n function use <T extends Model> (\n this: Model.Init<T>,\n apply?: Model.Assign<T>,\n repeat?: boolean\n ): T;\n\n function use <T extends Model> (\n this: Model.Init<T>,\n callback?: Model.Callback<T>,\n repeat?: boolean\n ): T;\n }\n}\n\nModel.use = function <T extends Model> (\n this: Model.Init<T>,\n argument?: Model.Assign<T> | Model.Callback<T>,\n repeat?: boolean){\n\n const context = Context.use(true);\n const render = Pragma.useFactory((refresh) => {\n let ready: boolean | undefined;\n let local: T;\n\n const instance = new this(argument);\n\n context.include(instance);\n \n const unwatch = createEffect(instance, current => {\n local = current;\n\n if(ready) \n refresh();\n });\n\n function didMount(){\n ready = true;\n return () => {\n unwatch();\n context.pop()\n instance.set(null);\n }\n }\n\n return (props?: Model.Assign<T> | Model.Callback<T>) => {\n Pragma.useLifecycle(didMount);\n\n if(ready && repeat && props){\n ready = false;\n\n if(typeof props == \"function\"){\n props.call(instance, instance);\n props = undefined\n }\n\n const update = instance.set(props);\n\n if(update)\n update.then(() => ready = true);\n else\n ready = true;\n }\n\n return local; \n };\n });\n\n return render(argument);\n}","import { createEffect, Model } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\n/** Type may not be undefined - instead will be null. */\ntype NoVoid<T> = T extends undefined | void ? null : T;\n\ntype ForceRefresh = {\n /** Request a refresh for current component. */\n (): void;\n \n /**\n * Request a refresh and again after promise either resolves or rejects.\n * \n * @param waitFor Promise to wait for.\n * @returns Promise which resolves, after refresh, to same value as `waitFor`.\n */\n <T = void>(waitFor: Promise<T>): Promise<T>;\n\n /**\n * Request refresh before and after async function.\n * A refresh will occur both before and after the given function.\n * \n * **Note:** Any actions performed before first `await` will occur prior to refresh.\n * \n * @param invoke Async function to invoke.\n * @returns Promise which resolves returned value after refresh.\n */\n <T = void>(invoke: () => Promise<T>): Promise<T>;\n};\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n type GetFactory<T extends Model, R> =\n (this: T, current: T, refresh: ForceRefresh) => R;\n\n type GetEffect<T extends Model> =\n (this: T, current: T, refresh: ForceRefresh) => null;\n\n /** Fetch instance of this class from context. */\n function get <T extends Model> (this: Model.Type<T>): T;\n \n /** Fetch instance of this class optionally. */\n function get <T extends Model> (this: Model.Type<T>, required: false): T | undefined;\n\n /** Fetch instance of this class from context. */\n function get <T extends Model> (this: Model.Type<T>, requireValues: true): Required<T>\n \n function get <T extends Model, R> (this: Model.Type<T>, factory: GetFactory<T, Promise<R> | R>): NoVoid<R>;\n \n // TODO: eagerly metch this so any nulls are caught - would prevent updates.\n function get <T extends Model> (this: Model.Type<T>, factory: GetEffect<T>): null;\n }\n}\n\nModel.get = function <T extends Model, R> (\n this: Model.Type<T>,\n argument?: boolean | Model.GetFactory<T, unknown>\n){\n const context = Pragma.useContext();\n const render = Pragma.useFactory((refresh) => {\n const instance = context.get(this);\n\n if(!instance)\n if(argument === false)\n return () => undefined;\n else\n throw new Error(`Could not find ${this} in context.`);\n\n let ready: boolean | undefined;\n let value: any;\n\n const unwatch = createEffect(instance, current => {\n if(typeof argument === \"function\"){\n const next = argument.call(current, current, update);\n\n if(next === value)\n return;\n\n value = next;\n }\n else\n value = current;\n\n if(ready)\n refresh();\n }, argument === true);\n\n if(value instanceof Promise){\n let error: Error | undefined;\n\n unwatch();\n\n value.then(x => value = x, e => error = e).finally(refresh);\n value = null;\n\n return () => {\n if(error)\n throw error;\n\n return value === undefined ? null : value;\n }\n }\n\n if(value === null){\n unwatch();\n return () => null;\n };\n\n function update<T>(action?: Promise<T> | (() => Promise<T>)): any {\n if(typeof action == \"function\")\n action = action();\n\n refresh();\n\n if(action)\n return action.finally(refresh);\n }\n\n function didMount(){\n ready = true;\n return unwatch;\n }\n\n return () => {\n Pragma.useLifecycle(didMount);\n return value === undefined ? null : value;\n }\n });\n\n return render() as R;\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,gBAAAA;AAAA;AAAA;;;ACAA,IAAAC,cAAsB;;;ACAtB,iBAA+B;AAC/B,mBAAkG;AAElG,IAAM,aAAS,4BAAc,IAAI,mBAAQ,CAAC;AAS1C,mBAAQ,MAAM,CAAC,WAAqB;AAClC,QAAM,cAAU,yBAAW,MAAM;AAEjC,SAAO,aACL,sBAAQ,MAAM,QAAQ,KAAK,GAA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O,MAAM;AAAA,IACf;AAAC;AAED,aAAS,OAAU,QAA+C;AAChE,UAAG,OAAO,UAAU;AAClB,iBAAS,OAAO;AAElB,cAAQ;AAER,UAAG;AACD,eAAO,OAAO,QAAQ,OAAO;AAAA,IACjC;AAEA,aAAS,WAAU;AACjB,cAAQ;AACR,aAAO;AAAA,IACT;AAEA,WAAO,MAAM;AACX,6BAAO,aAAa,QAAQ;AAC5B,aAAO,UAAU,SAAY,OAAO;AAAA,IACtC;AAAA,EACF,CAAC;AAED,SAAO,OAAO;AAChB;;;AJnFA,IAAMC,UAAS,CAAC;","names":["Pragma","import_mvc","Model","import_mvc","import_mvc","import_adapter","Pragma"]}
|
|
1
|
+
{"version":3,"sources":["../src/adapter.ts","../src/state.as.ts","../src/context.ts","../src/state.use.ts","../src/state.get.ts"],"sourcesContent":["namespace Pragma {}\n\nconst Pragma = {} as {\n useState<S>(initial: () => S): [S, (next: (previous: S) => S) => void];\n useEffect(effect: () => () => void, deps?: any[]): void;\n createElement(type: any, props?: any, ...children: any[]): any;\n};\n\nexport { Pragma };\n\nimport './state.as';\nimport './state.use';\nimport './state.get';\n","import { Context, watch, METHOD, State } from '@expressive/mvc';\nimport { FunctionComponent, ReactNode } from 'react';\n\nimport { provide } from './context';\nimport { Pragma } from './adapter';\n\ndeclare module '@expressive/mvc' {\n namespace State {\n type HasProps<T extends State> = {\n [K in Exclude<keyof T, keyof State>]?: T[K];\n };\n\n type ComponentProps<T extends State> = HasProps<T> & {\n /**\n * Callback for newly created instance. Only called once.\n * @returns Callback to run when instance is destroyed.\n */\n is?: (instance: T) => void;\n\n /**\n * A fallback react tree to show when suspended.\n * If not provided, `fallback` property of the State will be used.\n */\n fallback?: React.ReactNode;\n };\n\n type Props<T extends State> = T extends {\n render(props: infer P, self: any): any;\n }\n ? ComponentProps<T> & Omit<P, keyof AsComponent>\n : ComponentProps<T> & { children?: React.ReactNode };\n\n /**\n * Props which will not conflict with a State's use as a Component.\n *\n * Built-in properties must be optional, as they will always be omitted.\n */\n type RenderProps<T extends State> = HasProps<T> & {\n is?: never;\n get?: never;\n set?: never;\n };\n\n /** State which is not incompatable as Component in React. */\n interface AsComponent extends State.Valid {\n render?(props: RenderProps<this>, self: this): React.ReactNode;\n fallback?: React.ReactNode;\n }\n\n interface FC<\n T extends State,\n P extends State.Assign<T>\n > extends FunctionComponent<P & State.Props<T>> {\n displayName?: string;\n State: State.Extends<T>;\n }\n\n function as<T extends State, P extends State.Assign<T>>(\n this: State.New<T>,\n render: (props: P, self: T) => ReactNode\n ): FC<T, P>;\n }\n}\n\nState.as = function <T extends State.AsComponent, P extends State.Assign<T>>(\n this: State.Class<T>,\n render: (props: P, self: T) => ReactNode\n): State.FC<T, P> {\n const FC = Render.bind(this as State.Class, { render } as {});\n\n return Object.assign(FC, {\n displayName: this.name,\n State: this\n });\n};\n\nexport function Render<T extends State.AsComponent>(\n this: State.Class<T>,\n props: State.Props<T>,\n props2?: State.Props<T>\n) {\n const { is, ...rest } = { ...props, ...props2 };\n\n const ambient = Context.use();\n const state = Pragma.useState<(props: any) => any>(() => {\n const instance = this.new(rest as {}, is && ((x) => void is(x)));\n const context = ambient.push(instance);\n\n let ready: boolean | undefined;\n let active: T;\n\n watch(instance, (current) => {\n active = current;\n\n if (ready) state[1]((x) => x.bind(null));\n });\n\n function didMount() {\n ready = true;\n return () => {\n context.pop();\n instance.set(null);\n };\n }\n\n function Render(props: State.Props<T>) {\n const render = METHOD.get(active.render) || props.render || active.render;\n\n return render\n ? render.call(active, props as State.HasProps<T>, active)\n : props.children;\n }\n\n return (props: State.RenderProps<T>) => {\n ready = false;\n\n Pragma.useEffect(didMount, []);\n Promise.resolve(instance.set(props as {})).finally(() => {\n ready = true;\n });\n\n return provide(\n context,\n Pragma.createElement(Render, props as any),\n props.fallback || active.fallback,\n String(instance)\n );\n };\n });\n\n return state[0](rest);\n}\n","import State, { Context } from '@expressive/mvc';\nimport {\n createContext,\n createElement,\n ReactNode,\n Suspense,\n useContext,\n useEffect,\n useMemo\n} from 'react';\n\nexport const Layers = createContext(new Context());\n\ndeclare module '@expressive/mvc' {\n namespace Context {\n function use(create?: true): Context;\n function use(create: boolean): Context | null | undefined;\n }\n}\n\nContext.use = (create?: boolean) => {\n const ambient = useContext(Layers);\n\n return create ? useMemo(() => ambient.push(), [ambient]) : ambient;\n};\n\ndeclare namespace Consumer {\n type Props<T extends State> = {\n /** Type of controller to fetch from context. */\n for: State.Extends<T>;\n\n /**\n * Render function, will receive instance of desired controller.\n *\n * Called every render of parent component.\n * Similar to `State.get()`, updates to properties accessed in\n * this function will cause a refresh when they change.\n */\n children: (value: T) => ReactNode | void;\n };\n}\n\nfunction Consumer<T extends State>(props: Consumer.Props<T>) {\n return props.for.get((i) => props.children(i));\n}\n\ndeclare namespace Provider {\n interface Props<T extends State> {\n /** State or group of States to provide to descendant Consumers. */\n for: Context.Accept<T>;\n\n /**\n * Callback to run for each provided State.\n */\n forEach?: Context.Expect<T>;\n\n /**\n * Children to render within this Provider.\n */\n children?: ReactNode;\n\n /** A fallback tree to show when suspended. */\n fallback?: ReactNode;\n\n /**\n * A name for this Suspense boundary for instrumentation purposes.\n * The name will help identify this boundary in React DevTools.\n */\n name?: string | undefined;\n }\n}\n\nfunction Provider<T extends State>(props: Provider.Props<T>) {\n const context = Context.use(true);\n\n useEffect(() => () => context.pop(), [context]);\n\n context.use(props.for, (state) => {\n if (props.forEach) {\n const cleanup = props.forEach(state);\n\n if (cleanup) state.set(cleanup, null);\n }\n });\n\n return provide(context, props.children, props.fallback, props.name);\n}\n\nfunction provide(\n context: Context,\n children: ReactNode,\n fallback?: ReactNode,\n name?: string | undefined\n): ReactNode {\n if (fallback !== undefined)\n children = createElement(Suspense, { fallback, name }, children);\n\n return createElement(Layers.Provider, {\n key: context.id,\n value: context,\n children\n });\n}\n\nexport { Consumer, Provider, provide };\n","import { State, Context, watch } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\ndeclare module '@expressive/mvc' {\n namespace State {\n interface Valid {\n use?(...props: any[]): Promise<void> | void;\n }\n\n type UseArgs<T extends State> = T extends {\n use(...props: infer P): any;\n }\n ? P\n : State.Args<T>;\n\n function use<T extends State>(this: New<T>, ...args: UseArgs<T>): T;\n }\n}\n\nState.use = function <T extends State.Valid>(\n this: State.New<T>,\n ...args: any[]\n) {\n const ambient = Context.use();\n const state = Pragma.useState(() => {\n let ready: boolean | undefined;\n let active: T;\n\n const instance = this.new((x) =>\n x.use ? Promise.resolve(x.use(...args)) : args\n );\n const context = ambient.push(instance);\n\n watch(instance, (current) => {\n active = current;\n\n if (ready) state[1]((x) => x.bind(null));\n });\n\n function didMount() {\n ready = true;\n return () => {\n context.pop();\n instance.set(null);\n };\n }\n\n return (...args: State.Args<T>) => {\n Pragma.useEffect(didMount, []);\n\n if (ready) {\n ready = false;\n\n Promise.resolve(\n instance.use\n ? instance.use(...args)\n : Promise.all(\n args\n .flat()\n .map(\n (arg) =>\n typeof arg == 'object' &&\n instance.set(arg as State.Assign<T>)\n )\n )\n ).finally(() => {\n ready = true;\n });\n }\n\n return active;\n };\n });\n\n return state[0](...args);\n};\n","import { Context, watch, State } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\n/** Type may not be undefined - instead will be null. */\ntype NoVoid<T> = T extends undefined | void ? null : T;\n\ntype ForceRefresh = {\n /** Request a refresh for current component. */\n (): void;\n\n /**\n * Request a refresh and again after promise either resolves or rejects.\n *\n * @param waitFor Promise to wait for.\n * @returns Promise which resolves, after refresh, to same value as `waitFor`.\n */\n <T = void>(waitFor: Promise<T>): Promise<T>;\n\n /**\n * Request refresh before and after async function.\n * A refresh will occur both before and after the given function.\n *\n * **Note:** Any actions performed before first `await` will occur prior to refresh.\n *\n * @param invoke Async function to invoke.\n * @returns Promise which resolves returned value after refresh.\n */\n <T = void>(invoke: () => Promise<T>): Promise<T>;\n};\n\ndeclare module '@expressive/mvc' {\n namespace State {\n type GetFactory<T extends State, R> = (\n this: T,\n current: T,\n refresh: ForceRefresh\n ) => R;\n\n type GetEffect<T extends State> = (\n this: T,\n current: T,\n refresh: ForceRefresh\n ) => null;\n\n /** Fetch instance of this class from context. */\n function get<T extends State>(this: Extends<T>): T;\n\n /** Fetch instance of this class optionally. */\n function get<T extends State>(\n this: Extends<T>,\n required: false\n ): T | undefined;\n\n /** Fetch instance of this class from context. */\n function get<T extends State>(\n this: Extends<T>,\n requireValues: true\n ): Required<T>;\n\n function get<T extends State, R>(\n this: Extends<T>,\n factory: GetFactory<T, Promise<R> | R>\n ): NoVoid<R>;\n\n // TODO: eagerly match this so any nulls are caught - would prevent updates.\n function get<T extends State>(\n this: Extends<T>,\n factory: GetEffect<T>\n ): null;\n }\n}\n\nState.get = function <T extends State, R>(\n this: State.Extends<T>,\n argument?: boolean | State.GetFactory<T, unknown>\n) {\n const context = Context.use();\n const state = Pragma.useState(() => {\n const instance = context.get(this);\n\n if (!instance)\n if (argument === false) return () => undefined;\n else throw new Error(`Could not find ${this} in context.`);\n\n let ready: boolean | undefined;\n let value: any;\n\n function render() {\n state[1]((x) => x.bind(null));\n }\n\n function refresh<T>(action?: Promise<T> | (() => Promise<T>)): any {\n if (typeof action == 'function') action = action();\n\n render();\n\n if (action) return action.finally(render);\n }\n\n function onMount() {\n ready = true;\n return unwatch;\n }\n\n const unwatch = watch(\n instance,\n (current) => {\n if (typeof argument === 'function') {\n const next = argument.call(current, current, refresh);\n\n if (next === value) return;\n\n value = next;\n } else value = current;\n\n if (ready) render();\n },\n argument === true\n );\n\n if (value instanceof Promise) {\n let error: Error | undefined;\n\n unwatch();\n\n value\n .then(\n (x) => (value = x),\n (e) => (error = e)\n )\n .finally(render);\n\n value = null;\n\n return () => {\n if (error) throw error;\n\n return value === undefined ? null : value;\n };\n }\n\n if (value === null) {\n unwatch();\n return () => null;\n }\n\n return () => {\n Pragma.useEffect(onMount, []);\n return value === undefined ? null : value;\n };\n });\n\n return state[0]() as R;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,gBAAAA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