hasChangedSignificantly async refactor

Still checking for bugs
This commit is contained in:
Michael Schmatz 2014-04-22 08:54:35 -07:00
parent 0f435b41ae
commit 81809956b8
6 changed files with 1118 additions and 52 deletions

View file

@ -74,16 +74,21 @@ module.exports = class CastButtonView extends View
updateCastButton: -> updateCastButton: ->
return if _.some @spells, (spell) => not spell.loaded return if _.some @spells, (spell) => not spell.loaded
castable = _.some @spells, (spell) => spell.hasChangedSignificantly spell.getSource() #this is going to have some problems...
@castButtonGroup.toggleClass('castable', castable).toggleClass('casting', @casting) async.some _.values(@spells), (spell, callback) =>
if @casting spell.hasChangedSignificantly spell.getSource(), null, callback
s = $.i18n.t("play_level.tome_cast_button_casting", defaultValue: "Casting") , (castable) =>
else if castable console.log "Castable is #{castable}"
s = $.i18n.t("play_level.tome_cast_button_castable", defaultValue: "Cast Spell") + " " + @castShortcut
else @castButtonGroup.toggleClass('castable', castable).toggleClass('casting', @casting)
s = $.i18n.t("play_level.tome_cast_button_cast", defaultValue: "Spell Cast") if @casting
@castButton.text s s = $.i18n.t("play_level.tome_cast_button_casting", defaultValue: "Casting")
@castButton.prop 'disabled', not castable else if castable
s = $.i18n.t("play_level.tome_cast_button_castable", defaultValue: "Cast Spell") + " " + @castShortcut
else
s = $.i18n.t("play_level.tome_cast_button_cast", defaultValue: "Spell Cast")
@castButton.text s
@castButton.prop 'disabled', not castable
setAutocastDelay: (delay) -> setAutocastDelay: (delay) ->
#console.log "Set autocast delay to", delay #console.log "Set autocast delay to", delay

View file

@ -88,15 +88,27 @@ module.exports = class Spell
hasChanged: (newSource=null, currentSource=null) -> hasChanged: (newSource=null, currentSource=null) ->
(newSource ? @originalSource) isnt (currentSource ? @source) (newSource ? @originalSource) isnt (currentSource ? @source)
hasChangedSignificantly: (newSource=null, currentSource=null) -> hasChangedSignificantly: (newSource=null, currentSource=null, cb) ->
for thangID, spellThang of @thangs for thangID, spellThang of @thangs
aether = spellThang.aether aether = spellThang.aether
break break
unless aether unless aether
console.error @toString(), "couldn't find a spellThang with aether of", @thangs console.error @toString(), "couldn't find a spellThang with aether of", @thangs
return false return false
aether.hasChangedSignificantly (newSource ? @originalSource), (currentSource ? @source), true, true workerMessage =
function: "hasChangedSignificantly"
a: (newSource ? @originalSource)
spellKey: @spellKey
b: (currentSource ? @source)
careAboutLineNumbers: true
careAboutLint: true
@worker.addEventListener "message", (e) =>
workerData = JSON.parse e.data
if workerData.function is "hasChangedSignificantly"
@worker.removeEventListener("message",arguments.callee, false)
cb(workerData.hasChanged)
@worker.postMessage JSON.stringify(workerMessage)
createAether: (thang) -> createAether: (thang) ->
aceConfig = me.get('aceConfig') ? {} aceConfig = me.get('aceConfig') ? {}
aetherOptions = aetherOptions =

View file

@ -279,9 +279,10 @@ module.exports = class SpellView extends View
] ]
@onCodeChangeMetaHandler = => @onCodeChangeMetaHandler = =>
return if @eventsSuppressed return if @eventsSuppressed
if not @spellThang or @spell.hasChangedSignificantly @getSource(), @spellThang.aether.raw @spell.hasChangedSignificantly @getSource(), @spellThang.aether.raw, (hasChanged) =>
callback() for callback in onSignificantChange # Do these first if not @spellThang or hasChanged
callback() for callback in onAnyChange # Then these callback() for callback in onSignificantChange # Do these first
callback() for callback in onAnyChange # Then these
@aceDoc.on 'change', @onCodeChangeMetaHandler @aceDoc.on 'change', @onCodeChangeMetaHandler
setRecompileNeeded: (needed=true) => setRecompileNeeded: (needed=true) =>
@ -318,36 +319,37 @@ module.exports = class SpellView extends View
# to a new spellThang, we may want to refresh our Aether display. # to a new spellThang, we may want to refresh our Aether display.
return unless aether = @spellThang?.aether return unless aether = @spellThang?.aether
source = @getSource() source = @getSource()
codeHasChangedSignificantly = force or @spell.hasChangedSignificantly source, aether.raw @spell.hasChangedSignificantly source, aether.raw, (hasChanged) =>
needsUpdate = codeHasChangedSignificantly or @spellThang isnt @lastUpdatedAetherSpellThang codeHasChangedSignificantly = force or hasChanged
return if not needsUpdate and aether is @displayedAether needsUpdate = codeHasChangedSignificantly or @spellThang isnt @lastUpdatedAetherSpellThang
castAether = @spellThang.castAether return if not needsUpdate and aether is @displayedAether
codeIsAsCast = castAether and not @spell.hasChangedSignificantly source, castAether.raw castAether = @spellThang.castAether
aether = castAether if codeIsAsCast codeIsAsCast = castAether and not hasChanged
return if not needsUpdate and aether is @displayedAether aether = castAether if codeIsAsCast
return if not needsUpdate and aether is @displayedAether
# Now that that's figured out, perform the update.
# The web worker Aether won't track state, so don't have to worry about updating it # Now that that's figured out, perform the update.
@clearAetherDisplay() # The web worker Aether won't track state, so don't have to worry about updating it
workerMessage = @clearAetherDisplay()
function: "lint" workerMessage =
spellKey: @spell.spellKey function: "lint"
source: source spellKey: @spell.spellKey
source: source
@worker.addEventListener "message", (e) =>
workerData = JSON.parse e.data @worker.addEventListener "message", (e) =>
if workerData.function is "lint" workerData = JSON.parse e.data
@worker.removeEventListener("message",arguments.callee, false) if workerData.function is "lint"
aether.problems = workerData.lintMessages @worker.removeEventListener("message",arguments.callee, false)
aether.raw = source aether.problems = workerData.lintMessages
@displayAether aether aether.raw = source
@lastUpdatedAetherSpellThang = @spellThang @displayAether aether
@guessWhetherFinished aether if fromCodeChange @lastUpdatedAetherSpellThang = @spellThang
@worker.postMessage JSON.stringify(workerMessage) @guessWhetherFinished aether if fromCodeChange
#aether.transpile source if codeHasChangedSignificantly and not codeIsAsCast @worker.postMessage JSON.stringify(workerMessage)
#@displayAether aether #aether.transpile source if codeHasChangedSignificantly and not codeIsAsCast
#@lastUpdatedAetherSpellThang = @spellThang #@displayAether aether
#@guessWhetherFinished aether if fromCodeChange #@lastUpdatedAetherSpellThang = @spellThang
#@guessWhetherFinished aether if fromCodeChange
clearAetherDisplay: -> clearAetherDisplay: ->
problem.destroy() for problem in @problems problem.destroy() for problem in @problems
@ -417,10 +419,11 @@ module.exports = class SpellView extends View
return @onInfiniteLoop e if e.problem.id is "runtime_InfiniteLoop" return @onInfiniteLoop e if e.problem.id is "runtime_InfiniteLoop"
return unless e.problem.userInfo.methodName is @spell.name return unless e.problem.userInfo.methodName is @spell.name
return unless spellThang = _.find @spell.thangs, (spellThang, thangID) -> thangID is e.problem.userInfo.thangID return unless spellThang = _.find @spell.thangs, (spellThang, thangID) -> thangID is e.problem.userInfo.thangID
return if @spell.hasChangedSignificantly @getSource() # don't show this error if we've since edited the code @spell.hasChangedSignificantly @getSource(), null, (hasChanged) =>
spellThang.aether.addProblem e.problem return if hasChanged
@lastUpdatedAetherSpellThang = null # force a refresh without a re-transpile spellThang.aether.addProblem e.problem
@updateAether false, false @lastUpdatedAetherSpellThang = null # force a refresh without a re-transpile
@updateAether false, false
onInfiniteLoop: (e) -> onInfiniteLoop: (e) ->
return unless @spellThang return unless @spellThang
@ -595,7 +598,9 @@ module.exports = class SpellView extends View
@aceSession.setMode @editModes[aceConfig.language ? 'javascript'] @aceSession.setMode @editModes[aceConfig.language ? 'javascript']
dismiss: -> dismiss: ->
@recompile() if @spell.hasChangedSignificantly @getSource() @spell.hasChangedSignificantly @getSource(), null, (hasChanged) =>
@recompile() if hasChanged
destroy: -> destroy: ->
$(@ace?.container).find('.ace_gutter').off 'click', '.ace_error, .ace_warning, .ace_info', @onAnnotationClick $(@ace?.container).find('.ace_gutter').off 'click', '.ace_error, .ace_warning, .ace_info', @onAnnotationClick

View file

@ -215,5 +215,5 @@ module.exports = class TomeView extends View
destroy: -> destroy: ->
spell.destroy() for spellKey, spell of @spells spell.destroy() for spellKey, spell of @spells
@worker?._close() @worker?.terminate()
super() super()

View file

@ -59,6 +59,7 @@ exports.config =
# Aether before box2d for some strange Object.defineProperty thing # Aether before box2d for some strange Object.defineProperty thing
'bower_components/aether/build/aether.js' 'bower_components/aether/build/aether.js'
'bower_components/d3/d3.min.js' 'bower_components/d3/d3.min.js'
'vendor/scripts/async.js'
] ]
stylesheets: stylesheets:
defaultExtension: 'sass' defaultExtension: 'sass'

1043
vendor/scripts/async.js vendored Normal file

File diff suppressed because it is too large Load diff