-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support asynchronous results for canActivate, canDeactivate and…
… node listeners
- Loading branch information
Showing
7 changed files
with
331 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,73 @@ | ||
import asyncProcess from './async' | ||
import constants from './constants' | ||
|
||
let nameToIDs = name => { | ||
return name.split('.').reduce((ids, name) => { | ||
ids.push(ids.length ? ids[ids.length - 1] + '.' + name : name) | ||
return ids | ||
return ids.concat(ids.length ? ids[ids.length - 1] + '.' + name : name) | ||
}, []) | ||
} | ||
|
||
let boolToPromise = res => { | ||
if (res.then) return res | ||
return new Promise((resolve, reject) => res || res === undefined ? resolve() : reject()) | ||
} | ||
export default function transition(router, toState, fromState, callback) { | ||
let cancelled = false | ||
let cancel = () => cancelled = true | ||
let done = (err, res) => callback(cancelled ? constants.TRANSITION_CANCELLED : err, res) | ||
|
||
let processFn = (fn, toState, fromState) => { | ||
return new Promise((resolve, reject) => { | ||
return boolToPromise(fn(toState, fromState)) | ||
}) | ||
} | ||
let i | ||
let fromStateIds = fromState ? nameToIDs(fromState.name) : [] | ||
let toStateIds = nameToIDs(toState.name) | ||
let maxI = Math.min(fromStateIds.length, toStateIds.length) | ||
|
||
let process = (functions, toState, fromState) => { | ||
if (functions.length) { | ||
return processFn(functions[0], toState, fromState) | ||
.then(() => process(functions.slice(1), toState, fromState)) | ||
for (i = 0; i < maxI; i += 1) { | ||
if (fromStateIds[i] !== toStateIds[i]) break | ||
} | ||
return boolToPromise(true) | ||
} | ||
|
||
export default class Transition { | ||
constructor(router, toState, fromState) { | ||
let i | ||
let fromStateIds = fromState ? nameToIDs(fromState.name) : [] | ||
let toStateIds = nameToIDs(toState.name) | ||
let maxI = Math.min(fromStateIds.length, toStateIds.length) | ||
|
||
for (i = 0; i < maxI; i += 1) { | ||
if (fromStateIds[i] !== toStateIds[i]) break | ||
} | ||
let toDeactivate = fromStateIds.slice(i).reverse() | ||
let toActivate = toStateIds.slice(i) | ||
let intersection = i > 0 ? fromStateIds[i - 1] : '' | ||
|
||
return new Promise((resolveTransition, rejectTransition) => { | ||
this.cancel = rejectTransition | ||
let toDeactivate = fromStateIds.slice(i).reverse() | ||
let toActivate = toStateIds.slice(i) | ||
let intersection = fromState ? (i > 0 ? fromStateIds[i - 1] : '') : null | ||
|
||
// Deactivate | ||
let canDeactivate = (toState, fromState, cb) => { | ||
if (cancelled) done() | ||
else { | ||
let canDeactivateFunctions = toDeactivate | ||
.map(name => router._cmps(name)) | ||
.map(name => router._cmps[name]) | ||
.filter(comp => comp && comp.canDeactivate) | ||
.map(comp => comp.canDeactivate) | ||
|
||
let deactivation = process(canDeactivateFunctions, toState, fromState) | ||
asyncProcess( | ||
canDeactivateFunctions, toState, fromState, | ||
(err, res) => cb(err ? constants.CANNOT_DEACTIVATE : null, res) | ||
) | ||
} | ||
} | ||
|
||
// Activate | ||
let activation = deactivation.then(() => { | ||
let canActivateFunctions = toActivate | ||
.map(name => router._canAct[name]) | ||
.filter(_ => _) | ||
return process(canActivateFunctions, toState, fromState) | ||
}) | ||
let canActivate = (toState, fromState, cb) => { | ||
if (cancelled) done() | ||
else { | ||
let canActivateFunctions = toActivate | ||
.map(name => router._canAct[name]) | ||
.filter(_ => _) | ||
|
||
// Node listener | ||
let nodeListener = activation.then(() => router._invokeListeners('^' + intersection, toState, fromState)) | ||
asyncProcess( | ||
canActivateFunctions, toState, fromState, | ||
(err, res) => cb(err ? constants.CANNOT_ACTIVATE : null, res) | ||
) | ||
} | ||
} | ||
|
||
nodeListener.then(resolveTransition, rejectTransition) | ||
}); | ||
let nodeListener = (toState, fromState, cb) => { | ||
if (cancelled) done() | ||
else { | ||
let listeners = router._cbs['^' + intersection] || [] | ||
asyncProcess( | ||
listeners, toState, fromState, | ||
(err, res) => cb(err ? constants.NODE_LISTENER_ERR : null, res), | ||
true | ||
) | ||
} | ||
} | ||
|
||
let pipeline = fromState ? [canDeactivate, canActivate, nodeListener] : [canActivate] | ||
asyncProcess(pipeline, toState, fromState, done) | ||
|
||
return cancel | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export default function asyncProcess(functions, toState, fromState, callback, allowNoResult = false) { | ||
let remainingSteps = functions || [] | ||
|
||
let processFn = (done) => { | ||
if (!remainingSteps.length) return true | ||
|
||
let len = remainingSteps[0].length | ||
let res = remainingSteps[0](toState, fromState, done) | ||
|
||
if (typeof res === 'boolean') done(!res, res); | ||
|
||
else if (res && typeof res.then === 'function') { | ||
res.then(() => done(null, true), () => done(true, null)) | ||
} | ||
|
||
else if (len < 3 && allowNoResult) done(null, true) | ||
|
||
return false | ||
} | ||
|
||
let iterate = (err, res) => { | ||
if (err) callback(err) | ||
else { | ||
remainingSteps = remainingSteps.slice(1) | ||
next() | ||
} | ||
} | ||
|
||
let next = () => { | ||
let finished = processFn(iterate) | ||
if (finished) callback(null, true) | ||
} | ||
|
||
next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const constants = { | ||
ROUTER_NOT_STARTED : 0, | ||
SAME_STATES : 1, | ||
CANNOT_DEACTIVATE : 2, | ||
CANNOT_ACTIVATE : 3, | ||
NODE_LISTENER_ERR : 4, | ||
TRANSITION_CANCELLED : 5, | ||
} | ||
|
||
export default constants |
Oops, something went wrong.