@flatfile/listener 0.3.2 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +5 -2
- package/dist/index.js +29 -3
- package/dist/index.mjs +29 -3
- package/package.json +4 -1
- package/.eslintrc.js +0 -4
- package/.turbo/turbo-build.log +0 -17
- package/CHANGELOG.md +0 -113
- package/jest.config.js +0 -3
- package/src/event-drivers/Browser.ts +0 -37
- package/src/event-drivers/FlatfileVirtualMachine.ts +0 -25
- package/src/event-drivers/Webhook.ts +0 -0
- package/src/event-drivers/_EventDriver.ts +0 -32
- package/src/event-drivers/index.ts +0 -3
- package/src/events/authenticated.client.ts +0 -44
- package/src/events/cache.ts +0 -45
- package/src/events/event.handler.spec.ts +0 -89
- package/src/events/event.handler.ts +0 -192
- package/src/events/flatfile.event.ts +0 -66
- package/src/events/glob.match.spec.ts +0 -231
- package/src/events/glob.match.ts +0 -100
- package/src/events/index.ts +0 -3
- package/src/examples/Browser.ts +0 -0
- package/src/examples/Local.ts +0 -38
- package/src/flatfile.listener.spec.ts +0 -88
- package/src/flatfile.listener.ts +0 -78
- package/src/index.ts +0 -9
- package/src/plugins/index.ts +0 -1
- package/src/plugins/useConfigure.ts +0 -72
- package/tsconfig.json +0 -10
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { Event, RecordsWithLinks } from '@flatfile/api/api';
|
|
1
2
|
import { Flatfile } from '@flatfile/api';
|
|
2
3
|
|
|
3
4
|
declare class AuthenticatedClient {
|
|
4
5
|
_accessToken?: string;
|
|
5
6
|
_apiUrl?: string;
|
|
6
7
|
constructor(accessToken?: string, apiUrl?: string);
|
|
7
|
-
fetch(url: string): Promise<any>;
|
|
8
|
+
fetch(url: string, options?: any): Promise<any>;
|
|
8
9
|
/**
|
|
9
10
|
*
|
|
10
11
|
* @deprecated use @flatfile/cross-env-config instead
|
|
@@ -42,11 +43,12 @@ declare class FlatfileEvent extends AuthenticatedClient {
|
|
|
42
43
|
readonly topic: string;
|
|
43
44
|
readonly domain: string;
|
|
44
45
|
readonly target: string;
|
|
46
|
+
readonly origin: object;
|
|
45
47
|
readonly action: string;
|
|
46
48
|
readonly context: any;
|
|
47
49
|
readonly payload: any;
|
|
48
50
|
readonly cache: EventCache;
|
|
49
|
-
constructor(src:
|
|
51
|
+
constructor(src: Event, accessToken?: string, apiUrl?: string);
|
|
50
52
|
/**
|
|
51
53
|
* Should return either event body if expanded already or fetch data from the
|
|
52
54
|
* signed callback URL
|
|
@@ -56,6 +58,7 @@ declare class FlatfileEvent extends AuthenticatedClient {
|
|
|
56
58
|
get data(): Promise<any>;
|
|
57
59
|
private afterAllCallbacks;
|
|
58
60
|
afterAll<T>(callback: () => T, cacheKey?: string): void;
|
|
61
|
+
update(records: RecordsWithLinks): Promise<void>;
|
|
59
62
|
}
|
|
60
63
|
type EventCallback = (evt: FlatfileEvent) => void;
|
|
61
64
|
|
package/dist/index.js
CHANGED
|
@@ -136,14 +136,25 @@ var AuthenticatedClient = class {
|
|
|
136
136
|
this._accessToken = accessToken || bearerToken || "...";
|
|
137
137
|
this._apiUrl = apiUrl || FLATFILE_API_URL;
|
|
138
138
|
}
|
|
139
|
-
fetch(url) {
|
|
139
|
+
async fetch(url, options) {
|
|
140
140
|
const headers = {
|
|
141
141
|
Authorization: `Bearer ${this._accessToken}`,
|
|
142
142
|
"x-disable-hooks": "true"
|
|
143
143
|
};
|
|
144
|
-
const
|
|
145
|
-
|
|
144
|
+
const axiosInstance = import_axios.default.create({
|
|
145
|
+
headers
|
|
146
146
|
});
|
|
147
|
+
const fetchUrl = this._apiUrl + "/" + url;
|
|
148
|
+
const config = {
|
|
149
|
+
url: fetchUrl,
|
|
150
|
+
method: "GET",
|
|
151
|
+
...options
|
|
152
|
+
};
|
|
153
|
+
try {
|
|
154
|
+
const resp = await axiosInstance(config);
|
|
155
|
+
return resp.data.data;
|
|
156
|
+
} catch (err) {
|
|
157
|
+
}
|
|
147
158
|
}
|
|
148
159
|
/**
|
|
149
160
|
*
|
|
@@ -215,6 +226,7 @@ var FlatfileEvent = class extends AuthenticatedClient {
|
|
|
215
226
|
this.context = src.context;
|
|
216
227
|
this.payload = src.payload;
|
|
217
228
|
this.target = src.target || "";
|
|
229
|
+
this.origin = src.origin || {};
|
|
218
230
|
this.action = src.context?.actionName || "";
|
|
219
231
|
}
|
|
220
232
|
/**
|
|
@@ -236,6 +248,20 @@ var FlatfileEvent = class extends AuthenticatedClient {
|
|
|
236
248
|
this.afterAllCallbacks.set(key, callback);
|
|
237
249
|
}
|
|
238
250
|
}
|
|
251
|
+
async update(records) {
|
|
252
|
+
if (!this.src.dataUrl) {
|
|
253
|
+
throw new Error("Cannot set data on an event without a dataUrl");
|
|
254
|
+
}
|
|
255
|
+
const prepRecords = records.map((record) => {
|
|
256
|
+
record.messages?.map((message) => {
|
|
257
|
+
delete message.source;
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
await this.fetch(this.src.dataUrl, {
|
|
261
|
+
method: "POST",
|
|
262
|
+
data: JSON.stringify(prepRecords)
|
|
263
|
+
});
|
|
264
|
+
}
|
|
239
265
|
};
|
|
240
266
|
|
|
241
267
|
// ../../node_modules/wildcard-match/build/index.es.mjs
|
package/dist/index.mjs
CHANGED
|
@@ -93,14 +93,25 @@ var AuthenticatedClient = class {
|
|
|
93
93
|
this._accessToken = accessToken || bearerToken || "...";
|
|
94
94
|
this._apiUrl = apiUrl || FLATFILE_API_URL;
|
|
95
95
|
}
|
|
96
|
-
fetch(url) {
|
|
96
|
+
async fetch(url, options) {
|
|
97
97
|
const headers = {
|
|
98
98
|
Authorization: `Bearer ${this._accessToken}`,
|
|
99
99
|
"x-disable-hooks": "true"
|
|
100
100
|
};
|
|
101
|
-
const
|
|
102
|
-
|
|
101
|
+
const axiosInstance = axios.create({
|
|
102
|
+
headers
|
|
103
103
|
});
|
|
104
|
+
const fetchUrl = this._apiUrl + "/" + url;
|
|
105
|
+
const config = {
|
|
106
|
+
url: fetchUrl,
|
|
107
|
+
method: "GET",
|
|
108
|
+
...options
|
|
109
|
+
};
|
|
110
|
+
try {
|
|
111
|
+
const resp = await axiosInstance(config);
|
|
112
|
+
return resp.data.data;
|
|
113
|
+
} catch (err) {
|
|
114
|
+
}
|
|
104
115
|
}
|
|
105
116
|
/**
|
|
106
117
|
*
|
|
@@ -172,6 +183,7 @@ var FlatfileEvent = class extends AuthenticatedClient {
|
|
|
172
183
|
this.context = src.context;
|
|
173
184
|
this.payload = src.payload;
|
|
174
185
|
this.target = src.target || "";
|
|
186
|
+
this.origin = src.origin || {};
|
|
175
187
|
this.action = src.context?.actionName || "";
|
|
176
188
|
}
|
|
177
189
|
/**
|
|
@@ -193,6 +205,20 @@ var FlatfileEvent = class extends AuthenticatedClient {
|
|
|
193
205
|
this.afterAllCallbacks.set(key, callback);
|
|
194
206
|
}
|
|
195
207
|
}
|
|
208
|
+
async update(records) {
|
|
209
|
+
if (!this.src.dataUrl) {
|
|
210
|
+
throw new Error("Cannot set data on an event without a dataUrl");
|
|
211
|
+
}
|
|
212
|
+
const prepRecords = records.map((record) => {
|
|
213
|
+
record.messages?.map((message) => {
|
|
214
|
+
delete message.source;
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
await this.fetch(this.src.dataUrl, {
|
|
218
|
+
method: "POST",
|
|
219
|
+
data: JSON.stringify(prepRecords)
|
|
220
|
+
});
|
|
221
|
+
}
|
|
196
222
|
};
|
|
197
223
|
|
|
198
224
|
// ../../node_modules/wildcard-match/build/index.es.mjs
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flatfile/listener",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "A PubSub Listener for configuring and using Flatfile",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
"poll": "tsup src/dev.ts --format esm,cjs --watch --dts --onSuccess \"node dist/dev.js\"",
|
|
13
13
|
"test": "jest --passWithNoTests"
|
|
14
14
|
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/**"
|
|
17
|
+
],
|
|
15
18
|
"license": "MIT",
|
|
16
19
|
"devDependencies": {
|
|
17
20
|
"@flatfile/eslint-config-platform-sdk": "*",
|
package/.eslintrc.js
DELETED
package/.turbo/turbo-build.log
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @flatfile/listener@0.3.2 build
|
|
3
|
-
> tsup src/index.ts --format esm,cjs --dts
|
|
4
|
-
|
|
5
|
-
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
-
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
-
[34mCLI[39m tsup v6.7.0
|
|
8
|
-
[34mCLI[39m Target: es2020
|
|
9
|
-
[34mESM[39m Build start
|
|
10
|
-
[34mCJS[39m Build start
|
|
11
|
-
[32mESM[39m [1mdist/index.mjs [22m[32m16.07 KB[39m
|
|
12
|
-
[32mESM[39m ⚡️ Build success in 81ms
|
|
13
|
-
[32mCJS[39m [1mdist/index.js [22m[32m18.01 KB[39m
|
|
14
|
-
[32mCJS[39m ⚡️ Build success in 81ms
|
|
15
|
-
[34mDTS[39m Build start
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in 2157ms
|
|
17
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[32m7.32 KB[39m
|
package/CHANGELOG.md
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
# @flatfile/listener
|
|
2
|
-
|
|
3
|
-
## 0.3.2
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- 84f6807: resolves a minor bug with afterAll callbacks and adds recursive listener introspection
|
|
8
|
-
|
|
9
|
-
## 0.3.1
|
|
10
|
-
|
|
11
|
-
### Patch Changes
|
|
12
|
-
|
|
13
|
-
- 9ca9443: Event Cache Cleanup
|
|
14
|
-
|
|
15
|
-
## 0.3.0
|
|
16
|
-
|
|
17
|
-
### Minor Changes
|
|
18
|
-
|
|
19
|
-
- 2a30e13: Add cross-env library for accessing environment variables
|
|
20
|
-
|
|
21
|
-
## 0.2.0
|
|
22
|
-
|
|
23
|
-
### Minor Changes
|
|
24
|
-
|
|
25
|
-
- 637342b: Remove @flatfile/api from listener
|
|
26
|
-
|
|
27
|
-
## 0.1.2
|
|
28
|
-
|
|
29
|
-
### Patch Changes
|
|
30
|
-
|
|
31
|
-
- 22d2466: Pull last 5 seconds of events in Polling Driver
|
|
32
|
-
|
|
33
|
-
## 0.1.1
|
|
34
|
-
|
|
35
|
-
### Patch Changes
|
|
36
|
-
|
|
37
|
-
- 356c35d: Minor CI improvements and backwards compatible rename of Client to FlatfileListener
|
|
38
|
-
|
|
39
|
-
## 0.1.0
|
|
40
|
-
|
|
41
|
-
### Minor Changes
|
|
42
|
-
|
|
43
|
-
- 4d88069: Adds a major improvement to event filtering, allowing you to grep the key path and defaulting to matching any key in the object
|
|
44
|
-
|
|
45
|
-
## 0.0.10
|
|
46
|
-
|
|
47
|
-
### Patch Changes
|
|
48
|
-
|
|
49
|
-
- 4807720: removes unused node dependencies
|
|
50
|
-
- 82e80e3: Add in x-disable-hooks header
|
|
51
|
-
|
|
52
|
-
## 0.0.9
|
|
53
|
-
|
|
54
|
-
### Patch Changes
|
|
55
|
-
|
|
56
|
-
- 46357d9: flatfile deploy and develop
|
|
57
|
-
|
|
58
|
-
## 0.0.8
|
|
59
|
-
|
|
60
|
-
### Patch Changes
|
|
61
|
-
|
|
62
|
-
- 74852f3: Adds Event Cache
|
|
63
|
-
- 65aea7a: Adds conditional fetchApi variable to authenticated client to allow for client-side use
|
|
64
|
-
|
|
65
|
-
## 0.0.7
|
|
66
|
-
|
|
67
|
-
### Patch Changes
|
|
68
|
-
|
|
69
|
-
- Updated dependencies [72447b1]
|
|
70
|
-
- @flatfile/hooks@1.3.0
|
|
71
|
-
|
|
72
|
-
## 0.0.6
|
|
73
|
-
|
|
74
|
-
### Patch Changes
|
|
75
|
-
|
|
76
|
-
- 4039970: Adds 'x-disable-hooks': 'true' to event payload to x
|
|
77
|
-
|
|
78
|
-
## 0.0.5
|
|
79
|
-
|
|
80
|
-
### Patch Changes
|
|
81
|
-
|
|
82
|
-
- 55f9fff: Update API version and use configless space creation in listener example.
|
|
83
|
-
- Updated dependencies [55f9fff]
|
|
84
|
-
- @flatfile/schema@0.2.15
|
|
85
|
-
|
|
86
|
-
## 0.0.4
|
|
87
|
-
|
|
88
|
-
### Patch Changes
|
|
89
|
-
|
|
90
|
-
- 8ed4e8c: Bumps API version and adds support for record metadata to @flatfile/hooks.
|
|
91
|
-
- Updated dependencies [8ed4e8c]
|
|
92
|
-
- @flatfile/schema@0.2.14
|
|
93
|
-
- @flatfile/hooks@1.2.4
|
|
94
|
-
|
|
95
|
-
## 0.0.3
|
|
96
|
-
|
|
97
|
-
### Patch Changes
|
|
98
|
-
|
|
99
|
-
- c946831: Updates to use the dataUrl for getting records
|
|
100
|
-
|
|
101
|
-
## 0.0.2
|
|
102
|
-
|
|
103
|
-
### Patch Changes
|
|
104
|
-
|
|
105
|
-
- 2e25be3: Update packages with updated @flatfile/api
|
|
106
|
-
- Updated dependencies [2e25be3]
|
|
107
|
-
- @flatfile/schema@0.2.13
|
|
108
|
-
|
|
109
|
-
## 0.0.1
|
|
110
|
-
|
|
111
|
-
### Patch Changes
|
|
112
|
-
|
|
113
|
-
- 4b918a2: Adds the new @flatfile/listener package
|
package/jest.config.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { EventDriver } from './_EventDriver'
|
|
2
|
-
import { EventHandler } from '../events'
|
|
3
|
-
|
|
4
|
-
export class Browser extends EventDriver {
|
|
5
|
-
public _accessToken?: string
|
|
6
|
-
public _apiUrl?: string
|
|
7
|
-
public _environmentId?: string
|
|
8
|
-
|
|
9
|
-
constructor({
|
|
10
|
-
apiUrl,
|
|
11
|
-
accessToken,
|
|
12
|
-
environmentId,
|
|
13
|
-
}: {
|
|
14
|
-
apiUrl: string
|
|
15
|
-
accessToken: string
|
|
16
|
-
environmentId?: string
|
|
17
|
-
/**
|
|
18
|
-
* @deprecated
|
|
19
|
-
*/
|
|
20
|
-
fetchApi: any
|
|
21
|
-
}) {
|
|
22
|
-
super()
|
|
23
|
-
this._apiUrl = apiUrl
|
|
24
|
-
this._accessToken = accessToken
|
|
25
|
-
this._environmentId = environmentId || ''
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
mountEventHandler(handler: EventHandler): this {
|
|
29
|
-
handler.setVariables({
|
|
30
|
-
accessToken: this._accessToken,
|
|
31
|
-
apiUrl: this._apiUrl,
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
this._handler = handler
|
|
35
|
-
return this
|
|
36
|
-
}
|
|
37
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { EventDriver } from './_EventDriver'
|
|
2
|
-
import { EventHandler, FlatfileEvent } from '../events'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Flatfile's Virtual Machine is stateless / serverless. So when a new event
|
|
6
|
-
* is handled, it will just call `handle(event)`.
|
|
7
|
-
*/
|
|
8
|
-
export class FlatfileVirtualMachine extends EventDriver {
|
|
9
|
-
/**
|
|
10
|
-
* This method is triggered from within the Flatfile Core VM Runner. This
|
|
11
|
-
* EventDriver does not have to listen for events because this method will
|
|
12
|
-
* be invoked as necessary.
|
|
13
|
-
*
|
|
14
|
-
* @param event
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
handle(event: FlatfileEvent) {
|
|
18
|
-
this.dispatchEvent(event)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
mountEventHandler(handler: EventHandler): this {
|
|
22
|
-
this._handler = handler
|
|
23
|
-
return this
|
|
24
|
-
}
|
|
25
|
-
}
|
|
File without changes
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { EventHandler } from '../events'
|
|
2
|
-
|
|
3
|
-
export abstract class EventDriver {
|
|
4
|
-
_handler?: EventHandler
|
|
5
|
-
|
|
6
|
-
public get handler(): EventHandler {
|
|
7
|
-
if (!this._handler) {
|
|
8
|
-
throw new Error('handler not registered yet')
|
|
9
|
-
}
|
|
10
|
-
return this._handler
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Mount an event handler
|
|
15
|
-
*
|
|
16
|
-
* @param handler
|
|
17
|
-
*/
|
|
18
|
-
mountEventHandler(handler: EventHandler): this {
|
|
19
|
-
this._handler = handler
|
|
20
|
-
return this
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Dispatch an event
|
|
25
|
-
*
|
|
26
|
-
* @param e
|
|
27
|
-
*/
|
|
28
|
-
dispatchEvent(e: any): this {
|
|
29
|
-
this.handler.dispatchEvent(e)
|
|
30
|
-
return this
|
|
31
|
-
}
|
|
32
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { CrossEnvConfig } from '@flatfile/cross-env-config'
|
|
2
|
-
import axios from 'axios'
|
|
3
|
-
|
|
4
|
-
export class AuthenticatedClient {
|
|
5
|
-
public _accessToken?: string
|
|
6
|
-
public _apiUrl?: string
|
|
7
|
-
|
|
8
|
-
constructor(accessToken?: string, apiUrl?: string) {
|
|
9
|
-
const FLATFILE_API_URL =
|
|
10
|
-
CrossEnvConfig.get('AGENT_INTERNAL_URL') || 'http://localhost:3000'
|
|
11
|
-
const bearerToken = CrossEnvConfig.get('FLATFILE_BEARER_TOKEN')
|
|
12
|
-
|
|
13
|
-
this._accessToken = accessToken || bearerToken || '...'
|
|
14
|
-
|
|
15
|
-
this._apiUrl = apiUrl || FLATFILE_API_URL
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
fetch(url: string) {
|
|
19
|
-
const headers = {
|
|
20
|
-
Authorization: `Bearer ${this._accessToken}`,
|
|
21
|
-
'x-disable-hooks': 'true',
|
|
22
|
-
}
|
|
23
|
-
const fetchUrl = this._apiUrl + '/' + url
|
|
24
|
-
|
|
25
|
-
return axios
|
|
26
|
-
.get(fetchUrl, { headers })
|
|
27
|
-
.then((resp: any) => resp.data.data)
|
|
28
|
-
.catch((err: any) => {})
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
*
|
|
32
|
-
* @deprecated use @flatfile/cross-env-config instead
|
|
33
|
-
*/
|
|
34
|
-
public setVariables({
|
|
35
|
-
accessToken,
|
|
36
|
-
apiUrl,
|
|
37
|
-
}: {
|
|
38
|
-
accessToken?: string
|
|
39
|
-
apiUrl?: string
|
|
40
|
-
}) {
|
|
41
|
-
this._accessToken = accessToken
|
|
42
|
-
this._apiUrl = apiUrl
|
|
43
|
-
}
|
|
44
|
-
}
|
package/src/events/cache.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
export class EventCache {
|
|
2
|
-
private eventCache: Map<any, any> = new Map()
|
|
3
|
-
|
|
4
|
-
async init<T>(key: string, callback: () => Promise<T>): Promise<T> {
|
|
5
|
-
if (this.eventCache.get(key)) {
|
|
6
|
-
return this.eventCache.get(key)
|
|
7
|
-
} else {
|
|
8
|
-
const result = await callback()
|
|
9
|
-
this.eventCache.set(key, result)
|
|
10
|
-
return result
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async set<T>(key: string, callback: () => Promise<T>): Promise<T> {
|
|
15
|
-
if (this.eventCache.get(key)) {
|
|
16
|
-
const result = await callback()
|
|
17
|
-
this.eventCache.set(key, result)
|
|
18
|
-
return result
|
|
19
|
-
} else {
|
|
20
|
-
throw new Error('Cache key not found')
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
get<T>(key: string): T {
|
|
25
|
-
if (this.eventCache.get(key)) {
|
|
26
|
-
return this.eventCache.get(key)
|
|
27
|
-
} else {
|
|
28
|
-
throw new Error('Cache key not found')
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
delete(key?: string | string[]): void {
|
|
33
|
-
if (!key) {
|
|
34
|
-
this.eventCache.clear()
|
|
35
|
-
} else if (this.eventCache.get(key)) {
|
|
36
|
-
if (Array.isArray(key)) {
|
|
37
|
-
key.forEach((k) => this.eventCache.delete(k))
|
|
38
|
-
} else {
|
|
39
|
-
this.eventCache.delete(key)
|
|
40
|
-
}
|
|
41
|
-
} else {
|
|
42
|
-
throw new Error('Cache key not found')
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { EventHandler } from './event.handler'
|
|
2
|
-
|
|
3
|
-
describe('EventHandler', () => {
|
|
4
|
-
let testFn: jest.Mock
|
|
5
|
-
|
|
6
|
-
beforeEach(() => {
|
|
7
|
-
testFn = jest.fn()
|
|
8
|
-
})
|
|
9
|
-
|
|
10
|
-
describe('triggering', () => {
|
|
11
|
-
test('triggers basic event listeners', () => {
|
|
12
|
-
const handler = new EventHandler().on('foo', testFn)
|
|
13
|
-
|
|
14
|
-
handler.dispatchEvent({ topic: 'foo' })
|
|
15
|
-
expect(testFn).toHaveBeenCalledTimes(1)
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
test('triggers namespaced event listeners', () => {
|
|
19
|
-
const handler = new EventHandler().on('records:created', testFn)
|
|
20
|
-
|
|
21
|
-
handler.dispatchEvent({ topic: 'records:created' })
|
|
22
|
-
expect(testFn).toHaveBeenCalled()
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
test('triggers event listener for array of events', () => {
|
|
26
|
-
const handler = new EventHandler().on(
|
|
27
|
-
['records:created', 'records:updated'],
|
|
28
|
-
testFn
|
|
29
|
-
)
|
|
30
|
-
|
|
31
|
-
handler.dispatchEvent({ topic: 'records:created' })
|
|
32
|
-
handler.dispatchEvent({ topic: 'records:updated' })
|
|
33
|
-
expect(testFn).toHaveBeenCalledTimes(2)
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
test('triggers event listener for wildcard query of events', () => {
|
|
37
|
-
const handler = new EventHandler().on('records:*', testFn)
|
|
38
|
-
|
|
39
|
-
handler.dispatchEvent({ topic: 'records:created' })
|
|
40
|
-
handler.dispatchEvent({ topic: 'records:updated' })
|
|
41
|
-
handler.dispatchEvent({ topic: 'records:deleted' })
|
|
42
|
-
expect(testFn).toHaveBeenCalledTimes(3)
|
|
43
|
-
})
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
describe('filtering', () => {
|
|
47
|
-
test('trigger only when filter match - on listener', () => {
|
|
48
|
-
const handler = new EventHandler().on(
|
|
49
|
-
'records:created',
|
|
50
|
-
{ domain: 'foo' },
|
|
51
|
-
testFn
|
|
52
|
-
)
|
|
53
|
-
|
|
54
|
-
handler.dispatchEvent({ topic: 'records:created' })
|
|
55
|
-
handler.dispatchEvent({ topic: 'records:created', domain: 'foo' })
|
|
56
|
-
expect(testFn).toHaveBeenCalledTimes(1)
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
test('trigger only when filter match - on scope', () => {
|
|
60
|
-
const handler = new EventHandler({ domain: 'foo' }).on(
|
|
61
|
-
'records:created',
|
|
62
|
-
testFn
|
|
63
|
-
)
|
|
64
|
-
|
|
65
|
-
handler.dispatchEvent({ topic: 'records:created' })
|
|
66
|
-
handler.dispatchEvent({ topic: 'records:created', domain: 'foo' })
|
|
67
|
-
expect(testFn).toHaveBeenCalledTimes(1)
|
|
68
|
-
})
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
describe('child node', () => {
|
|
72
|
-
test('trigger a listener on a child node', async () => {
|
|
73
|
-
const handler = new EventHandler().addNode(
|
|
74
|
-
new EventHandler().on('records:created', testFn)
|
|
75
|
-
)
|
|
76
|
-
await handler.dispatchEvent({ topic: 'records:created' })
|
|
77
|
-
expect(testFn).toHaveBeenCalledTimes(1)
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
test('trigger only when filter match - on scope', async () => {
|
|
81
|
-
const handler = new EventHandler().addNode(
|
|
82
|
-
new EventHandler({ domain: 'foo' }).on('records:created', testFn)
|
|
83
|
-
)
|
|
84
|
-
await handler.dispatchEvent({ topic: 'records:created' })
|
|
85
|
-
await handler.dispatchEvent({ topic: 'records:created', domain: 'foo' })
|
|
86
|
-
expect(testFn).toHaveBeenCalledTimes(1)
|
|
87
|
-
})
|
|
88
|
-
})
|
|
89
|
-
})
|