@servicetitan/web-components 30.1.0 → 30.1.1
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/contexts/mfe-data-context.d.ts +8 -2
- package/dist/contexts/mfe-data-context.d.ts.map +1 -1
- package/dist/contexts/mfe-data-context.js +1 -1
- package/dist/contexts/mfe-data-context.js.map +1 -1
- package/dist/event-bus.d.ts +7 -5
- package/dist/event-bus.d.ts.map +1 -1
- package/dist/event-bus.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/types.d.ts +4 -4
- package/dist/types.d.ts.map +1 -1
- package/package.json +11 -11
- package/src/__tests__/event-bus-types.test.ts +125 -13
- package/src/contexts/mfe-data-context.ts +18 -6
- package/src/event-bus.ts +29 -20
- package/src/index.ts +2 -0
- package/src/types.ts +6 -7
|
@@ -1,8 +1,14 @@
|
|
|
1
|
+
type JSONPrimitive = string | number | boolean | null | undefined;
|
|
2
|
+
type JSON = {
|
|
3
|
+
[key: string]: JSONPrimitive | JSON[] | JSON;
|
|
4
|
+
};
|
|
5
|
+
export type MFEContextData<T> = {
|
|
6
|
+
[P in keyof T]: T[P] extends JSONPrimitive ? T[P] : T[P] extends (infer U)[] ? MFEContextData<U>[] : T[P] extends JSON ? MFEContextData<T[P]> : never;
|
|
7
|
+
};
|
|
1
8
|
/**
|
|
2
9
|
* Context used to pass data from the host to the MFEs
|
|
3
10
|
*/
|
|
4
|
-
import { JSON, Serializable } from '../types';
|
|
5
|
-
export type MFEContextData<T> = Serializable<T>;
|
|
6
11
|
export declare const MFEDataContext: import("react").Context<JSON>;
|
|
7
12
|
export declare function useMFEDataContext<T extends MFEContextData<T>>(): T;
|
|
13
|
+
export {};
|
|
8
14
|
//# sourceMappingURL=mfe-data-context.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mfe-data-context.d.ts","sourceRoot":"","sources":["../../src/contexts/mfe-data-context.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mfe-data-context.d.ts","sourceRoot":"","sources":["../../src/contexts/mfe-data-context.ts"],"names":[],"mappings":"AAEA,KAAK,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;AAGlE,KAAK,IAAI,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,aAAa,GAAG,IAAI,EAAE,GAAG,IAAI,CAAA;CAAE,CAAC;AAG7D,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI;KAC3B,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,aAAa,GACpC,CAAC,CAAC,CAAC,CAAC,GACJ,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GACtB,cAAc,CAAC,CAAC,CAAC,EAAE,GACnB,CAAC,CAAC,CAAC,CAAC,SAAS,IAAI,GACf,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GACpB,KAAK;CAClB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,+BAA0B,CAAC;AAEtD,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,cAAc,CAAC,CAAC,CAAC,OAI5D"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
1
2
|
/**
|
|
2
3
|
* Context used to pass data from the host to the MFEs
|
|
3
4
|
*/
|
|
4
|
-
import { createContext, useContext } from 'react';
|
|
5
5
|
export const MFEDataContext = createContext({});
|
|
6
6
|
export function useMFEDataContext() {
|
|
7
7
|
const context = useContext(MFEDataContext);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mfe-data-context.js","sourceRoot":"","sources":["../../src/contexts/mfe-data-context.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"mfe-data-context.js","sourceRoot":"","sources":["../../src/contexts/mfe-data-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAkBlD;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,aAAa,CAAO,EAAE,CAAC,CAAC;AAEtD,MAAM,UAAU,iBAAiB;IAC7B,MAAM,OAAO,GAAG,UAAU,CAAC,cAAc,CAAM,CAAC;IAEhD,OAAO,OAAO,CAAC;AACnB,CAAC"}
|
package/dist/event-bus.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { SymbolToken } from '@servicetitan/react-ioc';
|
|
2
|
-
import { JSON,
|
|
2
|
+
import { JSON, Serializable } from './types';
|
|
3
3
|
export declare const EVENT_BUS_TOKEN: SymbolToken<EventBus<never>>;
|
|
4
4
|
export declare function typedEventBusToken<EventTypes extends Serializable<EventTypes>>(): SymbolToken<EventBus<EventTypes>>;
|
|
5
|
-
export type EventHandler<EventTypes, Event> = (data: Event extends SymbolToken<infer DataType> ? Serializable<DataType> : Event extends keyof EventTypes ? EventTypes[Event] :
|
|
5
|
+
export type EventHandler<EventTypes, Event, T = never> = (data: [T] extends [never] ? Event extends SymbolToken<infer DataType> ? Serializable<DataType> : Event extends keyof EventTypes ? EventTypes[Event] : T : Serializable<T>) => void;
|
|
6
6
|
type JobPayload = JSON | void;
|
|
7
|
+
export type EventType = string | number | symbol;
|
|
8
|
+
export type SymbolTokenEventType<T, U extends EventType = EventType> = U extends SymbolToken<any> ? SymbolToken<T> : U;
|
|
7
9
|
export type JobEventHandler<Event> = (defer: {
|
|
8
10
|
/**
|
|
9
11
|
* Successfully complete a job.
|
|
@@ -43,19 +45,19 @@ export declare class EventBus<EventTypes extends Serializable<EventTypes> = neve
|
|
|
43
45
|
* @param event The event to handle.
|
|
44
46
|
* @param handler The handler to call when the event occurs.
|
|
45
47
|
*/
|
|
46
|
-
on: <Event extends SymbolToken<
|
|
48
|
+
on: <T extends Serializable<T> = never, Event extends SymbolToken<T> | keyof EventTypes = any>(event: SymbolTokenEventType<T, Event>, handler: EventHandler<EventTypes, Event, T>) => void;
|
|
47
49
|
/**
|
|
48
50
|
* Deregister an event handler.
|
|
49
51
|
* @param event The previously registered event.
|
|
50
52
|
* @param handler The previously registered handler.
|
|
51
53
|
*/
|
|
52
|
-
off: <Event extends SymbolToken<
|
|
54
|
+
off: <T extends Serializable<T> = never, Event extends SymbolToken<T> | keyof EventTypes = any>(event: SymbolTokenEventType<T, Event>, handler: EventHandler<EventTypes, Event, T>) => void;
|
|
53
55
|
/**
|
|
54
56
|
* Send an event.
|
|
55
57
|
* @param event The event to send.
|
|
56
58
|
* @param data The data to pass to the event's handler.
|
|
57
59
|
*/
|
|
58
|
-
emit: <Event extends SymbolToken<
|
|
60
|
+
emit: <T extends Serializable<T>, Event extends SymbolToken<T> | keyof EventTypes = any>(event: SymbolTokenEventType<T, Event>, ...data: Event extends SymbolToken<any> ? undefined extends T ? [T?] : [T] : [EventTypes] extends [never] ? [T?] : Event extends keyof EventTypes ? undefined extends EventTypes[Event] ? [EventTypes[Event]?] : [EventTypes[Event]] extends [never] ? [] : [EventTypes[Event]] : never) => void;
|
|
59
61
|
}
|
|
60
62
|
export {};
|
|
61
63
|
//# sourceMappingURL=event-bus.d.ts.map
|
package/dist/event-bus.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event-bus.d.ts","sourceRoot":"","sources":["../src/event-bus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAGnE,OAAO,EAAE,IAAI,EAAE,
|
|
1
|
+
{"version":3,"file":"event-bus.d.ts","sourceRoot":"","sources":["../src/event-bus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAGnE,OAAO,EAAE,IAAI,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE7C,eAAO,MAAM,eAAe,8BAA2C,CAAC;AAExE,wBA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AK,CAAC,GAC1B,CAAC,CAAC,CAAC,CAAC,GACJ,KAAK,SAAS,MAAM,UAAU,GAC5B,SAAS,SAAS,UAAU,CAAC,KAAK,CAAC,GAC/B,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,GACpB,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GACjC,EAAE,GACF,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,GACzB,KAAK,UAGjB;CACL"}
|
package/dist/event-bus.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event-bus.js","sourceRoot":"","sources":["../src/event-bus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAe,MAAM,yBAAyB,CAAC;AACnE,OAAO,IAAI,MAAM,MAAM,CAAC;AAIxB,MAAM,CAAC,MAAM,eAAe,GAAG,WAAW,CAAW,iBAAiB,CAAC,CAAC;AAExE,MAAM,UAAU,kBAAkB;IAC9B,OAAO,WAAW,CAAuB,iBAAiB,CAAC,CAAC;AAChE,CAAC;
|
|
1
|
+
{"version":3,"file":"event-bus.js","sourceRoot":"","sources":["../src/event-bus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAe,MAAM,yBAAyB,CAAC;AACnE,OAAO,IAAI,MAAM,MAAM,CAAC;AAIxB,MAAM,CAAC,MAAM,eAAe,GAAG,WAAW,CAAW,iBAAiB,CAAC,CAAC;AAExE,MAAM,UAAU,kBAAkB;IAC9B,OAAO,WAAW,CAAuB,iBAAiB,CAAC,CAAC;AAChE,CAAC;AAqCD,MAAM,OAAO,QAAQ;IAArB;QACI;;;;mBAAM;gBACF;;;;mBAIG;gBACH,EAAE,EAAE,CACA,KAAY,EACZ,OAA+B,EACjC,EAAE;oBACA,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;gBACpC,CAAC;gBAED;;;;mBAIG;gBACH,GAAG,EAAE,C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+1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mBAAmB,CAAC;AAClC,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mBAAmB,CAAC;AAClC,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAE3B,YAAY,EAAE,IAAI,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export type JSONPrimitive = string | number | boolean | null | undefined;
|
|
2
|
-
export type JSON = {
|
|
3
|
-
[key: string
|
|
4
|
-
};
|
|
2
|
+
export type JSON = JSONPrimitive | {
|
|
3
|
+
[key: string | number]: JSON;
|
|
4
|
+
} | JSON[];
|
|
5
5
|
export type Serializable<T> = {
|
|
6
|
-
[P in keyof T]: T[P] extends JSONPrimitive ? T[P] : T[P] extends
|
|
6
|
+
[P in keyof T]: T[P] extends JSONPrimitive ? T[P] : T[P] extends Function | Date | Map<any, any> | Set<any> | symbol ? never : T[P] extends (infer U)[] ? Serializable<U>[] : Serializable<T[P]>;
|
|
7
7
|
};
|
|
8
8
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;AAEzE,MAAM,MAAM,IAAI,GAAG,aAAa,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;CAAE,GAAG,IAAI,EAAE,CAAC;AAE7E,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI;KACzB,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,aAAa,GACpC,CAAC,CAAC,CAAC,CAAC,GACJ,CAAC,CAAC,CAAC,CAAC,SAAS,QAAQ,GAAG,IAAI,GAAG,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,MAAM,GAC9D,KAAK,GACL,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GACtB,YAAY,CAAC,CAAC,CAAC,EAAE,GACjB,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAC/B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@servicetitan/web-components",
|
|
3
|
-
"version": "30.1.
|
|
3
|
+
"version": "30.1.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -15,17 +15,17 @@
|
|
|
15
15
|
"src"
|
|
16
16
|
],
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@servicetitan/error-boundary": "30.1.
|
|
18
|
+
"@servicetitan/error-boundary": "30.1.1",
|
|
19
19
|
"lodash.get": "~4.4.2",
|
|
20
20
|
"mitt": "~3.0.1",
|
|
21
21
|
"semver": "~7.7.1"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@servicetitan/anvil2": "^1.31.
|
|
24
|
+
"@servicetitan/anvil2": "^1.31.1",
|
|
25
25
|
"@servicetitan/design-system": "~14.5.2",
|
|
26
|
-
"@servicetitan/launchdarkly-service": "30.1.
|
|
27
|
-
"@servicetitan/log-service": "30.1.
|
|
28
|
-
"@servicetitan/react-ioc": "30.1.
|
|
26
|
+
"@servicetitan/launchdarkly-service": "30.1.1",
|
|
27
|
+
"@servicetitan/log-service": "30.1.1",
|
|
28
|
+
"@servicetitan/react-ioc": "30.1.1",
|
|
29
29
|
"@testing-library/dom": "^10.4.0",
|
|
30
30
|
"@testing-library/jest-dom": "^6.6.3",
|
|
31
31
|
"@testing-library/react": "^16.3.0",
|
|
@@ -41,11 +41,11 @@
|
|
|
41
41
|
"shadow-dom-testing-library": "^1.12.0"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@servicetitan/anvil2": ">=1.31.
|
|
44
|
+
"@servicetitan/anvil2": ">=1.31.1",
|
|
45
45
|
"@servicetitan/design-system": ">=14.5.2",
|
|
46
|
-
"@servicetitan/launchdarkly-service": "30.1.
|
|
47
|
-
"@servicetitan/log-service": "30.1.
|
|
48
|
-
"@servicetitan/react-ioc": "30.1.
|
|
46
|
+
"@servicetitan/launchdarkly-service": "30.1.1",
|
|
47
|
+
"@servicetitan/log-service": "30.1.1",
|
|
48
|
+
"@servicetitan/react-ioc": "30.1.1",
|
|
49
49
|
"history": "~4.10.1",
|
|
50
50
|
"react": ">=17.0.2",
|
|
51
51
|
"react-dom": ">=17.0.2"
|
|
@@ -67,5 +67,5 @@
|
|
|
67
67
|
"cli": {
|
|
68
68
|
"webpack": false
|
|
69
69
|
},
|
|
70
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "02382f349ee120e1b30d9676484481f93059c054"
|
|
71
71
|
}
|
|
@@ -20,6 +20,10 @@ jest.mock('../event-bus', () => {
|
|
|
20
20
|
|
|
21
21
|
describe(`[web-components] ${EventBus.name}`, () => {
|
|
22
22
|
describe('type definitions', () => {
|
|
23
|
+
interface InterfaceDataType {
|
|
24
|
+
value: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
23
27
|
describe('EventBus<T>', () => {
|
|
24
28
|
test('rejects invalid data type', () => {
|
|
25
29
|
// @ts-expect-error Types of property 'foo' are incompatible.
|
|
@@ -33,15 +37,27 @@ describe(`[web-components] ${EventBus.name}`, () => {
|
|
|
33
37
|
describe('emit', () => {
|
|
34
38
|
test('accepts valid event with valid data', () => {
|
|
35
39
|
eventBus.emit('foo', '');
|
|
40
|
+
eventBus.emit('foo', 123);
|
|
41
|
+
eventBus.emit('foo', true);
|
|
36
42
|
eventBus.emit('bar', { value: 1 });
|
|
37
|
-
eventBus.emit(
|
|
43
|
+
eventBus.emit('foo', ['']);
|
|
44
|
+
eventBus.emit('foo', [123]);
|
|
45
|
+
eventBus.emit('foo', [true]);
|
|
46
|
+
eventBus.emit('bar', [{ value: 1 }]);
|
|
47
|
+
eventBus.emit('bar', [{ value: [1] }]);
|
|
38
48
|
eventBus.emit(123, '');
|
|
49
|
+
eventBus.emit(Symbol(), '');
|
|
39
50
|
});
|
|
40
51
|
|
|
41
52
|
test('accepts event with no data', () => {
|
|
42
53
|
eventBus.emit('foo');
|
|
43
54
|
});
|
|
44
55
|
|
|
56
|
+
test('accepts data declared as interface', () => {
|
|
57
|
+
const data: InterfaceDataType = { value: '' };
|
|
58
|
+
eventBus.emit('foo', data);
|
|
59
|
+
});
|
|
60
|
+
|
|
45
61
|
test('rejects invalid event', () => {
|
|
46
62
|
// @ts-expect-error Argument of type '() => void' is not assignable to parameter of type 'string | number | symbol | SymbolToken<any>'.
|
|
47
63
|
eventBus.emit(() => {}, '');
|
|
@@ -54,17 +70,26 @@ describe(`[web-components] ${EventBus.name}`, () => {
|
|
|
54
70
|
});
|
|
55
71
|
|
|
56
72
|
test('rejects invalid data', () => {
|
|
57
|
-
|
|
58
|
-
|
|
73
|
+
/*
|
|
74
|
+
* Note: This case isn't caught
|
|
75
|
+
* @ts-expect-error Argument of type '() => void' is not assignable to parameter of type 'JSON | JSONPrimitive'
|
|
76
|
+
* eventBus.emit('foo', () => {});
|
|
77
|
+
*/
|
|
59
78
|
|
|
60
|
-
// @ts-expect-error
|
|
61
|
-
eventBus.emit('foo',
|
|
79
|
+
// @ts-expect-error Type '() => void' is not assignable to type 'never'.
|
|
80
|
+
eventBus.emit('foo', { fn: () => {} });
|
|
62
81
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
});
|
|
82
|
+
// @ts-expect-error Type 'Date' is not assignable to type 'never'.
|
|
83
|
+
eventBus.emit('foo', { date: new Date() });
|
|
84
|
+
|
|
85
|
+
// @ts-expect-error Type 'Map<string, any>' is not assignable to type 'never'.
|
|
86
|
+
eventBus.emit('foo', { map: new Map<string, any>() });
|
|
87
|
+
|
|
88
|
+
// @ts-expect-error Type 'Set<string>' is not assignable to type 'never'.
|
|
89
|
+
eventBus.emit('foo', { set: new Set<string>() });
|
|
90
|
+
|
|
91
|
+
// @ts-expect-error Type 'symbol' is not assignable to type 'never'.
|
|
92
|
+
eventBus.emit('foo', { symbol: Symbol() });
|
|
68
93
|
});
|
|
69
94
|
});
|
|
70
95
|
|
|
@@ -157,6 +182,7 @@ describe(`[web-components] ${EventBus.name}`, () => {
|
|
|
157
182
|
|
|
158
183
|
describe('when event is symbolToken', () => {
|
|
159
184
|
const token = symbolToken<{ value: number }>('token');
|
|
185
|
+
const untypedToken = symbolToken('untyped-token');
|
|
160
186
|
const optionalPayloadToken = symbolToken<{ value: number } | undefined>(
|
|
161
187
|
'optionalToken'
|
|
162
188
|
);
|
|
@@ -166,6 +192,7 @@ describe(`[web-components] ${EventBus.name}`, () => {
|
|
|
166
192
|
eventBus.emit(token, { value: 1 });
|
|
167
193
|
eventBus.emit(optionalPayloadToken, { value: 1 });
|
|
168
194
|
eventBus.emit(optionalPayloadToken);
|
|
195
|
+
eventBus.emit(untypedToken);
|
|
169
196
|
});
|
|
170
197
|
|
|
171
198
|
test('rejects invalid data', () => {
|
|
@@ -190,6 +217,7 @@ describe(`[web-components] ${EventBus.name}`, () => {
|
|
|
190
217
|
test('accepts valid handler', () => {
|
|
191
218
|
eventBus.on(token, () => {});
|
|
192
219
|
eventBus.on(token, data => data.value++);
|
|
220
|
+
eventBus.on(untypedToken, data => data.value++);
|
|
193
221
|
});
|
|
194
222
|
|
|
195
223
|
test('rejects invalid handler', () => {
|
|
@@ -264,13 +292,95 @@ describe(`[web-components] ${EventBus.name}`, () => {
|
|
|
264
292
|
});
|
|
265
293
|
});
|
|
266
294
|
});
|
|
295
|
+
|
|
296
|
+
describe('with legacy type declaration', () => {
|
|
297
|
+
const numberToken = symbolToken<number>('number-token');
|
|
298
|
+
const untypedToken = symbolToken('untyped-token');
|
|
299
|
+
|
|
300
|
+
describe('emit', () => {
|
|
301
|
+
test('accepts valid event with valid data', () => {
|
|
302
|
+
eventBus.emit<number>('foo', 123);
|
|
303
|
+
eventBus.emit<number>(numberToken, 123);
|
|
304
|
+
eventBus.emit<number>(untypedToken, 123);
|
|
305
|
+
eventBus.emit<InterfaceDataType>('foo', { value: '' });
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
test('rejects invalid data type', () => {
|
|
309
|
+
// @ts-expect-error Type 'Function' does not satisfy the constraint 'Serializable<Function>'.
|
|
310
|
+
eventBus.emit<Function>('foo', () => {});
|
|
311
|
+
// @ts-expect-error Type '{ date: Date; }' does not satisfy the constraint 'Serializable<{ date: Date; }>'.
|
|
312
|
+
eventBus.emit<{ date: Date }>('foo', { date: new Date() });
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
test('rejects invalid data', () => {
|
|
316
|
+
// @ts-expect-error Type 'string' is not assignable to type 'number'.
|
|
317
|
+
eventBus.emit<number>('foo', '');
|
|
318
|
+
// @ts-expect-error Type 'string' is not assignable to type 'number'.
|
|
319
|
+
eventBus.emit<number>(numberToken, '');
|
|
320
|
+
// @ts-expect-error Type 'string' is not assignable to type 'number'.
|
|
321
|
+
eventBus.emit<number>(untypedToken, '');
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
describe('on', () => {
|
|
326
|
+
test('accepts valid handler', () => {
|
|
327
|
+
eventBus.on<number>('foo', (_: number) => {});
|
|
328
|
+
eventBus.on<number>(numberToken, (_: number) => {});
|
|
329
|
+
eventBus.on<number>(untypedToken, (_: number) => {});
|
|
330
|
+
eventBus.on<InterfaceDataType>('foo', (_: InterfaceDataType) => {});
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
test('rejects invalid data type', () => {
|
|
334
|
+
// @ts-expect-error Type 'Function' does not satisfy the constraint 'Serializable<Function>'.
|
|
335
|
+
eventBus.on<Function>('foo', () => {});
|
|
336
|
+
// @ts-expect-error Type 'Function' does not satisfy the constraint 'Serializable<Function>'.
|
|
337
|
+
eventBus.on<{ date: Date }>('foo', () => {});
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
test('rejects invalid handler', () => {
|
|
341
|
+
// @ts-expect-error Argument of type '(_: string) => void' is not assignable to parameter of type ...
|
|
342
|
+
eventBus.on<number>('foo', (_: string) => {});
|
|
343
|
+
// @ts-expect-error Argument of type '(_: string) => void' is not assignable to parameter of type ...
|
|
344
|
+
eventBus.on<number>(numberToken, (_: string) => {});
|
|
345
|
+
// @ts-expect-error Argument of type '(_: string) => void' is not assignable to parameter of type ...
|
|
346
|
+
eventBus.on<number>(untypedToken, (_: string) => {});
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
describe('off', () => {
|
|
351
|
+
test('accepts valid handler', () => {
|
|
352
|
+
eventBus.off<number>('foo', (_: number) => {});
|
|
353
|
+
eventBus.off<number>(numberToken, (_: number) => {});
|
|
354
|
+
eventBus.off<number>(untypedToken, (_: number) => {});
|
|
355
|
+
eventBus.off<InterfaceDataType>('foo', (_: InterfaceDataType) => {});
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
test('rejects invalid data type', () => {
|
|
359
|
+
// @ts-expect-error Type 'Function' does not satisfy the constraint 'Serializable<Function>'.
|
|
360
|
+
eventBus.off<Function>('foo', () => {});
|
|
361
|
+
// @ts-expect-error Type 'Function' does not satisfy the constraint 'Serializable<Function>'.
|
|
362
|
+
eventBus.off<{ date: Date }>('foo', () => {});
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
test('rejects invalid handler', () => {
|
|
366
|
+
// @ts-expect-error Argument of type '(_: number) => void' is not assignable to parameter of type ...
|
|
367
|
+
eventBus.off<string>('foo', (_: number) => {});
|
|
368
|
+
// @ts-expect-error Argument of type '(_: string) => void' is not assignable to parameter of type ...
|
|
369
|
+
eventBus.off<number>(numberToken, (_: string) => {});
|
|
370
|
+
// @ts-expect-error Argument of type '(_: string) => void' is not assignable to parameter of type ...
|
|
371
|
+
eventBus.off<number>(untypedToken, (_: string) => {});
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
});
|
|
267
375
|
});
|
|
268
376
|
|
|
269
377
|
describe('when eventBus is typed', () => {
|
|
270
378
|
interface TestEvents {
|
|
271
379
|
'number-event': { value: number };
|
|
272
380
|
'string-event': { value: string };
|
|
273
|
-
'
|
|
381
|
+
'array-event': [string, { value: string }];
|
|
382
|
+
'optional-data-event': { value: number } | undefined;
|
|
383
|
+
'interface-data-event': InterfaceDataType;
|
|
274
384
|
}
|
|
275
385
|
|
|
276
386
|
const eventBus = new EventBus<TestEvents>();
|
|
@@ -279,8 +389,10 @@ describe(`[web-components] ${EventBus.name}`, () => {
|
|
|
279
389
|
test('accepts valid events and data', () => {
|
|
280
390
|
eventBus.emit('number-event', { value: 1 });
|
|
281
391
|
eventBus.emit('string-event', { value: '' });
|
|
282
|
-
eventBus.emit('optional-
|
|
283
|
-
eventBus.emit('optional-
|
|
392
|
+
eventBus.emit('optional-data-event', { value: 1 });
|
|
393
|
+
eventBus.emit('optional-data-event');
|
|
394
|
+
const data: InterfaceDataType = { value: '' };
|
|
395
|
+
eventBus.emit('interface-data-event', data);
|
|
284
396
|
});
|
|
285
397
|
|
|
286
398
|
test('rejects invalid event', () => {
|
|
@@ -1,12 +1,24 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Context used to pass data from the host to the MFEs
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
1
|
import { createContext, useContext } from 'react';
|
|
6
|
-
import { JSON, Serializable } from '../types';
|
|
7
2
|
|
|
8
|
-
|
|
3
|
+
type JSONPrimitive = string | number | boolean | null | undefined;
|
|
4
|
+
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
6
|
+
type JSON = { [key: string]: JSONPrimitive | JSON[] | JSON };
|
|
9
7
|
|
|
8
|
+
// Type complexity below is required to support Interfaces being used
|
|
9
|
+
export type MFEContextData<T> = {
|
|
10
|
+
[P in keyof T]: T[P] extends JSONPrimitive
|
|
11
|
+
? T[P]
|
|
12
|
+
: T[P] extends (infer U)[]
|
|
13
|
+
? MFEContextData<U>[]
|
|
14
|
+
: T[P] extends JSON
|
|
15
|
+
? MFEContextData<T[P]>
|
|
16
|
+
: never;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Context used to pass data from the host to the MFEs
|
|
21
|
+
*/
|
|
10
22
|
export const MFEDataContext = createContext<JSON>({});
|
|
11
23
|
|
|
12
24
|
export function useMFEDataContext<T extends MFEContextData<T>>() {
|
package/src/event-bus.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { symbolToken, SymbolToken } from '@servicetitan/react-ioc';
|
|
2
2
|
import mitt from 'mitt';
|
|
3
3
|
|
|
4
|
-
import { JSON,
|
|
4
|
+
import { JSON, Serializable } from './types';
|
|
5
5
|
|
|
6
6
|
export const EVENT_BUS_TOKEN = symbolToken<EventBus>('EVENT_BUS_TOKEN');
|
|
7
7
|
|
|
@@ -9,16 +9,22 @@ export function typedEventBusToken<EventTypes extends Serializable<EventTypes>>(
|
|
|
9
9
|
return symbolToken<EventBus<EventTypes>>('EVENT_BUS_TOKEN');
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
export type EventHandler<EventTypes, Event> = (
|
|
13
|
-
data:
|
|
14
|
-
?
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
export type EventHandler<EventTypes, Event, T = never> = (
|
|
13
|
+
data: [T] extends [never]
|
|
14
|
+
? Event extends SymbolToken<infer DataType>
|
|
15
|
+
? Serializable<DataType>
|
|
16
|
+
: Event extends keyof EventTypes
|
|
17
|
+
? EventTypes[Event]
|
|
18
|
+
: T
|
|
19
|
+
: Serializable<T>
|
|
18
20
|
) => void;
|
|
19
21
|
|
|
20
22
|
type JobPayload = JSON | void;
|
|
21
23
|
|
|
24
|
+
export type EventType = string | number | symbol;
|
|
25
|
+
export type SymbolTokenEventType<T, U extends EventType = EventType> =
|
|
26
|
+
U extends SymbolToken<any> ? SymbolToken<T> : U;
|
|
27
|
+
|
|
22
28
|
export type JobEventHandler<Event> = (defer: {
|
|
23
29
|
/**
|
|
24
30
|
* Successfully complete a job.
|
|
@@ -94,9 +100,9 @@ export class EventBus<EventTypes extends Serializable<EventTypes> = never> {
|
|
|
94
100
|
* @param event The event to handle.
|
|
95
101
|
* @param handler The handler to call when the event occurs.
|
|
96
102
|
*/
|
|
97
|
-
on = <Event extends SymbolToken<
|
|
98
|
-
event: Event
|
|
99
|
-
handler: EventHandler<EventTypes, Event>
|
|
103
|
+
on = <T extends Serializable<T> = never, Event extends SymbolToken<T> | keyof EventTypes = any>(
|
|
104
|
+
event: SymbolTokenEventType<T, Event>,
|
|
105
|
+
handler: EventHandler<EventTypes, Event, T>
|
|
100
106
|
) => {
|
|
101
107
|
this.emitter.on(event, handler);
|
|
102
108
|
};
|
|
@@ -106,9 +112,12 @@ export class EventBus<EventTypes extends Serializable<EventTypes> = never> {
|
|
|
106
112
|
* @param event The previously registered event.
|
|
107
113
|
* @param handler The previously registered handler.
|
|
108
114
|
*/
|
|
109
|
-
off = <
|
|
110
|
-
|
|
111
|
-
|
|
115
|
+
off = <
|
|
116
|
+
T extends Serializable<T> = never,
|
|
117
|
+
Event extends SymbolToken<T> | keyof EventTypes = any,
|
|
118
|
+
>(
|
|
119
|
+
event: SymbolTokenEventType<T, Event>,
|
|
120
|
+
handler: EventHandler<EventTypes, Event, T>
|
|
112
121
|
) => {
|
|
113
122
|
this.emitter.off(event, handler);
|
|
114
123
|
};
|
|
@@ -118,14 +127,14 @@ export class EventBus<EventTypes extends Serializable<EventTypes> = never> {
|
|
|
118
127
|
* @param event The event to send.
|
|
119
128
|
* @param data The data to pass to the event's handler.
|
|
120
129
|
*/
|
|
121
|
-
emit = <Event extends SymbolToken<
|
|
122
|
-
event: Event
|
|
123
|
-
...data: Event extends SymbolToken<
|
|
124
|
-
? undefined extends
|
|
125
|
-
? [
|
|
126
|
-
: [
|
|
130
|
+
emit = <T extends Serializable<T>, Event extends SymbolToken<T> | keyof EventTypes = any>(
|
|
131
|
+
event: SymbolTokenEventType<T, Event>,
|
|
132
|
+
...data: Event extends SymbolToken<any>
|
|
133
|
+
? undefined extends T
|
|
134
|
+
? [T?]
|
|
135
|
+
: [T]
|
|
127
136
|
: [EventTypes] extends [never]
|
|
128
|
-
? [
|
|
137
|
+
? [T?]
|
|
129
138
|
: Event extends keyof EventTypes
|
|
130
139
|
? undefined extends EventTypes[Event]
|
|
131
140
|
? [EventTypes[Event]?]
|
package/src/index.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
export type JSONPrimitive = string | number | boolean | null | undefined;
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
export type JSON = { [key: string]: JSONPrimitive | JSON[] | JSON };
|
|
3
|
+
export type JSON = JSONPrimitive | { [key: string | number]: JSON } | JSON[];
|
|
5
4
|
|
|
6
5
|
export type Serializable<T> = {
|
|
7
6
|
[P in keyof T]: T[P] extends JSONPrimitive
|
|
8
7
|
? T[P]
|
|
9
|
-
: T[P] extends
|
|
10
|
-
?
|
|
11
|
-
: T[P] extends
|
|
12
|
-
? Serializable<
|
|
13
|
-
:
|
|
8
|
+
: T[P] extends Function | Date | Map<any, any> | Set<any> | symbol
|
|
9
|
+
? never
|
|
10
|
+
: T[P] extends (infer U)[]
|
|
11
|
+
? Serializable<U>[]
|
|
12
|
+
: Serializable<T[P]>;
|
|
14
13
|
};
|