@amazon/vinyl-util 3.0.1 → 3.2.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.
Files changed (55) hide show
  1. package/dist/browserTestUtil/index.js.map +2 -2
  2. package/dist/browserTestUtil/types/current/mock/dom/lib.dom.d.ts.map +1 -1
  3. package/dist/browserTestUtil/types/current/tsconfig.tsbuildinfo +1 -1
  4. package/dist/index.cjs +165 -178
  5. package/dist/index.cjs.map +4 -4
  6. package/dist/index.js +165 -178
  7. package/dist/index.js.map +4 -4
  8. package/dist/polyfill/index.js +35 -32
  9. package/dist/polyfill/index.js.map +3 -3
  10. package/dist/polyfill/tsconfig.tsbuildinfo +1 -1
  11. package/dist/test/benchmark.js.map +2 -2
  12. package/dist/testUtil/index.js +0 -36
  13. package/dist/testUtil/index.js.map +3 -3
  14. package/dist/testUtil/types/current/index.d.ts +0 -1
  15. package/dist/testUtil/types/current/index.d.ts.map +1 -1
  16. package/dist/testUtil/types/current/index.js +0 -1
  17. package/dist/testUtil/types/current/tsconfig.tsbuildinfo +1 -1
  18. package/dist/testUtil/types/ts3.7/index.d.ts +0 -1
  19. package/dist/testUtil/types/ts4.1/index.d.ts +0 -1
  20. package/dist/testUtil/types/ts4.7/index.d.ts +0 -1
  21. package/dist/types/current/index.d.ts +1 -1
  22. package/dist/types/current/index.d.ts.map +1 -1
  23. package/dist/types/current/index.js +1 -1
  24. package/dist/types/current/network/responseUtil.d.ts +21 -0
  25. package/dist/types/current/network/responseUtil.d.ts.map +1 -1
  26. package/dist/types/current/network/responseUtil.js +30 -0
  27. package/dist/types/current/tsconfig.tsbuildinfo +1 -1
  28. package/dist/types/current/util/async/never.d.ts.map +1 -1
  29. package/dist/types/current/util/type.d.ts.map +1 -1
  30. package/dist/types/current/util/webm/parseWebm.d.ts +43 -0
  31. package/dist/types/current/util/webm/parseWebm.d.ts.map +1 -0
  32. package/dist/types/current/util/webm/parseWebm.js +166 -0
  33. package/dist/types/ts3.7/index.d.ts +1 -1
  34. package/dist/types/ts3.7/network/responseUtil.d.ts +21 -0
  35. package/dist/types/ts3.7/util/webm/parseWebm.d.ts +43 -0
  36. package/dist/types/ts4.1/index.d.ts +1 -1
  37. package/dist/types/ts4.1/network/responseUtil.d.ts +21 -0
  38. package/dist/types/ts4.1/util/webm/parseWebm.d.ts +43 -0
  39. package/dist/types/ts4.7/index.d.ts +1 -1
  40. package/dist/types/ts4.7/network/responseUtil.d.ts +21 -0
  41. package/dist/types/ts4.7/util/webm/parseWebm.d.ts +43 -0
  42. package/dist/unit/types/current/tsconfig.tsbuildinfo +1 -1
  43. package/package.json +4 -4
  44. package/dist/testUtil/types/current/patch/addPatchTests.d.ts +0 -35
  45. package/dist/testUtil/types/current/patch/addPatchTests.d.ts.map +0 -1
  46. package/dist/testUtil/types/current/patch/addPatchTests.js +0 -54
  47. package/dist/testUtil/types/ts3.7/patch/addPatchTests.d.ts +0 -35
  48. package/dist/testUtil/types/ts4.1/patch/addPatchTests.d.ts +0 -35
  49. package/dist/testUtil/types/ts4.7/patch/addPatchTests.d.ts +0 -35
  50. package/dist/types/current/patch/patchTarget.d.ts +0 -114
  51. package/dist/types/current/patch/patchTarget.d.ts.map +0 -1
  52. package/dist/types/current/patch/patchTarget.js +0 -219
  53. package/dist/types/ts3.7/patch/patchTarget.d.ts +0 -114
  54. package/dist/types/ts4.1/patch/patchTarget.d.ts +0 -114
  55. package/dist/types/ts4.7/patch/patchTarget.d.ts +0 -114
@@ -1,54 +0,0 @@
1
- /*
2
- * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
- import { expectNothing } from '@amazon/vinyl-util/browserTestUtil';
6
- /**
7
- * Adds tests to ensure that a patch flag is enabled when the issue can be reproduced for the UA,
8
- * and when the patch is applied the issue is no longer reproducible.
9
- *
10
- * @param flagName The flag name, used in test descriptions.
11
- * @param description The condition which requires the patch to be enabled.
12
- * @param optionsProvider
13
- */
14
- export function addPatchTests(flagName, description, optionsProvider) {
15
- let options;
16
- beforeEach(() => {
17
- options = optionsProvider();
18
- });
19
- afterEach(() => {
20
- // null safety check required, if test is skipped, beforeEach is not called but
21
- // afterEach is.
22
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
23
- options?.patchedRef.dispose();
24
- });
25
- it('sets the default flag to true only if the issue can be reproduced', async () => {
26
- const canReproduce = await options.canReproduce(options.target);
27
- if (canReproduce) {
28
- if (!options.actualFlag)
29
- fail(`expected patch flag '${flagName}' to be true`);
30
- }
31
- else {
32
- if (options.actualFlag) {
33
- // The flag was true but the issue could not be reproduced.
34
- // Most issues being patched cannot be reproduced with 100% reliability.
35
- // Allow reproduction misses to be merely a warning instead of failing the test.
36
- const message = `flag '${flagName}' was true but issue not reproduced`;
37
- if (options.allowFalseNegative)
38
- console.warn(message);
39
- else
40
- fail(message);
41
- }
42
- }
43
- expectNothing();
44
- });
45
- describe(`when the ${flagName} is true`, () => {
46
- it(description, async () => {
47
- if (options.actualFlag) {
48
- if (await options.canReproduce(options.patchedRef.patched))
49
- fail('patch did not resolve issue');
50
- }
51
- expectNothing();
52
- });
53
- });
54
- }
@@ -1,35 +0,0 @@
1
- import { PatchedRef } from '@amazon/vinyl-util';
2
- export interface PatchTestOptions<T> {
3
- /**
4
- * True if the patch is enabled by default.
5
- */
6
- actualFlag: boolean | undefined;
7
- /**
8
- * The target, not patched.
9
- */
10
- target: T;
11
- /**
12
- * The patched target.
13
- */
14
- patchedRef: PatchedRef<T>;
15
- /**
16
- * Runs the scenario to reproduce the issue on the target.
17
- * @param target
18
- */
19
- canReproduce: (target: T) => Promise<boolean>;
20
- /**
21
- * If true and the tested flag is true but the issue cannot be reproduced, then only a
22
- * warning is logged, if false, then the test will be marked as a failure.
23
- */
24
- allowFalseNegative: boolean;
25
- }
26
- /**
27
- * Adds tests to ensure that a patch flag is enabled when the issue can be reproduced for the UA,
28
- * and when the patch is applied the issue is no longer reproducible.
29
- *
30
- * @param flagName The flag name, used in test descriptions.
31
- * @param description The condition which requires the patch to be enabled.
32
- * @param optionsProvider
33
- */
34
- export declare function addPatchTests<T>(flagName: string, description: string, optionsProvider: () => PatchTestOptions<T>): void;
35
- //# sourceMappingURL=addPatchTests.d.ts.map
@@ -1,35 +0,0 @@
1
- import type { PatchedRef } from '@amazon/vinyl-util';
2
- export interface PatchTestOptions<T> {
3
- /**
4
- * True if the patch is enabled by default.
5
- */
6
- actualFlag: boolean | undefined;
7
- /**
8
- * The target, not patched.
9
- */
10
- target: T;
11
- /**
12
- * The patched target.
13
- */
14
- patchedRef: PatchedRef<T>;
15
- /**
16
- * Runs the scenario to reproduce the issue on the target.
17
- * @param target
18
- */
19
- canReproduce: (target: T) => Promise<boolean>;
20
- /**
21
- * If true and the tested flag is true but the issue cannot be reproduced, then only a
22
- * warning is logged, if false, then the test will be marked as a failure.
23
- */
24
- allowFalseNegative: boolean;
25
- }
26
- /**
27
- * Adds tests to ensure that a patch flag is enabled when the issue can be reproduced for the UA,
28
- * and when the patch is applied the issue is no longer reproducible.
29
- *
30
- * @param flagName The flag name, used in test descriptions.
31
- * @param description The condition which requires the patch to be enabled.
32
- * @param optionsProvider
33
- */
34
- export declare function addPatchTests<T>(flagName: string, description: string, optionsProvider: () => PatchTestOptions<T>): void;
35
- //# sourceMappingURL=addPatchTests.d.ts.map
@@ -1,35 +0,0 @@
1
- import type { PatchedRef } from '@amazon/vinyl-util';
2
- export interface PatchTestOptions<T> {
3
- /**
4
- * True if the patch is enabled by default.
5
- */
6
- actualFlag: boolean | undefined;
7
- /**
8
- * The target, not patched.
9
- */
10
- target: T;
11
- /**
12
- * The patched target.
13
- */
14
- patchedRef: PatchedRef<T>;
15
- /**
16
- * Runs the scenario to reproduce the issue on the target.
17
- * @param target
18
- */
19
- canReproduce: (target: T) => Promise<boolean>;
20
- /**
21
- * If true and the tested flag is true but the issue cannot be reproduced, then only a
22
- * warning is logged, if false, then the test will be marked as a failure.
23
- */
24
- allowFalseNegative: boolean;
25
- }
26
- /**
27
- * Adds tests to ensure that a patch flag is enabled when the issue can be reproduced for the UA,
28
- * and when the patch is applied the issue is no longer reproducible.
29
- *
30
- * @param flagName The flag name, used in test descriptions.
31
- * @param description The condition which requires the patch to be enabled.
32
- * @param optionsProvider
33
- */
34
- export declare function addPatchTests<T>(flagName: string, description: string, optionsProvider: () => PatchTestOptions<T>): void;
35
- //# sourceMappingURL=addPatchTests.d.ts.map
@@ -1,114 +0,0 @@
1
- import { type Disposable } from '../core/disposable';
2
- import type { Unsubscribe } from '../core/Unsubscribe';
3
- import { type Signal } from '../event/Signal';
4
- import type { AnyRecord } from '../util/type';
5
- import { type LogTarget } from '../logging/LogTarget';
6
- export interface Patch<in T, in EventMap = AnyRecord> extends Disposable {
7
- /**
8
- * A signal that emits synthetic events.
9
- * These events will be dispatched on the proxy, not on the original event target.
10
- * If undefined, no events are fabricated.
11
- */
12
- readonly eventFabricated?: Signal<Event>;
13
- /**
14
- * Overridden properties.
15
- */
16
- readonly properties?: PropertyPatches<T>;
17
- /**
18
- * Overridden events.
19
- * Event patches may provide an alternate event to emit, or null to prevent an event from
20
- * dispatching on the proxy.
21
- *
22
- * Note:
23
- * Patched events have divergent behavior when using Event.stopImmediatePropagation.
24
- * All handlers added to the patched Proxy will still be called after stopImmediatePropagation.
25
- * It is the author's opinion that using stopImmediatePropagation is an anti-pattern, and one
26
- * handler should not be allowed to influence the behavior of neighboring handlers.
27
- */
28
- readonly events?: EventPatches<EventMap>;
29
- }
30
- /**
31
- * A record of property key to property patch.
32
- */
33
- export type PropertyPatches<in T> = {
34
- readonly [P in keyof T]?: PropertyPatch<T, P>;
35
- };
36
- /**
37
- * An override to a patched property.
38
- */
39
- export interface PropertyPatch<T, K extends keyof T> {
40
- /**
41
- * @return Returns the value to return on the proxy.
42
- */
43
- get?(): T[K];
44
- /**
45
- * @param newValue The property value attempting to be set.
46
- */
47
- set?(newValue: T[K]): void;
48
- }
49
- /**
50
- * Handles an event emitted from the event target. The returned event will be emitted in its
51
- * place on the proxy.
52
- *
53
- * Fabricated events from `eventFabricated` will not be passed to this handler and cannot be
54
- * squelched.
55
- *
56
- * @param event The original event from the event target. This should not be modified
57
- * directly, but may be cloned and a modified clone may be returned.
58
- *
59
- * @return Returns the event to emit on the proxy or null to squelch the event.
60
- */
61
- export type EventPatch<T> = (event: T) => T | null;
62
- /**
63
- * A record of event type to an event patch.
64
- */
65
- export type EventPatches<in EventMap> = {
66
- readonly [P in keyof EventMap]?: EventPatch<EventMap[P]>;
67
- };
68
- export type PatchFactory<T, EventMap> = (target: T) => Patch<T, EventMap>;
69
- export interface PatchedRef<T> extends Disposable {
70
- /**
71
- * The patched target.
72
- */
73
- readonly patched: T;
74
- /**
75
- * A signal for when a synthetic event was fabricated.
76
- */
77
- eventFabricated: Signal<Event>;
78
- /**
79
- * A signal for when an event was squelched.
80
- */
81
- eventSquelched: Signal<Event>;
82
- }
83
- /**
84
- * Creates a Proxy for patched event and properties on the target. All patches from the provided
85
- * factory list will be used in creating a proxy chain. Each patch is applied in order, and
86
- * patches can rely on their target having been patched with all previous patches.
87
- *
88
- * @param target
89
- * @param patchFactories A list of factories that each produce a new patch for the target.
90
- * @return Returns a disposable object with a Proxy reference with overridden event and property
91
- * behavior. When disposed, the event listeners are removed.
92
- */
93
- export declare function patchTarget<T extends object, EventMap = AnyRecord>(target: T, ...patchFactories: PatchFactory<T, EventMap>[]): PatchedRef<T>;
94
- /**
95
- * Logs debug messages when an event was squelched or fabricated.
96
- *
97
- * @param pathTarget
98
- * @param patched
99
- * @return Returns a function that when invoked, removes the logging.
100
- */
101
- export declare function logPatchedEvents(pathTarget: LogTarget, patched: PatchedRef<any>): Unsubscribe;
102
- /**
103
- * A tuple of `[flag, factory]`.
104
- * Patches will be applied when flag is true.
105
- */
106
- export type FlagAndPatchEntry<T extends object, EventMap, Key> = readonly [
107
- flagKey: Key,
108
- factory: PatchFactory<T, EventMap>
109
- ];
110
- /**
111
- * Given a list of [flagKey, patchFactory] tuples, patches the target with patches where flag is true.
112
- */
113
- export declare function patchTargetFromFlags<T extends object, EventMap, Flags>(target: T, flags: Flags, ...patchEntries: readonly FlagAndPatchEntry<T, EventMap, keyof Flags>[]): PatchedRef<T>;
114
- //# sourceMappingURL=patchTarget.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"patchTarget.d.ts","sourceRoot":"","sources":["../../../../src/patch/patchTarget.ts"],"names":[],"mappings":"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"}
@@ -1,219 +0,0 @@
1
- /*
2
- * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
- import { createDisposer, DisposedError, } from '../core/disposable';
6
- import { IllegalArgumentError } from '../error/IllegalArgumentError';
7
- import { EventHostImpl } from '../event/EventHost';
8
- import { emptySignal, SignalImpl } from '../event/Signal';
9
- import { logDebug } from '../logging/Logger';
10
- import { createLogPrefix } from '../logging/LogTarget';
11
- /**
12
- * Creates a Proxy for patched event and properties on the target. All patches from the provided
13
- * factory list will be used in creating a proxy chain. Each patch is applied in order, and
14
- * patches can rely on their target having been patched with all previous patches.
15
- *
16
- * @param target
17
- * @param patchFactories A list of factories that each produce a new patch for the target.
18
- * @return Returns a disposable object with a Proxy reference with overridden event and property
19
- * behavior. When disposed, the event listeners are removed.
20
- */
21
- export function patchTarget(target, ...patchFactories) {
22
- const n = patchFactories.length;
23
- if (n === 0) {
24
- // Not patched
25
- let disposed = false;
26
- return {
27
- patched: target,
28
- eventFabricated: emptySignal,
29
- eventSquelched: emptySignal,
30
- dispose: () => {
31
- // For consistency:
32
- if (disposed)
33
- throw new DisposedError();
34
- disposed = true;
35
- },
36
- };
37
- }
38
- else if (n === 1) {
39
- return _patchTarget(target, patchFactories[0]);
40
- }
41
- else {
42
- const { add, dispose } = createDisposer();
43
- let patched = target;
44
- const eventFabricated = add(new SignalImpl());
45
- const eventSquelched = add(new SignalImpl());
46
- const patchedChain = [];
47
- patchFactories.forEach((factory) => {
48
- const next = patchTarget(patched, factory);
49
- patched = next.patched;
50
- patchedChain.push(next);
51
- next.eventFabricated.listen((event) => eventFabricated.dispatch(event));
52
- next.eventSquelched.listen((event) => eventSquelched.dispatch(event));
53
- });
54
- // Patches must be disposed in reverse order
55
- for (let i = patchedChain.length - 1; i >= 0; i--) {
56
- add(patchedChain[i]);
57
- }
58
- return {
59
- patched,
60
- eventFabricated,
61
- eventSquelched,
62
- dispose,
63
- };
64
- }
65
- }
66
- /**
67
- * Creates a proxy to an event target that can patch properties and events.
68
- *
69
- * There are no modifications to the target, the returned proxy should be used in place of the
70
- * target.
71
- *
72
- * @param target
73
- * @param patchFactory Produces a new patch for the target.
74
- * @return Returns a disposable object with a Proxy reference with overridden event and property
75
- * behavior. When disposed, event listeners will be removed and the created patch and proxy will
76
- * be disposed.
77
- */
78
- function _patchTarget(target, patchFactory) {
79
- const { add, dispose } = createDisposer();
80
- const eventFabricated = add(new SignalImpl());
81
- const eventSquelched = add(new SignalImpl());
82
- // This method takes a factory instead of the EventTargetPatch directly to ensure that the
83
- // patch disposal is handled with the proxy disposal.
84
- const patch = add(patchFactory(target));
85
- // An event target used in place of the original target for patched events.
86
- const proxyEventHost = add(new EventHostImpl());
87
- patch.eventFabricated?.listen((event) => {
88
- eventFabricated.dispatch(event);
89
- proxyEventHost.dispatch(event.type, event);
90
- });
91
- const delegatedEvents = new Set();
92
- const subs = new Map();
93
- let addEventListener = undefined;
94
- let removeEventListener = undefined;
95
- if (isEventTarget(target)) {
96
- if (patch.events || patch.eventFabricated) {
97
- const eventPatches = patch.events ?? {};
98
- const observeTargetEvent = (type) => {
99
- if (!delegatedEvents.has(type)) {
100
- delegatedEvents.add(type);
101
- const targetHandler = (event) => {
102
- const patchedEvent = event.type in eventPatches
103
- ? eventPatches[event.type](event)
104
- : event;
105
- if (patchedEvent)
106
- proxyEventHost.dispatch(patchedEvent.type, patchedEvent);
107
- else
108
- eventSquelched.dispatch(event);
109
- };
110
- target.addEventListener(type, targetHandler);
111
- }
112
- };
113
- addEventListener = (type, callback, options) => {
114
- if (typeof callback !== 'function')
115
- throw new IllegalArgumentError('only function callbacks are supported');
116
- observeTargetEvent(type);
117
- subs.set(callback, proxyEventHost.on(type, callback, {
118
- once: typeof options === 'object'
119
- ? (options.once ?? false)
120
- : false,
121
- }));
122
- };
123
- removeEventListener = (_, callback) => {
124
- if (typeof callback !== 'function')
125
- throw new IllegalArgumentError('only function callbacks are supported');
126
- const sub = subs.get(callback);
127
- if (sub) {
128
- subs.delete(callback);
129
- sub();
130
- }
131
- };
132
- for (const eventPatchesKey in eventPatches) {
133
- observeTargetEvent(eventPatchesKey);
134
- }
135
- }
136
- else {
137
- addEventListener = target.addEventListener.bind(target);
138
- removeEventListener = target.removeEventListener.bind(target);
139
- }
140
- }
141
- const propertyPatches = patch.properties ?? {};
142
- const patched = new Proxy(target, {
143
- get(target, p, receiver) {
144
- const key = p;
145
- if (key === 'addEventListener') {
146
- return addEventListener;
147
- }
148
- else if (key === 'removeEventListener') {
149
- return removeEventListener;
150
- }
151
- const patch = propertyPatches[key];
152
- const value = patch && patch.get ? patch.get() : target[key];
153
- if (typeof value === 'function') {
154
- // Redirect the receiver from the Proxy to the target.
155
- // While it may make more sense to just use the Proxy as the receiver (Liskov
156
- // substitution principle), HTML DOM methods oddly have checks that enforce that the
157
- // receiver is strictly the Element.
158
- return function (...args) {
159
- return value.apply(this === receiver ? target : this, args);
160
- };
161
- }
162
- return value;
163
- },
164
- set(target, p, newValue) {
165
- const key = p;
166
- const patch = propertyPatches[key];
167
- if (patch && patch.set)
168
- patch.set(newValue);
169
- else
170
- target[key] = newValue;
171
- return true;
172
- },
173
- });
174
- return {
175
- patched,
176
- eventFabricated,
177
- eventSquelched,
178
- // Certain supported platforms do not support Proxy.revocable, but this is not a
179
- // requirement; we do not need to provide a Proxy to a consumer that needs its
180
- // lifespan controlled.
181
- dispose,
182
- };
183
- }
184
- /**
185
- * Logs debug messages when an event was squelched or fabricated.
186
- *
187
- * @param pathTarget
188
- * @param patched
189
- * @return Returns a function that when invoked, removes the logging.
190
- */
191
- export function logPatchedEvents(pathTarget, patched) {
192
- const eventSquelchedSub = patched.eventSquelched.listen((event) => logDebug(pathTarget, `'${event.type}' event squelched`));
193
- const eventFabricatedSub = patched.eventFabricated.listen((event) => logDebug(pathTarget, `'${event.type}' event fabricated`));
194
- return () => {
195
- eventSquelchedSub();
196
- eventFabricatedSub();
197
- };
198
- }
199
- function isEventTarget(target) {
200
- return 'addEventListener' in target;
201
- }
202
- /**
203
- * Given a list of [flagKey, patchFactory] tuples, patches the target with patches where flag is true.
204
- */
205
- export function patchTargetFromFlags(target, flags, ...patchEntries) {
206
- const pathTarget = {
207
- logPrefix: createLogPrefix('Patch'),
208
- };
209
- const filtered = patchEntries.filter(([key]) => {
210
- const enabled = flags[key] === true;
211
- if (enabled)
212
- logDebug(pathTarget, `Applying '${String(key)}' patch`);
213
- return enabled;
214
- });
215
- const patchedRef = patchTarget(target, ...filtered.map(([_, factory]) => factory));
216
- // Logs fabricated and squelched events.
217
- logPatchedEvents(pathTarget, patchedRef);
218
- return patchedRef;
219
- }
@@ -1,114 +0,0 @@
1
- import { Disposable } from '../core/disposable';
2
- import { Unsubscribe } from '../core/Unsubscribe';
3
- import { Signal } from '../event/Signal';
4
- import { AnyRecord } from '../util/type';
5
- import { LogTarget } from '../logging/LogTarget';
6
- export interface Patch<T, EventMap = AnyRecord> extends Disposable {
7
- /**
8
- * A signal that emits synthetic events.
9
- * These events will be dispatched on the proxy, not on the original event target.
10
- * If undefined, no events are fabricated.
11
- */
12
- readonly eventFabricated?: Signal<Event>;
13
- /**
14
- * Overridden properties.
15
- */
16
- readonly properties?: PropertyPatches<T>;
17
- /**
18
- * Overridden events.
19
- * Event patches may provide an alternate event to emit, or null to prevent an event from
20
- * dispatching on the proxy.
21
- *
22
- * Note:
23
- * Patched events have divergent behavior when using Event.stopImmediatePropagation.
24
- * All handlers added to the patched Proxy will still be called after stopImmediatePropagation.
25
- * It is the author's opinion that using stopImmediatePropagation is an anti-pattern, and one
26
- * handler should not be allowed to influence the behavior of neighboring handlers.
27
- */
28
- readonly events?: EventPatches<EventMap>;
29
- }
30
- /**
31
- * A record of property key to property patch.
32
- */
33
- export type PropertyPatches<T> = {
34
- readonly [P in keyof T]?: PropertyPatch<T, P>;
35
- };
36
- /**
37
- * An override to a patched property.
38
- */
39
- export interface PropertyPatch<T, K extends keyof T> {
40
- /**
41
- * @return Returns the value to return on the proxy.
42
- */
43
- get?(): T[K];
44
- /**
45
- * @param newValue The property value attempting to be set.
46
- */
47
- set?(newValue: T[K]): void;
48
- }
49
- /**
50
- * Handles an event emitted from the event target. The returned event will be emitted in its
51
- * place on the proxy.
52
- *
53
- * Fabricated events from `eventFabricated` will not be passed to this handler and cannot be
54
- * squelched.
55
- *
56
- * @param event The original event from the event target. This should not be modified
57
- * directly, but may be cloned and a modified clone may be returned.
58
- *
59
- * @return Returns the event to emit on the proxy or null to squelch the event.
60
- */
61
- export type EventPatch<T> = (event: T) => T | null;
62
- /**
63
- * A record of event type to an event patch.
64
- */
65
- export type EventPatches<EventMap> = {
66
- readonly [P in keyof EventMap]?: EventPatch<EventMap[P]>;
67
- };
68
- export type PatchFactory<T, EventMap> = (target: T) => Patch<T, EventMap>;
69
- export interface PatchedRef<T> extends Disposable {
70
- /**
71
- * The patched target.
72
- */
73
- readonly patched: T;
74
- /**
75
- * A signal for when a synthetic event was fabricated.
76
- */
77
- eventFabricated: Signal<Event>;
78
- /**
79
- * A signal for when an event was squelched.
80
- */
81
- eventSquelched: Signal<Event>;
82
- }
83
- /**
84
- * Creates a Proxy for patched event and properties on the target. All patches from the provided
85
- * factory list will be used in creating a proxy chain. Each patch is applied in order, and
86
- * patches can rely on their target having been patched with all previous patches.
87
- *
88
- * @param target
89
- * @param patchFactories A list of factories that each produce a new patch for the target.
90
- * @return Returns a disposable object with a Proxy reference with overridden event and property
91
- * behavior. When disposed, the event listeners are removed.
92
- */
93
- export declare function patchTarget<T extends object, EventMap = AnyRecord>(target: T, ...patchFactories: PatchFactory<T, EventMap>[]): PatchedRef<T>;
94
- /**
95
- * Logs debug messages when an event was squelched or fabricated.
96
- *
97
- * @param pathTarget
98
- * @param patched
99
- * @return Returns a function that when invoked, removes the logging.
100
- */
101
- export declare function logPatchedEvents(pathTarget: LogTarget, patched: PatchedRef<any>): Unsubscribe;
102
- /**
103
- * A tuple of `[flag, factory]`.
104
- * Patches will be applied when flag is true.
105
- */
106
- export type FlagAndPatchEntry<T extends object, EventMap, Key> = readonly [
107
- /*flagKey*/ Key,
108
- /*factory*/ PatchFactory<T, EventMap>
109
- ];
110
- /**
111
- * Given a list of [flagKey, patchFactory] tuples, patches the target with patches where flag is true.
112
- */
113
- export declare function patchTargetFromFlags<T extends object, EventMap, Flags>(target: T, flags: Flags, ...patchEntries: readonly FlagAndPatchEntry<T, EventMap, keyof Flags>[]): PatchedRef<T>;
114
- //# sourceMappingURL=patchTarget.d.ts.map