From 58671f373ac5e69a68fd3250bdf40395c6e757c2 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 15 Nov 2022 16:08:51 -0500 Subject: [PATCH] feat: support both history 4.x and 5.x --- package.json | 5 ++-- src/lib/adapters/historyjs/index.tsx | 20 +++++++++++-- yarn.lock | 44 +++++++++++++++++++++++----- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index fe0e75b..5aa4938 100644 --- a/package.json +++ b/package.json @@ -76,7 +76,7 @@ "memoize-one": "^6.0.0" }, "peerDependencies": { - "history": "^5", + "history": "^5|^4", "immer": ">=7", "next": ">=11", "react": ">=16.8", @@ -108,6 +108,7 @@ "@testing-library/user-event": "^14.4.3", "@types/jest": "^29.2.2", "@types/node": "^17.0.35", + "@types/history": "4.7.11", "awesome-typescript-loader": "^5.2.1", "babel-loader": "^8.2.5", "browser-env": "^3.3.0", @@ -115,7 +116,7 @@ "cz-conventional-changelog": "^3.3.0", "expect": "^28.1.0", "gh-pages": "^4.0.0", - "history": "^5.3.0", + "history": "^4.10.1", "immer": "^9.0.16", "jest": "^29.3.1", "jest-environment-jsdom": "^29.3.1", diff --git a/src/lib/adapters/historyjs/index.tsx b/src/lib/adapters/historyjs/index.tsx index 8b89509..0bee380 100644 --- a/src/lib/adapters/historyjs/index.tsx +++ b/src/lib/adapters/historyjs/index.tsx @@ -1,5 +1,5 @@ /* tslint:disable:no-expression-statement readonly-array */ -import { History } from 'history' +import { Action, History, Location } from 'history' import React, { forwardRef, ReactNode, @@ -65,8 +65,22 @@ export const GeschichteWithHistory = forwardRef( ) useEffect(() => { - return history.listen((update) => { - const { action, location } = update + return history.listen((update, maybeAction) => { + // tslint:disable-next-line:no-let + let action: Action + // tslint:disable-next-line:no-let + let location: Location + // history.js 4.x + if (maybeAction) { + location = update + action = maybeAction + } else { + // history.js 5.x + // @ts-ignore + action = update.action + // @ts-ignore + location = update.location + } // don't handle our own actions if ( (action === 'REPLACE' || action === 'PUSH') && diff --git a/yarn.lock b/yarn.lock index 620be12..a9e3fa0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1347,7 +1347,7 @@ pirates "^4.0.5" source-map-support "^0.5.16" -"@babel/runtime@^7.0.0": +"@babel/runtime@^7.0.0", "@babel/runtime@^7.1.2": version "7.20.1" resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.20.1.tgz#1148bb33ab252b165a06698fde7576092a78b4a9" integrity sha512-mrzLkl6U9YLF8qpqI7TB82PESyEGjm/0Ly91jG575eVxMMlb8fYfOXFZIJ8XfLrJZQbm7dlKry2bJmXBUEkdFg== @@ -3533,6 +3533,11 @@ dependencies: "@types/unist" "*" +"@types/history@4.7.11": + version "4.7.11" + resolved "https://registry.yarnpkg.com/@types/history/-/history-4.7.11.tgz#56588b17ae8f50c53983a524fc3cc47437969d64" + integrity sha512-qjDJRrmvBMiTx+jyLxvLfJU7UznFuokDv4f3WRuriHKERccVpFU+8XMQUAbDzoiJCsmexxRExQeMwwCdamSKDA== + "@types/html-minifier-terser@^5.0.0": version "5.1.2" resolved "https://registry.yarnpkg.com/@types/html-minifier-terser/-/html-minifier-terser-5.1.2.tgz#693b316ad323ea97eed6b38ed1a3cc02b1672b57" @@ -8329,12 +8334,17 @@ he@^1.2.0: resolved "https://registry.yarnpkg.com/he/-/he-1.2.0.tgz#84ae65fa7eafb165fddb61566ae14baf05664f0f" integrity sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw== -history@^5.3.0: - version "5.3.0" - resolved "https://registry.yarnpkg.com/history/-/history-5.3.0.tgz#1548abaa245ba47992f063a0783db91ef201c73b" - integrity sha512-ZqaKwjjrAYUYfLG+htGaIIZ4nioX2L70ZUMIFysS3xvBsSG4x/n1V6TXV3N8ZYNuFGlDirFg32T7B6WOUPDYcQ== +history@^4.10.1: + version "4.10.1" + resolved "https://registry.yarnpkg.com/history/-/history-4.10.1.tgz#33371a65e3a83b267434e2b3f3b1b4c58aad4cf3" + integrity sha512-36nwAD620w12kuzPAsyINPWJqlNbij+hpK1k9XRloDtym8mxzGYl2c17LnV6IAGB2Dmg4tEa7G7DlawS0+qjew== dependencies: - "@babel/runtime" "^7.7.6" + "@babel/runtime" "^7.1.2" + loose-envify "^1.2.0" + resolve-pathname "^3.0.0" + tiny-invariant "^1.0.2" + tiny-warning "^1.0.0" + value-equal "^1.0.1" hmac-drbg@^1.0.1: version "1.0.1" @@ -10414,7 +10424,7 @@ longest@^2.0.1: resolved "https://registry.yarnpkg.com/longest/-/longest-2.0.1.tgz#781e183296aa94f6d4d916dc335d0d17aefa23f8" integrity sha1-eB4YMpaqlPbU2RbcM10NF676I/g= -loose-envify@^1.1.0, loose-envify@^1.4.0: +loose-envify@^1.1.0, loose-envify@^1.2.0, loose-envify@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf" integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q== @@ -13378,6 +13388,11 @@ resolve-global@^1.0.0: dependencies: global-dirs "^0.1.1" +resolve-pathname@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/resolve-pathname/-/resolve-pathname-3.0.0.tgz#99d02224d3cf263689becbb393bc560313025dcd" + integrity sha512-C7rARubxI8bXFNB/hqcp/4iUeIXJhJZvFPFPiSPRnhU5UPxzMFIl+2E6yY6c4k9giDJAhtV+enfA+G89N6Csng== + resolve-typescript-plugin@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/resolve-typescript-plugin/-/resolve-typescript-plugin-1.2.0.tgz#411271a1661b7ce050d2fff51d9ee66311ffa78f" @@ -14682,11 +14697,21 @@ timers-browserify@^2.0.4: dependencies: setimmediate "^1.0.4" +tiny-invariant@^1.0.2: + version "1.3.1" + resolved "https://registry.yarnpkg.com/tiny-invariant/-/tiny-invariant-1.3.1.tgz#8560808c916ef02ecfd55e66090df23a4b7aa642" + integrity sha512-AD5ih2NlSssTCwsMznbvwMZpJ1cbhkGd2uueNxzv2jDlEeZdU04JQfRnggJQ8DrcVBGjAsCKwFBbDlVNtEMlzw== + tiny-relative-date@^1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/tiny-relative-date/-/tiny-relative-date-1.3.0.tgz#fa08aad501ed730f31cc043181d995c39a935e07" integrity sha512-MOQHpzllWxDCHHaDno30hhLfbouoYlOI8YlMNtvKe1zXbjEVhbcEovQxvZrPvtiYW630GQDoMMarCnjfyfHA+A== +tiny-warning@^1.0.0: + version "1.0.3" + resolved "https://registry.yarnpkg.com/tiny-warning/-/tiny-warning-1.0.3.tgz#94a30db453df4c643d0fd566060d60a875d84754" + integrity sha512-lBN9zLN/oAf68o3zNXYrdCt1kP8WsiGW8Oo2ka41b2IM5JL/S1CTyX1rW0mb/zSuJun0ZUrDxx4sqvYS2FWzPA== + tmp@^0.0.33: version "0.0.33" resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9" @@ -15425,6 +15450,11 @@ validate-npm-package-name@^4.0.0: dependencies: builtins "^5.0.0" +value-equal@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/value-equal/-/value-equal-1.0.1.tgz#1e0b794c734c5c0cade179c437d356d931a34d6c" + integrity sha512-NOJ6JZCAWr0zlxZt+xqCHNTEKOsrks2HQd4MqhP1qy4z1SkbEP467eNx6TgDKXMvUOb+OENfJCZwM+16n7fRfw== + vary@~1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"