3
3
import { connect , createStore } from './store' ;
4
4
import { QueryString , cleanObject , GemError , absoluteLocation } from './utils' ;
5
5
6
- const nativeHistory = window . history ;
6
+ const nativeHistory = globalThis . history ;
7
7
const nativePushState : typeof nativeHistory . pushState = nativeHistory . pushState . bind ( nativeHistory ) ;
8
8
const nativeReplaceState : typeof nativeHistory . replaceState = nativeHistory . replaceState . bind ( nativeHistory ) ;
9
9
@@ -107,7 +107,7 @@ function updateHistory(p: UpdateHistoryParams, native: typeof nativeHistory.push
107
107
const url = getUrlBarPath ( path ) + new QueryString ( query ) + hash ;
108
108
const prevHash = decodeURIComponent ( location . hash ) ;
109
109
native ( state , title , url ) ;
110
- if ( prevHash !== hash ) window . dispatchEvent ( new CustomEvent ( 'hashchange' ) ) ;
110
+ if ( prevHash !== hash ) dispatchEvent ( new CustomEvent ( 'hashchange' ) ) ;
111
111
}
112
112
113
113
// 跨框架时,调用者对 basePath 无感知
@@ -128,7 +128,7 @@ function updateHistoryByNative(data: any, title: string, originUrl: string, nati
128
128
const prevHash = location . hash ;
129
129
native ( state , title , url ) ;
130
130
// `location.hash` 和 `hash` 都已经进行了 url 编码,可以直接进行相等判断
131
- if ( prevHash !== hash ) window . dispatchEvent ( new CustomEvent ( 'hashchange' ) ) ;
131
+ if ( prevHash !== hash ) dispatchEvent ( new CustomEvent ( 'hashchange' ) ) ;
132
132
}
133
133
134
134
const gemBasePathStore = createStore ( {
@@ -185,16 +185,15 @@ const gemHistory = new GemHistory();
185
185
186
186
const gemTitleStore = createStore ( { defaultTitle : document . title , url : '' , title : '' } ) ;
187
187
188
- const _GEMHISTORY = { history : gemHistory , titleStore : gemTitleStore , basePathStore : gemBasePathStore } ;
188
+ const HISTORY = { history : gemHistory , titleStore : gemTitleStore , basePathStore : gemBasePathStore } ;
189
189
190
190
declare global {
191
- interface Window {
192
- _GEMHISTORY ?: typeof _GEMHISTORY ;
193
- }
191
+ // eslint-disable-next-line no-var
192
+ var _GEMHISTORY : typeof HISTORY | undefined ;
194
193
}
195
194
196
- if ( ! window . _GEMHISTORY ) {
197
- window . _GEMHISTORY = _GEMHISTORY ;
195
+ if ( ! globalThis . _GEMHISTORY ) {
196
+ globalThis . _GEMHISTORY = HISTORY ;
198
197
199
198
nativeHistory . pushState = function ( state : any , title : string , path : string ) {
200
199
updateHistoryByNative ( state , title , path , nativePushState ) ;
@@ -205,7 +204,7 @@ if (!window._GEMHISTORY) {
205
204
} ;
206
205
207
206
// 点击 `<a>`
208
- window . addEventListener ( 'hashchange' , ( { isTrusted } ) => {
207
+ addEventListener ( 'hashchange' , ( { isTrusted } ) => {
209
208
if ( isTrusted ) {
210
209
gemHistory . replace ( { hash : location . hash } ) ;
211
210
}
@@ -250,7 +249,7 @@ if (!window._GEMHISTORY) {
250
249
* 表示 popstate handler 中正在进行导航
251
250
*/
252
251
let navigating = false ;
253
- window . addEventListener ( 'popstate' , ( event ) => {
252
+ addEventListener ( 'popstate' , ( event ) => {
254
253
const newState = event . state as HistoryState | null ;
255
254
if ( ! newState ?. $key ) {
256
255
// 比如作为其他框架 app 的宿主 app
@@ -316,5 +315,5 @@ if (!window._GEMHISTORY) {
316
315
} ) ;
317
316
}
318
317
319
- const { history, titleStore, basePathStore } = window . _GEMHISTORY ;
318
+ const { history, titleStore, basePathStore } = globalThis . _GEMHISTORY ;
320
319
export { history , titleStore , basePathStore } ;
0 commit comments