@expressive/react 0.37.0 → 0.38.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
@@ -88,8 +88,10 @@ 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
  }
94
96
  /** Fetch instance of this class from context. */
95
97
  function get<T extends Model>(this: Model.Type<T>, ignore?: true): T;
@@ -98,7 +100,13 @@ declare module '@expressive/mvc' {
98
100
  function get<T extends Model, R>(this: Model.Type<T>, factory: get.Factory<T, Promise<R> | R>): NoVoid<R>;
99
101
  function get<T extends Model, R>(this: Model.Type<T>, factory: get.Factory<T, null>): NoVoid<R> | null;
100
102
  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;
103
+ 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>>;
102
110
  }
103
111
  }
104
112
 
package/dist/index.js CHANGED
@@ -21,17 +21,31 @@ 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
+ ref: () => import_mvc4.ref,
30
+ set: () => import_mvc4.set,
31
+ use: () => import_mvc4.use
32
32
  });
33
33
  module.exports = __toCommonJS(src_exports);
34
- var import_mvc2 = require("@expressive/mvc");
34
+ var import_mvc3 = require("@expressive/mvc");
35
+
36
+ // src/component.ts
37
+ function component(render) {
38
+ const Component = (inputProps) => {
39
+ const props = this.use(inputProps, true);
40
+ for (const key in inputProps)
41
+ if (!(key in props))
42
+ Object.defineProperty(props, key, { value: inputProps[key] });
43
+ return render(props);
44
+ };
45
+ Component.using = this;
46
+ Component.displayName = this.name;
47
+ return Component;
48
+ }
35
49
 
36
50
  // src/useContext.ts
37
51
  var import_mvc = require("@expressive/mvc");
@@ -62,29 +76,37 @@ function setContext(model, context = useContext()) {
62
76
  }
63
77
 
64
78
  // src/useLocal.ts
65
- function useLocal(apply, repeat) {
79
+ var import_mvc2 = require("@expressive/mvc");
80
+ var PENDING = /* @__PURE__ */ new WeakMap();
81
+ function useLocal(argument, repeat) {
66
82
  const state = (0, import_react2.useState)(() => {
67
- let shouldApply = !!apply;
83
+ let apply;
68
84
  let enabled;
69
85
  let local;
70
- const instance = this.new();
86
+ const instance = new this();
87
+ if (argument)
88
+ PENDING.set(instance, apply = () => {
89
+ if (typeof argument == "function")
90
+ argument(instance);
91
+ else if (argument) {
92
+ for (const key in instance)
93
+ if (argument.hasOwnProperty(key))
94
+ instance[key] = argument[key];
95
+ }
96
+ if (!repeat)
97
+ apply = void 0;
98
+ });
99
+ instance.set(0);
71
100
  const release = instance.get((current) => {
72
101
  local = current;
73
102
  if (enabled)
74
103
  state[1]((x) => x.bind(null));
75
104
  });
76
105
  return (props) => {
77
- if (shouldApply) {
106
+ argument = props;
107
+ if (apply && enabled) {
78
108
  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;
109
+ apply();
88
110
  const update = instance.set();
89
111
  if (update)
90
112
  update.then(() => enabled = true);
@@ -102,8 +124,14 @@ function useLocal(apply, repeat) {
102
124
  return local;
103
125
  };
104
126
  });
105
- return state[0](apply);
127
+ return state[0](argument);
106
128
  }
129
+ import_mvc2.Model.on((_, subject) => {
130
+ const pending = PENDING.get(subject);
131
+ if (pending)
132
+ pending();
133
+ return null;
134
+ });
107
135
 
108
136
  // src/useRemote.ts
109
137
  function useRemote(argument) {
@@ -163,7 +191,7 @@ function useRemote(argument) {
163
191
  }
164
192
 
165
193
  // src/index.ts
166
- var import_mvc3 = require("@expressive/mvc");
194
+ var import_mvc4 = require("@expressive/mvc");
167
195
 
168
196
  // src/consumer.ts
169
197
  function Consumer(props) {
@@ -195,9 +223,10 @@ function Provider(props) {
195
223
  }
196
224
 
197
225
  // src/index.ts
198
- import_mvc2.Model.get = useRemote;
199
- import_mvc2.Model.use = useLocal;
200
- import_mvc2.Context.get = getContext;
226
+ import_mvc3.Model.get = useRemote;
227
+ import_mvc3.Model.use = useLocal;
228
+ import_mvc3.Model.as = component;
229
+ import_mvc3.Context.get = getContext;
201
230
  // Annotate the CommonJS export names for ESM import in node:
202
231
  0 && (module.exports = {
203
232
  Consumer,
package/dist/index.mjs CHANGED
@@ -1,5 +1,19 @@
1
1
  // src/index.ts
2
- import { Context as Context2, Model as Model2 } from "@expressive/mvc";
2
+ import { Context as Context2, 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";
@@ -34,29 +48,37 @@ function setContext(model, context = useContext()) {
34
48
  }
35
49
 
36
50
  // src/useLocal.ts
37
- function useLocal(apply, repeat) {
51
+ import { Model as Model2 } from "@expressive/mvc";
52
+ var PENDING = /* @__PURE__ */ new WeakMap();
53
+ function useLocal(argument, repeat) {
38
54
  const state = useState(() => {
39
- let shouldApply = !!apply;
55
+ let apply;
40
56
  let enabled;
41
57
  let local;
42
- const instance = this.new();
58
+ const instance = new this();
59
+ if (argument)
60
+ PENDING.set(instance, apply = () => {
61
+ if (typeof argument == "function")
62
+ argument(instance);
63
+ else if (argument) {
64
+ for (const key in instance)
65
+ if (argument.hasOwnProperty(key))
66
+ instance[key] = argument[key];
67
+ }
68
+ if (!repeat)
69
+ apply = void 0;
70
+ });
71
+ instance.set(0);
43
72
  const release = instance.get((current) => {
44
73
  local = current;
45
74
  if (enabled)
46
75
  state[1]((x) => x.bind(null));
47
76
  });
48
77
  return (props) => {
49
- if (shouldApply) {
78
+ argument = props;
79
+ if (apply && enabled) {
50
80
  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;
81
+ apply();
60
82
  const update = instance.set();
61
83
  if (update)
62
84
  update.then(() => enabled = true);
@@ -74,8 +96,14 @@ function useLocal(apply, repeat) {
74
96
  return local;
75
97
  };
76
98
  });
77
- return state[0](apply);
99
+ return state[0](argument);
78
100
  }
101
+ Model2.on((_, subject) => {
102
+ const pending = PENDING.get(subject);
103
+ if (pending)
104
+ pending();
105
+ return null;
106
+ });
79
107
 
80
108
  // src/useRemote.ts
81
109
  function useRemote(argument) {
@@ -167,15 +195,16 @@ function Provider(props) {
167
195
  }
168
196
 
169
197
  // src/index.ts
170
- Model2.get = useRemote;
171
- Model2.use = useLocal;
198
+ Model3.get = useRemote;
199
+ Model3.use = useLocal;
200
+ Model3.as = component;
172
201
  Context2.get = getContext;
173
202
  export {
174
203
  Consumer,
175
- Model2 as Model,
204
+ Model3 as Model,
176
205
  Provider,
177
206
  add,
178
- Model2 as default,
207
+ Model3 as default,
179
208
  get,
180
209
  ref,
181
210
  set,
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.38.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.38.0",
46
+ "gitHead": "8f3d4c7939e483043de4391ee0edb877bb7da63c"
47
47
  }