/
gcm.clj
151 lines (106 loc) · 6.85 KB
/
gcm.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
(ns chromex.ext.gcm
"Use chrome.gcm to enable apps and extensions to send and receive messages through the Google Cloud Messaging Service.
* available since Chrome 35
* https://developer.chrome.com/extensions/gcm"
(:refer-clojure :only [defmacro defn apply declare meta let partial])
(:require [chromex.wrapgen :refer [gen-wrap-helper]]
[chromex.callgen :refer [gen-call-helper gen-tap-all-events-call]]))
(declare api-table)
(declare gen-call)
; -- properties -------------------------------------------------------------------------------------------------------------
(defmacro get-max-message-size
"The maximum size (in bytes) of all key/value pairs in a message.
https://developer.chrome.com/extensions/gcm#property-MAX_MESSAGE_SIZE."
([] (gen-call :property ::max-message-size &form)))
; -- functions --------------------------------------------------------------------------------------------------------------
(defmacro register
"Registers the application with GCM. The registration ID will be returned by the callback. If register is called again with
the same list of senderIds, the same registration ID will be returned.
|sender-ids| - A list of server IDs that are allowed to send messages to the application. It should contain at least
one and no more than 100 sender IDs.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [registration-id] where:
|registration-id| - A registration ID assigned to the application by the GCM.
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/extensions/gcm#method-register."
([sender-ids] (gen-call :function ::register &form sender-ids)))
(defmacro unregister
"Unregisters the application from GCM.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [].
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/extensions/gcm#method-unregister."
([] (gen-call :function ::unregister &form)))
(defmacro send
"Sends a message according to its contents.
|message| - A message to send to the other party via GCM.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [message-id] where:
|message-id| - The ID of the message that the callback was issued for.
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/extensions/gcm#method-send."
([message] (gen-call :function ::send &form message)))
; -- events -----------------------------------------------------------------------------------------------------------------
;
; docs: https://github.com/binaryage/chromex/#tapping-events
(defmacro tap-on-message-events
"Fired when a message is received through GCM.
Events will be put on the |channel| with signature [::on-message [message]] where:
|message| - A message received from another party via GCM.
Note: |args| will be passed as additional parameters into Chrome event's .addListener call.
https://developer.chrome.com/extensions/gcm#event-onMessage."
([channel & args] (apply gen-call :event ::on-message &form channel args)))
(defmacro tap-on-messages-deleted-events
"Fired when a GCM server had to delete messages sent by an app server to the application. See Messages deleted event section
of Cloud Messaging documentation for details on handling this event.
Events will be put on the |channel| with signature [::on-messages-deleted []].
Note: |args| will be passed as additional parameters into Chrome event's .addListener call.
https://developer.chrome.com/extensions/gcm#event-onMessagesDeleted."
([channel & args] (apply gen-call :event ::on-messages-deleted &form channel args)))
(defmacro tap-on-send-error-events
"Fired when it was not possible to send a message to the GCM server.
Events will be put on the |channel| with signature [::on-send-error [error]] where:
|error| - An error that occured while trying to send the message either in Chrome or on the GCM server. Application can
retry sending the message with a reasonable backoff and possibly longer time-to-live.
Note: |args| will be passed as additional parameters into Chrome event's .addListener call.
https://developer.chrome.com/extensions/gcm#event-onSendError."
([channel & args] (apply gen-call :event ::on-send-error &form channel args)))
; -- convenience ------------------------------------------------------------------------------------------------------------
(defmacro tap-all-events
"Taps all valid non-deprecated events in chromex.ext.gcm namespace."
[chan]
(gen-tap-all-events-call api-table (meta &form) chan))
; ---------------------------------------------------------------------------------------------------------------------------
; -- API TABLE --------------------------------------------------------------------------------------------------------------
; ---------------------------------------------------------------------------------------------------------------------------
(def api-table
{:namespace "chrome.gcm",
:since "35",
:properties [{:id ::max-message-size, :name "MAX_MESSAGE_SIZE", :return-type "unknown-type"}],
:functions
[{:id ::register,
:name "register",
:callback? true,
:params
[{:name "sender-ids", :type "[array-of-strings]"}
{:name "callback", :type :callback, :callback {:params [{:name "registration-id", :type "string"}]}}]}
{:id ::unregister, :name "unregister", :callback? true, :params [{:name "callback", :type :callback}]}
{:id ::send,
:name "send",
:callback? true,
:params
[{:name "message", :type "object"}
{:name "callback", :type :callback, :callback {:params [{:name "message-id", :type "string"}]}}]}],
:events
[{:id ::on-message, :name "onMessage", :params [{:name "message", :type "object"}]}
{:id ::on-messages-deleted, :name "onMessagesDeleted"}
{:id ::on-send-error, :name "onSendError", :params [{:name "error", :type "object"}]}]})
; -- helpers ----------------------------------------------------------------------------------------------------------------
; code generation for native API wrapper
(defmacro gen-wrap [kind item-id config & args]
(apply gen-wrap-helper api-table kind item-id config args))
; code generation for API call-site
(def gen-call (partial gen-call-helper api-table))