@lemon-fe/mini-app 1.2.6 → 1.3.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,5 @@
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
+
1
3
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
2
4
 
3
5
  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."); }
@@ -12,6 +14,12 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
12
14
 
13
15
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
14
16
 
17
+ 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; }
18
+
19
+ 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); } }
20
+
21
+ 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); }); }; }
22
+
15
23
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
16
24
 
17
25
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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,113 +65,234 @@ export default function AppContainer(props) {
48
65
  state = _useState2[0],
49
66
  setState = _useState2[1];
50
67
 
51
- var navigate = useCallback(function (path, otherParams) {
52
- setState(function (prev) {
53
- var routes = prev.routes;
54
-
55
- var _parsePath = parsePath(path),
56
- pathname = _parsePath.pathname,
57
- search = _parsePath.search;
58
-
59
- var params = search ? Object.fromEntries(Array.from(new URLSearchParams(search).entries())) : {};
60
- Object.assign(params, otherParams);
61
- var routeIndex = routes.findIndex(function (item) {
62
- return item.route.path === pathname;
63
- });
68
+ var preventRemove = useRef(new Map());
69
+
70
+ var backToRoute = /*#__PURE__*/function () {
71
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(routes, index) {
72
+ var targetIndex, i, key, preventCallback, shoudPrevent;
73
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
74
+ while (1) {
75
+ switch (_context.prev = _context.next) {
76
+ case 0:
77
+ targetIndex = index;
78
+ i = routes.length - 1;
79
+
80
+ case 2:
81
+ if (!(i > index)) {
82
+ _context.next = 15;
83
+ break;
84
+ }
85
+
86
+ key = routes[i].route.path;
87
+ preventCallback = preventRemove.current.get(key);
88
+
89
+ if (!preventCallback) {
90
+ _context.next = 12;
91
+ break;
92
+ }
93
+
94
+ _context.next = 8;
95
+ return preventCallback();
96
+
97
+ case 8:
98
+ shoudPrevent = _context.sent;
99
+
100
+ if (!shoudPrevent) {
101
+ _context.next = 12;
102
+ break;
103
+ }
104
+
105
+ targetIndex = i;
106
+ return _context.abrupt("break", 15);
107
+
108
+ case 12:
109
+ i--;
110
+ _context.next = 2;
111
+ break;
112
+
113
+ case 15:
114
+ return _context.abrupt("return", routes.slice(0, targetIndex + 1));
115
+
116
+ case 16:
117
+ case "end":
118
+ return _context.stop();
119
+ }
120
+ }
121
+ }, _callee);
122
+ }));
64
123
 
65
- if (routeIndex >= 0) {
66
- var nextRoutes = routes.slice(0, routeIndex + 1);
67
- nextRoutes[routeIndex] = _objectSpread(_objectSpread({}, routes[routeIndex]), {}, {
68
- params: params
69
- });
70
- return {
71
- routes: nextRoutes
72
- };
73
- }
124
+ return function backToRoute(_x, _x2) {
125
+ return _ref.apply(this, arguments);
126
+ };
127
+ }();
74
128
 
75
- var page = pages.find(function (item) {
76
- return item.path === pathname;
129
+ var updateRoutes = useCallback(function (routes) {
130
+ setState(function (prev) {
131
+ return _objectSpread(_objectSpread({}, prev), {}, {
132
+ routes: routes
77
133
  });
78
-
79
- if (page !== undefined) {
80
- var _nextRoutes = _toConsumableArray(routes);
81
-
82
- _nextRoutes.push({
83
- route: page,
84
- params: params
85
- });
86
-
87
- return {
88
- routes: _nextRoutes
89
- };
90
- }
91
-
92
- return prev;
93
134
  });
94
135
  }, []);
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
-
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
- };
136
+ var navigate = useMemoizedFun( /*#__PURE__*/function () {
137
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(path, otherParams) {
138
+ var routes, _parsePath, pathname, search, params, routeIndex, nextRoutes, page;
139
+
140
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
141
+ while (1) {
142
+ switch (_context2.prev = _context2.next) {
143
+ case 0:
144
+ routes = state.routes;
145
+ _parsePath = parsePath(path), pathname = _parsePath.pathname, search = _parsePath.search;
146
+ params = search ? Object.fromEntries(Array.from(new URLSearchParams(search).entries())) : {};
147
+ Object.assign(params, otherParams);
148
+ routeIndex = routes.findIndex(function (item) {
149
+ return item.route.path === pathname;
150
+ });
151
+
152
+ if (!(routeIndex >= 0)) {
153
+ _context2.next = 11;
154
+ break;
155
+ }
156
+
157
+ _context2.next = 8;
158
+ return backToRoute(routes, routeIndex);
159
+
160
+ case 8:
161
+ nextRoutes = _context2.sent;
162
+ nextRoutes[routeIndex] = _objectSpread(_objectSpread({}, routes[routeIndex]), {}, {
163
+ params: params
164
+ });
165
+ return _context2.abrupt("return", updateRoutes(nextRoutes));
166
+
167
+ case 11:
168
+ page = pages.find(function (item) {
169
+ return item.path === pathname;
170
+ });
171
+
172
+ if (!(page !== undefined)) {
173
+ _context2.next = 14;
174
+ break;
175
+ }
176
+
177
+ return _context2.abrupt("return", updateRoutes([].concat(_toConsumableArray(routes), [{
178
+ route: page,
179
+ params: params
180
+ }])));
181
+
182
+ case 14:
183
+ case "end":
184
+ return _context2.stop();
185
+ }
133
186
  }
187
+ }, _callee2);
188
+ }));
134
189
 
135
- return {
136
- routes: routes.slice(0, -1).concat(newRoute)
137
- };
138
- }
139
-
140
- return prev;
141
- });
142
- }, []);
143
- var goBack = useCallback(function () {
144
- setState(function (prev) {
145
- var nextRoutes = _toConsumableArray(prev.routes);
190
+ return function (_x3, _x4) {
191
+ return _ref2.apply(this, arguments);
192
+ };
193
+ }());
194
+ var replace = useMemoizedFun( /*#__PURE__*/function () {
195
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(path, otherParams) {
196
+ var routes, _parsePath2, pathname, search, params, routeIndex, nextRoutes, page, newRoute;
197
+
198
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
199
+ while (1) {
200
+ switch (_context3.prev = _context3.next) {
201
+ case 0:
202
+ routes = state.routes;
203
+ _parsePath2 = parsePath(path), pathname = _parsePath2.pathname, search = _parsePath2.search;
204
+ params = search ? Object.fromEntries(Array.from(new URLSearchParams(search).entries())) : {};
205
+ Object.assign(params, otherParams);
206
+ routeIndex = routes.findIndex(function (item) {
207
+ return item.route.path === pathname;
208
+ });
209
+
210
+ if (!(routeIndex >= 0)) {
211
+ _context3.next = 12;
212
+ break;
213
+ }
214
+
215
+ _context3.next = 8;
216
+ return backToRoute(routes, routeIndex);
217
+
218
+ case 8:
219
+ nextRoutes = _context3.sent;
220
+ nextRoutes[routeIndex] = _objectSpread(_objectSpread({}, routes[routeIndex]), {}, {
221
+ params: params
222
+ });
223
+ updateRoutes(nextRoutes);
224
+ return _context3.abrupt("return");
225
+
226
+ case 12:
227
+ page = pages.find(function (item) {
228
+ return item.path === pathname;
229
+ });
230
+
231
+ if (!(page !== undefined)) {
232
+ _context3.next = 20;
233
+ break;
234
+ }
235
+
236
+ newRoute = {
237
+ route: page,
238
+ params: params
239
+ };
240
+
241
+ if (!(routes.length <= 0)) {
242
+ _context3.next = 18;
243
+ break;
244
+ }
245
+
246
+ updateRoutes([newRoute]);
247
+ return _context3.abrupt("return");
248
+
249
+ case 18:
250
+ updateRoutes(routes.slice(0, -1).concat(newRoute));
251
+ return _context3.abrupt("return");
252
+
253
+ case 20:
254
+ case "end":
255
+ return _context3.stop();
256
+ }
257
+ }
258
+ }, _callee3);
259
+ }));
146
260
 
147
- if (nextRoutes.length > 1) {
148
- nextRoutes.pop();
149
- return _objectSpread(_objectSpread({}, prev), {}, {
150
- routes: nextRoutes
151
- });
261
+ return function (_x5, _x6) {
262
+ return _ref3.apply(this, arguments);
263
+ };
264
+ }());
265
+ var goBack = useMemoizedFun( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
266
+ var routes, nextRoutes;
267
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
268
+ while (1) {
269
+ switch (_context4.prev = _context4.next) {
270
+ case 0:
271
+ routes = state.routes;
272
+
273
+ if (!(routes.length > 1)) {
274
+ _context4.next = 7;
275
+ break;
276
+ }
277
+
278
+ _context4.next = 4;
279
+ return backToRoute(routes, routes.length - 2);
280
+
281
+ case 4:
282
+ nextRoutes = _context4.sent;
283
+ updateRoutes(nextRoutes);
284
+ return _context4.abrupt("return");
285
+
286
+ case 7:
287
+ mini.exit();
288
+
289
+ case 8:
290
+ case "end":
291
+ return _context4.stop();
292
+ }
152
293
  }
153
-
154
- mini.exit();
155
- return prev;
156
- });
157
- }, []);
294
+ }, _callee4);
295
+ })));
158
296
  useEffect(function () {
159
297
  return mini.addListener(function (e) {
160
298
  if (e === GO_BACK) {
@@ -185,6 +323,7 @@ export default function AppContainer(props) {
185
323
  goBack: goBack,
186
324
  replace: replace
187
325
  },
326
+ preventRemove: preventRemove.current,
188
327
  headerStyle: headerStyle
189
328
  };
190
329
  }, [navigate, goBack, replace, headerStyle]);
@@ -59,6 +59,7 @@
59
59
  }
60
60
 
61
61
  .lemon-route-body {
62
+ position: relative;
62
63
  flex: 1;
63
64
  min-height: 0;
64
65
  }
@@ -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.3.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": "d8e1da1a747ce526b80273636a3bcac9e9ce1ffe"
35
35
  }