@expressive/react 0.37.0 → 0.39.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/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Model, Context } from '@expressive/mvc';
2
- export { Model, add, Model as default, get, ref, set, use } from '@expressive/mvc';
2
+ export { Model, add, Model as default, get, has, ref, set, use } from '@expressive/mvc';
3
3
  import { FunctionComponentElement, ReactNode } from 'react';
4
4
 
5
5
  declare namespace Consumer {
@@ -88,9 +88,17 @@ declare module '@expressive/mvc' {
88
88
  <T = void>(invoke: () => Promise<T>): Promise<T>;
89
89
  };
90
90
  }
91
- namespace use {
92
- type Callback<T extends Model> = (instance: T) => void;
91
+ interface Component<T extends Model, P extends Model.Values<T>> {
92
+ (props: P): JSX.Element;
93
+ using: Model.Type<T>;
94
+ displayName: string;
93
95
  }
96
+ /**
97
+ * Creates a component which reflects this Model. All managed properties may be assigned using props.
98
+ *
99
+ * @param render Function which renders component. This function receives all Model state merged with props. Normal subscription behavior still applies.
100
+ */
101
+ function as<T extends Model, P = {}>(this: Model.New<T>, render: (using: T & P) => React.ReactNode): Component<T, P & Model.Values<T>>;
94
102
  /** Fetch instance of this class from context. */
95
103
  function get<T extends Model>(this: Model.Type<T>, ignore?: true): T;
96
104
  /** Fetch instance of this class optionally. May be undefined, but will never subscribe. */
@@ -98,7 +106,7 @@ declare module '@expressive/mvc' {
98
106
  function get<T extends Model, R>(this: Model.Type<T>, factory: get.Factory<T, Promise<R> | R>): NoVoid<R>;
99
107
  function get<T extends Model, R>(this: Model.Type<T>, factory: get.Factory<T, null>): NoVoid<R> | null;
100
108
  function use<T extends Model>(this: Model.New<T>, apply?: Model.Values<T>, repeat?: boolean): T;
101
- function use<T extends Model>(this: Model.New<T>, callback?: use.Callback<T>, repeat?: boolean): T;
109
+ function use<T extends Model>(this: Model.New<T>, callback?: ((instance: T) => void), repeat?: boolean): T;
102
110
  }
103
111
  }
104
112
 
package/dist/index.js CHANGED
@@ -21,17 +21,32 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
23
  Consumer: () => Consumer,
24
- Model: () => import_mvc2.Model,
24
+ Model: () => import_mvc3.Model,
25
25
  Provider: () => Provider,
26
- add: () => import_mvc3.add,
27
- default: () => import_mvc2.Model,
28
- get: () => import_mvc3.get,
29
- ref: () => import_mvc3.ref,
30
- set: () => import_mvc3.set,
31
- use: () => import_mvc3.use
26
+ add: () => import_mvc4.add,
27
+ default: () => import_mvc3.Model,
28
+ get: () => import_mvc4.get,
29
+ has: () => import_mvc4.has,
30
+ ref: () => import_mvc4.ref,
31
+ set: () => import_mvc4.set,
32
+ use: () => import_mvc4.use
32
33
  });
33
34
  module.exports = __toCommonJS(src_exports);
34
- var import_mvc2 = require("@expressive/mvc");
35
+ var import_mvc3 = require("@expressive/mvc");
36
+
37
+ // src/component.ts
38
+ function component(render) {
39
+ const Component = (inputProps) => {
40
+ const props = this.use(inputProps, true);
41
+ for (const key in inputProps)
42
+ if (!(key in props))
43
+ Object.defineProperty(props, key, { value: inputProps[key] });
44
+ return render(props);
45
+ };
46
+ Component.using = this;
47
+ Component.displayName = this.name;
48
+ return Component;
49
+ }
35
50
 
36
51
  // src/useContext.ts
37
52
  var import_mvc = require("@expressive/mvc");
@@ -43,18 +58,21 @@ function useContext() {
43
58
  return (0, import_react.useContext)(Shared);
44
59
  }
45
60
  function createContext(value, children) {
46
- return (0, import_react.createElement)(Shared.Provider, { key: value.key, value }, children);
61
+ return (0, import_react.createElement)(Shared.Provider, { key: value.id, value }, children);
47
62
  }
48
- function getContext(model, resolve) {
49
- const waiting = Register.get(model);
63
+ function fromContext(from, resolve) {
64
+ const waiting = Register.get(from);
65
+ const context = (context2) => {
66
+ resolve(context2.get(this));
67
+ };
50
68
  if (waiting instanceof import_mvc.Context)
51
- resolve(waiting);
69
+ context(waiting);
52
70
  else if (waiting)
53
- waiting.push(resolve);
71
+ waiting.push(context);
54
72
  else
55
- Register.set(model, [resolve]);
73
+ Register.set(from, [context]);
56
74
  }
57
- function setContext(model, context = useContext()) {
75
+ function setContext(model, context) {
58
76
  const waiting = Register.get(model);
59
77
  if (waiting instanceof Array)
60
78
  waiting.forEach((cb) => cb(context));
@@ -62,36 +80,46 @@ function setContext(model, context = useContext()) {
62
80
  }
63
81
 
64
82
  // src/useLocal.ts
65
- function useLocal(apply, repeat) {
83
+ var import_mvc2 = require("@expressive/mvc");
84
+ var PENDING = /* @__PURE__ */ new WeakMap();
85
+ function useLocal(argument, repeat) {
66
86
  const state = (0, import_react2.useState)(() => {
67
- let shouldApply = !!apply;
87
+ let apply;
68
88
  let enabled;
69
89
  let local;
70
- const instance = this.new();
90
+ const instance = new this();
91
+ if (argument)
92
+ PENDING.set(instance, apply = () => {
93
+ if (typeof argument == "function")
94
+ argument(instance);
95
+ else if (argument) {
96
+ for (const key in instance)
97
+ if (argument.hasOwnProperty(key))
98
+ instance[key] = argument[key];
99
+ }
100
+ if (!repeat)
101
+ apply = void 0;
102
+ });
103
+ instance.set(0);
71
104
  const release = instance.get((current) => {
72
105
  local = current;
73
106
  if (enabled)
74
107
  state[1]((x) => x.bind(null));
75
108
  });
76
109
  return (props) => {
77
- if (shouldApply) {
110
+ argument = props;
111
+ if (apply && enabled) {
78
112
  enabled = false;
79
- if (typeof props == "function")
80
- props(instance);
81
- else if (props) {
82
- for (const key in instance)
83
- if (props.hasOwnProperty(key))
84
- instance[key] = props[key];
85
- }
86
- if (!repeat)
87
- shouldApply = false;
113
+ apply();
88
114
  const update = instance.set();
89
115
  if (update)
90
116
  update.then(() => enabled = true);
91
117
  else
92
118
  enabled = true;
93
119
  }
94
- setContext(instance);
120
+ const context = useContext();
121
+ setContext(instance, context);
122
+ context.has(instance);
95
123
  (0, import_react2.useEffect)(() => {
96
124
  enabled = true;
97
125
  return () => {
@@ -102,8 +130,14 @@ function useLocal(apply, repeat) {
102
130
  return local;
103
131
  };
104
132
  });
105
- return state[0](apply);
133
+ return state[0](argument);
106
134
  }
135
+ import_mvc2.Model.on((_, subject) => {
136
+ const pending = PENDING.get(subject);
137
+ if (pending)
138
+ pending();
139
+ return null;
140
+ });
107
141
 
108
142
  // src/useRemote.ts
109
143
  function useRemote(argument) {
@@ -163,15 +197,15 @@ function useRemote(argument) {
163
197
  }
164
198
 
165
199
  // src/index.ts
166
- var import_mvc3 = require("@expressive/mvc");
200
+ var import_mvc4 = require("@expressive/mvc");
167
201
 
168
202
  // src/consumer.ts
169
203
  function Consumer(props) {
170
- const { children, has, get: get2, for: Type } = props;
204
+ const { children, has: has2, get: get2, for: Type } = props;
171
205
  if (typeof children == "function")
172
206
  return children(Type.get());
173
- const instance = Type.get(!!has);
174
- const callback = has || get2;
207
+ const instance = Type.get(!!has2);
208
+ const callback = has2 || get2;
175
209
  if (typeof callback == "function")
176
210
  callback(instance);
177
211
  return null;
@@ -180,24 +214,25 @@ function Consumer(props) {
180
214
  // src/provider.ts
181
215
  function Provider(props) {
182
216
  let { for: included, use: assign } = props;
183
- const context = useContext();
184
- const value = (0, import_react2.useMemo)(() => context.push(), []);
185
- value.include(included).forEach((isExplicit, model) => {
217
+ const ambient = useContext();
218
+ const context = (0, import_react2.useMemo)(() => ambient.push(), []);
219
+ context.include(included).forEach((isExplicit, model) => {
186
220
  if (assign && isExplicit) {
187
221
  for (const K in assign)
188
222
  if (K in model)
189
223
  model.set(K, assign[K]);
190
224
  }
191
- setContext(model, value);
225
+ setContext(model, context);
192
226
  });
193
- (0, import_react2.useEffect)(() => () => value.pop(), []);
194
- return createContext(value, props.children);
227
+ (0, import_react2.useEffect)(() => () => context.pop(), []);
228
+ return createContext(context, props.children);
195
229
  }
196
230
 
197
231
  // src/index.ts
198
- import_mvc2.Model.get = useRemote;
199
- import_mvc2.Model.use = useLocal;
200
- import_mvc2.Context.get = getContext;
232
+ import_mvc3.Model.at = fromContext;
233
+ import_mvc3.Model.as = component;
234
+ import_mvc3.Model.get = useRemote;
235
+ import_mvc3.Model.use = useLocal;
201
236
  // Annotate the CommonJS export names for ESM import in node:
202
237
  0 && (module.exports = {
203
238
  Consumer,
@@ -205,6 +240,7 @@ import_mvc2.Context.get = getContext;
205
240
  Provider,
206
241
  add,
207
242
  get,
243
+ has,
208
244
  ref,
209
245
  set,
210
246
  use
package/dist/index.mjs CHANGED
@@ -1,13 +1,23 @@
1
1
  // src/index.ts
2
- import { Context as Context2, Model as Model2 } from "@expressive/mvc";
2
+ import { Model as Model3 } from "@expressive/mvc";
3
+
4
+ // src/component.ts
5
+ function component(render) {
6
+ const Component = (inputProps) => {
7
+ const props = this.use(inputProps, true);
8
+ for (const key in inputProps)
9
+ if (!(key in props))
10
+ Object.defineProperty(props, key, { value: inputProps[key] });
11
+ return render(props);
12
+ };
13
+ Component.using = this;
14
+ Component.displayName = this.name;
15
+ return Component;
16
+ }
3
17
 
4
18
  // src/useContext.ts
5
19
  import { Context } from "@expressive/mvc";
6
- import {
7
- createContext as reactCreateContext,
8
- useContext as reactUseContext,
9
- createElement
10
- } from "react";
20
+ import { createContext as reactCreateContext, createElement, useContext as reactUseContext } from "react";
11
21
  import { useState, useEffect, useMemo } from "react";
12
22
  var Shared = reactCreateContext(new Context());
13
23
  var Register = /* @__PURE__ */ new WeakMap();
@@ -15,18 +25,21 @@ function useContext() {
15
25
  return reactUseContext(Shared);
16
26
  }
17
27
  function createContext(value, children) {
18
- return createElement(Shared.Provider, { key: value.key, value }, children);
28
+ return createElement(Shared.Provider, { key: value.id, value }, children);
19
29
  }
20
- function getContext(model, resolve) {
21
- const waiting = Register.get(model);
30
+ function fromContext(from, resolve) {
31
+ const waiting = Register.get(from);
32
+ const context = (context2) => {
33
+ resolve(context2.get(this));
34
+ };
22
35
  if (waiting instanceof Context)
23
- resolve(waiting);
36
+ context(waiting);
24
37
  else if (waiting)
25
- waiting.push(resolve);
38
+ waiting.push(context);
26
39
  else
27
- Register.set(model, [resolve]);
40
+ Register.set(from, [context]);
28
41
  }
29
- function setContext(model, context = useContext()) {
42
+ function setContext(model, context) {
30
43
  const waiting = Register.get(model);
31
44
  if (waiting instanceof Array)
32
45
  waiting.forEach((cb) => cb(context));
@@ -34,36 +47,46 @@ function setContext(model, context = useContext()) {
34
47
  }
35
48
 
36
49
  // src/useLocal.ts
37
- function useLocal(apply, repeat) {
50
+ import { Model as Model2 } from "@expressive/mvc";
51
+ var PENDING = /* @__PURE__ */ new WeakMap();
52
+ function useLocal(argument, repeat) {
38
53
  const state = useState(() => {
39
- let shouldApply = !!apply;
54
+ let apply;
40
55
  let enabled;
41
56
  let local;
42
- const instance = this.new();
57
+ const instance = new this();
58
+ if (argument)
59
+ PENDING.set(instance, apply = () => {
60
+ if (typeof argument == "function")
61
+ argument(instance);
62
+ else if (argument) {
63
+ for (const key in instance)
64
+ if (argument.hasOwnProperty(key))
65
+ instance[key] = argument[key];
66
+ }
67
+ if (!repeat)
68
+ apply = void 0;
69
+ });
70
+ instance.set(0);
43
71
  const release = instance.get((current) => {
44
72
  local = current;
45
73
  if (enabled)
46
74
  state[1]((x) => x.bind(null));
47
75
  });
48
76
  return (props) => {
49
- if (shouldApply) {
77
+ argument = props;
78
+ if (apply && enabled) {
50
79
  enabled = false;
51
- if (typeof props == "function")
52
- props(instance);
53
- else if (props) {
54
- for (const key in instance)
55
- if (props.hasOwnProperty(key))
56
- instance[key] = props[key];
57
- }
58
- if (!repeat)
59
- shouldApply = false;
80
+ apply();
60
81
  const update = instance.set();
61
82
  if (update)
62
83
  update.then(() => enabled = true);
63
84
  else
64
85
  enabled = true;
65
86
  }
66
- setContext(instance);
87
+ const context = useContext();
88
+ setContext(instance, context);
89
+ context.has(instance);
67
90
  useEffect(() => {
68
91
  enabled = true;
69
92
  return () => {
@@ -74,8 +97,14 @@ function useLocal(apply, repeat) {
74
97
  return local;
75
98
  };
76
99
  });
77
- return state[0](apply);
100
+ return state[0](argument);
78
101
  }
102
+ Model2.on((_, subject) => {
103
+ const pending = PENDING.get(subject);
104
+ if (pending)
105
+ pending();
106
+ return null;
107
+ });
79
108
 
80
109
  // src/useRemote.ts
81
110
  function useRemote(argument) {
@@ -135,15 +164,15 @@ function useRemote(argument) {
135
164
  }
136
165
 
137
166
  // src/index.ts
138
- import { add, get, use, ref, set } from "@expressive/mvc";
167
+ import { add, get, use, ref, set, has } from "@expressive/mvc";
139
168
 
140
169
  // src/consumer.ts
141
170
  function Consumer(props) {
142
- const { children, has, get: get2, for: Type } = props;
171
+ const { children, has: has2, get: get2, for: Type } = props;
143
172
  if (typeof children == "function")
144
173
  return children(Type.get());
145
- const instance = Type.get(!!has);
146
- const callback = has || get2;
174
+ const instance = Type.get(!!has2);
175
+ const callback = has2 || get2;
147
176
  if (typeof callback == "function")
148
177
  callback(instance);
149
178
  return null;
@@ -152,31 +181,33 @@ function Consumer(props) {
152
181
  // src/provider.ts
153
182
  function Provider(props) {
154
183
  let { for: included, use: assign } = props;
155
- const context = useContext();
156
- const value = useMemo(() => context.push(), []);
157
- value.include(included).forEach((isExplicit, model) => {
184
+ const ambient = useContext();
185
+ const context = useMemo(() => ambient.push(), []);
186
+ context.include(included).forEach((isExplicit, model) => {
158
187
  if (assign && isExplicit) {
159
188
  for (const K in assign)
160
189
  if (K in model)
161
190
  model.set(K, assign[K]);
162
191
  }
163
- setContext(model, value);
192
+ setContext(model, context);
164
193
  });
165
- useEffect(() => () => value.pop(), []);
166
- return createContext(value, props.children);
194
+ useEffect(() => () => context.pop(), []);
195
+ return createContext(context, props.children);
167
196
  }
168
197
 
169
198
  // src/index.ts
170
- Model2.get = useRemote;
171
- Model2.use = useLocal;
172
- Context2.get = getContext;
199
+ Model3.at = fromContext;
200
+ Model3.as = component;
201
+ Model3.get = useRemote;
202
+ Model3.use = useLocal;
173
203
  export {
174
204
  Consumer,
175
- Model2 as Model,
205
+ Model3 as Model,
176
206
  Provider,
177
207
  add,
178
- Model2 as default,
208
+ Model3 as default,
179
209
  get,
210
+ has,
180
211
  ref,
181
212
  set,
182
213
  use
package/package.json CHANGED
@@ -31,7 +31,7 @@
31
31
  "preversion": "npm run test && npm run build"
32
32
  },
33
33
  "dependencies": {
34
- "@expressive/mvc": "^0.37.0"
34
+ "@expressive/mvc": "^0.39.0"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "react": ">=16.8.0 <19.0.0"
@@ -42,6 +42,6 @@
42
42
  "react": "^18.2.0",
43
43
  "react-test-renderer": "^18.2.0"
44
44
  },
45
- "version": "0.37.0",
46
- "gitHead": "aac8ff030e577922a5fceb87d9b0b5d44bed8f54"
45
+ "version": "0.39.0",
46
+ "gitHead": "40cda872145802a7e87964695840d7ee27053844"
47
47
  }