1
+ # https://github.com/TelescopeJS/Telescope/blob/master/packages/telescope-lib/lib/callbacks.js
2
+
3
+ ###
4
+ # Callback hooks provide an easy way to add extra steps to common operations.
5
+ # @namespace Rocket.callbacks
6
+ ###
7
+ Rocket .callbacks = {}
8
+
9
+ ###
10
+ # Callback priorities
11
+ ###
12
+ Rocket .callbacks .priority =
13
+ HIGH : - 1
14
+ MEDIUM : 0
15
+ LOW : 1
16
+
17
+ ###
18
+ # Add a callback function to a hook
19
+ # @param {String} hook - The name of the hook
20
+ # @param {Function} callback - The callback function
21
+ ###
22
+
23
+ Rocket .callbacks .add = (hook , callback , priority ) ->
24
+ # if callback array doesn't exist yet, initialize it
25
+ priority ?= Rocket .callbacks .priority .MEDIUM
26
+ unless _ .isNumber priority
27
+ priority = Rocket .callbacks .priority .MEDIUM
28
+ callback .priority = priority
29
+ Rocket .callbacks [hook] ?= []
30
+ Rocket .callbacks [hook].push callback
31
+ return
32
+
33
+ ###
34
+ # Remove a callback from a hook
35
+ # @param {string} hook - The name of the hook
36
+ # @param {string} functionName - The name of the function to remove
37
+ ###
38
+
39
+ Rocket .callbacks .remove = (hookName , callbackName ) ->
40
+ Rocket .callbacks [hookName] = _ .reject Rocket .callbacks [hookName], (callback ) ->
41
+ callback .name is callbackName
42
+ return
43
+
44
+ ###
45
+ # Successively run all of a hook's callbacks on an item
46
+ # @param {String} hook - The name of the hook
47
+ # @param {Object} item - The post, comment, modifier, etc. on which to run the callbacks
48
+ # @param {Object} [constant] - An optional constant that will be passed along to each callback
49
+ # @returns {Object} Returns the item after it's been through all the callbacks for this hook
50
+ ###
51
+
52
+ Rocket .callbacks .run = (hook , item , constant ) ->
53
+ callbacks = Rocket .callbacks [hook]
54
+ if !! callbacks ? .length
55
+ # if the hook exists, and contains callbacks to run
56
+ _ .sortBy (callbacks, (callback ) -> return callback .priority or Rocket .callbacks .priority .MEDIUM ).reduce (result, callback) ->
57
+ # console.log(callback.name);
58
+ callback result, constant
59
+ , item
60
+ else
61
+ # else, just return the item unchanged
62
+ item
63
+
64
+ ###
65
+ # Successively run all of a hook's callbacks on an item, in async mode (only works on server)
66
+ # @param {String} hook - The name of the hook
67
+ # @param {Object} item - The post, comment, modifier, etc. on which to run the callbacks
68
+ # @param {Object} [constant] - An optional constant that will be passed along to each callback
69
+ ###
70
+
71
+ Rocket .callbacks .runAsync = (hook , item , constant ) ->
72
+ callbacks = Rocket .callbacks [hook]
73
+ if Meteor .isServer and !! callbacks ? .length
74
+ # use defer to avoid holding up client
75
+ Meteor .defer ->
76
+ # run all post submit server callbacks on post object successively
77
+ _ .sortBy (callbacks, (callback ) -> return callback .priority or Rocket .callbacks .priority .MEDIUM ).forEach (callback) ->
78
+ # console.log(callback.name);
79
+ callback item, constant
80
+ return
81
+ return
82
+ else
83
+ return item
84
+ return
0 commit comments