Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into update-so-many-de…
Browse files Browse the repository at this point in the history
…pendencies
  • Loading branch information
TehShrike committed Jul 13, 2021
2 parents ae71fbc + 6b74ead commit 9edc1bb
Show file tree
Hide file tree
Showing 6 changed files with 7,762 additions and 2,074 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# [6.2.0](https://github.com/TehShrike/abstract-state-router/releases/tag/v6.2.0)

- feature: allow dynamic parameter defaults via functions [#144](https://github.com/TehShrike/abstract-state-router/pull/144)

# [6.1.0](https://github.com/TehShrike/abstract-state-router/releases/tag/v6.1.0)

- feature: added `getActiveState` method [#121](https://github.com/TehShrike/abstract-state-router/pull/121)
Expand Down
16 changes: 13 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ module.exports = function StateProvider(makeRenderer, rootElement, stateRouterOp
router.add(route, parameters => onRouteChange(state, parameters))
}

function computeDefaultParams(defaultParams) {
let computedDefaultParams = {}

const defaultParamKeyValuePairs = Object.keys(defaultParams).forEach(key => {
computedDefaultParams[key] = typeof defaultParams[key] === 'function' ? defaultParams[key]() : defaultParams[key]
})

return computedDefaultParams
}

function getStatesToResolve(stateChanges) {
return stateChanges.change.concat(stateChanges.create).map(prototypalStateHolder.get)
}
Expand Down Expand Up @@ -241,7 +251,7 @@ module.exports = function StateProvider(makeRenderer, rootElement, stateRouterOp
})

if (needToApplyDefaults) {
throw redirector(newStateName, extend(defaultParams, parameters))
throw redirector(newStateName, extend(computeDefaultParams(defaultParams), parameters))
}
return state
}).then(ifNotCancelled(state => {
Expand Down Expand Up @@ -335,9 +345,9 @@ module.exports = function StateProvider(makeRenderer, rootElement, stateRouterOp
const destinationStateName = stateName === null ? getGuaranteedPreviousState().name : stateName

const destinationState = prototypalStateHolder.get(destinationStateName) || {}
const defaultParams = destinationState.defaultParameters || destinationState.defaultQuerystringParameters
const defaultParams = destinationState.defaultParameters || destinationState.defaultQuerystringParameters || {}

parameters = extend(defaultParams, parameters)
parameters = extend(computeDefaultParams(defaultParams), parameters)

prototypalStateHolder.guaranteeAllStatesExist(destinationStateName)
const route = prototypalStateHolder.buildFullStateRoute(destinationStateName)
Expand Down
Loading

0 comments on commit 9edc1bb

Please sign in to comment.