summaryrefslogtreecommitdiffhomepage
path: root/app/lib
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@codeispoetry.ru>2017-05-23 13:31:07 +0100
committerAndrej Mihajlov <and@codeispoetry.ru>2017-06-05 13:18:52 +0300
commitc166b83b56f3a9f98be7ac9fe3674a652e58f7d8 (patch)
tree1e110a4f988343aaf58598a78244184b67e7f0e3 /app/lib
parentfdb86c8ab71493cd10d176505653bb411a6921e2 (diff)
downloadmullvadvpn-c166b83b56f3a9f98be7ac9fe3674a652e58f7d8.tar.xz
mullvadvpn-c166b83b56f3a9f98be7ac9fe3674a652e58f7d8.zip
Add transitions
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/transition-rule.js74
1 files changed, 74 insertions, 0 deletions
diff --git a/app/lib/transition-rule.js b/app/lib/transition-rule.js
new file mode 100644
index 0000000000..f0c7020870
--- /dev/null
+++ b/app/lib/transition-rule.js
@@ -0,0 +1,74 @@
+// @flow
+
+export type TransitionDescriptor = {
+ name: string,
+ duration: number
+};
+
+export type TransitionFork = {
+ forward: TransitionDescriptor,
+ backward: TransitionDescriptor
+};
+
+/**
+ * Transition rule
+ *
+ * @class TransitionRule
+ */
+export default class TransitionRule {
+
+ from: ?string;
+ to: string;
+ fork: TransitionFork;
+ dir: 'forward' | 'backward' = 'forward';
+
+ /**
+ * Creates an instance of TransitionRule.
+ * @param {string} from - source route to match against, pass null for any.
+ * @param {string} to - destination route to match against
+ * @param {TransitionFork} fork - transition
+ *
+ * @memberof TransitionRule
+ */
+ constructor(from: ?string, to: string, fork: TransitionFork) {
+ this.from = from;
+ this.to = to;
+ this.fork = fork;
+ }
+
+ /**
+ * Attempts to match the transition between routes A -> B and B -> A
+ *
+ * @param {string} [fromRoute] source route, pass null for any
+ * @param {string} toRoute
+ * @returns {boolean} true if matches, otherwise false
+ *
+ * @memberof TransitionRule
+ */
+ match(fromRoute: ?string, toRoute: string): boolean {
+ if((!this.from || this.from === fromRoute) && this.to === toRoute) {
+ this.dir = 'forward';
+ return true;
+ }
+
+ if((!this.from || this.from === toRoute) && this.to === fromRoute) {
+ this.dir = 'backward';
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Returns transition descriptor.
+ * Make sure you run match() before to obtain the direction
+ * of transition before calling this method
+ *
+ * @returns {TransitionDescriptor} transitionDescriptor
+ *
+ * @memberof TransitionRule
+ */
+ transitionDescriptor(): TransitionDescriptor {
+ return this.fork[this.dir];
+ }
+} \ No newline at end of file