Trying to restore functionality of achievement plugin but without memory leak.

This commit is contained in:
Nick Winter 2015-01-05 21:57:23 -08:00
parent 45b90804ec
commit 9272ceb23e
3 changed files with 27 additions and 30 deletions

View file

@ -34,6 +34,14 @@ preload = (arrayOfImages) ->
$(arrayOfImages).each ->
$('<img/>')[0].src = @
# IE9 doesn't expose console object unless debugger tools are loaded
window.console ?=
info: ->
log: ->
error: ->
debug: ->
console.debug ?= console.log # Needed for IE10 and earlier
Application = initialize: ->
Router = require('core/Router')
@isProduction = -> document.location.href.search('codecombat.com') isnt -1

View file

@ -19,14 +19,10 @@ class CocoModel extends Backbone.Model
@on 'error', @onError, @
@on 'add', @onLoaded, @
@saveBackup = _.debounce(@saveBackup, 500)
# IE9 doesn't expose console object unless debugger tools are loaded
unless console?
window.console =
info: ->
log: ->
error: ->
debug: ->
console.debug = console.log unless console.debug # Needed for IE10 and earlier
@usesVersions = @schema().properties.version?
backupKey: ->
if @usesVersions then @id else @id + ':' + @attributes.__v # TODO: doesn't work because __v doesn't actually increment?
setProjection: (project) ->
return if project is @project
@ -94,16 +90,16 @@ class CocoModel extends Backbone.Model
loadFromBackup: ->
return unless @saveBackups
existing = storage.load @id # + @attributes.__v # TODO: try and test this, also only use __v for non-versioned, otherwise just id
existing = storage.load @backupKey()
if existing
@set(existing, {silent: true})
CocoModel.backedUp[@id] = @
CocoModel.backedUp[@backupKey()] = @
saveBackup: -> @saveBackupNow()
saveBackupNow: ->
storage.save(@id, @attributes) # TODO: use __v
CocoModel.backedUp[@id] = @ # TODO
storage.save(@backupKey(), @attributes)
CocoModel.backedUp[@backupKey()] = @
@backedUp = {}
schema: -> return @constructor.schema
@ -204,7 +200,7 @@ class CocoModel extends Backbone.Model
@clearBackup()
clearBackup: ->
storage.remove @id
storage.remove @backupKey()
hasLocalChanges: ->
@_revertAttributes and not _.isEqual @attributes, @_revertAttributes
@ -384,18 +380,18 @@ class CocoModel extends Backbone.Model
# Store parent data for the next block...
if data?.i18n
pathToData[path] = data
if _.string.endsWith path, 'i18n'
i18n = data
# grab the parent data
parentPath = path[0...-5]
parentData = pathToData[parentPath]
# use it to determine what properties actually need to be translated
props = workingSchema.props or []
props = (prop for prop in props when parentData[prop])
# get a list of lang codes where its object has keys for every prop to be translated
coverage = _.filter(_.keys(i18n), (langCode) ->
translations = i18n[langCode]
@ -403,7 +399,7 @@ class CocoModel extends Backbone.Model
)
langCodeArrays.push coverage
)
return unless langCodeArrays.length
# language codes that are covered for every i18n object are fully covered
overallCoverage = _.intersection(langCodeArrays...)

View file

@ -12,20 +12,15 @@ AchievablePlugin = (schema, options) ->
User = require '../users/User' # Avoid mutual inclusion cycles
Achievement = require '../achievements/Achievement'
before = {}
return
# Keep track the document before it's saved
schema.post 'init', (doc) ->
#doc.beforeDoc = doc.toObject() # TODO: switch to this
before[doc.id] = doc.toObject() # TODO: switch from this, run the testzzz
# TODO check out how many objects go unreleased
doc.unchangedCopy = doc.toObject()
# Check if an achievement has been earned
schema.post 'save', (doc) ->
isNew = not doc.isInit('_id') or not (doc.id of before)
originalDocObj = before[doc.id] unless isNew
isNew = not doc.isInit('_id') or not doc.unchangedCopy
if doc.isInit('_id') and not doc.id of before
if doc.isInit('_id') and not doc.unchangedCopy
log.warn 'document was already initialized but did not go through `init` and is therefore treated as new while it might not be'
category = doc.constructor.collection.name
@ -39,11 +34,9 @@ AchievablePlugin = (schema, options) ->
query = achievement.get('query')
return log.warn("Empty achievement query for #{achievement.get('name')}.") if _.isEmpty query
isRepeatable = achievement.get('proportionalTo')?
alreadyAchieved = if isNew then false else LocalMongo.matchesQuery originalDocObj, query
alreadyAchieved = if isNew then false else LocalMongo.matchesQuery doc.unchangedCopy, query
newlyAchieved = LocalMongo.matchesQuery(docObj, query)
return unless newlyAchieved and (not alreadyAchieved or isRepeatable)
EarnedAchievement.createForAchievement(achievement, doc, originalDocObj)
delete before[doc.id] if doc.id of before # TODO: don't do it!
EarnedAchievement.createForAchievement(achievement, doc, doc.unchangedCopy)
module.exports = AchievablePlugin