Skip to content

Commit 40eee56

Browse files
committed
refactor all thing things
1 parent cf08510 commit 40eee56

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

app/assets/javascripts/client/index.coffee

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,31 +82,28 @@ class @Abba
8282
this
8383

8484
start: (name, options = {}) =>
85-
if previous = @getVariantCookie()
85+
if variant = @getPreviousVariant()
8686
# Use the same variant as before, don't record anything
87-
variant = (v for v in @variants when v.name is previous)[0]
87+
@chooseVariant(variant)
88+
return this
8889

89-
else if name?
90+
if name?
9091
# Custom variant provided
91-
variant = (v for v in @variants when v.name is name)[0]
92+
variant = @getVariantForName(name)
9293
variant or=
9394
name: name
9495
control: options.control
95-
@recordStart(variant)
9696

9797
else
9898
# Or choose a random one
9999
random = Math.floor(Math.random() * @variants.length)
100100
variant = @variants[random]
101101

102-
throw new Error('No variants added') unless variant
103-
@recordStart(variant)
104-
105-
variant?.callback?()
106-
@chosen = variant
102+
throw new Error('No variants added') unless variant
103+
@recordStart(variant)
104+
@chooseVariant(variant)
107105
this
108106

109-
# Complete experiment now
110107
complete: (name) =>
111108
# Optionally pass a name, or read from the cookie
112109
name or= @getVariantCookie()
@@ -132,6 +129,13 @@ class @Abba
132129

133130
# Private
134131

132+
getVariantForName: (name) =>
133+
(v for v in @variants when v.name is name)[0]
134+
135+
chooseVariant: (variant) =>
136+
variant?.callback?()
137+
@chosen = variant
138+
135139
recordStart: (variant) =>
136140
# Record which experiment was run on the server
137141
request(
@@ -150,6 +154,10 @@ class @Abba
150154

151155
# Variant Cookie
152156

157+
getPreviousVariant: =>
158+
if name = @getVariantCookie()
159+
@getVariantForName(name)
160+
153161
getVariantCookie: =>
154162
getCookie("abbaVariant_#{@name}")
155163

0 commit comments

Comments
 (0)