Comparing version 0.0.5 to 0.0.6
import type { Action, BlockerListener, Location, State } from './def'; | ||
export default class Blocker { | ||
blocking?: Promise<boolean>; | ||
listener: BlockerListener[]; | ||
block: (listener: BlockerListener | string) => () => void; | ||
_emitListener: (location: number | Location<State>, action: Action) => Promise<boolean>; | ||
canLeave: (location: number | Location<State>, action: Action) => Promise<boolean>; | ||
} |
@@ -42,2 +42,3 @@ "use strict"; | ||
var _this = this; | ||
this.blocking = undefined; | ||
this.listener = []; | ||
@@ -52,3 +53,3 @@ this.block = function (listener) { | ||
}; | ||
this.canLeave = function (location, action) { return __awaiter(_this, void 0, void 0, function () { | ||
this._emitListener = function (location, action) { return __awaiter(_this, void 0, void 0, function () { | ||
var i, cb, res, err_1; | ||
@@ -58,5 +59,2 @@ return __generator(this, function (_a) { | ||
case 0: | ||
if (this.listener.length === 0) { | ||
return [2 /*return*/, true]; | ||
} | ||
i = 0; | ||
@@ -78,2 +76,3 @@ _a.label = 1; | ||
err_1 = _a.sent(); | ||
console.error(err_1); | ||
return [2 /*return*/, false]; | ||
@@ -87,2 +86,18 @@ case 5: | ||
}); }; | ||
this.canLeave = function (location, action) { | ||
if (_this.listener.length === 0) { | ||
return Promise.resolve(true); | ||
} | ||
if (_this.blocking) { | ||
return Promise.resolve(false); | ||
} | ||
var blocking = _this._emitListener(location, action); | ||
if (blocking instanceof Promise) { | ||
_this.blocking = blocking; | ||
blocking.then(function () { | ||
_this.blocking = undefined; | ||
}); | ||
} | ||
return blocking; | ||
}; | ||
} | ||
@@ -89,0 +104,0 @@ return Blocker; |
@@ -71,3 +71,3 @@ "use strict"; | ||
var globalState = globalHistory.state || {}; | ||
var globalPath = utils_1.getCurrentLocationPath(); | ||
var globalPath = utils_1.getCurrentLocationPath(hashRouter); | ||
// 是否用户触发跳转 | ||
@@ -81,3 +81,3 @@ var isUserAction = false; | ||
var revertCallback = function () { return undefined; }; | ||
var historyLength = (globalState.index || 0) + 1; | ||
var historyLength = (globalState[exports.HISTORY_INDEX_NAME] || 0) + 1; | ||
var getGlobalIndex = function () { return globalState[exports.HISTORY_INDEX_NAME] || 0; }; | ||
@@ -90,3 +90,3 @@ var go = function (delta) { return __awaiter(void 0, void 0, void 0, function () { | ||
globalIndex = getGlobalIndex(); | ||
max = historyLength - 1 - globalIndex; | ||
max = Math.max(0, historyLength - 1 - globalIndex); | ||
min = -globalIndex; | ||
@@ -188,3 +188,3 @@ realDelta = Math.min(max, Math.max(min, delta)); | ||
} | ||
var currentPath = utils_1.getCurrentLocationPath(); | ||
var currentPath = utils_1.getCurrentLocationPath(hashRouter); | ||
var currentState = globalHistory.state || {}; | ||
@@ -205,10 +205,12 @@ var globalIndex = getGlobalIndex(); | ||
var nextLocation_1 = __assign(__assign({}, currentPath), { state: currentState.state }); | ||
var canGo_1 = function () { return blocker.canLeave(nextLocation_1, action).then(function (ok) { | ||
if (ok) { | ||
isInitiative = true; | ||
initiativeActionType = 'GO'; | ||
isRevert = false; | ||
} | ||
return ok; | ||
}); }; | ||
var canGo_1 = function () { | ||
return blocker.canLeave(nextLocation_1, action).then(function (ok) { | ||
if (ok) { | ||
isInitiative = true; | ||
initiativeActionType = 'GO'; | ||
isRevert = false; | ||
} | ||
return ok; | ||
}); | ||
}; | ||
if (action === def_1.Action.Replace) { | ||
@@ -224,3 +226,4 @@ // 如果是 Replace 的,则需要通过 Replace 返回 | ||
if (ok) { | ||
history.go(1); | ||
globalHistory.go(1); | ||
// history.go(1); | ||
} | ||
@@ -235,3 +238,4 @@ }); | ||
if (ok) { | ||
history.go(-1); | ||
globalHistory.go(-1); | ||
// history.go(-1); | ||
} | ||
@@ -249,2 +253,4 @@ }); | ||
} | ||
// 前进时更新长度 | ||
historyLength = Math.max(historyLength, index); | ||
// 相应处理结束,更新状态及触发事件 | ||
@@ -251,0 +257,0 @@ isInitiative = false; |
@@ -57,3 +57,3 @@ "use strict"; | ||
if (hashRouter) { | ||
return exports.parsePath(hash, state.state); | ||
return exports.parsePath(hash.replace(/^#/, ''), state.state); | ||
} | ||
@@ -60,0 +60,0 @@ return exports.parsePath({ hash: hash, pathname: pathname, query: {}, search: search, state: state.state }); |
{ | ||
"name": "k-history", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"main": "./lib/index.js", | ||
@@ -5,0 +5,0 @@ "files": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34488
708