2013-10-05 12:21:57 +00:00
|
|
|
EventEmitter = require('events').EventEmitter
|
2013-05-30 12:16:54 +00:00
|
|
|
IDWeakMap = require 'id-weak-map'
|
2013-07-26 08:39:22 +00:00
|
|
|
v8Util = process.atomBinding 'v8_util'
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2013-10-05 12:21:57 +00:00
|
|
|
# Class to reference all objects.
|
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: ->
|
2013-07-29 08:35:25 +00:00
|
|
|
@nextId = 0
|
2013-04-26 15:26:41 +00:00
|
|
|
@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]
|
|
|
|
|
2014-04-25 08:45:14 +00:00
|
|
|
@forRenderView: (key) ->
|
2013-04-26 15:26:41 +00:00
|
|
|
@stores[key] = new ObjectsStore unless @stores[key]?
|
|
|
|
@stores[key]
|
|
|
|
|
2014-04-25 08:45:14 +00:00
|
|
|
@releaseForRenderView: (key) ->
|
2013-05-01 02:59:29 +00:00
|
|
|
delete @stores[key]
|
|
|
|
|
2013-10-05 12:53:31 +00:00
|
|
|
class ObjectsRegistry extends EventEmitter
|
2013-10-05 12:21:57 +00:00
|
|
|
constructor: ->
|
2013-12-15 08:15:18 +00:00
|
|
|
@setMaxListeners Number.MAX_VALUE
|
|
|
|
|
2013-10-05 12:21:57 +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.
|
|
|
|
@objectsWeakMap = new IDWeakMap
|
|
|
|
@objectsWeakMap.add = (obj) ->
|
|
|
|
id = IDWeakMap::add.call this, obj
|
|
|
|
v8Util.setHiddenValue obj, 'atomId', id
|
|
|
|
id
|
|
|
|
|
|
|
|
# Register a new object, the object would be kept referenced until you release
|
|
|
|
# it explicitly.
|
2014-04-25 08:45:14 +00:00
|
|
|
add: (key, obj) ->
|
2013-10-05 12:21:57 +00:00
|
|
|
# Some native objects may already been added to objectsWeakMap, be care not
|
|
|
|
# to add it twice.
|
|
|
|
@objectsWeakMap.add obj unless v8Util.getHiddenValue obj, 'atomId'
|
|
|
|
id = v8Util.getHiddenValue obj, 'atomId'
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
# We use a difference key because the same object can be referenced for
|
|
|
|
# multiple times by the same renderer view.
|
2014-04-25 08:45:14 +00:00
|
|
|
store = ObjectsStore.forRenderView key
|
2013-10-05 12:21:57 +00:00
|
|
|
storeId = store.add obj
|
|
|
|
|
|
|
|
[id, storeId]
|
|
|
|
|
|
|
|
# Get an object according to its id.
|
|
|
|
get: (id) ->
|
|
|
|
@objectsWeakMap.get id
|
|
|
|
|
|
|
|
# Remove an object according to its storeId.
|
2014-04-25 08:45:14 +00:00
|
|
|
remove: (key, storeId) ->
|
|
|
|
ObjectsStore.forRenderView(key).remove storeId
|
2013-10-05 12:21:57 +00:00
|
|
|
|
|
|
|
# Clear all references to objects from renderer view.
|
2014-04-25 08:45:14 +00:00
|
|
|
clear: (key) ->
|
|
|
|
@emit "clear-#{key}"
|
|
|
|
ObjectsStore.releaseForRenderView key
|
2013-10-05 12:21:57 +00:00
|
|
|
|
|
|
|
module.exports = new ObjectsRegistry
|