@mic-rexjs/usecases-react 3.6.3 → 4.0.0-beta.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.
Files changed (68) hide show
  1. package/dist/index.min.js +1 -1
  2. package/es/classes/OptionsSet/index.d.ts +6 -0
  3. package/es/classes/OptionsSet/index.js +44 -0
  4. package/es/classes/OptionsSet/types.d.ts +8 -0
  5. package/es/hooks/useContextValue/index.d.ts +6 -0
  6. package/es/hooks/useContextValue/index.js +20 -0
  7. package/es/hooks/useContextualItem/index.d.ts +2 -2
  8. package/es/hooks/useContextualItem/index.js +8 -28
  9. package/es/hooks/useEntity/index.d.ts +1 -1
  10. package/es/hooks/useEntity/index.js +21 -15
  11. package/es/hooks/useExec/index.d.ts +2 -2
  12. package/es/hooks/useExec/index.js +19 -6
  13. package/es/hooks/useExec/types.d.ts +3 -0
  14. package/es/hooks/useFullArguments/types.d.ts +2 -2
  15. package/es/hooks/useOptions/index.d.ts +5 -0
  16. package/es/hooks/useOptions/index.js +54 -0
  17. package/es/hooks/useOptions/types.d.ts +5 -0
  18. package/es/hooks/useOptionsSet/index.d.ts +5 -0
  19. package/es/hooks/useOptionsSet/index.js +16 -0
  20. package/es/hooks/useProvider/index.d.ts +1 -3
  21. package/es/hooks/useProvider/index.js +2 -10
  22. package/es/hooks/useReducers/index.d.ts +1 -1
  23. package/es/hooks/useReducers/index.js +6 -25
  24. package/es/hooks/useRootCreation/index.d.ts +4 -0
  25. package/es/hooks/useRootCreation/index.js +20 -0
  26. package/es/hooks/useRootCreation/types.d.ts +3 -0
  27. package/es/hooks/useRootCreation/types.js +1 -0
  28. package/es/hooks/useStore/index.js +7 -12
  29. package/es/hooks/useUseCase/index.js +12 -6
  30. package/es/hooks/useUseCase/index.test.js +286 -126
  31. package/es/hooks/useUseCase/types.d.ts +4 -5
  32. package/es/methods/cacheCall/index.d.ts +2 -0
  33. package/es/methods/cacheCall/index.js +38 -0
  34. package/es/methods/cacheCall/types.d.ts +12 -0
  35. package/es/methods/cacheCall/types.js +1 -0
  36. package/es/methods/cacheCalls/index.d.ts +2 -0
  37. package/es/methods/cacheCalls/index.js +12 -0
  38. package/es/usecases/contextUseCase/types.d.ts +3 -1
  39. package/es/usecases/lifecycleEventUseCase/types.d.ts +14 -0
  40. package/es/usecases/lifecycleUseCase/index.js +5 -1
  41. package/lib/classes/OptionsSet/index.js +94 -0
  42. package/lib/hooks/useContextValue/index.js +26 -0
  43. package/lib/hooks/useContextualItem/index.js +8 -28
  44. package/lib/hooks/useEntity/index.js +20 -14
  45. package/lib/hooks/useExec/index.js +18 -5
  46. package/lib/hooks/useOptions/index.js +61 -0
  47. package/lib/hooks/useOptionsSet/index.js +22 -0
  48. package/lib/hooks/useProvider/index.js +1 -9
  49. package/lib/hooks/useReducers/index.js +8 -31
  50. package/lib/hooks/useRootCreation/index.js +26 -0
  51. package/lib/hooks/useRootCreation/types.js +5 -0
  52. package/lib/hooks/useStore/index.js +7 -12
  53. package/lib/hooks/useUseCase/index.js +13 -7
  54. package/lib/hooks/useUseCase/index.test.js +322 -127
  55. package/lib/methods/cacheCall/index.js +44 -0
  56. package/lib/methods/cacheCall/types.js +5 -0
  57. package/lib/methods/cacheCalls/index.js +19 -0
  58. package/lib/usecases/lifecycleUseCase/index.js +5 -1
  59. package/package.json +3 -3
  60. package/es/hooks/useContextualItem/types.d.ts +0 -3
  61. package/es/usecases/methodUseCase/index.d.ts +0 -2
  62. package/es/usecases/methodUseCase/index.js +0 -72
  63. package/es/usecases/methodUseCase/types.d.ts +0 -21
  64. package/lib/usecases/methodUseCase/index.js +0 -79
  65. /package/es/{hooks/useContextualItem → classes/OptionsSet}/types.js +0 -0
  66. /package/es/{usecases/methodUseCase → hooks/useOptions}/types.js +0 -0
  67. /package/lib/{hooks/useContextualItem → classes/OptionsSet}/types.js +0 -0
  68. /package/lib/{usecases/methodUseCase → hooks/useOptions}/types.js +0 -0
@@ -1,6 +1,7 @@
1
1
  import { UseCaseProvider } from '../useProvider/types';
2
2
  import { UseCase } from '@mic-rexjs/usecases';
3
3
  import { EntityReducerMap, EntityReducers, InferableEntityUseCase, ReducerMap, RestArguments, SymbolSet, SymbolSetTarget } from '@mic-rexjs/usecases/es/types';
4
+ import { VoidifyEventOptions } from '../../classes/OptionsSet/types';
4
5
  import { MatchPropertyFailedResult } from '../../entities/matchPropertyFailedResult/types';
5
6
  import { Dependencies, EntityInitializer } from '../../types';
6
7
  import { ContextualEntityReducers } from '../../usecases/contextUseCase/types';
@@ -20,8 +21,6 @@ export type RootCoreCollection<T, TEntityReducers extends EntityReducerMap<T>> =
20
21
  * @deprecated 请用 `CoreCollection` 代替
21
22
  */
22
23
  export type ContextualCoreCollection<T, TEntityReducers extends EntityReducerMap<T>> = CoreCollection<T, TEntityReducers>;
23
- export interface EntityGetter<T> extends EntityInitializer<T> {
24
- }
25
24
  export interface EntityWatchEvent<T, TValue = unknown> extends MatchPropertyFailedResult<T, TValue> {
26
25
  }
27
26
  export interface EntityWatcher<T, TValue = unknown> {
@@ -37,11 +36,11 @@ export type ExtractFieldType<T, TPath> = TPath extends `${infer TKey}.${infer TS
37
36
  export type EntityWatchMap<T> = {
38
37
  [K in FieldPath<T>]?: EntityWatcher<T, ExtractFieldType<T, K>>;
39
38
  };
40
- export interface UseCaseHookContextualOptions<T> {
39
+ export interface ContextualUseCaseHookOptions<T> {
41
40
  watch?: EntityWatchMap<T>;
42
41
  onChange?(newEntity: T, oldEntity: T): void;
43
42
  }
44
- export interface UseCaseHookOwnOptions<T, TUseCaseOptions extends object> extends UseCaseHookContextualOptions<T> {
43
+ export interface UseCaseHookOwnOptions<T, TUseCaseOptions extends object> extends ContextualUseCaseHookOptions<T> {
45
44
  options?: TUseCaseOptions;
46
45
  }
47
46
  export type UnextendFunctionTypes<T> = T extends (...args: infer TArgs) => infer TReturn ? (...args: TArgs) => TReturn : T;
@@ -53,7 +52,7 @@ export interface ReducersHook {
53
52
  <T extends ReducerMap, TUseCaseOptions extends object = object>(usecase: UseCase<T & SymbolSetTarget<NonEntitySymbolSet>, TUseCaseOptions>, options?: TUseCaseOptions, deps?: Dependencies): T;
54
53
  }
55
54
  export interface ContextualCoreCollectionHook {
56
- <T, TEntityReducers extends EntityReducerMap<T>>(usecase: InferableEntityUseCase<T, TEntityReducers>, options?: UseCaseHookContextualOptions<T>, deps?: Dependencies): CoreCollection<T, TEntityReducers>;
55
+ <T, TEntityReducers extends EntityReducerMap<T>, TUseCaseOptions extends object = object>(usecase: InferableEntityUseCase<T, TEntityReducers, TUseCaseOptions>, options?: UseCaseHookOptions<T, VoidifyEventOptions<TUseCaseOptions>>): CoreCollection<T, TEntityReducers>;
57
56
  }
58
57
  export interface RootCoreCollectionHook {
59
58
  <T, TEntityReducers extends EntityReducerMap<T>, TUseCaseOptions extends object = object>(entity: T | EntityInitializer<T>, usecase: InferableEntityUseCase<T, TEntityReducers & EntityReducers<T>, TUseCaseOptions>, options?: UseCaseHookOptions<T, TUseCaseOptions>, deps?: Dependencies): CoreCollection<T, EntityReducers<T, TEntityReducers>>;
@@ -0,0 +1,2 @@
1
+ import { CacheableFactory, CacheCallOptions } from './types';
2
+ export declare const cacheCall: <T extends CacheableFactory>(factory: T, options?: CacheCallOptions) => T;
@@ -0,0 +1,38 @@
1
+ import { objectUtilsUseCase } from '../../usecases/objectUtilsUseCase';
2
+ export const cacheCall = (factory, options) => {
3
+ let cache;
4
+ const _objectUtilsUseCase = objectUtilsUseCase(),
5
+ isEqual = _objectUtilsUseCase.isEqual;
6
+ const _ref = options || {},
7
+ _ref$onShouldCache = _ref.onShouldCache,
8
+ onShouldCache = _ref$onShouldCache === void 0 ? () => {
9
+ return true;
10
+ } : _ref$onShouldCache,
11
+ _ref$onCompare = _ref.onCompare,
12
+ onCompare = _ref$onCompare === void 0 ? (newArgs, oldArgs) => {
13
+ return isEqual(newArgs, oldArgs);
14
+ } : _ref$onCompare;
15
+ return new Proxy(factory, {
16
+ apply(target, thisArg) {
17
+ let argList = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
18
+ const args = argList;
19
+ const cacheable = onShouldCache();
20
+ if (cache && cacheable) {
21
+ const _cache = cache,
22
+ parameters = _cache.parameters,
23
+ returnValue = _cache.returnValue;
24
+ if (onCompare(parameters, args)) {
25
+ return returnValue;
26
+ }
27
+ }
28
+ const returnValue = Reflect.apply(target, thisArg, args);
29
+ if (cacheable) {
30
+ cache = {
31
+ parameters: args,
32
+ returnValue
33
+ };
34
+ }
35
+ return returnValue;
36
+ }
37
+ });
38
+ };
@@ -0,0 +1,12 @@
1
+ import { RestArguments } from '@mic-rexjs/usecases/es/types';
2
+ export interface CacheableFactory {
3
+ (...args: RestArguments): unknown;
4
+ }
5
+ export interface CacheCallOptions {
6
+ onShouldCache?(): boolean;
7
+ onCompare?(newArgs: RestArguments, oldArgs: RestArguments): boolean;
8
+ }
9
+ export interface CallCache<T extends CacheableFactory> {
10
+ parameters: Parameters<T>;
11
+ returnValue: ReturnType<T>;
12
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import { CacheableFactory, CacheCallOptions } from '../cacheCall/types';
2
+ export declare const cacheCalls: <T extends Record<string, CacheableFactory>>(sourceFactories: T, options?: CacheCallOptions) => T;
@@ -0,0 +1,12 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import { cacheCall } from '../cacheCall';
3
+ export const cacheCalls = (sourceFactories, options) => {
4
+ const cachedFactories = {};
5
+ Object.entries(sourceFactories).forEach(_ref => {
6
+ let _ref2 = _slicedToArray(_ref, 2),
7
+ key = _ref2[0],
8
+ factory = _ref2[1];
9
+ cachedFactories[key] = cacheCall(factory, options);
10
+ });
11
+ return cachedFactories;
12
+ };
@@ -1,13 +1,15 @@
1
1
  import { AsyncEntityGenerator, EntityGenerator, EntityReducer, EntityStore, ReducerKeys, Reducers } from '@mic-rexjs/usecases';
2
2
  import { EntityReducerMap, InferableUseCase, ReducerMap } from '@mic-rexjs/usecases/es/types';
3
+ import { OptionsSet } from '../../classes/OptionsSet';
3
4
  import { ArgumentTypes } from '../../enums/ArgumentTypes';
4
5
  import { Statuses } from '../../enums/Statuses';
5
6
  export interface UseCaseContextValue<T extends ReducerMap> {
6
7
  reducers: T;
7
8
  statuses: Statuses;
8
9
  }
9
- export interface EntityUseCaseContextValue<T, TEntityReducers extends EntityReducerMap<T>> extends UseCaseContextValue<ContextualEntityReducers<T, TEntityReducers>> {
10
+ export interface EntityUseCaseContextValue<T, TEntityReducers extends EntityReducerMap<T>, TOptions = object> extends UseCaseContextValue<ContextualEntityReducers<T, TEntityReducers>> {
10
11
  store: EntityStore<T>;
12
+ optionsSet: OptionsSet<TOptions>;
11
13
  }
12
14
  export interface UseCaseContext<T> extends React.Context<T | null> {
13
15
  }
@@ -10,6 +10,13 @@ export interface LifecycleEvents<T = unknown> {
10
10
  * `unmount` 的时候触发。
11
11
  */
12
12
  onUnbind(entity: T): void;
13
+ /**
14
+ * 使用新的 `entity` 的时候触发,包括:
15
+ *
16
+ * 1. `onBind` 的时候;
17
+ * 2. `onUpdate` 或 `onChange` 的时候。
18
+ */
19
+ onAdopt(newEntity: T, oldEntity?: T): void;
13
20
  /**
14
21
  * 非渲染阶段触发,随后会触发组件的更新:
15
22
  *
@@ -24,6 +31,13 @@ export interface LifecycleEvents<T = unknown> {
24
31
  * 2. 由 `useUseCase(entity, usecase)` 中的 `entity` 变化触发 - 渲染阶段。
25
32
  */
26
33
  onUpdate(newEntity: T, oldEntity: T): void;
34
+ /**
35
+ * 当 `entity` 被废弃时候调用,包括以下两种情况:
36
+ *
37
+ * 1. `onUpdate` 或 `onChange` 的时候;
38
+ * 2. `onUnbind` 的时候。
39
+ */
40
+ onDiscard(entity: T): void;
27
41
  }
28
42
  export interface CaptureLifecycleEventsCallback<T> {
29
43
  (): T;
@@ -2,7 +2,9 @@ import { lifecycleEventUseCase } from '../lifecycleEventUseCase';
2
2
  import { entityUseCase } from '@mic-rexjs/usecases';
3
3
  export const lifecycleUseCase = function () {
4
4
  let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
5
- const onBind = options.onBind,
5
+ const onAdopt = options.onAdopt,
6
+ onDiscard = options.onDiscard,
7
+ onBind = options.onBind,
6
8
  onUnbind = options.onUnbind,
7
9
  onChange = options.onChange,
8
10
  onUpdate = options.onUpdate;
@@ -10,6 +12,8 @@ export const lifecycleUseCase = function () {
10
12
  setLifecycleEvents = _lifecycleEventUseCas.setLifecycleEvents;
11
13
  const entityReducers = entityUseCase();
12
14
  setLifecycleEvents({
15
+ onAdopt,
16
+ onDiscard,
13
17
  onBind,
14
18
  onUnbind,
15
19
  onChange,
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.OptionsSet = void 0;
8
+ var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
9
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
10
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
11
+ var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
12
+ var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
13
+ var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));
14
+ var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
15
+ var _wrapNativeSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/wrapNativeSuper"));
16
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
17
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
18
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
19
+ function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(t).constructor) : o.apply(t, e)); }
20
+ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
21
+ function _superPropGet(t, o, e, r) { var p = (0, _get2["default"])((0, _getPrototypeOf2["default"])(1 & r ? t.prototype : t), o, e); return 2 & r && "function" == typeof p ? function (t) { return p.apply(e, t); } : p; }
22
+ function _classPrivateFieldInitSpec(e, t, a) {
23
+ _checkPrivateRedeclaration(e, t), t.set(e, a);
24
+ }
25
+ function _checkPrivateRedeclaration(e, t) {
26
+ if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object");
27
+ }
28
+ function _classPrivateFieldGet(s, a) {
29
+ return s.get(_assertClassBrand(s, a));
30
+ }
31
+ function _classPrivateFieldSet(s, a, r) {
32
+ return s.set(_assertClassBrand(s, a), r), r;
33
+ }
34
+ function _assertClassBrand(e, t, n) {
35
+ if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n;
36
+ throw new TypeError("Private element is not present on this object");
37
+ }
38
+ var _rootOptions = /*#__PURE__*/new WeakMap();
39
+ var OptionsSet = exports.OptionsSet = /*#__PURE__*/function (_Set) {
40
+ function OptionsSet() {
41
+ var _this;
42
+ (0, _classCallCheck2["default"])(this, OptionsSet);
43
+ _this = _callSuper(this, OptionsSet, arguments);
44
+ _classPrivateFieldInitSpec(_this, _rootOptions, null);
45
+ return _this;
46
+ }
47
+ (0, _inherits2["default"])(OptionsSet, _Set);
48
+ return (0, _createClass2["default"])(OptionsSet, [{
49
+ key: "add",
50
+ value: function add(options, root) {
51
+ if ((0, _typeof2["default"])(options) !== 'object') {
52
+ return this;
53
+ }
54
+ if (root) {
55
+ _classPrivateFieldSet(_rootOptions, this, options);
56
+ }
57
+ _superPropGet(OptionsSet, "add", this, 3)([options]);
58
+ return this;
59
+ }
60
+ }, {
61
+ key: "apply",
62
+ value: function apply(type, thisArgument, argumentsList) {
63
+ var returnValue;
64
+ var rootOptions = _classPrivateFieldGet(_rootOptions, this);
65
+ var _ref = rootOptions || {},
66
+ rootEvent = _ref[type];
67
+ if (typeof rootEvent === 'function') {
68
+ // 最先调用根节点的事件
69
+ returnValue = Reflect.apply(rootEvent, thisArgument, argumentsList);
70
+ }
71
+ var _iterator = _createForOfIteratorHelper(this),
72
+ _step;
73
+ try {
74
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
75
+ var options = _step.value;
76
+ if (options === rootOptions) {
77
+ continue;
78
+ }
79
+ var event = options[type];
80
+ if (typeof event !== 'function') {
81
+ continue;
82
+ }
83
+ // 非根节点事件,不处理返回值
84
+ Reflect.apply(event, thisArgument, argumentsList);
85
+ }
86
+ } catch (err) {
87
+ _iterator.e(err);
88
+ } finally {
89
+ _iterator.f();
90
+ }
91
+ return returnValue;
92
+ }
93
+ }]);
94
+ }(/*#__PURE__*/(0, _wrapNativeSuper2["default"])(Set));
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useContextValue = void 0;
7
+ var _ahooks = require("ahooks");
8
+ var _Statuses = require("../../enums/Statuses");
9
+ var useContextValue = exports.useContextValue = function useContextValue(store, reducers, optionsSet, statuses) {
10
+ var entity = store.value;
11
+ return (0, _ahooks.useCreation)(function () {
12
+ void entity;
13
+ if ((statuses & _Statuses.Statuses.EntityEnabled) === _Statuses.Statuses.EntityEnabled) {
14
+ return {
15
+ store: store,
16
+ reducers: reducers,
17
+ optionsSet: optionsSet,
18
+ statuses: statuses
19
+ };
20
+ }
21
+ return {
22
+ reducers: reducers,
23
+ statuses: statuses
24
+ };
25
+ }, [entity, store, reducers, optionsSet, statuses]);
26
+ };
@@ -4,34 +4,14 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.useContextualItem = void 0;
7
- var _useDepsKey = require("../useDepsKey");
8
- var _ahooks = require("ahooks");
7
+ var _useRootCreation = require("../useRootCreation");
9
8
  var _Statuses = require("../../enums/Statuses");
10
- var _methodUseCase2 = require("../../usecases/methodUseCase");
11
- var useContextualItem = exports.useContextualItem = function useContextualItem(contextItem, statuses, createFactory) {
9
+ var useContextualItem = exports.useContextualItem = function useContextualItem(contextItem, statuses, callback) {
12
10
  var deps = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
13
- var depsKey = (0, _useDepsKey.useDepsKey)(deps);
14
- var factoryRef = (0, _ahooks.useLatest)(createFactory);
15
- var onCache = (0, _ahooks.useMemoizedFn)(function () {
16
- var _methodUseCase = (0, _methodUseCase2.methodUseCase)(),
17
- cacheCall = _methodUseCase.cacheCall;
18
- var currentFactory = factoryRef.current;
19
- // 防止在以下 `onCreate()` 多次调用实际 `factory`
20
- return cacheCall(currentFactory, {
21
- onCompare: function onCompare() {
22
- return true;
23
- }
24
- });
25
- });
26
- var onCreate = (0, _ahooks.useCreation)(function () {
27
- void depsKey;
28
- return onCache();
29
- }, [depsKey, onCache]);
30
- return (0, _ahooks.useCreation)(function () {
31
- var rootEnabled = (statuses & _Statuses.Statuses.RootEnabled) === _Statuses.Statuses.RootEnabled;
32
- if (!rootEnabled && contextItem) {
33
- return contextItem;
34
- }
35
- return onCreate();
36
- }, [statuses, contextItem, onCreate]);
11
+ var rootEnabled = (statuses & _Statuses.Statuses.RootEnabled) === _Statuses.Statuses.RootEnabled;
12
+ var rootItem = (0, _useRootCreation.useRootCreation)(statuses, callback, false, deps);
13
+ if (rootEnabled) {
14
+ return rootItem;
15
+ }
16
+ return contextItem;
37
17
  };
@@ -14,11 +14,11 @@ var _react = require("react");
14
14
  var _Statuses = require("../../enums/Statuses");
15
15
  var _triggerWatchers = require("../../methods/triggerWatchers");
16
16
  var _lifecycleEventUseCase = require("../../usecases/lifecycleEventUseCase");
17
- var useEntity = exports.useEntity = function useEntity(store, statuses, options) {
17
+ var useEntity = exports.useEntity = function useEntity(store, statuses, hookOptions) {
18
18
  var entity = store.value;
19
19
  var update = (0, _ahooks.useUpdate)();
20
20
  var isInitializing = (0, _useEntityInitializing.useEntityInitializing)(statuses);
21
- var optionsRef = (0, _ahooks.useLatest)(options);
21
+ var hookOptionsRef = (0, _ahooks.useLatest)(hookOptions);
22
22
  var prevEntityRef = (0, _react.useRef)(entity);
23
23
  var entityEnabled = (statuses & _Statuses.Statuses.EntityEnabled) === _Statuses.Statuses.EntityEnabled;
24
24
  var entityRootEnabled = (statuses & _Statuses.Statuses.EntityRootEnabled) === _Statuses.Statuses.EntityRootEnabled;
@@ -34,7 +34,7 @@ var useEntity = exports.useEntity = function useEntity(store, statuses, options)
34
34
  return createKey();
35
35
  });
36
36
  var onTriggerWatchers = (0, _ahooks.useMemoizedFn)(function (newEntity, oldEntity) {
37
- var watch = optionsRef.current.watch;
37
+ var watch = hookOptionsRef.current.watch;
38
38
  if (!watch) {
39
39
  return;
40
40
  }
@@ -51,14 +51,18 @@ var useEntity = exports.useEntity = function useEntity(store, statuses, options)
51
51
  return;
52
52
  }
53
53
  var _getLifecycleEvents = getLifecycleEvents(lifecycleKey),
54
- onUpdate = _getLifecycleEvents.onUpdate;
54
+ onUpdate = _getLifecycleEvents.onUpdate,
55
+ onAdopt = _getLifecycleEvents.onAdopt,
56
+ onDiscard = _getLifecycleEvents.onDiscard;
55
57
  prevEntityRef.current = newEntity;
56
58
  onTriggerWatchers(newEntity, oldEntity);
59
+ onDiscard === null || onDiscard === void 0 || onDiscard(oldEntity);
60
+ onAdopt === null || onAdopt === void 0 || onAdopt(newEntity, oldEntity);
57
61
  onUpdate === null || onUpdate === void 0 || onUpdate(newEntity, oldEntity);
58
62
  });
59
63
  var onEntityChange = (0, _ahooks.useMemoizedFn)(function (newEntity, oldEntity) {
60
- var currentOptions = optionsRef.current;
61
- var onChange1 = currentOptions.onChange;
64
+ var currentHookOptions = hookOptionsRef.current;
65
+ var onChange1 = currentHookOptions.onChange;
62
66
  var _getLifecycleEvents2 = getLifecycleEvents(lifecycleKey),
63
67
  onChange2 = _getLifecycleEvents2.onChange;
64
68
  onEntityUpdate(newEntity, oldEntity);
@@ -74,12 +78,14 @@ var useEntity = exports.useEntity = function useEntity(store, statuses, options)
74
78
  return;
75
79
  }
76
80
  var _bindLifecycleEventsK = bindLifecycleEventsKey(lifecycleKey),
77
- onBind = _bindLifecycleEventsK.onBind;
81
+ onBind = _bindLifecycleEventsK.onBind,
82
+ onAdopt = _bindLifecycleEventsK.onAdopt;
78
83
  if (!isInitializing) {
79
84
  return;
80
85
  }
81
86
  onBind === null || onBind === void 0 || onBind(entity);
82
- });
87
+ onAdopt === null || onAdopt === void 0 || onAdopt(entity);
88
+ }, null, []);
83
89
  (0, _ahooks.useUnmount)(function () {
84
90
  if (!entityRootEnabled) {
85
91
  return;
@@ -87,9 +93,11 @@ var useEntity = exports.useEntity = function useEntity(store, statuses, options)
87
93
  // 要使用最新的 `entity`
88
94
  var currentEntity = store.value;
89
95
  var _getLifecycleEvents3 = getLifecycleEvents(lifecycleKey),
90
- onUnbind = _getLifecycleEvents3.onUnbind;
96
+ onUnbind = _getLifecycleEvents3.onUnbind,
97
+ onDiscard = _getLifecycleEvents3.onDiscard;
91
98
  deleteLifecycleEvents(lifecycleKey);
92
99
  onUnbind === null || onUnbind === void 0 || onUnbind(currentEntity);
100
+ onDiscard === null || onDiscard === void 0 || onDiscard(currentEntity);
93
101
  });
94
102
  /**
95
103
  * 需要马上 `watch`,因为 `useEffect` 在子组件内优先执行,
@@ -100,14 +108,12 @@ var useEntity = exports.useEntity = function useEntity(store, statuses, options)
100
108
  return;
101
109
  }
102
110
  store.watch(onEntityChange);
103
- }, [store, entityEnabled, onEntityChange]);
111
+ },
104
112
  /**
105
113
  * 在变化时候,组件更新前,使用 `unwatch`
106
114
  */
107
- (0, _react.useEffect)(function () {
108
- return function () {
109
- store.unwatch(onEntityChange);
110
- };
115
+ function () {
116
+ store.unwatch(onEntityChange);
111
117
  }, [store, entityEnabled, onEntityChange]);
112
118
  (0, _ahooks.useUpdateEffect)(function () {
113
119
  onEntityUpdate();
@@ -6,14 +6,27 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.useExec = void 0;
7
7
  var _useDepsKey = require("../useDepsKey");
8
8
  var _ahooks = require("ahooks");
9
- var useExec = exports.useExec = function useExec(callback) {
10
- var deps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [callback];
9
+ var _react = require("react");
10
+ var useExec = exports.useExec = function useExec(callback, destructor) {
11
+ var deps = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [callback];
11
12
  var depsKey = (0, _useDepsKey.useDepsKey)(deps);
12
- var execCallback = (0, _ahooks.useMemoizedFn)(function () {
13
+ var destructorRef = (0, _react.useRef)(null);
14
+ var onDestruct = function onDestruct() {
15
+ var _destructorRef$curren;
16
+ var isUnmount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
17
+ (_destructorRef$curren = destructorRef.current) === null || _destructorRef$curren === void 0 || _destructorRef$curren.call(destructorRef, isUnmount);
18
+ };
19
+ var onExec = (0, _ahooks.useMemoizedFn)(function () {
20
+ onDestruct();
21
+ // `destructor` 一定是在执行回调的时候,同步绑定的,保持两个回调内的闭包变量一致
22
+ destructorRef.current = destructor || null;
13
23
  return callback();
14
24
  });
25
+ (0, _ahooks.useUnmount)(function () {
26
+ onDestruct(true);
27
+ });
15
28
  return (0, _ahooks.useCreation)(function () {
16
29
  void depsKey;
17
- return execCallback();
18
- }, [depsKey, execCallback]);
30
+ return onExec();
31
+ }, [depsKey, onExec]);
19
32
  };
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.useOptions = void 0;
8
+ var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
9
+ var _useExec = require("../useExec");
10
+ var _useRootCreation = require("../useRootCreation");
11
+ var _Statuses = require("../../enums/Statuses");
12
+ var _excluded = ["watch", "onChange"];
13
+ var useOptions = exports.useOptions = function useOptions(options, optionsSet, statuses) {
14
+ var deps = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
15
+ var entityEnabled = (statuses & _Statuses.Statuses.EntityEnabled) === _Statuses.Statuses.EntityEnabled;
16
+ var entityRootEnabled = (statuses & _Statuses.Statuses.EntityRootEnabled) === _Statuses.Statuses.EntityRootEnabled;
17
+ var watch = options.watch,
18
+ onChange = options.onChange,
19
+ restOptions = (0, _objectWithoutProperties2["default"])(options, _excluded);
20
+ var _options$options = options.options,
21
+ propertyOptions = _options$options === void 0 ? restOptions : _options$options;
22
+ var contextualOptions = propertyOptions;
23
+ var hookOptions = entityEnabled ? {
24
+ watch: watch,
25
+ onChange: onChange
26
+ } : {};
27
+ var onProxyEvent = function onProxyEvent(propertyName) {
28
+ return new Proxy(function () {}, {
29
+ apply: function apply(fn, thisArgument, argumentList) {
30
+ return optionsSet.apply(propertyName, thisArgument, argumentList);
31
+ }
32
+ });
33
+ };
34
+ var onGet = function onGet(target, propertyName, receiver) {
35
+ var isStringProperty = typeof propertyName === 'string';
36
+ var isEvent = isStringProperty && /^on[A-Z]/.test(propertyName);
37
+ if (!isEvent) {
38
+ return Reflect.get(target, propertyName, receiver);
39
+ }
40
+ return onProxyEvent(propertyName);
41
+ };
42
+ var rootOptions = (0, _useRootCreation.useRootCreation)(statuses, function () {
43
+ return new Proxy(contextualOptions, {
44
+ get: onGet
45
+ });
46
+ }, true, deps);
47
+ var entityUseCaseOptions = rootOptions || contextualOptions;
48
+ var usecaseOptions = entityEnabled ? entityUseCaseOptions : options;
49
+ (0, _useExec.useExec)(function () {
50
+ if (!entityEnabled) {
51
+ return;
52
+ }
53
+ optionsSet.add(contextualOptions, entityRootEnabled);
54
+ }, function () {
55
+ optionsSet["delete"](contextualOptions);
56
+ });
57
+ return {
58
+ hookOptions: hookOptions,
59
+ usecaseOptions: usecaseOptions
60
+ };
61
+ };
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useOptionsSet = void 0;
7
+ var _useRootCreation = require("../useRootCreation");
8
+ var _react = require("react");
9
+ var _OptionsSet = require("../../classes/OptionsSet");
10
+ var useOptionsSet = exports.useOptionsSet = function useOptionsSet(context, statuses) {
11
+ // 过滤掉三个特殊属性
12
+ var contextValue = (0, _react.useContext)(context);
13
+ var _ref = contextValue || {},
14
+ contextOptionsSet = _ref.optionsSet;
15
+ var rootEventsSet = (0, _useRootCreation.useRootCreation)(statuses, function () {
16
+ return new _OptionsSet.OptionsSet();
17
+ });
18
+ if (rootEventsSet) {
19
+ return rootEventsSet;
20
+ }
21
+ return contextOptionsSet;
22
+ };
@@ -8,15 +8,7 @@ exports.useProvider = void 0;
8
8
  var _useConstantFn = require("../useConstantFn");
9
9
  var _ahooks = require("ahooks");
10
10
  var _react = _interopRequireDefault(require("react"));
11
- var _contextUseCase2 = require("../../usecases/contextUseCase");
12
- var useProvider = exports.useProvider = function useProvider(statuses, context, store, reducers) {
13
- var entity = store.value;
14
- var contextValue = (0, _ahooks.useCreation)(function () {
15
- var _contextUseCase = (0, _contextUseCase2.contextUseCase)(),
16
- createContextValue = _contextUseCase.createContextValue;
17
- void entity;
18
- return createContextValue(store, reducers, statuses);
19
- }, [statuses, entity, store, reducers]);
11
+ var useProvider = exports.useProvider = function useProvider(context, contextValue) {
20
12
  var contextValueRef = (0, _ahooks.useLatest)(contextValue);
21
13
  return (0, _useConstantFn.useConstantFn)(function (_ref) {
22
14
  var children = _ref.children,
@@ -1,53 +1,30 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
3
  Object.defineProperty(exports, "__esModule", {
5
4
  value: true
6
5
  });
7
6
  exports.useReducers = void 0;
8
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
- var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
10
- var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
11
7
  var _useContextualItem = require("../useContextualItem");
12
8
  var _usecases = require("@mic-rexjs/usecases");
13
- var _ahooks = require("ahooks");
14
9
  var _react = require("react");
15
10
  var _Statuses = require("../../enums/Statuses");
16
- var _methodUseCase2 = require("../../usecases/methodUseCase");
17
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
18
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
19
- var _excluded = ["watch", "onChange", "options"];
20
- var useReducers = exports.useReducers = function useReducers(usecase, context, store, statuses, options) {
11
+ var useReducers = exports.useReducers = function useReducers(usecase, store, context, statuses, usecaseOptions) {
21
12
  var deps = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : [];
22
- var optionsRef = (0, _ahooks.useLatest)(options);
23
13
  var contextValue = (0, _react.useContext)(context);
24
14
  var _ref = contextValue || {},
25
15
  _ref$reducers = _ref.reducers,
26
16
  contextReducers = _ref$reducers === void 0 ? null : _ref$reducers;
27
- var entityRootEnabled = (statuses & _Statuses.Statuses.EntityRootEnabled) === _Statuses.Statuses.EntityRootEnabled;
17
+ var entityEnabled = (statuses & _Statuses.Statuses.EntityEnabled) === _Statuses.Statuses.EntityEnabled;
28
18
  var onCreate = function onCreate() {
29
- var _methodUseCase = (0, _methodUseCase2.methodUseCase)(),
30
- captureCalls = _methodUseCase.captureCalls;
31
- var opts = captureCalls(options, function (key, callArgs) {
32
- var _optionsRef$current$k2;
33
- var _optionsRef$current$k, _optionsRef$current;
34
- return (_optionsRef$current$k = (_optionsRef$current = optionsRef.current)[key]) === null || _optionsRef$current$k === void 0 ? void 0 : (_optionsRef$current$k2 = _optionsRef$current$k).call.apply(_optionsRef$current$k2, [_optionsRef$current].concat((0, _toConsumableArray2["default"])(callArgs)));
35
- });
36
- if (!entityRootEnabled) {
37
- return usecase(opts);
19
+ if (!entityEnabled) {
20
+ return usecase(usecaseOptions);
38
21
  }
39
- var hookOptions = opts;
40
- var watch = hookOptions.watch,
41
- onChange = hookOptions.onChange,
42
- usecaseOptions = hookOptions.options,
43
- restUseCaseOptions = (0, _objectWithoutProperties2["default"])(hookOptions, _excluded);
44
- return (0, _usecases.createEntityReducers)(store, usecase, _objectSpread(_objectSpread(_objectSpread({}, restUseCaseOptions), usecaseOptions), {}, {
22
+ return (0, _usecases.createEntityReducers)(store, usecase, {
23
+ options: usecaseOptions,
45
24
  onGenerate: function onGenerate(newEntity, result) {
46
25
  return result;
47
26
  }
48
- }));
27
+ });
49
28
  };
50
- return (0, _useContextualItem.useContextualItem)(contextReducers, statuses, function () {
51
- return onCreate();
52
- }, deps);
29
+ return (0, _useContextualItem.useContextualItem)(contextReducers, statuses, onCreate, deps);
53
30
  };