manages browsing session history jsr.io/@mary/history
typescript jsr
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: new random key field

mary.my.id a04861db 575882c9

verified
+10 -5
+10 -5
lib/mod.ts
··· 35 35 index: number; 36 36 /** A value of arbitrary data associated with this location. */ 37 37 state: unknown; 38 - /** A unique string associated with this location */ 38 + /** A unique identifier for the specific history entry */ 39 39 id: string; 40 + /** A unique identifier for the history entry's slot in the entries list */ 41 + key: string; 40 42 } 41 43 42 44 /** ··· 134 136 interface HistoryState { 135 137 usr: unknown; 136 138 id?: string; 139 + key?: string; 137 140 idx: number; 138 141 } 139 142 ··· 168 171 index: state.idx, 169 172 state: state.usr || null, 170 173 id: state.id || 'default', 174 + key: state.key || 'default', 171 175 }; 172 176 }; 173 177 ··· 230 234 globalHistory.replaceState({ ...globalHistory.state, idx: (location.index = 0) }, ''); 231 235 } 232 236 233 - // state defaults to `null` because `window.history.state` does 234 - const getNextLocation = (to: To, index: number, state: unknown = null): Location => { 237 + const getNextLocation = (replace: boolean, to: To, index: number, state: unknown): Location => { 235 238 return { 236 239 pathname: location.pathname, 237 240 hash: '', ··· 240 243 index, 241 244 state, 242 245 id: randomId(), 246 + key: replace ? location.key : randomId(), 243 247 }; 244 248 }; 245 249 ··· 248 252 { 249 253 usr: nextLocation.state, 250 254 id: nextLocation.id, 255 + key: nextLocation.key, 251 256 idx: nextLocation.index, 252 257 }, 253 258 createHref(nextLocation), ··· 263 268 emitter.emit('update', { action: nextAction, location }); 264 269 }; 265 270 266 - const navigate = (to: To, { replace, state }: NavigateOptions = {}): void => { 271 + const navigate = (to: To, { replace = false, state = null }: NavigateOptions = {}): void => { 267 272 const nextAction: Action = !replace ? 'push' : 'replace'; 268 273 const nextIndex = location.index + (!replace ? 1 : 0); 269 - const nextLocation = getNextLocation(to, nextIndex, state); 274 + const nextLocation = getNextLocation(replace, to, nextIndex, state); 270 275 271 276 const retry = () => { 272 277 navigate(to, { replace, state });