Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 155 lines (108 sloc) 3.123 kB
3df421d @substack emit stats events instead of all the other things
authored
1 waitlist
2 ========
3
9565b85 @substack simple shared example
authored
4 Manage consumers standing in a queue for limited resources.
3df421d @substack emit stats events instead of all the other things
authored
5
9565b85 @substack simple shared example
authored
6 example
7 =======
3df421d @substack emit stats events instead of all the other things
authored
8
9565b85 @substack simple shared example
authored
9 ``` js
10 var waitlist = require('waitlist');
11 var EventEmitter = require('events').EventEmitter;
12
13 var ws = waitlist();
14
15 ws.add('beep', { id : 0 });
16 ws.add('boop', { id : 1 });
17
18 (function consume (userId) {
19 setTimeout(function () {
20 var em = new EventEmitter;
21 em.on('spot', function (n) {
22 console.log('user ' + userId + ' in spot ' + n);
23 });
24
25 em.on('available', function (res) {
26 console.log('user ' + userId
27 + ' using resource ' + JSON.stringify(res)
28 );
29 });
30
31 em.on('expire', function () {
32 console.log('user ' + userId + ' expired');
33 });
34
35 ws.acquire(Math.random() * 1000, em.emit.bind(em));
36
37 consume(userId + 1);
38 }, Math.random() * 500);
39 })(0)
40 ```
41
42 output:
43
44 ```
45 $ node examples/shared.js
46 user 0 using resource {"id":0}
47 user 1 using resource {"id":1}
48 user 2 in spot 1
49 user 2 in spot 1
50 user 3 in spot 2
51 user 0 expired
52 user 2 using resource {"id":0}
53 user 3 in spot 1
54 user 3 in spot 1
55 user 4 in spot 2
56 user 3 in spot 1
57 user 4 in spot 2
58 user 5 in spot 3
59 user 1 expired
60 user 3 using resource {"id":1}
61 user 4 in spot 1
62 user 5 in spot 2
63 ^C
64 ```
65
66 methods
67 =======
68
8bae482 @substack now fully documented
authored
69 var waitlist = require('waitlist')
9565b85 @substack simple shared example
authored
70
8bae482 @substack now fully documented
authored
71 var ws = waitlist()
72 -------------------
73
74 Create a new waitlist.
75
76 ws.add(name, res)
77 -----------------
78
79 Add a new resource `name` with the resource object `res`.
80 `res` will be made available to consumers.
81
82 ws.remove(name)
83 ---------------
84
85 Remove a resource by its `name`.
86
87 ws.acquire(time, emit)
88 ----------------------
89
90 Try to acquire an available resource for `time` milliseconds.
91
92 If `time` is 0, the session won't expire automatically.
93
94 Resource events will be passed through the `emit` function supplied.
95
e9c38cc @substack comment about resource ordering
authored
96 The available resource that was last used the longest time ago will be favored
97 in computing which resource to deliver.
98
8bae482 @substack now fully documented
authored
99 ws.release(token)
100 -----------------
101
102 Release a granted token.
103
104 ws.on('stats', cb)
105 ------------------
106
107 Emitted whenever the queue stats of available resources and connected clients
108 changes. `cb(stats)` is called with the `ws.stats` object which looks like:
109
110 ``` js
111 { resources : 2, waiting : 0, using : 1 },
112 ```
113
114 resource events
115 ===============
116
117 The `emit` function passed to `ws.acquire()` receives these events:
118
119 'token', token
120 --------------
121
122 Emits the token for the current session right after `acquire()` is called.
123
124 'spot', ix
125 ----------
126
127 If the resource isn't available right away, emit the position in the queue.
128
129 'available', res, name, time
130 ----------------------------
131
132 Emitted when a resource `res` with the name `name` is ready for `time`
133 milliseconds of use.
134
135 'expire'
136 --------
137
138 Emitted when a session's alloted time is up.
139
140 'release'
141 ---------
142
143 Emitted when a session is finished either through `ws.release()` or expiry.
9565b85 @substack simple shared example
authored
144
145 install
146 =======
147
148 With [npm](http://npmjs.org) do:
149
150 ```
151 npm install waitlist
152 ```
153
154 [![build status](https://secure.travis-ci.org/substack/node-waitlist.png)](http://travis-ci.org/substack/node-waitlist)
Something went wrong with that request. Please try again.