-
Notifications
You must be signed in to change notification settings - Fork 1
/
shouldEmitNew.js
63 lines (47 loc) · 1.7 KB
/
shouldEmitNew.js
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
/*
* shouldEmitNew.js:
*
* (C) 2012 Crosstalk Systems Inc.
*/
var createWorldPath = require( './createWorldPath' ),
matchesHistoricalEvent = require( './matchesHistoricalEvent' );
//
// ### function shouldEmitNewConstructor ( wrapper )
// #### @wrapper {object} the worker wrapper to construct assertion for
// Constructs the shouldEmitNew helper assertion for the wrapper specified. This
// means that we expect a new event "from now on" instead of since start
// of history.
//
var shouldEmitNewConstructor = function shouldEmitNewConstructor ( wrapper ) {
var history = wrapper.history;
var shouldEmitNew = function shouldEmitNew ( workerReference, message, params,
scope, callback ) {
// workerReference is optional
if ( typeof( workerReference ) != 'object' ) {
callback = scope;
scope = params;
params = message;
message = workerReference;
workerReference = null;
} // if ( typeof( workerReference ) != 'object' )
var markedOutLength = history._out.length;
var eventToMatch = {
message : message,
params : params,
scope : scope,
callback : callback
};
if ( workerReference ) eventToMatch.workerReference = workerReference;
// 1. check history since markedOutLength if this already happened
if ( matchesHistoricalEvent( history._out.slice( markedOutLength ),
eventToMatch ) ) {
return wrapper;
}
// 2. hasn't happened yet so attach an event listener that will trigger
// this world path
return createWorldPath( wrapper, eventToMatch,
markedOutLength );
}; // shouldEmitNew
return shouldEmitNew;
}; // shouldEmitNewConstructor
module.exports = shouldEmitNewConstructor;