| 83 | } |
| 84 | |
| 85 | function _resolveBlockerOpts( |
| 86 | opts?: UseBlockerOpts | LegacyBlockerOpts | LegacyBlockerFn, |
| 87 | condition?: boolean | any, |
| 88 | ): UseBlockerOpts { |
| 89 | if (opts === undefined) { |
| 90 | return { |
| 91 | shouldBlockFn: () => true, |
| 92 | withResolver: false, |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if ('shouldBlockFn' in opts) { |
| 97 | return opts |
| 98 | } |
| 99 | |
| 100 | if (typeof opts === 'function') { |
| 101 | const shouldBlock = Boolean(condition ?? true) |
| 102 | |
| 103 | const _customBlockerFn = async () => { |
| 104 | if (shouldBlock) return await opts() |
| 105 | return false |
| 106 | } |
| 107 | |
| 108 | return { |
| 109 | shouldBlockFn: _customBlockerFn, |
| 110 | enableBeforeUnload: shouldBlock, |
| 111 | withResolver: false, |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | const shouldBlock = Solid.createMemo(() => Boolean(opts.condition ?? true)) |
| 116 | |
| 117 | const _customBlockerFn = async () => { |
| 118 | if (shouldBlock() && opts.blockerFn !== undefined) { |
| 119 | return await opts.blockerFn() |
| 120 | } |
| 121 | return shouldBlock() |
| 122 | } |
| 123 | |
| 124 | return { |
| 125 | get shouldBlockFn() { |
| 126 | return _customBlockerFn |
| 127 | }, |
| 128 | get enableBeforeUnload() { |
| 129 | return shouldBlock() |
| 130 | }, |
| 131 | get withResolver() { |
| 132 | return opts.blockerFn === undefined |
| 133 | }, |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | export function useBlocker< |
| 138 | TRouter extends AnyRouter = RegisteredRouter, |