@lemon-fe/mini-app 1.2.6 → 1.2.7-alpha.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.
@@ -1,3 +1,11 @@
1
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2
+
3
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return generator._invoke = function (innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; }(innerFn, self, context), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; this._invoke = function (method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); }; } function maybeInvokeDelegate(delegate, context) { var method = delegate.iterator[context.method]; if (undefined === method) { if (context.delegate = null, "throw" === context.method) { if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel; context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method"); } return ContinueSentinel; } var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) { if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; } return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (object) { var keys = []; for (var key in object) { keys.push(key); } return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) { "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); } }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, catch: function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
4
+
5
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
6
+
7
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
8
+
1
9
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
2
10
 
3
11
  function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
@@ -24,7 +32,7 @@ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Sy
24
32
 
25
33
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
26
34
 
27
- import React, { cloneElement, useCallback, useEffect, useMemo, useState } from 'react';
35
+ import React, { cloneElement, useCallback, useEffect, useMemo, useRef, useState } from 'react';
28
36
  import { TransitionGroup } from 'react-transition-group';
29
37
  import { parsePath } from '../../utils/path';
30
38
  import { NavigationContext } from '../../context';
@@ -32,6 +40,15 @@ import Screen from '../Screen';
32
40
  import { GO_BACK } from '../../constants';
33
41
  import { mini } from '../../global';
34
42
  import './index.css';
43
+
44
+ function useMemoizedFun(callback) {
45
+ var ref = useRef(callback);
46
+ ref.current = callback;
47
+ return function () {
48
+ return ref.current.apply(ref, arguments);
49
+ };
50
+ }
51
+
35
52
  export default function AppContainer(props) {
36
53
  var pages = props.pages,
37
54
  children = props.children,
@@ -48,6 +65,7 @@ export default function AppContainer(props) {
48
65
  state = _useState2[0],
49
66
  setState = _useState2[1];
50
67
 
68
+ var preventRemove = useRef(new Map());
51
69
  var navigate = useCallback(function (path, otherParams) {
52
70
  setState(function (prev) {
53
71
  var routes = prev.routes;
@@ -92,69 +110,173 @@ export default function AppContainer(props) {
92
110
  return prev;
93
111
  });
94
112
  }, []);
95
- var replace = useCallback(function (path, otherParams) {
96
- setState(function (prev) {
97
- var routes = prev.routes;
98
-
99
- var _parsePath2 = parsePath(path),
100
- pathname = _parsePath2.pathname,
101
- search = _parsePath2.search;
102
-
103
- var params = search ? Object.fromEntries(Array.from(new URLSearchParams(search).entries())) : {};
104
- Object.assign(params, otherParams);
105
- var routeIndex = routes.findIndex(function (item) {
106
- return item.route.path === pathname;
107
- });
108
113
 
109
- if (routeIndex >= 0) {
110
- var nextRoutes = routes.slice(0, routeIndex + 1);
111
- nextRoutes[routeIndex] = _objectSpread(_objectSpread({}, routes[routeIndex]), {}, {
112
- params: params
113
- });
114
- return {
115
- routes: nextRoutes
116
- };
117
- }
118
-
119
- var page = pages.find(function (item) {
120
- return item.path === pathname;
121
- });
122
-
123
- if (page !== undefined) {
124
- var newRoute = {
125
- route: page,
126
- params: params
127
- };
128
-
129
- if (routes.length <= 0) {
130
- return {
131
- routes: [newRoute]
132
- };
114
+ var backToRoute = /*#__PURE__*/function () {
115
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(routes, index) {
116
+ var targetIndex, i, key, preventCallback, shoudPrevent;
117
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
118
+ while (1) {
119
+ switch (_context.prev = _context.next) {
120
+ case 0:
121
+ targetIndex = index;
122
+ i = routes.length - 1;
123
+
124
+ case 2:
125
+ if (!(i > index)) {
126
+ _context.next = 15;
127
+ break;
128
+ }
129
+
130
+ key = routes[i].route.path;
131
+ preventCallback = preventRemove.current.get(key);
132
+
133
+ if (!preventCallback) {
134
+ _context.next = 12;
135
+ break;
136
+ }
137
+
138
+ _context.next = 8;
139
+ return preventCallback();
140
+
141
+ case 8:
142
+ shoudPrevent = _context.sent;
143
+
144
+ if (!shoudPrevent) {
145
+ _context.next = 12;
146
+ break;
147
+ }
148
+
149
+ targetIndex = i;
150
+ return _context.abrupt("break", 15);
151
+
152
+ case 12:
153
+ i--;
154
+ _context.next = 2;
155
+ break;
156
+
157
+ case 15:
158
+ return _context.abrupt("return", routes.slice(0, targetIndex + 1));
159
+
160
+ case 16:
161
+ case "end":
162
+ return _context.stop();
163
+ }
133
164
  }
165
+ }, _callee);
166
+ }));
134
167
 
135
- return {
136
- routes: routes.slice(0, -1).concat(newRoute)
137
- };
138
- }
168
+ return function backToRoute(_x, _x2) {
169
+ return _ref.apply(this, arguments);
170
+ };
171
+ }();
139
172
 
140
- return prev;
173
+ var updateRoutes = useCallback(function (routes) {
174
+ setState(function (prev) {
175
+ return _objectSpread(_objectSpread({}, prev), {}, {
176
+ routes: routes
177
+ });
141
178
  });
142
179
  }, []);
143
- var goBack = useCallback(function () {
144
- setState(function (prev) {
145
- var nextRoutes = _toConsumableArray(prev.routes);
180
+ var replace = useMemoizedFun( /*#__PURE__*/function () {
181
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(path, otherParams) {
182
+ var routes, _parsePath2, pathname, search, params, routeIndex, nextRoutes, page, newRoute;
183
+
184
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
185
+ while (1) {
186
+ switch (_context2.prev = _context2.next) {
187
+ case 0:
188
+ routes = state.routes;
189
+ _parsePath2 = parsePath(path), pathname = _parsePath2.pathname, search = _parsePath2.search;
190
+ params = search ? Object.fromEntries(Array.from(new URLSearchParams(search).entries())) : {};
191
+ Object.assign(params, otherParams);
192
+ routeIndex = routes.findIndex(function (item) {
193
+ return item.route.path === pathname;
194
+ });
195
+
196
+ if (!(routeIndex >= 0)) {
197
+ _context2.next = 12;
198
+ break;
199
+ }
200
+
201
+ _context2.next = 8;
202
+ return backToRoute(routes, routeIndex);
203
+
204
+ case 8:
205
+ nextRoutes = _context2.sent;
206
+ nextRoutes[routeIndex] = _objectSpread(_objectSpread({}, routes[routeIndex]), {}, {
207
+ params: params
208
+ });
209
+ updateRoutes(nextRoutes);
210
+ return _context2.abrupt("return");
211
+
212
+ case 12:
213
+ page = pages.find(function (item) {
214
+ return item.path === pathname;
215
+ });
216
+
217
+ if (!(page !== undefined)) {
218
+ _context2.next = 20;
219
+ break;
220
+ }
221
+
222
+ newRoute = {
223
+ route: page,
224
+ params: params
225
+ };
226
+
227
+ if (!(routes.length <= 0)) {
228
+ _context2.next = 18;
229
+ break;
230
+ }
231
+
232
+ updateRoutes([newRoute]);
233
+ return _context2.abrupt("return");
234
+
235
+ case 18:
236
+ updateRoutes(routes.slice(0, -1).concat(newRoute));
237
+ return _context2.abrupt("return");
238
+
239
+ case 20:
240
+ case "end":
241
+ return _context2.stop();
242
+ }
243
+ }
244
+ }, _callee2);
245
+ }));
146
246
 
147
- if (nextRoutes.length > 1) {
148
- nextRoutes.pop();
149
- return _objectSpread(_objectSpread({}, prev), {}, {
150
- routes: nextRoutes
151
- });
247
+ return function (_x3, _x4) {
248
+ return _ref2.apply(this, arguments);
249
+ };
250
+ }());
251
+ var goBack = useMemoizedFun( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
252
+ var nextRoutes;
253
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
254
+ while (1) {
255
+ switch (_context3.prev = _context3.next) {
256
+ case 0:
257
+ if (!(state.routes.length > 1)) {
258
+ _context3.next = 6;
259
+ break;
260
+ }
261
+
262
+ _context3.next = 3;
263
+ return backToRoute(state.routes, state.routes.length - 2);
264
+
265
+ case 3:
266
+ nextRoutes = _context3.sent;
267
+ updateRoutes(nextRoutes);
268
+ return _context3.abrupt("return");
269
+
270
+ case 6:
271
+ mini.exit();
272
+
273
+ case 7:
274
+ case "end":
275
+ return _context3.stop();
276
+ }
152
277
  }
153
-
154
- mini.exit();
155
- return prev;
156
- });
157
- }, []);
278
+ }, _callee3);
279
+ })));
158
280
  useEffect(function () {
159
281
  return mini.addListener(function (e) {
160
282
  if (e === GO_BACK) {
@@ -185,6 +307,7 @@ export default function AppContainer(props) {
185
307
  goBack: goBack,
186
308
  replace: replace
187
309
  },
310
+ preventRemove: preventRemove.current,
188
311
  headerStyle: headerStyle
189
312
  };
190
313
  }, [navigate, goBack, replace, headerStyle]);
@@ -76,9 +76,10 @@ export default function Screen(props) {
76
76
  var ctx = useMemo(function () {
77
77
  return {
78
78
  params: params,
79
- focused: focused
79
+ focused: focused,
80
+ route: route
80
81
  };
81
- }, [params, focused]);
82
+ }, [params, focused, route]);
82
83
  return /*#__PURE__*/React.createElement(CSSTransition, _objectSpread({
83
84
  timeout: 300,
84
85
  classNames: "lemon-transition",
@@ -1,13 +1,16 @@
1
1
  import type { CSSProperties } from 'react';
2
+ import type { NavigationStackScreen } from '../typings';
2
3
  export declare const NavigationContext: import("react").Context<{
3
4
  navigation: {
4
5
  navigate: (path: string, params?: Record<string, any>) => void;
5
6
  goBack: () => void;
6
7
  replace: (path: string, params?: Record<string, any>) => void;
7
8
  };
9
+ preventRemove: Map<string, () => Promise<boolean>>;
8
10
  headerStyle?: CSSProperties | undefined;
9
11
  }>;
10
12
  export declare const ScreenContext: import("react").Context<{
11
13
  params?: Record<string, any> | undefined;
12
14
  focused: boolean;
15
+ route: NavigationStackScreen;
13
16
  }>;
@@ -4,8 +4,10 @@ export var NavigationContext = /*#__PURE__*/createContext({
4
4
  navigate: function navigate() {},
5
5
  goBack: function goBack() {},
6
6
  replace: function replace() {}
7
- }
7
+ },
8
+ preventRemove: new Map()
8
9
  });
9
10
  export var ScreenContext = /*#__PURE__*/createContext({
10
- focused: false
11
+ focused: false,
12
+ route: {}
11
13
  });
@@ -5,3 +5,4 @@ export declare function useNavigation(): {
5
5
  };
6
6
  export declare function useParams<T extends Record<string, any> | void = void>(): T;
7
7
  export declare function useIsFocused(): boolean;
8
+ export declare function usePreventRemove(callback: () => Promise<boolean>): void;
package/es/hooks/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { useContext } from 'react';
1
+ import { useContext, useEffect } from 'react';
2
2
  import { NavigationContext, ScreenContext } from '../context';
3
3
  export function useNavigation() {
4
4
  var ctx = useContext(NavigationContext);
@@ -11,4 +11,18 @@ export function useParams() {
11
11
  export function useIsFocused() {
12
12
  var ctx = useContext(ScreenContext);
13
13
  return ctx.focused;
14
+ }
15
+ export function usePreventRemove(callback) {
16
+ var _useContext = useContext(NavigationContext),
17
+ preventRemove = _useContext.preventRemove;
18
+
19
+ var _useContext2 = useContext(ScreenContext),
20
+ route = _useContext2.route;
21
+
22
+ useEffect(function () {
23
+ preventRemove.set(route.path, callback);
24
+ return function () {
25
+ preventRemove.delete(route.path);
26
+ };
27
+ }, [callback, preventRemove, route]);
14
28
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lemon-fe/mini-app",
3
- "version": "1.2.6",
3
+ "version": "1.2.7-alpha.0",
4
4
  "description": "> TODO: description",
5
5
  "author": "鲁盛杰 <lusj@cnlemon.net>",
6
6
  "homepage": "",
@@ -31,5 +31,5 @@
31
31
  "react": "^17.0.2",
32
32
  "react-dom": "17.0.2"
33
33
  },
34
- "gitHead": "409551a9fb0476ae3fefdb1a68479b92c7f8a945"
34
+ "gitHead": "49e994d2f828cec059d9aca4bb3f711a082485da"
35
35
  }