mobx-react-use-autorun 3.0.8 → 3.0.11

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/LICENSE CHANGED
@@ -1,21 +1,24 @@
1
- MIT License
1
+ ## MIT License
2
2
 
3
- Copyright (c) 2021 Robert Taussig
3
+ Copyright (c) 2022 zdu https://github.com/zdu-strong/mobx-react-use-autorun
4
4
 
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
5
+ Permission is hereby granted, free of charge, to any person
6
+ obtaining a copy of this software and associated documentation
7
+ files (the "Software"), to deal in the Software without
8
+ restriction, including without limitation the rights to use,
9
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the
11
+ Software is furnished to do so, subject to the following
12
+ conditions:
11
13
 
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
14
16
 
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
+ OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -90,36 +90,6 @@ Form validation<br/>
90
90
  </div>
91
91
  })
92
92
 
93
- ## Notes - Ignore frequent asynchronous calls with useAsyncExhaust
94
-
95
- import { TextField } from "@mui/material";
96
- import { useMobxState, observer, useAsyncExhaust } from 'mobx-react-use-autorun';
97
- import axios from 'axios';
98
-
99
- export default observer(() => {
100
-
101
- const state = useMobxState({
102
- list: [],
103
- searchText: ""
104
- });
105
-
106
- const search = useAsyncExhaust(async () => {
107
- state.list = await axios.get("/search");
108
- });
109
-
110
- return (
111
- <TextField
112
- label="Please enter search content"
113
- variant="outlined"
114
- value={state.searchText}
115
- onChange={(e) => {
116
- state.searchText = e.target.value;
117
- search();
118
- }}
119
- />
120
- );
121
- })
122
-
123
93
  ## Notes - Define global mutable data
124
94
 
125
95
  import { observable } from 'mobx-react-use-autorun';
@@ -136,4 +106,8 @@ Form validation<br/>
136
106
  ## Learn More
137
107
 
138
108
  1. React UI framework (https://reactjs.org)<br/>
139
- 2. React hooks (https://www.npmjs.com/package/react-use)<br/>
109
+ 2. React hooks (https://www.npmjs.com/package/react-use)<br/>
110
+
111
+ ## License
112
+
113
+ This project is licensed under the terms of the [MIT license](./LICENSE.md).
package/dist/index.d.ts CHANGED
@@ -1,9 +1,7 @@
1
1
  import './lib/mobx_config';
2
2
  import { toJS, observable } from 'mobx';
3
3
  import { observer } from 'mobx-react-lite';
4
- import { useAsyncExhaust } from './lib/useAsyncExhaust';
5
4
  import { useMobxEffect } from './lib/useMobxEffect';
6
5
  import { useMobxState } from './lib/useMobxState';
7
6
  export { toJS, observable, observer };
8
7
  export { useMobxEffect, useMobxState };
9
- export { useAsyncExhaust };
package/dist/index.js CHANGED
@@ -1,10 +1,8 @@
1
1
  import './lib/mobx_config';
2
2
  import { toJS, observable } from 'mobx';
3
3
  import { observer } from 'mobx-react-lite';
4
- import { useAsyncExhaust } from './lib/useAsyncExhaust';
5
4
  import { useMobxEffect } from './lib/useMobxEffect';
6
5
  import { useMobxState } from './lib/useMobxState';
7
6
  export { toJS, observable, observer };
8
7
  export { useMobxEffect, useMobxState };
9
- export { useAsyncExhaust };
10
8
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.tsx"],"names":[],"mappings":"AAAA,OAAO,mBAAmB,CAAA;AAC1B,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAA;AACrC,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,CAAA;AACtC,OAAO,EAAE,eAAe,EAAE,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.tsx"],"names":[],"mappings":"AAAA,OAAO,mBAAmB,CAAA;AAC1B,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAA;AACrC,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,CAAA"}
package/lib/index.tsx CHANGED
@@ -1,10 +1,8 @@
1
1
  import './lib/mobx_config'
2
2
  import { toJS, observable } from 'mobx';
3
3
  import { observer } from 'mobx-react-lite';
4
- import { useAsyncExhaust } from './lib/useAsyncExhaust';
5
4
  import { useMobxEffect } from './lib/useMobxEffect';
6
5
  import { useMobxState } from './lib/useMobxState';
7
6
 
8
7
  export { toJS, observable, observer }
9
- export { useMobxEffect, useMobxState }
10
- export { useAsyncExhaust }
8
+ export { useMobxEffect, useMobxState }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobx-react-use-autorun",
3
- "version": "3.0.8",
3
+ "version": "3.0.11",
4
4
  "description": "React Hook for mobx",
5
5
  "scripts": {
6
6
  "build": "npm install -g typescript@4.6.3 @types/node@17.0.23 ts-node@10.7.0 cross-env@7.0.3 && cross-env TS_NODE_SKIP_PROJECT=true ts-node bin/mobx_react_build.ts",
@@ -10,9 +10,7 @@
10
10
  "dependencies": {
11
11
  "mobx": ">=6.5.0",
12
12
  "mobx-react-lite": ">=3.3.0",
13
- "react-use": ">=17.3.2",
14
- "rxjs": ">=7.5.5",
15
- "rxjs-exhaustmap-with-trailing": ">=2.0.0"
13
+ "react-use": ">=17.3.2"
16
14
  },
17
15
  "peerDependencies": {
18
16
  "react": ">=16.8.0"
@@ -1 +0,0 @@
1
- export declare function useAsyncExhaust<T>(callback: T): T;
@@ -1,134 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- var __generator = (this && this.__generator) || function (thisArg, body) {
11
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
- function verb(n) { return function (v) { return step([n, v]); }; }
14
- function step(op) {
15
- if (f) throw new TypeError("Generator is already executing.");
16
- while (_) try {
17
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
- if (y = 0, t) op = [op[0] & 2, t.value];
19
- switch (op[0]) {
20
- case 0: case 1: t = op; break;
21
- case 4: _.label++; return { value: op[1], done: false };
22
- case 5: _.label++; y = op[1]; op = [0]; continue;
23
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
- default:
25
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
- if (t[2]) _.ops.pop();
30
- _.trys.pop(); continue;
31
- }
32
- op = body.call(thisArg, _);
33
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
- }
36
- };
37
- import { toJS } from 'mobx';
38
- import { useRef } from 'react';
39
- import { useMount, useUnmount } from 'react-use';
40
- import { catchError, concatMap, debounceTime, EMPTY, from, lastValueFrom, ReplaySubject, Subscription } from 'rxjs';
41
- import { exhaustMapWithTrailing } from 'rxjs-exhaustmap-with-trailing';
42
- import { useMobxState } from './useMobxState';
43
- export function useAsyncExhaust(callback) {
44
- var _this = this;
45
- var runCallback = function () {
46
- if (state.isUnmount) {
47
- var resultSubjectPromise = Promise.reject(new Error("Cancelled!"));
48
- resultSubjectPromise.catch(function () { return null; });
49
- return resultSubjectPromise;
50
- }
51
- else {
52
- var resultSubject = new ReplaySubject(0);
53
- var resultSubjectPromise = lastValueFrom(resultSubject);
54
- state.resultSubjectList.push({
55
- subject: resultSubject,
56
- promise: resultSubjectPromise
57
- });
58
- state.subject.next({
59
- params: arguments
60
- });
61
- return resultSubjectPromise;
62
- }
63
- };
64
- var state = useRef({
65
- subscription: new Subscription(),
66
- subject: new ReplaySubject(0),
67
- resultSubjectList: [],
68
- isUnmount: false,
69
- }).current;
70
- var source = useMobxState({}, { callback: callback });
71
- useMount(function () {
72
- state.subscription.add(state.subject.pipe(debounceTime(0), exhaustMapWithTrailing(function (_a) {
73
- var params = _a.params;
74
- var resultSubjectList = toJS(state.resultSubjectList);
75
- return from((function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
76
- switch (_a.label) {
77
- case 0: return [4 /*yield*/, source.callback.apply(source, params)];
78
- case 1: return [2 /*return*/, _a.sent()];
79
- }
80
- }); }); })()).pipe(concatMap(function (result) {
81
- var _loop_1 = function (resultSubject) {
82
- resultSubject.subject.next(result);
83
- resultSubject.subject.complete();
84
- if (resultSubject.subject.closed) {
85
- var index = state.resultSubjectList.findIndex(function (s) { return s === resultSubject; });
86
- if (index >= 0) {
87
- state.resultSubjectList.splice(index, 1);
88
- }
89
- }
90
- };
91
- for (var _i = 0, resultSubjectList_1 = resultSubjectList; _i < resultSubjectList_1.length; _i++) {
92
- var resultSubject = resultSubjectList_1[_i];
93
- _loop_1(resultSubject);
94
- }
95
- return EMPTY;
96
- }), catchError(function (error) {
97
- var _loop_2 = function (resultSubject) {
98
- resultSubject.subject.error(error);
99
- if (resultSubject.subject.closed) {
100
- var index = state.resultSubjectList.findIndex(function (s) { return s === resultSubject; });
101
- if (index >= 0) {
102
- state.resultSubjectList.splice(index, 1);
103
- }
104
- }
105
- };
106
- for (var _i = 0, resultSubjectList_2 = resultSubjectList; _i < resultSubjectList_2.length; _i++) {
107
- var resultSubject = resultSubjectList_2[_i];
108
- _loop_2(resultSubject);
109
- }
110
- return EMPTY;
111
- }));
112
- })).subscribe());
113
- });
114
- useUnmount(function () {
115
- state.subscription.unsubscribe();
116
- state.isUnmount = true;
117
- var _loop_3 = function (resultSubject) {
118
- resultSubject.promise.catch(function () { return null; });
119
- resultSubject.subject.error(new Error("Cancelled!"));
120
- if (resultSubject.subject.closed) {
121
- var index = state.resultSubjectList.findIndex(function (s) { return s === resultSubject; });
122
- if (index >= 0) {
123
- state.resultSubjectList.splice(index, 1);
124
- }
125
- }
126
- };
127
- for (var _i = 0, _a = toJS(state.resultSubjectList); _i < _a.length; _i++) {
128
- var resultSubject = _a[_i];
129
- _loop_3(resultSubject);
130
- }
131
- });
132
- return runCallback;
133
- }
134
- //# sourceMappingURL=useAsyncExhaust.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useAsyncExhaust.js","sourceRoot":"","sources":["../../lib/lib/useAsyncExhaust.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AACpH,OAAO,EAAE,sBAAsB,EAAE,MAA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CAAC,CAAC;yBAC5C;qBACJ;;gBAPL,KAA4B,UAAiB,EAAjB,uCAAiB,EAAjB,+BAAiB,EAAjB,IAAiB;oBAAxC,IAAM,aAAa,0BAAA;4BAAb,aAAa;iBAQvB;gBACD,OAAO,KAAK,CAAC;YACjB,CAAC,CAAC,CACL,CAAC;QACN,CAAC,CAAC,CACL,CAAC,SAAS,EAAE,CAAC,CAAC;IACnB,CAAC,CAAC,CAAA;IAEF,UAAU,CAAC;QACP,KAAK,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC;QACjC,KAAK,CAAC,SAAS,GAAG,IAAI,CAAC;gCACZ,aAAa;YACpB,aAAa,CAAC,OAAO,CAAC,KAAK,CAAC,cAAM,OAAA,IAAI,EAAJ,CAAI,CAAC,CAAC;YACxC,aAAa,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;YACrD,IAAI,aAAa,CAAC,OAAO,CAAC,MAAM,EAAE;gBAC9B,IAAM,KAAK,GAAG,KAAK,CAAC,iBAAiB,CAAC,SAAS,CAAC,UAAA,CAAC,IAAI,OAAA,CAAC,KAAK,aAAa,EAAnB,CAAmB,CAAC,CAAC;gBAC1E,IAAI,KAAK,IAAI,CAAC,EAAE;oBACZ,KAAK,CAAC,iBAAiB,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;iBAC5C;aACJ;;QARL,KAA4B,UAA6B,EAA7B,KAAA,IAAI,CAAC,KAAK,CAAC,iBAAiB,CAAC,EAA7B,cAA6B,EAA7B,IAA6B;YAApD,IAAM,aAAa,SAAA;oBAAb,aAAa;SASvB;IACL,CAAC,CAAC,CAAA;IAEF,OAAO,WAAgB,CAAC;AAC5B,CAAC"}
@@ -1,93 +0,0 @@
1
- import { toJS } from 'mobx';
2
- import { useRef } from 'react';
3
- import { useMount, useUnmount } from 'react-use';
4
- import { catchError, concatMap, debounceTime, EMPTY, from, lastValueFrom, ReplaySubject, Subscription } from 'rxjs';
5
- import { exhaustMapWithTrailing } from 'rxjs-exhaustmap-with-trailing';
6
- import { useMobxState } from './useMobxState';
7
-
8
- export function useAsyncExhaust<T>(callback: T) {
9
-
10
- const runCallback: any = function () {
11
- if (state.isUnmount) {
12
- const resultSubjectPromise = Promise.reject(new Error("Cancelled!"));
13
- resultSubjectPromise.catch(() => null);
14
- return resultSubjectPromise;
15
- } else {
16
- const resultSubject = new ReplaySubject(0);
17
- const resultSubjectPromise = lastValueFrom(resultSubject);
18
- state.resultSubjectList.push({
19
- subject: resultSubject,
20
- promise: resultSubjectPromise
21
- });
22
- state.subject.next({
23
- params: arguments
24
- });
25
- return resultSubjectPromise;
26
- }
27
- }
28
-
29
- const state = useRef({
30
- subscription: new Subscription(),
31
- subject: new ReplaySubject(0),
32
- resultSubjectList: [] as {
33
- subject: ReplaySubject<any>,
34
- promise: Promise<any>,
35
- }[],
36
- isUnmount: false,
37
- }).current;
38
-
39
- const source = useMobxState({}, { callback });
40
-
41
- useMount(() => {
42
- state.subscription.add(state.subject.pipe(
43
- debounceTime(0),
44
- exhaustMapWithTrailing(({ params }: any) => {
45
- const resultSubjectList = toJS(state.resultSubjectList);
46
- return from((async () => { return await (source.callback as any)(...params) })()).pipe(
47
- concatMap((result) => {
48
- for (const resultSubject of resultSubjectList) {
49
- resultSubject.subject.next(result);
50
- resultSubject.subject.complete();
51
- if (resultSubject.subject.closed) {
52
- const index = state.resultSubjectList.findIndex(s => s === resultSubject);
53
- if (index >= 0) {
54
- state.resultSubjectList.splice(index, 1);
55
- }
56
- }
57
- }
58
- return EMPTY;
59
- }),
60
- catchError((error) => {
61
- for (const resultSubject of resultSubjectList) {
62
- resultSubject.subject.error(error);
63
- if (resultSubject.subject.closed) {
64
- const index = state.resultSubjectList.findIndex(s => s === resultSubject);
65
- if (index >= 0) {
66
- state.resultSubjectList.splice(index, 1);
67
- }
68
- }
69
- }
70
- return EMPTY;
71
- })
72
- );
73
- }),
74
- ).subscribe());
75
- })
76
-
77
- useUnmount(() => {
78
- state.subscription.unsubscribe();
79
- state.isUnmount = true;
80
- for (const resultSubject of toJS(state.resultSubjectList)) {
81
- resultSubject.promise.catch(() => null);
82
- resultSubject.subject.error(new Error("Cancelled!"));
83
- if (resultSubject.subject.closed) {
84
- const index = state.resultSubjectList.findIndex(s => s === resultSubject);
85
- if (index >= 0) {
86
- state.resultSubjectList.splice(index, 1);
87
- }
88
- }
89
- }
90
- })
91
-
92
- return runCallback as T;
93
- }