Last active
August 29, 2015 13:56
-
-
Save zacheryph/9334880 to your computer and use it in GitHub Desktop.
Interactor pattern in CoffeeScript
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
# Interactor pattern | |
# idea pulled blatently from: https://github.com/collectiveidea/interactor | |
# | |
# NEED TO DO: | |
# - add bluebird, so ALL perform() calls are promises. | |
# - 'rollback' support to crawl 'backwards' when there is an error | |
# - 'fail()' for the rollback | |
# | |
# Question: | |
# - Is doing a pattern like this in JS useless? | |
# - suggestions on doing it better? | |
# - does this translate well to being useful with vanilla JS? | |
class Interactor | |
constructor: (context) -> | |
@context = context | |
@perform: (context) -> | |
instance = new this(context) | |
instance.perform.call(instance) | |
instance | |
class InteractorOrganizer extends Interactor | |
@perform: (ctx) -> | |
for inter in this.organize | |
temp = inter.perform(ctx) | |
ctx = temp.context | |
class UserSignin extends Interactor | |
perform: -> | |
@context.logged_in = true | |
console.log("testing #{@context.user} + #{@smash()}") | |
smash: -> | |
"see me now" | |
class StoreSession extends Interactor | |
perform: -> | |
console.log "stuff stored to session: #{JSON.stringify(@context)}" | |
class ProcessSignin extends InteractorOrganizer | |
@organize: [ | |
UserSignin, | |
StoreSession | |
] | |
# Example Usage: | |
# | |
UserSignin.perform({user: "MY USER"}) | |
# testing MY USER + see me now | |
ProcessSignin.perform({user: "Organize User"}) | |
# testing Organize User + see me now | |
# stuff stored to session: {"user":"Organize User","logged_in":true} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First!