@expressive/react 0.38.0 → 0.40.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 {
@@ -93,6 +93,12 @@ declare module '@expressive/mvc' {
93
93
  using: Model.Type<T>;
94
94
  displayName: string;
95
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>>;
96
102
  /** Fetch instance of this class from context. */
97
103
  function get<T extends Model>(this: Model.Type<T>, ignore?: true): T;
98
104
  /** Fetch instance of this class optionally. May be undefined, but will never subscribe. */
@@ -101,12 +107,6 @@ declare module '@expressive/mvc' {
101
107
  function get<T extends Model, R>(this: Model.Type<T>, factory: get.Factory<T, null>): NoVoid<R> | null;
102
108
  function use<T extends Model>(this: Model.New<T>, apply?: Model.Values<T>, repeat?: boolean): T;
103
109
  function use<T extends Model>(this: Model.New<T>, callback?: ((instance: T) => void), repeat?: boolean): T;
104
- /**
105
- * Creates a component which reflects this Model. All managed properties may be assigned using props.
106
- *
107
- * @param render Function which renders component. This function receives all Model state merged with props. Normal subscription behavior still applies.
108
- */
109
- function as<T extends Model, P = {}>(this: Model.New<T>, render: (using: T & P) => React.ReactNode): Component<T, P & Model.Values<T>>;
110
110
  }
111
111
  }
112
112
 
package/dist/index.js CHANGED
@@ -26,6 +26,7 @@ __export(src_exports, {
26
26
  add: () => import_mvc4.add,
27
27
  default: () => import_mvc3.Model,
28
28
  get: () => import_mvc4.get,
29
+ has: () => import_mvc4.has,
29
30
  ref: () => import_mvc4.ref,
30
31
  set: () => import_mvc4.set,
31
32
  use: () => import_mvc4.use
@@ -34,7 +35,7 @@ module.exports = __toCommonJS(src_exports);
34
35
  var import_mvc3 = require("@expressive/mvc");
35
36
 
36
37
  // src/component.ts
37
- function component(render) {
38
+ function createComponent(render) {
38
39
  const Component = (inputProps) => {
39
40
  const props = this.use(inputProps, true);
40
41
  for (const key in inputProps)
@@ -57,18 +58,21 @@ function useContext() {
57
58
  return (0, import_react.useContext)(Shared);
58
59
  }
59
60
  function createContext(value, children) {
60
- 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);
61
62
  }
62
- function getContext(model, resolve) {
63
- const waiting = Register.get(model);
63
+ function usingContext(from, resolve) {
64
+ const waiting = Register.get(from);
65
+ const context = (context2) => {
66
+ resolve(context2.get(this));
67
+ };
64
68
  if (waiting instanceof import_mvc.Context)
65
- resolve(waiting);
69
+ context(waiting);
66
70
  else if (waiting)
67
- waiting.push(resolve);
71
+ waiting.push(context);
68
72
  else
69
- Register.set(model, [resolve]);
73
+ Register.set(from, [context]);
70
74
  }
71
- function setContext(model, context = useContext()) {
75
+ function setContext(model, context) {
72
76
  const waiting = Register.get(model);
73
77
  if (waiting instanceof Array)
74
78
  waiting.forEach((cb) => cb(context));
@@ -113,7 +117,9 @@ function useLocal(argument, repeat) {
113
117
  else
114
118
  enabled = true;
115
119
  }
116
- setContext(instance);
120
+ const context = useContext();
121
+ setContext(instance, context);
122
+ context.has(instance);
117
123
  (0, import_react2.useEffect)(() => {
118
124
  enabled = true;
119
125
  return () => {
@@ -195,11 +201,11 @@ var import_mvc4 = require("@expressive/mvc");
195
201
 
196
202
  // src/consumer.ts
197
203
  function Consumer(props) {
198
- const { children, has, get: get2, for: Type } = props;
204
+ const { children, has: has2, get: get2, for: Type } = props;
199
205
  if (typeof children == "function")
200
206
  return children(Type.get());
201
- const instance = Type.get(!!has);
202
- const callback = has || get2;
207
+ const instance = Type.get(!!has2);
208
+ const callback = has2 || get2;
203
209
  if (typeof callback == "function")
204
210
  callback(instance);
205
211
  return null;
@@ -208,25 +214,25 @@ function Consumer(props) {
208
214
  // src/provider.ts
209
215
  function Provider(props) {
210
216
  let { for: included, use: assign } = props;
211
- const context = useContext();
212
- const value = (0, import_react2.useMemo)(() => context.push(), []);
213
- 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) => {
214
220
  if (assign && isExplicit) {
215
221
  for (const K in assign)
216
222
  if (K in model)
217
223
  model.set(K, assign[K]);
218
224
  }
219
- setContext(model, value);
225
+ setContext(model, context);
220
226
  });
221
- (0, import_react2.useEffect)(() => () => value.pop(), []);
222
- return createContext(value, props.children);
227
+ (0, import_react2.useEffect)(() => () => context.pop(), []);
228
+ return createContext(context, props.children);
223
229
  }
224
230
 
225
231
  // src/index.ts
232
+ import_mvc3.Model.at = usingContext;
233
+ import_mvc3.Model.as = createComponent;
226
234
  import_mvc3.Model.get = useRemote;
227
235
  import_mvc3.Model.use = useLocal;
228
- import_mvc3.Model.as = component;
229
- import_mvc3.Context.get = getContext;
230
236
  // Annotate the CommonJS export names for ESM import in node:
231
237
  0 && (module.exports = {
232
238
  Consumer,
@@ -234,6 +240,7 @@ import_mvc3.Context.get = getContext;
234
240
  Provider,
235
241
  add,
236
242
  get,
243
+ has,
237
244
  ref,
238
245
  set,
239
246
  use
package/dist/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  // src/index.ts
2
- import { Context as Context2, Model as Model3 } from "@expressive/mvc";
2
+ import { Model as Model3 } from "@expressive/mvc";
3
3
 
4
4
  // src/component.ts
5
- function component(render) {
5
+ function createComponent(render) {
6
6
  const Component = (inputProps) => {
7
7
  const props = this.use(inputProps, true);
8
8
  for (const key in inputProps)
@@ -17,11 +17,7 @@ function component(render) {
17
17
 
18
18
  // src/useContext.ts
19
19
  import { Context } from "@expressive/mvc";
20
- import {
21
- createContext as reactCreateContext,
22
- useContext as reactUseContext,
23
- createElement
24
- } from "react";
20
+ import { createContext as reactCreateContext, createElement, useContext as reactUseContext } from "react";
25
21
  import { useState, useEffect, useMemo } from "react";
26
22
  var Shared = reactCreateContext(new Context());
27
23
  var Register = /* @__PURE__ */ new WeakMap();
@@ -29,18 +25,21 @@ function useContext() {
29
25
  return reactUseContext(Shared);
30
26
  }
31
27
  function createContext(value, children) {
32
- return createElement(Shared.Provider, { key: value.key, value }, children);
28
+ return createElement(Shared.Provider, { key: value.id, value }, children);
33
29
  }
34
- function getContext(model, resolve) {
35
- const waiting = Register.get(model);
30
+ function usingContext(from, resolve) {
31
+ const waiting = Register.get(from);
32
+ const context = (context2) => {
33
+ resolve(context2.get(this));
34
+ };
36
35
  if (waiting instanceof Context)
37
- resolve(waiting);
36
+ context(waiting);
38
37
  else if (waiting)
39
- waiting.push(resolve);
38
+ waiting.push(context);
40
39
  else
41
- Register.set(model, [resolve]);
40
+ Register.set(from, [context]);
42
41
  }
43
- function setContext(model, context = useContext()) {
42
+ function setContext(model, context) {
44
43
  const waiting = Register.get(model);
45
44
  if (waiting instanceof Array)
46
45
  waiting.forEach((cb) => cb(context));
@@ -85,7 +84,9 @@ function useLocal(argument, repeat) {
85
84
  else
86
85
  enabled = true;
87
86
  }
88
- setContext(instance);
87
+ const context = useContext();
88
+ setContext(instance, context);
89
+ context.has(instance);
89
90
  useEffect(() => {
90
91
  enabled = true;
91
92
  return () => {
@@ -163,15 +164,15 @@ function useRemote(argument) {
163
164
  }
164
165
 
165
166
  // src/index.ts
166
- import { add, get, use, ref, set } from "@expressive/mvc";
167
+ import { add, get, use, ref, set, has } from "@expressive/mvc";
167
168
 
168
169
  // src/consumer.ts
169
170
  function Consumer(props) {
170
- const { children, has, get: get2, for: Type } = props;
171
+ const { children, has: has2, get: get2, for: Type } = props;
171
172
  if (typeof children == "function")
172
173
  return children(Type.get());
173
- const instance = Type.get(!!has);
174
- const callback = has || get2;
174
+ const instance = Type.get(!!has2);
175
+ const callback = has2 || get2;
175
176
  if (typeof callback == "function")
176
177
  callback(instance);
177
178
  return null;
@@ -180,25 +181,25 @@ function Consumer(props) {
180
181
  // src/provider.ts
181
182
  function Provider(props) {
182
183
  let { for: included, use: assign } = props;
183
- const context = useContext();
184
- const value = useMemo(() => context.push(), []);
185
- value.include(included).forEach((isExplicit, model) => {
184
+ const ambient = useContext();
185
+ const context = useMemo(() => ambient.push(), []);
186
+ context.include(included).forEach((isExplicit, model) => {
186
187
  if (assign && isExplicit) {
187
188
  for (const K in assign)
188
189
  if (K in model)
189
190
  model.set(K, assign[K]);
190
191
  }
191
- setContext(model, value);
192
+ setContext(model, context);
192
193
  });
193
- useEffect(() => () => value.pop(), []);
194
- return createContext(value, props.children);
194
+ useEffect(() => () => context.pop(), []);
195
+ return createContext(context, props.children);
195
196
  }
196
197
 
197
198
  // src/index.ts
199
+ Model3.at = usingContext;
200
+ Model3.as = createComponent;
198
201
  Model3.get = useRemote;
199
202
  Model3.use = useLocal;
200
- Model3.as = component;
201
- Context2.get = getContext;
202
203
  export {
203
204
  Consumer,
204
205
  Model3 as Model,
@@ -206,6 +207,7 @@ export {
206
207
  add,
207
208
  Model3 as default,
208
209
  get,
210
+ has,
209
211
  ref,
210
212
  set,
211
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.38.0"
34
+ "@expressive/mvc": "^0.40.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.38.0",
46
- "gitHead": "8f3d4c7939e483043de4391ee0edb877bb7da63c"
45
+ "version": "0.40.0",
46
+ "gitHead": "1eb515499dc2d6571d0cc97406192ce8eb0714d2"
47
47
  }