2013-04-26 13:14:29 +00:00
|
|
|
IDWeakMap = require 'id_weak_map'
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2013-04-26 15:26:41 +00:00
|
|
|
class ObjectsStore
|
|
|
|
@stores = {}
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2013-04-26 15:26:41 +00:00
|
|
|
constructor: ->
|
|
|
|
@nextId = 0
|
|
|
|
@objects = []
|
|
|
|
|
|
|
|
getNextId: ->
|
|
|
|
++@nextId
|
|
|
|
|
|
|
|
add: (obj) ->
|
|
|
|
id = @getNextId()
|
|
|
|
@objects[id] = obj
|
|
|
|
id
|
|
|
|
|
|
|
|
has: (id) ->
|
|
|
|
@objects[id]?
|
|
|
|
|
|
|
|
remove: (id) ->
|
2013-04-26 15:58:49 +00:00
|
|
|
throw new Error("Invalid key #{id} for ObjectsStore") unless @has id
|
2013-04-26 15:26:41 +00:00
|
|
|
delete @objects[id]
|
|
|
|
|
|
|
|
get: (id) ->
|
|
|
|
throw new Error("Invalid key #{id} for ObjectsStore") unless @has id
|
|
|
|
@objects[id]
|
|
|
|
|
|
|
|
@forRenderView: (process_id, routing_id) ->
|
|
|
|
key = "#{process_id}_#{routing_id}"
|
|
|
|
@stores[key] = new ObjectsStore unless @stores[key]?
|
|
|
|
@stores[key]
|
|
|
|
|
2013-04-26 15:58:49 +00:00
|
|
|
# Objects in weak map will be not referenced (so we won't leak memory), and
|
|
|
|
# every object created in browser will have a unique id in weak map.
|
2013-04-26 15:26:41 +00:00
|
|
|
objectsWeakMap = new IDWeakMap
|
|
|
|
objectsWeakMap.add = (obj) ->
|
|
|
|
id = IDWeakMap::add.call this, obj
|
2013-04-26 14:33:31 +00:00
|
|
|
Object.defineProperty obj, 'id',
|
|
|
|
enumerable: true, writable: false, value: id
|
|
|
|
id
|
|
|
|
|
2013-04-26 14:25:30 +00:00
|
|
|
process.on 'ATOM_BROWSER_INTERNAL_NEW', (obj) ->
|
2013-04-26 15:26:41 +00:00
|
|
|
# It's possible that user created a object in browser side and then want to
|
|
|
|
# get it in renderer via remote.getObject. So we must add every native object
|
2013-04-26 15:58:49 +00:00
|
|
|
# created in browser to the weak map even it may not be referenced by the
|
|
|
|
# renderer.
|
2013-04-26 15:26:41 +00:00
|
|
|
objectsWeakMap.add obj
|
2013-04-26 14:25:30 +00:00
|
|
|
|
|
|
|
exports.add = (process_id, routing_id, obj) ->
|
2013-04-26 15:58:49 +00:00
|
|
|
# Some native objects may already been added to objectsWeakMap, be care not
|
|
|
|
# to add it twice.
|
2013-04-26 15:26:41 +00:00
|
|
|
objectsWeakMap.add obj unless obj.id?
|
2013-04-26 14:25:30 +00:00
|
|
|
|
2013-04-26 15:58:49 +00:00
|
|
|
# Store and reference the object, then return the storeId which points to
|
|
|
|
# where the object is stored. The caller can later dereference the object
|
|
|
|
# with the storeId.
|
2013-04-26 15:26:41 +00:00
|
|
|
store = ObjectsStore.forRenderView process_id, routing_id
|
|
|
|
store.add obj
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2013-04-26 14:25:30 +00:00
|
|
|
exports.get = (id) ->
|
2013-04-26 15:26:41 +00:00
|
|
|
objectsWeakMap.get id
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2013-04-26 15:26:41 +00:00
|
|
|
exports.remove = (process_id, routing_id, storeId) ->
|
|
|
|
ObjectsStore.forRenderView(process_id, routing_id).remove storeId
|