@searchspring/snap-event-manager 0.20.0
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/LICENSE +21 -0
- package/README.md +92 -0
- package/dist/cjs/EventManager.d.ts +9 -0
- package/dist/cjs/EventManager.d.ts.map +1 -0
- package/dist/cjs/EventManager.js +74 -0
- package/dist/cjs/MiddlewareManager.d.ts +10 -0
- package/dist/cjs/MiddlewareManager.d.ts.map +1 -0
- package/dist/cjs/MiddlewareManager.js +126 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +16 -0
- package/dist/cjs/types.d.ts +4 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +2 -0
- package/dist/esm/EventManager.d.ts +9 -0
- package/dist/esm/EventManager.d.ts.map +1 -0
- package/dist/esm/EventManager.js +19 -0
- package/dist/esm/MiddlewareManager.d.ts +10 -0
- package/dist/esm/MiddlewareManager.d.ts.map +1 -0
- package/dist/esm/MiddlewareManager.js +41 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/types.d.ts +4 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +1 -0
- package/package.json +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Searchspring
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# Snap Event Manager
|
|
2
|
+
|
|
3
|
+
<a href="https://www.npmjs.com/package/@searchspring/snap-event-manager"><img alt="NPM Status" src="https://img.shields.io/npm/v/@searchspring/snap-event-manager.svg?style=flat"></a>
|
|
4
|
+
|
|
5
|
+
The Snap Event Manager is used to create events and attach middleware to them.
|
|
6
|
+
|
|
7
|
+
When used as a service of a controller it allows you to hook into controller events at critical times in the life cycle. It also allows for custom events to be used throughout your implementation.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## Dependency
|
|
11
|
+
|
|
12
|
+
Snap Event Manager is a dependency of [@searchspring/snap-controller](https://github.com/searchspring/snap/tree/main/packages/snap-controller) <a href="https://www.npmjs.com/package/@searchspring/snap-controller"><img alt="NPM Status" src="https://img.shields.io/npm/v/@searchspring/snap-controller.svg?style=flat"></a>
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install --save @searchspring/snap-event-manager
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Import
|
|
21
|
+
```typescript
|
|
22
|
+
import { EventManager } from '@searchspring/snap-event-manager';
|
|
23
|
+
```
|
|
24
|
+
## Controller usage
|
|
25
|
+
Snap Event Manager is a dependency of Snap Controller and it is recommended to use methods of the controller to attach events to the EventManager. Additionally, different events exist for the different controllers - see the Controller documentation for more details.
|
|
26
|
+
|
|
27
|
+
## Standalone usage
|
|
28
|
+
### `on` method
|
|
29
|
+
Used to attach middleware to an event. If the event name previously had middleware attached, it will add to the middleware stack.
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
const eventManager = new EventManager();
|
|
33
|
+
|
|
34
|
+
eventManager.on('interestingEvent', async (eventData, next) => {
|
|
35
|
+
// do something with the eventData
|
|
36
|
+
|
|
37
|
+
// pass control to the next middleware attached to the event
|
|
38
|
+
await next();
|
|
39
|
+
|
|
40
|
+
// do something after other middleware has fired
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
If a middleware returns `false` the entire middleware flow is interrupted and any remaining middleware is not executed.
|
|
45
|
+
|
|
46
|
+
### `fire` method
|
|
47
|
+
Invoke custom event. Data passed into the second parameter gets handed off to the middleware attached with the `on` method.
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
eventManager.fire('interestingEvent', { data: { some: 'string' } });
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Middleware
|
|
54
|
+
Middleware provide a way for mutating or modifying the data passed in during the `fire` method.
|
|
55
|
+
|
|
56
|
+
### Execution order
|
|
57
|
+
The first middleware attached with the `on` method is the first to execute. When calling `await next()`, control flows to the next attached middleware. This happens until the final middleware has been called after which control flows back up to the first middleware attached. The first middleware gets the first, and last opportunity to modify the data.
|
|
58
|
+
|
|
59
|
+
### Order Flow Example
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
eventManager.on('interestingEvent', async (data, next) => {
|
|
63
|
+
console.log('first middleware start');
|
|
64
|
+
await next();
|
|
65
|
+
console.log('first middleware end');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
eventManager.on('interestingEvent', async (data, next) => {
|
|
69
|
+
console.log('second middleware start');
|
|
70
|
+
await next();
|
|
71
|
+
console.log('second middleware end');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
eventManager.on('interestingEvent', async (data, next) => {
|
|
75
|
+
console.log('third middleware start');
|
|
76
|
+
await next();
|
|
77
|
+
console.log('third middleware end');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
eventManager.fire('interestingEvent', { data: { some: 'string' } } );
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
After firing the `interestingEvent` event, the following would be displayed in the console:
|
|
85
|
+
```text
|
|
86
|
+
first middleware start
|
|
87
|
+
second middleware start
|
|
88
|
+
third middleware start
|
|
89
|
+
third middleware end
|
|
90
|
+
second middleware end
|
|
91
|
+
first middleware end
|
|
92
|
+
```
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Middleware } from './types';
|
|
2
|
+
import { MiddlewareManager } from './MiddlewareManager';
|
|
3
|
+
export declare class EventManager {
|
|
4
|
+
events: Record<string, MiddlewareManager<any>>;
|
|
5
|
+
constructor();
|
|
6
|
+
fire<T>(event: string, context: T): Promise<void>;
|
|
7
|
+
on<T>(event: string, ...func: Middleware<T>[]): void;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=EventManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EventManager.d.ts","sourceRoot":"","sources":["../../src/EventManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD,qBAAa,YAAY;IACjB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,GAAG,CAAC,CAAC,CAAM;;IAMrD,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAQvD,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI;CAOpD"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.EventManager = void 0;
|
|
40
|
+
var MiddlewareManager_1 = require("./MiddlewareManager");
|
|
41
|
+
var EventManager = /** @class */ (function () {
|
|
42
|
+
function EventManager() {
|
|
43
|
+
this.events = {};
|
|
44
|
+
// noop
|
|
45
|
+
}
|
|
46
|
+
EventManager.prototype.fire = function (event, context) {
|
|
47
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
48
|
+
return __generator(this, function (_a) {
|
|
49
|
+
switch (_a.label) {
|
|
50
|
+
case 0:
|
|
51
|
+
if (!this.events[event]) return [3 /*break*/, 2];
|
|
52
|
+
return [4 /*yield*/, this.events[event].dispatch(context)];
|
|
53
|
+
case 1:
|
|
54
|
+
_a.sent();
|
|
55
|
+
_a.label = 2;
|
|
56
|
+
case 2: return [2 /*return*/, Promise.resolve()];
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
EventManager.prototype.on = function (event) {
|
|
62
|
+
var _a;
|
|
63
|
+
var func = [];
|
|
64
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
65
|
+
func[_i - 1] = arguments[_i];
|
|
66
|
+
}
|
|
67
|
+
if (!this.events[event]) {
|
|
68
|
+
this.events[event] = new MiddlewareManager_1.MiddlewareManager();
|
|
69
|
+
}
|
|
70
|
+
(_a = this.events[event]).use.apply(_a, func);
|
|
71
|
+
};
|
|
72
|
+
return EventManager;
|
|
73
|
+
}());
|
|
74
|
+
exports.EventManager = EventManager;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Middleware } from './types';
|
|
2
|
+
export declare class MiddlewareManager<T> {
|
|
3
|
+
functions: Middleware<T>[];
|
|
4
|
+
constructor();
|
|
5
|
+
use(...func: Middleware<T>[]): void;
|
|
6
|
+
remove(func: Middleware<T>): void;
|
|
7
|
+
clear(): void;
|
|
8
|
+
dispatch(context: T): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=MiddlewareManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MiddlewareManager.d.ts","sourceRoot":"","sources":["../../src/MiddlewareManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAY,MAAM,SAAS,CAAC;AAEpD,qBAAa,iBAAiB,CAAC,CAAC;IAC/B,SAAS,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC;;IAM3B,GAAG,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI;IAInC,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI;IAQjC,KAAK,IAAI,IAAI;IAIP,QAAQ,CAAC,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;CAOzC"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.MiddlewareManager = void 0;
|
|
40
|
+
var MiddlewareManager = /** @class */ (function () {
|
|
41
|
+
function MiddlewareManager() {
|
|
42
|
+
this.functions = [];
|
|
43
|
+
}
|
|
44
|
+
MiddlewareManager.prototype.use = function () {
|
|
45
|
+
var _a;
|
|
46
|
+
var func = [];
|
|
47
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
48
|
+
func[_i] = arguments[_i];
|
|
49
|
+
}
|
|
50
|
+
(_a = this.functions).push.apply(_a, func);
|
|
51
|
+
};
|
|
52
|
+
MiddlewareManager.prototype.remove = function (func) {
|
|
53
|
+
var stringyFunc = func.toString();
|
|
54
|
+
this.functions = this.functions.filter(function (func) {
|
|
55
|
+
return func.toString() != stringyFunc;
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
MiddlewareManager.prototype.clear = function () {
|
|
59
|
+
this.functions = [];
|
|
60
|
+
};
|
|
61
|
+
MiddlewareManager.prototype.dispatch = function (context) {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
63
|
+
var cancelling;
|
|
64
|
+
return __generator(this, function (_a) {
|
|
65
|
+
switch (_a.label) {
|
|
66
|
+
case 0: return [4 /*yield*/, runFunctionsWithAbortWrapper(context, this.functions)];
|
|
67
|
+
case 1:
|
|
68
|
+
cancelling = _a.sent();
|
|
69
|
+
if (cancelling == true) {
|
|
70
|
+
throw new Error('cancelled');
|
|
71
|
+
}
|
|
72
|
+
return [2 /*return*/];
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
return MiddlewareManager;
|
|
78
|
+
}());
|
|
79
|
+
exports.MiddlewareManager = MiddlewareManager;
|
|
80
|
+
function runFunctionsWithAbortWrapper(context, functions) {
|
|
81
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
82
|
+
var cancelled;
|
|
83
|
+
return __generator(this, function (_a) {
|
|
84
|
+
switch (_a.label) {
|
|
85
|
+
case 0:
|
|
86
|
+
cancelled = false;
|
|
87
|
+
return [4 /*yield*/, runFunctions(context, functions, function (proceed) {
|
|
88
|
+
if (proceed === false)
|
|
89
|
+
cancelled = true;
|
|
90
|
+
})];
|
|
91
|
+
case 1:
|
|
92
|
+
_a.sent();
|
|
93
|
+
return [2 /*return*/, cancelled];
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
function runFunctions(context, functions, callback) {
|
|
99
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
100
|
+
var func, proceed;
|
|
101
|
+
var _this = this;
|
|
102
|
+
return __generator(this, function (_a) {
|
|
103
|
+
switch (_a.label) {
|
|
104
|
+
case 0:
|
|
105
|
+
if (!functions.length) {
|
|
106
|
+
return [2 /*return*/];
|
|
107
|
+
}
|
|
108
|
+
func = functions[0];
|
|
109
|
+
return [4 /*yield*/, func(context, function () { return __awaiter(_this, void 0, void 0, function () {
|
|
110
|
+
return __generator(this, function (_a) {
|
|
111
|
+
switch (_a.label) {
|
|
112
|
+
case 0: return [4 /*yield*/, runFunctions(context, functions.slice(1), callback)];
|
|
113
|
+
case 1:
|
|
114
|
+
_a.sent();
|
|
115
|
+
return [2 /*return*/];
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}); })];
|
|
119
|
+
case 1:
|
|
120
|
+
proceed = _a.sent();
|
|
121
|
+
callback(proceed);
|
|
122
|
+
return [2 /*return*/];
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.EventManager = void 0;
|
|
14
|
+
var EventManager_1 = require("./EventManager");
|
|
15
|
+
Object.defineProperty(exports, "EventManager", { enumerable: true, get: function () { return EventManager_1.EventManager; } });
|
|
16
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,oBAAY,IAAI,GAAG,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAE9C,oBAAY,UAAU,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,GAAG,OAAO,GAAG,IAAI,CAAC;AAEjG,oBAAY,QAAQ,GAAG,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI,KAAK,IAAI,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Middleware } from './types';
|
|
2
|
+
import { MiddlewareManager } from './MiddlewareManager';
|
|
3
|
+
export declare class EventManager {
|
|
4
|
+
events: Record<string, MiddlewareManager<any>>;
|
|
5
|
+
constructor();
|
|
6
|
+
fire<T>(event: string, context: T): Promise<void>;
|
|
7
|
+
on<T>(event: string, ...func: Middleware<T>[]): void;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=EventManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EventManager.d.ts","sourceRoot":"","sources":["../../src/EventManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD,qBAAa,YAAY;IACjB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,GAAG,CAAC,CAAC,CAAM;;IAMrD,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAQvD,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI;CAOpD"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { MiddlewareManager } from './MiddlewareManager';
|
|
2
|
+
export class EventManager {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.events = {};
|
|
5
|
+
// noop
|
|
6
|
+
}
|
|
7
|
+
async fire(event, context) {
|
|
8
|
+
if (this.events[event]) {
|
|
9
|
+
await this.events[event].dispatch(context);
|
|
10
|
+
}
|
|
11
|
+
return Promise.resolve();
|
|
12
|
+
}
|
|
13
|
+
on(event, ...func) {
|
|
14
|
+
if (!this.events[event]) {
|
|
15
|
+
this.events[event] = new MiddlewareManager();
|
|
16
|
+
}
|
|
17
|
+
this.events[event].use(...func);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Middleware } from './types';
|
|
2
|
+
export declare class MiddlewareManager<T> {
|
|
3
|
+
functions: Middleware<T>[];
|
|
4
|
+
constructor();
|
|
5
|
+
use(...func: Middleware<T>[]): void;
|
|
6
|
+
remove(func: Middleware<T>): void;
|
|
7
|
+
clear(): void;
|
|
8
|
+
dispatch(context: T): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=MiddlewareManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MiddlewareManager.d.ts","sourceRoot":"","sources":["../../src/MiddlewareManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAY,MAAM,SAAS,CAAC;AAEpD,qBAAa,iBAAiB,CAAC,CAAC;IAC/B,SAAS,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC;;IAM3B,GAAG,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI;IAInC,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI;IAQjC,KAAK,IAAI,IAAI;IAIP,QAAQ,CAAC,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;CAOzC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export class MiddlewareManager {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.functions = [];
|
|
4
|
+
}
|
|
5
|
+
use(...func) {
|
|
6
|
+
this.functions.push(...func);
|
|
7
|
+
}
|
|
8
|
+
remove(func) {
|
|
9
|
+
const stringyFunc = func.toString();
|
|
10
|
+
this.functions = this.functions.filter((func) => {
|
|
11
|
+
return func.toString() != stringyFunc;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
clear() {
|
|
15
|
+
this.functions = [];
|
|
16
|
+
}
|
|
17
|
+
async dispatch(context) {
|
|
18
|
+
const cancelling = await runFunctionsWithAbortWrapper(context, this.functions);
|
|
19
|
+
if (cancelling == true) {
|
|
20
|
+
throw new Error('cancelled');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
async function runFunctionsWithAbortWrapper(context, functions) {
|
|
25
|
+
let cancelled = false;
|
|
26
|
+
await runFunctions(context, functions, (proceed) => {
|
|
27
|
+
if (proceed === false)
|
|
28
|
+
cancelled = true;
|
|
29
|
+
});
|
|
30
|
+
return cancelled;
|
|
31
|
+
}
|
|
32
|
+
async function runFunctions(context, functions, callback) {
|
|
33
|
+
if (!functions.length) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const func = functions[0];
|
|
37
|
+
const proceed = await func(context, async () => {
|
|
38
|
+
await runFunctions(context, functions.slice(1), callback);
|
|
39
|
+
});
|
|
40
|
+
callback(proceed);
|
|
41
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,oBAAY,IAAI,GAAG,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAE9C,oBAAY,UAAU,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,GAAG,OAAO,GAAG,IAAI,CAAC;AAEjG,oBAAY,QAAQ,GAAG,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI,KAAK,IAAI,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@searchspring/snap-event-manager",
|
|
3
|
+
"version": "0.20.0",
|
|
4
|
+
"description": "Snap Event Manager",
|
|
5
|
+
"main": "dist/cjs/index.js",
|
|
6
|
+
"module": "dist/esm/index.js",
|
|
7
|
+
"author": "Searchspring",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"repository": "https://github.com/searchspring/snap",
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "rm -rf ./dist && tsc && tsc -p tsconfig.cjs.json",
|
|
15
|
+
"build:docs": "typedoc --out docs src/index.ts",
|
|
16
|
+
"dev": "tsc --watch",
|
|
17
|
+
"format": "prettier --write 'src/**/*.{js,jsx,ts,tsx}'",
|
|
18
|
+
"lint": "eslint 'src/**/*.{js,jsx,ts,tsx}'",
|
|
19
|
+
"test": "jest --passWithNoTests",
|
|
20
|
+
"test:watch": "jest --watch"
|
|
21
|
+
},
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"files": [
|
|
24
|
+
"dist/**/*"
|
|
25
|
+
],
|
|
26
|
+
"gitHead": "122405b27b497c7bb6a189c30535fdc197bc3ef0"
|
|
27
|
+
}
|