@expressive/react 0.49.0 → 0.50.1

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
@@ -41,7 +41,7 @@ declare namespace Provider {
41
41
  value: Context;
42
42
  children?: ReactNode;
43
43
  }>;
44
- type Item = Model | Model.Type;
44
+ type Item = Model | Model.Init;
45
45
  type Multiple<T extends Item = Item> = {
46
46
  [key: string | number]: T;
47
47
  };
@@ -98,7 +98,7 @@ declare module '@expressive/mvc' {
98
98
  *
99
99
  * @param render Function which renders component. This function receives all Model state merged with props. Normal subscription behavior still applies.
100
100
  */
101
- function as<T extends Model, P = {}>(this: Model.Type<T>, render: (using: T & P) => React.ReactNode): Component<T, P & Model.Assign<T>>;
101
+ function as<T extends Model, P = {}>(this: Model.Init<T>, render: (using: T & P) => React.ReactNode): Component<T, P & Model.Assign<T>>;
102
102
  /** Fetch instance of this class from context. */
103
103
  function get<T extends Model>(this: Model.Type<T>): T;
104
104
  /** Fetch instance of this class optionally. */
@@ -107,8 +107,8 @@ declare module '@expressive/mvc' {
107
107
  function get<T extends Model>(this: Model.Type<T>, expectValues: true): Required<T>;
108
108
  function get<T extends Model, R>(this: Model.Type<T>, factory: get.Factory<T, Promise<R> | R>): NoVoid<R>;
109
109
  function get<T extends Model, R>(this: Model.Type<T>, factory: get.Factory<T, null>): NoVoid<R> | null;
110
- function use<T extends Model>(this: Model.Type<T>, apply?: Model.Assign<T>, repeat?: boolean): T;
111
- function use<T extends Model>(this: Model.Type<T>, callback?: Model.Callback<T>, repeat?: boolean): T;
110
+ function use<T extends Model>(this: Model.Init<T>, apply?: Model.Assign<T>, repeat?: boolean): T;
111
+ function use<T extends Model>(this: Model.Init<T>, callback?: Model.Callback<T>, repeat?: boolean): T;
112
112
  }
113
113
  }
114
114
 
package/dist/index.js CHANGED
@@ -21,20 +21,22 @@ 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_mvc4.Model,
24
+ Model: () => import_mvc6.Model,
25
25
  Provider: () => Provider,
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
26
+ default: () => import_mvc6.Model,
27
+ get: () => import_mvc7.get,
28
+ has: () => import_mvc7.has,
29
+ ref: () => import_mvc7.ref,
30
+ set: () => import_mvc7.set,
31
+ use: () => import_mvc7.use
32
32
  });
33
33
  module.exports = __toCommonJS(src_exports);
34
- var import_mvc4 = require("@expressive/mvc");
34
+ var import_mvc6 = require("@expressive/mvc");
35
35
 
36
36
  // src/component.ts
37
+ var import_mvc = require("@expressive/mvc");
37
38
  function createComponent(render) {
39
+ import_mvc.Model.is(this);
38
40
  const Component = (inputProps) => {
39
41
  const props = this.use(inputProps, true);
40
42
  for (const key in inputProps)
@@ -47,19 +49,25 @@ function createComponent(render) {
47
49
  return Component;
48
50
  }
49
51
 
52
+ // src/useLocal.ts
53
+ var import_mvc3 = require("@expressive/mvc");
54
+
50
55
  // src/useContext.ts
51
- var import_mvc = require("@expressive/mvc");
56
+ var import_mvc2 = require("@expressive/mvc");
52
57
  var import_react = require("react");
53
58
  var import_react2 = require("react");
54
- var Shared = (0, import_react.createContext)(new import_mvc.Context());
55
- var useContext = () => (0, import_react.useContext)(Shared);
56
- var createContext = (value, children) => (0, import_react.createElement)(Shared.Provider, { key: value.id, value }, children);
59
+ var Shared = (0, import_react.createContext)(new import_mvc2.Context());
60
+ var useShared = () => (0, import_react.useContext)(Shared);
61
+ var createProvider = (value, children) => (0, import_react.createElement)(Shared.Provider, { key: value.id, value }, children);
57
62
 
58
63
  // src/useLocal.ts
59
64
  function useLocal(argument, repeat) {
65
+ const outer = useShared();
60
66
  const state = (0, import_react2.useState)(() => {
61
67
  let enabled;
68
+ let context;
62
69
  let local;
70
+ import_mvc3.Model.is(this);
63
71
  const instance = new this(argument);
64
72
  const release = instance.get((current) => {
65
73
  local = current;
@@ -67,6 +75,8 @@ function useLocal(argument, repeat) {
67
75
  state[1]((x) => x.bind(null));
68
76
  });
69
77
  return (props) => {
78
+ if (!context)
79
+ context = outer.push({ instance });
70
80
  if (repeat && enabled) {
71
81
  enabled = false;
72
82
  if (typeof props == "function")
@@ -79,15 +89,12 @@ function useLocal(argument, repeat) {
79
89
  else
80
90
  enabled = true;
81
91
  }
82
- const context = useContext();
83
- context.has(instance);
84
- if (!local)
85
- instance.set();
86
92
  (0, import_react2.useEffect)(() => {
87
93
  enabled = true;
88
94
  return () => {
89
95
  release();
90
96
  instance.set(null);
97
+ context.pop();
91
98
  };
92
99
  }, []);
93
100
  return local;
@@ -97,9 +104,9 @@ function useLocal(argument, repeat) {
97
104
  }
98
105
 
99
106
  // src/useRemote.ts
100
- var import_mvc2 = require("@expressive/mvc");
107
+ var import_mvc4 = require("@expressive/mvc");
101
108
  function useRemote(argument) {
102
- const context = useContext();
109
+ const context = useShared();
103
110
  const state = (0, import_react2.useState)(() => {
104
111
  const instance = context.get(this);
105
112
  const refresh = () => state[1]((x) => x.bind(null));
@@ -118,7 +125,7 @@ function useRemote(argument) {
118
125
  }
119
126
  let release;
120
127
  let value;
121
- release = (0, import_mvc2.effect)(instance, (current) => {
128
+ release = (0, import_mvc4.effect)(instance, (current) => {
122
129
  if (typeof argument === "function") {
123
130
  const next = argument.call(current, current, forceUpdate);
124
131
  if (next === value)
@@ -153,14 +160,14 @@ function useRemote(argument) {
153
160
  }
154
161
 
155
162
  // src/index.ts
156
- var import_mvc5 = require("@expressive/mvc");
163
+ var import_mvc7 = require("@expressive/mvc");
157
164
 
158
165
  // src/consumer.ts
159
166
  function Consumer(props) {
160
167
  const { children, has: has2, get: get2, for: Type } = props;
161
168
  if (typeof children == "function")
162
- return children(Type.get());
163
- const context = useContext();
169
+ return Type.get(children);
170
+ const context = useShared();
164
171
  const instance = (0, import_react2.useMemo)(() => {
165
172
  const instance2 = context.get(Type);
166
173
  if (!instance2 && has2)
@@ -174,25 +181,25 @@ function Consumer(props) {
174
181
  }
175
182
 
176
183
  // src/provider.ts
177
- var import_mvc3 = require("@expressive/mvc");
184
+ var import_mvc5 = require("@expressive/mvc");
178
185
  function Provider(props) {
179
186
  let { for: include, use: assign } = props;
180
- const ambient = useContext();
187
+ const ambient = useShared();
181
188
  const context = (0, import_react2.useMemo)(() => ambient.push(), []);
182
- if (typeof include == "function" || include instanceof import_mvc3.Model)
189
+ if (typeof include == "function" || include instanceof import_mvc5.Model)
183
190
  include = { [""]: include };
184
191
  context.include(include, (model, explicit) => {
185
192
  if (assign && explicit)
186
193
  model.set(assign);
187
194
  });
188
195
  (0, import_react2.useEffect)(() => () => context.pop(), []);
189
- return createContext(context, props.children);
196
+ return createProvider(context, props.children);
190
197
  }
191
198
 
192
199
  // src/index.ts
193
- import_mvc4.Model.as = createComponent;
194
- import_mvc4.Model.get = useRemote;
195
- import_mvc4.Model.use = useLocal;
200
+ import_mvc6.Model.as = createComponent;
201
+ import_mvc6.Model.get = useRemote;
202
+ import_mvc6.Model.use = useLocal;
196
203
  // Annotate the CommonJS export names for ESM import in node:
197
204
  0 && (module.exports = {
198
205
  Consumer,
package/dist/index.mjs CHANGED
@@ -1,8 +1,10 @@
1
1
  // src/index.ts
2
- import { Model as Model3 } from "@expressive/mvc";
2
+ import { Model as Model5 } from "@expressive/mvc";
3
3
 
4
4
  // src/component.ts
5
+ import { Model } from "@expressive/mvc";
5
6
  function createComponent(render) {
7
+ Model.is(this);
6
8
  const Component = (inputProps) => {
7
9
  const props = this.use(inputProps, true);
8
10
  for (const key in inputProps)
@@ -15,19 +17,25 @@ function createComponent(render) {
15
17
  return Component;
16
18
  }
17
19
 
20
+ // src/useLocal.ts
21
+ import { Model as Model2 } from "@expressive/mvc";
22
+
18
23
  // src/useContext.ts
19
24
  import { Context } from "@expressive/mvc";
20
- import { createContext as reactCreateContext, createElement, useContext as reactUseContext } from "react";
25
+ import { createContext, createElement, useContext } from "react";
21
26
  import { useState, useEffect, useMemo } from "react";
22
- var Shared = reactCreateContext(new Context());
23
- var useContext = () => reactUseContext(Shared);
24
- var createContext = (value, children) => createElement(Shared.Provider, { key: value.id, value }, children);
27
+ var Shared = createContext(new Context());
28
+ var useShared = () => useContext(Shared);
29
+ var createProvider = (value, children) => createElement(Shared.Provider, { key: value.id, value }, children);
25
30
 
26
31
  // src/useLocal.ts
27
32
  function useLocal(argument, repeat) {
33
+ const outer = useShared();
28
34
  const state = useState(() => {
29
35
  let enabled;
36
+ let context;
30
37
  let local;
38
+ Model2.is(this);
31
39
  const instance = new this(argument);
32
40
  const release = instance.get((current) => {
33
41
  local = current;
@@ -35,6 +43,8 @@ function useLocal(argument, repeat) {
35
43
  state[1]((x) => x.bind(null));
36
44
  });
37
45
  return (props) => {
46
+ if (!context)
47
+ context = outer.push({ instance });
38
48
  if (repeat && enabled) {
39
49
  enabled = false;
40
50
  if (typeof props == "function")
@@ -47,15 +57,12 @@ function useLocal(argument, repeat) {
47
57
  else
48
58
  enabled = true;
49
59
  }
50
- const context = useContext();
51
- context.has(instance);
52
- if (!local)
53
- instance.set();
54
60
  useEffect(() => {
55
61
  enabled = true;
56
62
  return () => {
57
63
  release();
58
64
  instance.set(null);
65
+ context.pop();
59
66
  };
60
67
  }, []);
61
68
  return local;
@@ -67,7 +74,7 @@ function useLocal(argument, repeat) {
67
74
  // src/useRemote.ts
68
75
  import { effect } from "@expressive/mvc";
69
76
  function useRemote(argument) {
70
- const context = useContext();
77
+ const context = useShared();
71
78
  const state = useState(() => {
72
79
  const instance = context.get(this);
73
80
  const refresh = () => state[1]((x) => x.bind(null));
@@ -127,8 +134,8 @@ import { get, use, ref, set, has } from "@expressive/mvc";
127
134
  function Consumer(props) {
128
135
  const { children, has: has2, get: get2, for: Type } = props;
129
136
  if (typeof children == "function")
130
- return children(Type.get());
131
- const context = useContext();
137
+ return Type.get(children);
138
+ const context = useShared();
132
139
  const instance = useMemo(() => {
133
140
  const instance2 = context.get(Type);
134
141
  if (!instance2 && has2)
@@ -142,30 +149,30 @@ function Consumer(props) {
142
149
  }
143
150
 
144
151
  // src/provider.ts
145
- import { Model as Model2 } from "@expressive/mvc";
152
+ import { Model as Model4 } from "@expressive/mvc";
146
153
  function Provider(props) {
147
154
  let { for: include, use: assign } = props;
148
- const ambient = useContext();
155
+ const ambient = useShared();
149
156
  const context = useMemo(() => ambient.push(), []);
150
- if (typeof include == "function" || include instanceof Model2)
157
+ if (typeof include == "function" || include instanceof Model4)
151
158
  include = { [""]: include };
152
159
  context.include(include, (model, explicit) => {
153
160
  if (assign && explicit)
154
161
  model.set(assign);
155
162
  });
156
163
  useEffect(() => () => context.pop(), []);
157
- return createContext(context, props.children);
164
+ return createProvider(context, props.children);
158
165
  }
159
166
 
160
167
  // src/index.ts
161
- Model3.as = createComponent;
162
- Model3.get = useRemote;
163
- Model3.use = useLocal;
168
+ Model5.as = createComponent;
169
+ Model5.get = useRemote;
170
+ Model5.use = useLocal;
164
171
  export {
165
172
  Consumer,
166
- Model3 as Model,
173
+ Model5 as Model,
167
174
  Provider,
168
- Model3 as default,
175
+ Model5 as default,
169
176
  get,
170
177
  has,
171
178
  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.49.0"
34
+ "@expressive/mvc": "^0.50.1"
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.49.0",
46
- "gitHead": "3a1423ef6d335d0e876904459d360a2e0ebdff7b"
45
+ "version": "0.50.1",
46
+ "gitHead": "c74ee393d29e18ae4c4bc59bf73834ac21fb2b6b"
47
47
  }