Make the CallbacksRegistry a separate module, though rather small.
This commit is contained in:
parent
f725927c44
commit
67f998357c
4 changed files with 34 additions and 38 deletions
1
atom.gyp
1
atom.gyp
|
@ -14,6 +14,7 @@
|
||||||
'browser/atom/atom.coffee',
|
'browser/atom/atom.coffee',
|
||||||
'browser/atom/objects_registry.coffee',
|
'browser/atom/objects_registry.coffee',
|
||||||
'browser/atom/rpc_server.coffee',
|
'browser/atom/rpc_server.coffee',
|
||||||
|
'common/api/lib/callbacks_registry.coffee',
|
||||||
'common/api/lib/clipboard.coffee',
|
'common/api/lib/clipboard.coffee',
|
||||||
'common/api/lib/id_weak_map.coffee',
|
'common/api/lib/id_weak_map.coffee',
|
||||||
'common/api/lib/shell.coffee',
|
'common/api/lib/shell.coffee',
|
||||||
|
|
|
@ -1,36 +1,22 @@
|
||||||
binding = process.atomBinding 'dialog'
|
binding = process.atomBinding 'dialog'
|
||||||
|
CallbacksRegistry = require 'callbacks_registry'
|
||||||
EventEmitter = require('events').EventEmitter
|
EventEmitter = require('events').EventEmitter
|
||||||
ipc = require 'ipc'
|
ipc = require 'ipc'
|
||||||
|
|
||||||
FileDialog = binding.FileDialog
|
FileDialog = binding.FileDialog
|
||||||
FileDialog.prototype.__proto__ = EventEmitter.prototype
|
FileDialog.prototype.__proto__ = EventEmitter.prototype
|
||||||
|
|
||||||
class CallbacksRegistry
|
callbacksRegistry = new CallbacksRegistry
|
||||||
@nextId = 0
|
|
||||||
@callbacks = {}
|
|
||||||
|
|
||||||
@add: (callback) ->
|
|
||||||
@callbacks[++@nextId] = callback
|
|
||||||
@nextId
|
|
||||||
|
|
||||||
@get: (id) ->
|
|
||||||
@callbacks[id]
|
|
||||||
|
|
||||||
@call: (id, args...) ->
|
|
||||||
@callbacks[id].call global, args...
|
|
||||||
|
|
||||||
@remove: (id) ->
|
|
||||||
delete @callbacks[id]
|
|
||||||
|
|
||||||
fileDialog = new FileDialog
|
fileDialog = new FileDialog
|
||||||
|
|
||||||
fileDialog.on 'selected', (event, callbackId, paths...) ->
|
fileDialog.on 'selected', (event, callbackId, paths...) ->
|
||||||
CallbacksRegistry.call callbackId, 'selected', paths...
|
callbacksRegistry.call callbackId, 'selected', paths...
|
||||||
CallbacksRegistry.remove callbackId
|
callbacksRegistry.remove callbackId
|
||||||
|
|
||||||
fileDialog.on 'cancelled', (event, callbackId) ->
|
fileDialog.on 'cancelled', (event, callbackId) ->
|
||||||
CallbacksRegistry.call callbackId, 'cancelled'
|
callbacksRegistry.call callbackId, 'cancelled'
|
||||||
CallbacksRegistry.remove callbackId
|
callbacksRegistry.remove callbackId
|
||||||
|
|
||||||
validateOptions = (options) ->
|
validateOptions = (options) ->
|
||||||
return false unless typeof options is 'object'
|
return false unless typeof options is 'object'
|
||||||
|
@ -55,7 +41,7 @@ selectFileWrap = (window, options, callback, type, title) ->
|
||||||
|
|
||||||
throw new TypeError('Bad arguments') unless validateOptions options
|
throw new TypeError('Bad arguments') unless validateOptions options
|
||||||
|
|
||||||
callbackId = CallbacksRegistry.add callback
|
callbackId = callbacksRegistry.add callback
|
||||||
|
|
||||||
fileDialog.selectFile window.getProcessId(), window.getRoutingId(),
|
fileDialog.selectFile window.getProcessId(), window.getRoutingId(),
|
||||||
options.type,
|
options.type,
|
||||||
|
|
21
common/api/lib/callbacks_registry.coffee
Normal file
21
common/api/lib/callbacks_registry.coffee
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
module.exports =
|
||||||
|
class CallbacksRegistry
|
||||||
|
constructor: ->
|
||||||
|
@nextId = 0
|
||||||
|
@callbacks = {}
|
||||||
|
|
||||||
|
add: (callback) ->
|
||||||
|
@callbacks[++@nextId] = callback
|
||||||
|
@nextId
|
||||||
|
|
||||||
|
get: (id) ->
|
||||||
|
@callbacks[id]
|
||||||
|
|
||||||
|
call: (id, args...) ->
|
||||||
|
@get(id).call global, args...
|
||||||
|
|
||||||
|
apply: (id, args...) ->
|
||||||
|
@get(id).apply global, args...
|
||||||
|
|
||||||
|
remove: (id) ->
|
||||||
|
delete @callbacks[id]
|
|
@ -1,21 +1,9 @@
|
||||||
ipc = require 'ipc'
|
ipc = require 'ipc'
|
||||||
|
CallbacksRegistry = require 'callbacks_registry'
|
||||||
v8_util = process.atomBinding 'v8_util'
|
v8_util = process.atomBinding 'v8_util'
|
||||||
|
|
||||||
currentContextExist = true
|
currentContextExist = true
|
||||||
|
callbacksRegistry = new CallbacksRegistry
|
||||||
class CallbacksRegistry
|
|
||||||
@nextId = 0
|
|
||||||
@callbacks = {}
|
|
||||||
|
|
||||||
@add: (callback) ->
|
|
||||||
@callbacks[++@nextId] = callback
|
|
||||||
@nextId
|
|
||||||
|
|
||||||
@call: (id, args) ->
|
|
||||||
@callbacks[id].apply global, args
|
|
||||||
|
|
||||||
@remove: (id) ->
|
|
||||||
delete @callbacks[id]
|
|
||||||
|
|
||||||
# Convert the arguments object into an array of meta data.
|
# Convert the arguments object into an array of meta data.
|
||||||
wrapArgs = (args) ->
|
wrapArgs = (args) ->
|
||||||
|
@ -23,7 +11,7 @@ wrapArgs = (args) ->
|
||||||
if typeof value is 'object' and v8_util.getHiddenValue value, 'isRemoteObject'
|
if typeof value is 'object' and v8_util.getHiddenValue value, 'isRemoteObject'
|
||||||
type: 'object', id: value.id
|
type: 'object', id: value.id
|
||||||
else if typeof value is 'function'
|
else if typeof value is 'function'
|
||||||
type: 'function', id: CallbacksRegistry.add(value)
|
type: 'function', id: callbacksRegistry.add(value)
|
||||||
else
|
else
|
||||||
type: 'value', value: value
|
type: 'value', value: value
|
||||||
|
|
||||||
|
@ -85,11 +73,11 @@ metaToValue = (meta) ->
|
||||||
|
|
||||||
# Browser calls a callback in renderer.
|
# Browser calls a callback in renderer.
|
||||||
ipc.on 'ATOM_RENDERER_CALLBACK', (id, args) ->
|
ipc.on 'ATOM_RENDERER_CALLBACK', (id, args) ->
|
||||||
CallbacksRegistry.call id, metaToValue(args)
|
callbacksRegistry.apply id, metaToValue(args)
|
||||||
|
|
||||||
# A callback in browser is released.
|
# A callback in browser is released.
|
||||||
ipc.on 'ATOM_RENDERER_RELEASE_CALLBACK', (id) ->
|
ipc.on 'ATOM_RENDERER_RELEASE_CALLBACK', (id) ->
|
||||||
CallbacksRegistry.remove id
|
callbacksRegistry.remove id
|
||||||
|
|
||||||
# Release all resources of current render view when it's going to be unloaded.
|
# Release all resources of current render view when it's going to be unloaded.
|
||||||
window.addEventListener 'unload', (event) ->
|
window.addEventListener 'unload', (event) ->
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue