Make objects registry code more structured.
This commit is contained in:
parent
1524ced816
commit
1e1fec15b6
2 changed files with 47 additions and 36 deletions
|
@ -1,5 +1,4 @@
|
|||
bindings = process.atomBinding 'app'
|
||||
objectsRegistry = require '../../atom/objects-registry.js'
|
||||
EventEmitter = require('events').EventEmitter
|
||||
|
||||
Application = bindings.Application
|
||||
|
@ -11,7 +10,7 @@ app.getHomeDir = ->
|
|||
process.env[if process.platform is 'win32' then 'USERPROFILE' else 'HOME']
|
||||
|
||||
app.getBrowserWindows = ->
|
||||
objectsRegistry.getAllWindows()
|
||||
require('../../atom/objects-registry.js').getAllWindows()
|
||||
|
||||
app.setApplicationMenu = (menu) ->
|
||||
require('menu').setApplicationMenu menu
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
BrowserWindow = require 'browser-window'
|
||||
EventEmitter = require('events').EventEmitter
|
||||
IDWeakMap = require 'id-weak-map'
|
||||
v8Util = process.atomBinding 'v8_util'
|
||||
|
||||
# Class to reference all objects.
|
||||
class ObjectsStore
|
||||
@stores = {}
|
||||
|
||||
|
@ -37,46 +39,56 @@ class ObjectsStore
|
|||
key = "#{processId}_#{routingId}"
|
||||
delete @stores[key]
|
||||
|
||||
# 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) ->
|
||||
class ObjectsRegistry
|
||||
constructor: ->
|
||||
# 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
|
||||
|
||||
windowsWeakMap = new IDWeakMap
|
||||
|
||||
process.on 'ATOM_BROWSER_INTERNAL_NEW', (obj) ->
|
||||
# Remember all windows.
|
||||
# Remember all windows in the weak map.
|
||||
@windowsWeakMap = new IDWeakMap
|
||||
process.on 'ATOM_BROWSER_INTERNAL_NEW', (obj) =>
|
||||
if obj.constructor is BrowserWindow
|
||||
id = windowsWeakMap.add obj
|
||||
obj.on 'destroyed', ->
|
||||
windowsWeakMap.remove id
|
||||
id = @windowsWeakMap.add obj
|
||||
obj.on 'destroyed', => @windowsWeakMap.remove id
|
||||
|
||||
exports.add = (processId, routingId, obj) ->
|
||||
# Register a new object, the object would be kept referenced until you release
|
||||
# it explicitly.
|
||||
add: (processId, routingId, obj) ->
|
||||
# Some native objects may already been added to objectsWeakMap, be care not
|
||||
# to add it twice.
|
||||
objectsWeakMap.add obj unless v8Util.getHiddenValue obj, 'atomId'
|
||||
@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.
|
||||
store = ObjectsStore.forRenderView processId, routingId
|
||||
storeId = store.add obj
|
||||
|
||||
[id, storeId]
|
||||
|
||||
exports.get = (id) ->
|
||||
objectsWeakMap.get id
|
||||
# Get an object according to its id.
|
||||
get: (id) ->
|
||||
@objectsWeakMap.get id
|
||||
|
||||
exports.getAllWindows = () ->
|
||||
keys = windowsWeakMap.keys()
|
||||
windowsWeakMap.get key for key in keys
|
||||
|
||||
exports.remove = (processId, routingId, storeId) ->
|
||||
# Remove an object according to its storeId.
|
||||
remove: (processId, routingId, storeId) ->
|
||||
ObjectsStore.forRenderView(processId, routingId).remove storeId
|
||||
|
||||
exports.clear = (processId, routingId) ->
|
||||
# Clear all references to objects from renderer view.
|
||||
clear: (processId, routingId) ->
|
||||
ObjectsStore.releaseForRenderView processId, routingId
|
||||
|
||||
# Return an array of all browser windows.
|
||||
getAllWindows: ->
|
||||
keys = @windowsWeakMap.keys()
|
||||
@windowsWeakMap.get key for key in keys
|
||||
|
||||
module.exports = new ObjectsRegistry
|
||||
|
|
Loading…
Reference in a new issue