2013-04-26 13:14:29 +00:00
|
|
|
IDWeakMap = require 'id_weak_map'
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2013-04-26 13:14:29 +00:00
|
|
|
globalStore = {}
|
|
|
|
globalMap = new IDWeakMap
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2013-04-26 14:33:31 +00:00
|
|
|
addObjectToWeakMap = (obj) ->
|
|
|
|
id = globalMap.add obj
|
|
|
|
Object.defineProperty obj, 'id',
|
|
|
|
enumerable: true, writable: false, value: id
|
|
|
|
id
|
|
|
|
|
2013-04-26 13:14:29 +00:00
|
|
|
getStoreForRenderView = (process_id, routing_id) ->
|
|
|
|
key = "#{process_id}_#{routing_id}"
|
|
|
|
globalStore[key] = {} unless globalStore[key]?
|
|
|
|
globalStore[key]
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2013-04-26 14:25:30 +00:00
|
|
|
process.on 'ATOM_BROWSER_INTERNAL_NEW', (obj) ->
|
|
|
|
# For objects created in browser scripts, keep a weak reference here.
|
2013-04-26 14:33:31 +00:00
|
|
|
addObjectToWeakMap obj
|
2013-04-26 14:25:30 +00:00
|
|
|
|
|
|
|
exports.add = (process_id, routing_id, obj) ->
|
|
|
|
# Some native types may already been added to globalMap, in that case we
|
|
|
|
# don't add it twice.
|
2013-04-26 14:33:31 +00:00
|
|
|
if obj.id?
|
|
|
|
id = obj.id
|
|
|
|
else
|
|
|
|
id = addObjectToWeakMap obj
|
2013-04-26 14:25:30 +00:00
|
|
|
|
2013-04-26 13:14:29 +00:00
|
|
|
store = getStoreForRenderView process_id, routing_id
|
2013-04-26 14:25:30 +00:00
|
|
|
|
|
|
|
# It's possible that a render view may want to get the same remote object
|
|
|
|
# twice, since we only allow one reference of one object per render view,
|
|
|
|
# we throw when the object is already referenced.
|
|
|
|
throw new Error("Object #{id} is already referenced") if store[id]?
|
|
|
|
|
2013-04-26 13:14:29 +00:00
|
|
|
store[id] = obj
|
|
|
|
id
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2013-04-26 14:25:30 +00:00
|
|
|
exports.get = (id) ->
|
2013-04-26 13:14:29 +00:00
|
|
|
globalMap.get id
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2013-04-26 13:14:29 +00:00
|
|
|
exports.remove = (process_id, routing_id, id) ->
|
|
|
|
store = getStoreForRenderView process_id, routing_id
|
|
|
|
delete store[id]
|