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'
|
bindings = process.atomBinding 'app'
|
||||||
objectsRegistry = require '../../atom/objects-registry.js'
|
|
||||||
EventEmitter = require('events').EventEmitter
|
EventEmitter = require('events').EventEmitter
|
||||||
|
|
||||||
Application = bindings.Application
|
Application = bindings.Application
|
||||||
|
@ -11,7 +10,7 @@ app.getHomeDir = ->
|
||||||
process.env[if process.platform is 'win32' then 'USERPROFILE' else 'HOME']
|
process.env[if process.platform is 'win32' then 'USERPROFILE' else 'HOME']
|
||||||
|
|
||||||
app.getBrowserWindows = ->
|
app.getBrowserWindows = ->
|
||||||
objectsRegistry.getAllWindows()
|
require('../../atom/objects-registry.js').getAllWindows()
|
||||||
|
|
||||||
app.setApplicationMenu = (menu) ->
|
app.setApplicationMenu = (menu) ->
|
||||||
require('menu').setApplicationMenu menu
|
require('menu').setApplicationMenu menu
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
BrowserWindow = require 'browser-window'
|
BrowserWindow = require 'browser-window'
|
||||||
|
EventEmitter = require('events').EventEmitter
|
||||||
IDWeakMap = require 'id-weak-map'
|
IDWeakMap = require 'id-weak-map'
|
||||||
v8Util = process.atomBinding 'v8_util'
|
v8Util = process.atomBinding 'v8_util'
|
||||||
|
|
||||||
|
# Class to reference all objects.
|
||||||
class ObjectsStore
|
class ObjectsStore
|
||||||
@stores = {}
|
@stores = {}
|
||||||
|
|
||||||
|
@ -37,46 +39,56 @@ class ObjectsStore
|
||||||
key = "#{processId}_#{routingId}"
|
key = "#{processId}_#{routingId}"
|
||||||
delete @stores[key]
|
delete @stores[key]
|
||||||
|
|
||||||
# Objects in weak map will be not referenced (so we won't leak memory), and
|
class ObjectsRegistry
|
||||||
# every object created in browser will have a unique id in weak map.
|
constructor: ->
|
||||||
objectsWeakMap = new IDWeakMap
|
# Objects in weak map will be not referenced (so we won't leak memory), and
|
||||||
objectsWeakMap.add = (obj) ->
|
# every object created in browser will have a unique id in weak map.
|
||||||
id = IDWeakMap::add.call this, obj
|
@objectsWeakMap = new IDWeakMap
|
||||||
v8Util.setHiddenValue obj, 'atomId', id
|
@objectsWeakMap.add = (obj) ->
|
||||||
id
|
id = IDWeakMap::add.call this, obj
|
||||||
|
v8Util.setHiddenValue obj, 'atomId', id
|
||||||
|
id
|
||||||
|
|
||||||
windowsWeakMap = new IDWeakMap
|
# 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
|
||||||
|
|
||||||
process.on 'ATOM_BROWSER_INTERNAL_NEW', (obj) ->
|
# Register a new object, the object would be kept referenced until you release
|
||||||
# Remember all windows.
|
# it explicitly.
|
||||||
if obj.constructor is BrowserWindow
|
add: (processId, routingId, obj) ->
|
||||||
id = windowsWeakMap.add obj
|
# Some native objects may already been added to objectsWeakMap, be care not
|
||||||
obj.on 'destroyed', ->
|
# to add it twice.
|
||||||
windowsWeakMap.remove id
|
@objectsWeakMap.add obj unless v8Util.getHiddenValue obj, 'atomId'
|
||||||
|
id = v8Util.getHiddenValue obj, 'atomId'
|
||||||
|
|
||||||
exports.add = (processId, routingId, obj) ->
|
# Store and reference the object, then return the storeId which points to
|
||||||
# Some native objects may already been added to objectsWeakMap, be care not
|
# where the object is stored. The caller can later dereference the object
|
||||||
# to add it twice.
|
# with the storeId.
|
||||||
objectsWeakMap.add obj unless v8Util.getHiddenValue obj, 'atomId'
|
# We use a difference key because the same object can be referenced for
|
||||||
id = v8Util.getHiddenValue obj, 'atomId'
|
# multiple times by the same renderer view.
|
||||||
|
store = ObjectsStore.forRenderView processId, routingId
|
||||||
|
storeId = store.add obj
|
||||||
|
|
||||||
# Store and reference the object, then return the storeId which points to
|
[id, storeId]
|
||||||
# where the object is stored. The caller can later dereference the object
|
|
||||||
# with the storeId.
|
|
||||||
store = ObjectsStore.forRenderView processId, routingId
|
|
||||||
storeId = store.add obj
|
|
||||||
|
|
||||||
[id, storeId]
|
# Get an object according to its id.
|
||||||
|
get: (id) ->
|
||||||
|
@objectsWeakMap.get id
|
||||||
|
|
||||||
exports.get = (id) ->
|
# Remove an object according to its storeId.
|
||||||
objectsWeakMap.get id
|
remove: (processId, routingId, storeId) ->
|
||||||
|
ObjectsStore.forRenderView(processId, routingId).remove storeId
|
||||||
|
|
||||||
exports.getAllWindows = () ->
|
# Clear all references to objects from renderer view.
|
||||||
keys = windowsWeakMap.keys()
|
clear: (processId, routingId) ->
|
||||||
windowsWeakMap.get key for key in keys
|
ObjectsStore.releaseForRenderView processId, routingId
|
||||||
|
|
||||||
exports.remove = (processId, routingId, storeId) ->
|
# Return an array of all browser windows.
|
||||||
ObjectsStore.forRenderView(processId, routingId).remove storeId
|
getAllWindows: ->
|
||||||
|
keys = @windowsWeakMap.keys()
|
||||||
|
@windowsWeakMap.get key for key in keys
|
||||||
|
|
||||||
exports.clear = (processId, routingId) ->
|
module.exports = new ObjectsRegistry
|
||||||
ObjectsStore.releaseForRenderView processId, routingId
|
|
||||||
|
|
Loading…
Reference in a new issue