@expressive/react 0.47.0 → 0.49.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
@@ -100,9 +100,11 @@ declare module '@expressive/mvc' {
100
100
  */
101
101
  function as<T extends Model, P = {}>(this: Model.Type<T>, render: (using: T & P) => React.ReactNode): Component<T, P & Model.Assign<T>>;
102
102
  /** Fetch instance of this class from context. */
103
- function get<T extends Model>(this: Model.Type<T>, passive?: true): T;
104
- /** Fetch instance of this class optionally. May be undefined, but will never subscribe. */
105
- function get<T extends Model>(this: Model.Type<T>, required: boolean): T | undefined;
103
+ function get<T extends Model>(this: Model.Type<T>): T;
104
+ /** Fetch instance of this class optionally. */
105
+ function get<T extends Model>(this: Model.Type<T>, expect: false): T | undefined;
106
+ /** Fetch instance of this class from context. */
107
+ function get<T extends Model>(this: Model.Type<T>, expectValues: true): Required<T>;
106
108
  function get<T extends Model, R>(this: Model.Type<T>, factory: get.Factory<T, Promise<R> | R>): NoVoid<R>;
107
109
  function get<T extends Model, R>(this: Model.Type<T>, factory: get.Factory<T, null>): NoVoid<R> | null;
108
110
  function use<T extends Model>(this: Model.Type<T>, apply?: Model.Assign<T>, repeat?: boolean): T;
package/dist/index.js CHANGED
@@ -21,17 +21,17 @@ 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_mvc4.Model,
25
25
  Provider: () => Provider,
26
- default: () => import_mvc2.Model,
27
- get: () => import_mvc3.get,
28
- has: () => import_mvc3.has,
29
- ref: () => import_mvc3.ref,
30
- set: () => import_mvc3.set,
31
- use: () => import_mvc3.use
26
+ default: () => import_mvc4.Model,
27
+ get: () => import_mvc5.get,
28
+ has: () => import_mvc5.has,
29
+ ref: () => import_mvc5.ref,
30
+ set: () => import_mvc5.set,
31
+ use: () => import_mvc5.use
32
32
  });
33
33
  module.exports = __toCommonJS(src_exports);
34
- var import_mvc2 = require("@expressive/mvc");
34
+ var import_mvc4 = require("@expressive/mvc");
35
35
 
36
36
  // src/component.ts
37
37
  function createComponent(render) {
@@ -52,31 +52,8 @@ var import_mvc = require("@expressive/mvc");
52
52
  var import_react = require("react");
53
53
  var import_react2 = require("react");
54
54
  var Shared = (0, import_react.createContext)(new import_mvc.Context());
55
- var Register = /* @__PURE__ */ new WeakMap();
56
- function useContext() {
57
- return (0, import_react.useContext)(Shared);
58
- }
59
- function createContext(value, children) {
60
- return (0, import_react.createElement)(Shared.Provider, { key: value.id, value }, children);
61
- }
62
- function usingContext(from, resolve) {
63
- const waiting = Register.get(from);
64
- const callback = (context) => {
65
- resolve(context.get(this));
66
- };
67
- if (waiting instanceof import_mvc.Context)
68
- callback(waiting);
69
- else if (waiting)
70
- waiting.push(callback);
71
- else
72
- Register.set(from, [callback]);
73
- }
74
- function setContext(model, context) {
75
- const waiting = Register.get(model);
76
- if (waiting instanceof Array)
77
- waiting.forEach((cb) => cb(context));
78
- Register.set(model, context);
79
- }
55
+ var useContext = () => (0, import_react.useContext)(Shared);
56
+ var createContext = (value, children) => (0, import_react.createElement)(Shared.Provider, { key: value.id, value }, children);
80
57
 
81
58
  // src/useLocal.ts
82
59
  function useLocal(argument, repeat) {
@@ -103,7 +80,6 @@ function useLocal(argument, repeat) {
103
80
  enabled = true;
104
81
  }
105
82
  const context = useContext();
106
- setContext(instance, context);
107
83
  context.has(instance);
108
84
  if (!local)
109
85
  instance.set();
@@ -121,6 +97,7 @@ function useLocal(argument, repeat) {
121
97
  }
122
98
 
123
99
  // src/useRemote.ts
100
+ var import_mvc2 = require("@expressive/mvc");
124
101
  function useRemote(argument) {
125
102
  const context = useContext();
126
103
  const state = (0, import_react2.useState)(() => {
@@ -132,8 +109,6 @@ function useRemote(argument) {
132
109
  };
133
110
  else
134
111
  throw new Error(`Could not find ${this} in context.`);
135
- if (typeof argument === "boolean")
136
- return () => instance;
137
112
  function forceUpdate(action) {
138
113
  if (typeof action == "function")
139
114
  action = action();
@@ -143,7 +118,7 @@ function useRemote(argument) {
143
118
  }
144
119
  let release;
145
120
  let value;
146
- release = instance.get((current) => {
121
+ release = (0, import_mvc2.effect)(instance, (current) => {
147
122
  if (typeof argument === "function") {
148
123
  const next = argument.call(current, current, forceUpdate);
149
124
  if (next === value)
@@ -153,7 +128,7 @@ function useRemote(argument) {
153
128
  value = current;
154
129
  if (release)
155
130
  refresh();
156
- });
131
+ }, argument === true);
157
132
  if (value instanceof Promise) {
158
133
  let error;
159
134
  release();
@@ -178,14 +153,20 @@ function useRemote(argument) {
178
153
  }
179
154
 
180
155
  // src/index.ts
181
- var import_mvc3 = require("@expressive/mvc");
156
+ var import_mvc5 = require("@expressive/mvc");
182
157
 
183
158
  // src/consumer.ts
184
159
  function Consumer(props) {
185
160
  const { children, has: has2, get: get2, for: Type } = props;
186
161
  if (typeof children == "function")
187
162
  return children(Type.get());
188
- const instance = Type.get(!!has2);
163
+ const context = useContext();
164
+ const instance = (0, import_react2.useMemo)(() => {
165
+ const instance2 = context.get(Type);
166
+ if (!instance2 && has2)
167
+ throw new Error(`Could not find ${Type} in context.`);
168
+ return instance2;
169
+ }, []);
189
170
  const callback = has2 || get2;
190
171
  if (typeof callback == "function")
191
172
  callback(instance);
@@ -193,24 +174,25 @@ function Consumer(props) {
193
174
  }
194
175
 
195
176
  // src/provider.ts
177
+ var import_mvc3 = require("@expressive/mvc");
196
178
  function Provider(props) {
197
- let { for: included, use: assign } = props;
179
+ let { for: include, use: assign } = props;
198
180
  const ambient = useContext();
199
181
  const context = (0, import_react2.useMemo)(() => ambient.push(), []);
200
- context.include(included, (model, explicit) => {
182
+ if (typeof include == "function" || include instanceof import_mvc3.Model)
183
+ include = { [""]: include };
184
+ context.include(include, (model, explicit) => {
201
185
  if (assign && explicit)
202
186
  model.set(assign);
203
- setContext(model, context);
204
187
  });
205
188
  (0, import_react2.useEffect)(() => () => context.pop(), []);
206
189
  return createContext(context, props.children);
207
190
  }
208
191
 
209
192
  // src/index.ts
210
- import_mvc2.Model.at = usingContext;
211
- import_mvc2.Model.as = createComponent;
212
- import_mvc2.Model.get = useRemote;
213
- import_mvc2.Model.use = useLocal;
193
+ import_mvc4.Model.as = createComponent;
194
+ import_mvc4.Model.get = useRemote;
195
+ import_mvc4.Model.use = useLocal;
214
196
  // Annotate the CommonJS export names for ESM import in node:
215
197
  0 && (module.exports = {
216
198
  Consumer,
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/index.ts
2
- import { Model as Model2 } from "@expressive/mvc";
2
+ import { Model as Model3 } from "@expressive/mvc";
3
3
 
4
4
  // src/component.ts
5
5
  function createComponent(render) {
@@ -20,31 +20,8 @@ import { Context } from "@expressive/mvc";
20
20
  import { createContext as reactCreateContext, createElement, useContext as reactUseContext } from "react";
21
21
  import { useState, useEffect, useMemo } from "react";
22
22
  var Shared = reactCreateContext(new Context());
23
- var Register = /* @__PURE__ */ new WeakMap();
24
- function useContext() {
25
- return reactUseContext(Shared);
26
- }
27
- function createContext(value, children) {
28
- return createElement(Shared.Provider, { key: value.id, value }, children);
29
- }
30
- function usingContext(from, resolve) {
31
- const waiting = Register.get(from);
32
- const callback = (context) => {
33
- resolve(context.get(this));
34
- };
35
- if (waiting instanceof Context)
36
- callback(waiting);
37
- else if (waiting)
38
- waiting.push(callback);
39
- else
40
- Register.set(from, [callback]);
41
- }
42
- function setContext(model, context) {
43
- const waiting = Register.get(model);
44
- if (waiting instanceof Array)
45
- waiting.forEach((cb) => cb(context));
46
- Register.set(model, context);
47
- }
23
+ var useContext = () => reactUseContext(Shared);
24
+ var createContext = (value, children) => createElement(Shared.Provider, { key: value.id, value }, children);
48
25
 
49
26
  // src/useLocal.ts
50
27
  function useLocal(argument, repeat) {
@@ -71,7 +48,6 @@ function useLocal(argument, repeat) {
71
48
  enabled = true;
72
49
  }
73
50
  const context = useContext();
74
- setContext(instance, context);
75
51
  context.has(instance);
76
52
  if (!local)
77
53
  instance.set();
@@ -89,6 +65,7 @@ function useLocal(argument, repeat) {
89
65
  }
90
66
 
91
67
  // src/useRemote.ts
68
+ import { effect } from "@expressive/mvc";
92
69
  function useRemote(argument) {
93
70
  const context = useContext();
94
71
  const state = useState(() => {
@@ -100,8 +77,6 @@ function useRemote(argument) {
100
77
  };
101
78
  else
102
79
  throw new Error(`Could not find ${this} in context.`);
103
- if (typeof argument === "boolean")
104
- return () => instance;
105
80
  function forceUpdate(action) {
106
81
  if (typeof action == "function")
107
82
  action = action();
@@ -111,7 +86,7 @@ function useRemote(argument) {
111
86
  }
112
87
  let release;
113
88
  let value;
114
- release = instance.get((current) => {
89
+ release = effect(instance, (current) => {
115
90
  if (typeof argument === "function") {
116
91
  const next = argument.call(current, current, forceUpdate);
117
92
  if (next === value)
@@ -121,7 +96,7 @@ function useRemote(argument) {
121
96
  value = current;
122
97
  if (release)
123
98
  refresh();
124
- });
99
+ }, argument === true);
125
100
  if (value instanceof Promise) {
126
101
  let error;
127
102
  release();
@@ -153,7 +128,13 @@ function Consumer(props) {
153
128
  const { children, has: has2, get: get2, for: Type } = props;
154
129
  if (typeof children == "function")
155
130
  return children(Type.get());
156
- const instance = Type.get(!!has2);
131
+ const context = useContext();
132
+ const instance = useMemo(() => {
133
+ const instance2 = context.get(Type);
134
+ if (!instance2 && has2)
135
+ throw new Error(`Could not find ${Type} in context.`);
136
+ return instance2;
137
+ }, []);
157
138
  const callback = has2 || get2;
158
139
  if (typeof callback == "function")
159
140
  callback(instance);
@@ -161,29 +142,30 @@ function Consumer(props) {
161
142
  }
162
143
 
163
144
  // src/provider.ts
145
+ import { Model as Model2 } from "@expressive/mvc";
164
146
  function Provider(props) {
165
- let { for: included, use: assign } = props;
147
+ let { for: include, use: assign } = props;
166
148
  const ambient = useContext();
167
149
  const context = useMemo(() => ambient.push(), []);
168
- context.include(included, (model, explicit) => {
150
+ if (typeof include == "function" || include instanceof Model2)
151
+ include = { [""]: include };
152
+ context.include(include, (model, explicit) => {
169
153
  if (assign && explicit)
170
154
  model.set(assign);
171
- setContext(model, context);
172
155
  });
173
156
  useEffect(() => () => context.pop(), []);
174
157
  return createContext(context, props.children);
175
158
  }
176
159
 
177
160
  // src/index.ts
178
- Model2.at = usingContext;
179
- Model2.as = createComponent;
180
- Model2.get = useRemote;
181
- Model2.use = useLocal;
161
+ Model3.as = createComponent;
162
+ Model3.get = useRemote;
163
+ Model3.use = useLocal;
182
164
  export {
183
165
  Consumer,
184
- Model2 as Model,
166
+ Model3 as Model,
185
167
  Provider,
186
- Model2 as default,
168
+ Model3 as default,
187
169
  get,
188
170
  has,
189
171
  ref,
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.47.0"
34
+ "@expressive/mvc": "^0.49.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.47.0",
46
- "gitHead": "7c0ddd5ea9b7e08bd5aa21aa8e1ab65ceef17fcf"
45
+ "version": "0.49.0",
46
+ "gitHead": "3a1423ef6d335d0e876904459d360a2e0ebdff7b"
47
47
  }