mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-23 23:58:02 -05:00
hasChangedSignificantly async refactor
Still checking for bugs
This commit is contained in:
parent
0f435b41ae
commit
81809956b8
6 changed files with 1118 additions and 52 deletions
|
@ -74,16 +74,21 @@ module.exports = class CastButtonView extends View
|
|||
|
||||
updateCastButton: ->
|
||||
return if _.some @spells, (spell) => not spell.loaded
|
||||
castable = _.some @spells, (spell) => spell.hasChangedSignificantly spell.getSource()
|
||||
@castButtonGroup.toggleClass('castable', castable).toggleClass('casting', @casting)
|
||||
if @casting
|
||||
s = $.i18n.t("play_level.tome_cast_button_casting", defaultValue: "Casting")
|
||||
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
|
||||
#this is going to have some problems...
|
||||
async.some _.values(@spells), (spell, callback) =>
|
||||
spell.hasChangedSignificantly spell.getSource(), null, callback
|
||||
, (castable) =>
|
||||
console.log "Castable is #{castable}"
|
||||
|
||||
@castButtonGroup.toggleClass('castable', castable).toggleClass('casting', @casting)
|
||||
if @casting
|
||||
s = $.i18n.t("play_level.tome_cast_button_casting", defaultValue: "Casting")
|
||||
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) ->
|
||||
#console.log "Set autocast delay to", delay
|
||||
|
|
|
@ -88,14 +88,26 @@ module.exports = class Spell
|
|||
hasChanged: (newSource=null, currentSource=null) ->
|
||||
(newSource ? @originalSource) isnt (currentSource ? @source)
|
||||
|
||||
hasChangedSignificantly: (newSource=null, currentSource=null) ->
|
||||
hasChangedSignificantly: (newSource=null, currentSource=null, cb) ->
|
||||
for thangID, spellThang of @thangs
|
||||
aether = spellThang.aether
|
||||
break
|
||||
unless aether
|
||||
console.error @toString(), "couldn't find a spellThang with aether of", @thangs
|
||||
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) ->
|
||||
aceConfig = me.get('aceConfig') ? {}
|
||||
|
|
|
@ -279,9 +279,10 @@ module.exports = class SpellView extends View
|
|||
]
|
||||
@onCodeChangeMetaHandler = =>
|
||||
return if @eventsSuppressed
|
||||
if not @spellThang or @spell.hasChangedSignificantly @getSource(), @spellThang.aether.raw
|
||||
callback() for callback in onSignificantChange # Do these first
|
||||
callback() for callback in onAnyChange # Then these
|
||||
@spell.hasChangedSignificantly @getSource(), @spellThang.aether.raw, (hasChanged) =>
|
||||
if not @spellThang or hasChanged
|
||||
callback() for callback in onSignificantChange # Do these first
|
||||
callback() for callback in onAnyChange # Then these
|
||||
@aceDoc.on 'change', @onCodeChangeMetaHandler
|
||||
|
||||
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.
|
||||
return unless aether = @spellThang?.aether
|
||||
source = @getSource()
|
||||
codeHasChangedSignificantly = force or @spell.hasChangedSignificantly source, aether.raw
|
||||
needsUpdate = codeHasChangedSignificantly or @spellThang isnt @lastUpdatedAetherSpellThang
|
||||
return if not needsUpdate and aether is @displayedAether
|
||||
castAether = @spellThang.castAether
|
||||
codeIsAsCast = castAether and not @spell.hasChangedSignificantly source, castAether.raw
|
||||
aether = castAether if codeIsAsCast
|
||||
return if not needsUpdate and aether is @displayedAether
|
||||
@spell.hasChangedSignificantly source, aether.raw, (hasChanged) =>
|
||||
codeHasChangedSignificantly = force or hasChanged
|
||||
needsUpdate = codeHasChangedSignificantly or @spellThang isnt @lastUpdatedAetherSpellThang
|
||||
return if not needsUpdate and aether is @displayedAether
|
||||
castAether = @spellThang.castAether
|
||||
codeIsAsCast = castAether and not hasChanged
|
||||
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
|
||||
@clearAetherDisplay()
|
||||
workerMessage =
|
||||
function: "lint"
|
||||
spellKey: @spell.spellKey
|
||||
source: source
|
||||
# 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
|
||||
@clearAetherDisplay()
|
||||
workerMessage =
|
||||
function: "lint"
|
||||
spellKey: @spell.spellKey
|
||||
source: source
|
||||
|
||||
@worker.addEventListener "message", (e) =>
|
||||
workerData = JSON.parse e.data
|
||||
if workerData.function is "lint"
|
||||
@worker.removeEventListener("message",arguments.callee, false)
|
||||
aether.problems = workerData.lintMessages
|
||||
aether.raw = source
|
||||
@displayAether aether
|
||||
@lastUpdatedAetherSpellThang = @spellThang
|
||||
@guessWhetherFinished aether if fromCodeChange
|
||||
@worker.postMessage JSON.stringify(workerMessage)
|
||||
#aether.transpile source if codeHasChangedSignificantly and not codeIsAsCast
|
||||
#@displayAether aether
|
||||
#@lastUpdatedAetherSpellThang = @spellThang
|
||||
#@guessWhetherFinished aether if fromCodeChange
|
||||
@worker.addEventListener "message", (e) =>
|
||||
workerData = JSON.parse e.data
|
||||
if workerData.function is "lint"
|
||||
@worker.removeEventListener("message",arguments.callee, false)
|
||||
aether.problems = workerData.lintMessages
|
||||
aether.raw = source
|
||||
@displayAether aether
|
||||
@lastUpdatedAetherSpellThang = @spellThang
|
||||
@guessWhetherFinished aether if fromCodeChange
|
||||
@worker.postMessage JSON.stringify(workerMessage)
|
||||
#aether.transpile source if codeHasChangedSignificantly and not codeIsAsCast
|
||||
#@displayAether aether
|
||||
#@lastUpdatedAetherSpellThang = @spellThang
|
||||
#@guessWhetherFinished aether if fromCodeChange
|
||||
|
||||
clearAetherDisplay: ->
|
||||
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 unless e.problem.userInfo.methodName is @spell.name
|
||||
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
|
||||
spellThang.aether.addProblem e.problem
|
||||
@lastUpdatedAetherSpellThang = null # force a refresh without a re-transpile
|
||||
@updateAether false, false
|
||||
@spell.hasChangedSignificantly @getSource(), null, (hasChanged) =>
|
||||
return if hasChanged
|
||||
spellThang.aether.addProblem e.problem
|
||||
@lastUpdatedAetherSpellThang = null # force a refresh without a re-transpile
|
||||
@updateAether false, false
|
||||
|
||||
onInfiniteLoop: (e) ->
|
||||
return unless @spellThang
|
||||
|
@ -595,7 +598,9 @@ module.exports = class SpellView extends View
|
|||
@aceSession.setMode @editModes[aceConfig.language ? 'javascript']
|
||||
|
||||
dismiss: ->
|
||||
@recompile() if @spell.hasChangedSignificantly @getSource()
|
||||
@spell.hasChangedSignificantly @getSource(), null, (hasChanged) =>
|
||||
@recompile() if hasChanged
|
||||
|
||||
|
||||
destroy: ->
|
||||
$(@ace?.container).find('.ace_gutter').off 'click', '.ace_error, .ace_warning, .ace_info', @onAnnotationClick
|
||||
|
|
|
@ -215,5 +215,5 @@ module.exports = class TomeView extends View
|
|||
|
||||
destroy: ->
|
||||
spell.destroy() for spellKey, spell of @spells
|
||||
@worker?._close()
|
||||
@worker?.terminate()
|
||||
super()
|
||||
|
|
|
@ -59,6 +59,7 @@ exports.config =
|
|||
# Aether before box2d for some strange Object.defineProperty thing
|
||||
'bower_components/aether/build/aether.js'
|
||||
'bower_components/d3/d3.min.js'
|
||||
'vendor/scripts/async.js'
|
||||
]
|
||||
stylesheets:
|
||||
defaultExtension: 'sass'
|
||||
|
|
1043
vendor/scripts/async.js
vendored
Normal file
1043
vendor/scripts/async.js
vendored
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue