-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistate.js.html
399 lines (327 loc) · 10.9 KB
/
listate.js.html
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>listate Source: listate.js</title>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
<link type="text/css" rel="stylesheet" href="styles/site.spacelab.css">
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top ">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="index.html">listate</a>
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#topNavigation">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse" id="topNavigation">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="modules.list.html" class="dropdown-toggle" data-toggle="dropdown">Modules<b class="caret"></b></a>
<ul class="dropdown-menu ">
<li><a href="module-listate.html">listate</a></li><li><a href="module-listate_extra.html">listate/extra</a></li>
</ul>
</li>
</ul>
<div class="col-sm-3 col-md-3">
<form class="navbar-form" role="search">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="q" id="search-input">
<div class="input-group-btn">
<button class="btn btn-default" id="search-submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="container" id="toc-content">
<div class="row">
<div class="col-md-12">
<div id="main">
<h1 class="page-title">Source: listate.js</h1>
<section>
<article>
<pre
class="sunlight-highlight-javascript linenums">/*
* listate
* https://github.com/gamtiq/listate
*
* Copyright (c) 2017-2019 Denis Sikuler
* Licensed under the MIT license.
*/
/* global clearTimeout, setTimeout */
/**
* Library for listening on changes of Redux store state.
*
* @module listate
*/
/**
* Store object.
*
* @typedef {Object} Store
*
* @property {Function} dispatch
* Dispatches an action to trigger a state change.
* @property {Function} getState
* Returns the current state.
* @property {Function} subscribe
* Adds a change listener.
*/
/**
* Parameter that is passed when calling the listener.
*
* @typedef {Object} HandleParam
*
* @property {any} current
* The current state or a part of the current state if `filter` is set.
* @property {any} data
* The auxiliary data (value of `listener.data` parameter).
* @property {Function} dispatch
* Reference to `dispatch` method of the store.
* @property {any} prev
* The previous state or a part of the previous state if `filter` is set.
* @property {object} prevState
* The previous state.
* @property {object} state
* The current state.
* @property {module:listate~Store} store
* The store for which listener is registered.
* @property {Function} unlisten
* The function that removes/unsubscribes the listener.
*/
/**
* Check whether current value (state) is not equal previous value (state).
*
* Uses `!==` for comparison.
*
* @param {object} state
* A current value (state).
* @param {object} prevState
* A previous value (state).
* @return {boolean}
* `true` if current value is not equal previous value.
*/
export function baseWhen(state, prevState) {
return state !== prevState;
}
// eslint-disable-next-line max-params, require-jsdoc
function run(func, context, param, once) {
return () => {
func.call(context, param);
if (once) {
param.unlisten();
}
};
}
/**
* Add/register state change listener for the given store.
*
* @example
* import listen from 'listate';
*
* const store = createStore(reducer, initState);
*
* listen(store, {
* filter: (state) => state.section,
* when: (current, prev) => current !== prev && current !== 'exit',
* delay: 1000,
* handle: (data) => {
* // data.current === state.section
* localStorage.setItem('selectedSection', data.current);
* }
* });
*
* @param {module:listate~Store} store
* Store for which listener should be added/registered.
* @param {Function | object} listener
* Specifies listener that should be called on a state change.
* Can be a function or an object that defines listener settings/details.
* @param {Function} listener.handle
* Listener that should be called on a state change.
* @param {boolean | object} [listener.context]
* Object that should be used as `this` value when calling the listener.
* When `true` is passed `listener` object will be used as `this`.
* False value (by default) means that `null` will be used as the context object.
* @param {any} [listener.data]
* Any data that should be passed into the listener.
* @param {number} [listener.delay]
* Specifies that listener should be called after the given number of milliseconds have elapsed.
* Works similar to `debounce`: when several requests for the listener call arrive during the specified period
* only the last one will be applied after the timeout.
* `0` is acceptable value that means the listener should be called asynchronuosly.
* Negative number means that the listener should be called without delay.
* @param {Function} [listener.filter=(state) => state]
* Function (selector) to extract state part which will be used inside `when` to determine
* whether the listener should be called. By default the entire state will be used.
* @param {boolean} [listener.once=false]
* Whether the listener should be called just once.
* @param {Function} [listener.when=baseWhen]
* Function to determine whether the listener should be called.
* The listener will be called if the function returns true.
* The following parameters will be passed into the function:
*
* * the current state or a part of the current state if `filter` is set.
* * the previous state or a part of the previous state if `filter` is set.
* * an object that will be passed into listener.
*
* @return {Function}
* A function that removes/unsubscribes the listener.
* @alias module:listate.listen
*/
export default function listen(store, listener) {
const settings = typeof listener === 'function'
? {handle: listener}
: listener;
const { handle, data, filter, once } = settings;
let context = settings.context || null;
if (context && typeof context !== 'object') {
context = listener;
}
const delay = typeof settings.delay === 'number'
? settings.delay
: -1;
const when = settings.when || baseWhen;
let prevState = store.getState();
let prev = filter
? filter(prevState)
: prevState;
let timeoutId;
const unlisten = store.subscribe(() => {
const state = store.getState();
const current = filter
? filter(state)
: state;
const param = {
current,
prev,
state,
prevState,
data,
store,
dispatch: store.dispatch,
unlisten
};
prevState = state;
if (when(current, prev, param) && handle) {
prev = current;
if (delay < 0) {
handle.call(context, param);
if (once) {
unlisten();
}
}
else {
clearTimeout(timeoutId);
timeoutId = setTimeout(run(handle, context, param, once), delay);
}
}
else {
prev = current;
}
});
return unlisten;
}
export { listen };
</pre>
</article>
</section>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="modal fade" id="searchResults">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Search results</h4>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
<footer>
<span class="copyright">
Copyright (c) 2017-2019 Denis Sikuler
</span>
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.2</a>
on 2019-06-22T20:49:22+03:00
using the <a href="https://github.com/docstrap/docstrap">DocStrap template</a>.
</span>
</footer>
<script src="scripts/docstrap.lib.js"></script>
<script src="scripts/toc.js"></script>
<script type="text/javascript" src="scripts/fulltext-search-ui.js"></script>
<script>
$( function () {
$( "[id*='$']" ).each( function () {
var $this = $( this );
$this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) );
} );
$( ".tutorial-section pre, .readme-section pre, pre.prettyprint.source" ).each( function () {
var $this = $( this );
var example = $this.find( "code" );
exampleText = example.html();
var lang = /{@lang (.*?)}/.exec( exampleText );
if ( lang && lang[1] ) {
exampleText = exampleText.replace( lang[0], "" );
example.html( exampleText );
lang = lang[1];
} else {
var langClassMatch = example.parent()[0].className.match(/lang\-(\S+)/);
lang = langClassMatch ? langClassMatch[1] : "javascript";
}
if ( lang ) {
$this
.addClass( "sunlight-highlight-" + lang )
.addClass( "linenums" )
.html( example.html() );
}
} );
Sunlight.highlightAll( {
lineNumbers : true,
showMenu : true,
enableDoclinks : true
} );
$.catchAnchorLinks( {
navbarOffset: 10
} );
$( "#toc" ).toc( {
anchorName : function ( i, heading, prefix ) {
return $( heading ).attr( "id" ) || ( prefix + i );
},
selectors : "#toc-content h1,#toc-content h2,#toc-content h3,#toc-content h4",
showAndHide : false,
smoothScrolling: true
} );
$( "#main span[id^='toc']" ).addClass( "toc-shim" );
$( '.dropdown-toggle' ).dropdown();
$( "table" ).each( function () {
var $this = $( this );
$this.addClass('table');
} );
} );
</script>
<!--Navigation and Symbol Display-->
<!--Google Analytics-->
<script type="text/javascript">
$(document).ready(function() {
SearcherDisplay.init();
});
</script>
</body>
</html>