-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex-snippet.json
250 lines (250 loc) · 6.79 KB
/
ex-snippet.json
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
{
"GenServer module": {
"prefix": "genserver",
"body": [
"defmodule ${1:ModuleName} do",
"\t@moduledoc \"\"\"",
"\tmodule doc",
"\t\"\"\"",
"\tuse GenServer, restart: :${2|permanent,transient,temporary|}",
"",
"\t##########",
"\t# Client #",
"\t##########",
"",
"\tdef start_link(args) do",
"\t\tGenServer.start_link(__MODULE__, args, name: ${3|__MODULE__,via(name)|})",
"\tend",
"",
"\t@doc \"\"\"",
"\tfunction doc",
"\t\"\"\"",
"\tdef ${4:call}(args) do",
"\t\tGenServer.call(__MODULE__, {:${4:call}, args})",
"\tend",
"",
"\t@doc \"\"\"",
"\tfunction doc",
"\t\"\"\"",
"\tdef ${5:cast}(args) do",
"\t\tGenServer.cast(__MODULE__, {:${5:cast}, args})",
"\tend",
"",
"\t##########",
"\t# Server #",
"\t##########",
"",
"\tdef init(args) do",
"\t\t{:ok, state}",
"\t\t",
"\t\t# Optional: execute handle_continue callback asynchronously after init",
"\t\t# {:ok, state, {:continue, :setup}}",
"\tend",
"",
"\t# Optional: handle work asynchronously after init",
"\t# def handle_continue(:setup, state) do",
"\t# {:noreply, state}",
"\t# end",
"",
"\tdef handle_call({:${4:call}, args}, _from, state) do",
"\t\t{:reply, args, state}",
"\tend",
"",
"\tdef handle_cast(:${5:cast}, _from, state) do",
"\t\t{:noreply, state}",
"\tend",
"",
"\t# Optional: use registry to register genserver process",
"\t# defp via(name), do: {:via, Registry, {YourRegistryModule, name}}",
"end"
],
"description": "Template for genserver module"
},
"GenStage producer": {
"prefix": "genstage.producer",
"body": [
"defmodule ${1:GenStage.Producer} do",
"\t@moduledoc \"\"\"",
"\tmodule doc",
"\t\"\"\"",
"\tuse GenStage",
"",
"\t##########",
"\t# Client #",
"\t##########",
"",
"\tdef start_link(args) do",
"\t\tGenStage.start_link(__MODULE__, args, name: __MODULE__)",
"\tend",
"",
"\t@doc \"\"\"",
"\tCan be invoked to dispact event to consumer when needed.",
"\t\"\"\"",
"\tdef ${2:dispact_events}(args) do",
"\t\tGenStage.call(__MODULE__, {:${2:dispact_events}, args})",
"\tend",
"",
"\t##########",
"\t# Server #",
"\t##########",
"",
"\tdef init(initial_state) do",
"\t\t{:producer, initial_state}",
"\t\t",
"\t\t# Optional: you could configure the producer buffer size (default to 10_000)",
"\t\t# {:producer, initial_state, buffer_size: 10_000}",
"\t\t",
"\t\t# Optional: you could configure the producer buffer keep to :first or :last (default to :last)",
"\t\t# {:producer, initial_state, buffer_size: 10_000, buffer_keep: :last}",
"\tend",
"",
"\tdef handle_cast({:${2:dispact_events}, args}, state) do",
"\t\t{:noreply, args, state}",
"\tend",
"",
"\t@doc \"\"\"",
"\tOnly invoked when consumer is requesting for a demand.",
"\t\"\"\"",
"\tdef handle_demand(demand, state) do",
"\t\tevents = []",
"\t\t{:noreply, events, state}",
"\tend",
"end"
],
"description": "Template for genstage producer"
},
"GenStage consumer": {
"prefix": "genstage.consumer",
"body": [
"defmodule ${1:GenStage.Consumer} do",
"\t@moduledoc \"\"\"",
"\tmodule doc",
"\t\"\"\"",
"\tuse GenStage",
"",
"\t##########",
"\t# Client #",
"\t##########",
"",
"\tdef start_link(args) do",
"\t\tGenStage.start_link(__MODULE__, args)",
"\tend",
"",
"\t##########",
"\t# Server #",
"\t##########",
"",
"\tdef init(initial_state) do",
"\t\tsub_opts = [{${2:Producer}, min_demand: ${3:0}, max_demand: ${4:10}}]",
"\t\t{:consumer, initial_state, subscribe_to: sub_opts}",
"\tend",
"",
"\tdef handle_events(events, _from, state) do",
"\t\t# Process list of events",
"\t\t",
"\t\t# Enum.each(events, fn event ->",
"\t\t# process(event)",
"\t\t# end)",
"\t\t",
"\t\t{:noreply, [], state}",
"\tend",
"end"
],
"description": "Template for genstage consumer"
},
"GenStage producer-consumer": {
"prefix": "genstage.producerconsumer",
"body": [
"defmodule ${1:GenStage.ProducerConsumer} do",
"\t@moduledoc \"\"\"",
"\tmodule doc",
"\t\"\"\"",
"\tuse GenStage",
"",
"\t##########",
"\t# Client #",
"\t##########",
"",
"\tdef start_link(args) do",
"\t\tGenStage.start_link(__MODULE__, args, name: __MODULE__)",
"\tend",
"",
"\t##########",
"\t# Server #",
"\t##########",
"",
"\tdef init(initial_state) do",
"\t\tsub_opts = [{${2:Producer}, min_demand: ${3:0}, max_demand: ${4:10}}]",
"\t\t{:producer_consumer, initial_state, subscribe_to: sub_opts}",
"\tend",
"",
"\tdef handle_events(events, _from, state) do",
"\t\t# Process list of events",
"\t\t# events = Enum.filter(events, &filter?/1)",
"\t\t{:noreply, events, state}",
"\tend",
"end"
],
"description": "Template for genstage producer-consumer"
},
"Supervisor module": {
"prefix": "supervisor",
"body": [
"defmodule ${1:OTP}.${2:Supervisor} do",
"\t# See https://hexdocs.pm/elixir/Supervisor.html#module-restart-values-restart",
"\t# to understand the restart strategy",
"\tuse Supervisor, restart: :${3|permanent,temporary,transient|}",
"",
"\tdef start_link(args) do",
"\t\tSupervisor.start_link(__MODULE__, args)",
"\tend",
"",
"\tdef init(args) do",
"\t\tchildren = [",
"\t\t\t{${4:Child}, args}",
"\t\t]",
"\t\t",
"\t\t# See https://hexdocs.pm/elixir/Supervisor.html#module-supervisor-strategies-and-options",
"\t\t# to understand the strategies and supported options",
"\t\topts = [strategy: :${5|one_for_one,one_for_all,rest_for_one|}, max_seconds: 30]",
"\t\tSupervisor.init(children, opts)",
"\tend",
"end"
],
"description": "Template for supervisor module"
},
"ConsumerSupervisor module": {
"prefix": "consumersupervisor",
"body": [
"defmodule ${1:OTP}.${2:ConsumerSupervisor} do",
"\tuse ConsumerSupervisor",
"",
"\tdef start_link(_args) do",
"\t\tConsumerSupervisor.start_link(__MODULE__, :ok)",
"\tend",
"",
"\tdef init(:ok) do",
"\t\tchildren = [",
"\t\t\t%{",
"\t\t\t\tid: ${3:Consumer},",
"\t\t\t\tstart: {${3:Consumer}, :${4:start_link}, []},",
"\t\t\t\trestart: :${5|temporary,transient|}",
"\t\t\t}",
"\t\t]",
"\t\t",
"\t\topts = [",
"\t\t\tstrategy: :one_for_one,",
"\t\t\tsubscribe_to: [",
"\t\t\t\t# Optional: set max_demand to System.schedulers_online() to adjust",
"\t\t\t\t# the concurrency limit according to your system resources",
"\t\t\t\t{Producer, max_demand: ${6:2}}",
"\t\t\t]",
"\t\t]",
"\t\t",
"\t\tConsumerSupervisor.init(children, opts)",
"\tend",
"end"
],
"description": "Template for consumer supervisor"
}
}