Skip to content

Commit

Permalink
fix: duplicate setup to keep cancelable a writable property
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Sep 27, 2024
1 parent 778c568 commit 1183ea9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/interceptors/WebSocket/utils/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe(CancelableMessageEvent, () => {
expect(event.defaultPrevented).toBe(false)
})

it('initiates a canceaable event', () => {
it('initiates a cancelable event', () => {
const event = new CancelableMessageEvent('message', {
data: 'hello',
cancelable: true,
Expand Down
102 changes: 54 additions & 48 deletions src/interceptors/WebSocket/utils/events.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,6 @@
const kCancelable = Symbol('kCancelable')
const kDefaultPrevented = Symbol('kDefaultPrevented')

function makeCancelableEvent(event: Event) {
if (typeof Reflect.get(event, kCancelable) !== 'undefined') {
return event
}

Object.defineProperties(event, {
[kCancelable]: {
value: event.cancelable,
enumerable: false,
writable: true,
},
[kDefaultPrevented]: {
value: event.defaultPrevented,
enumerable: false,
writable: true,
},
cancelable: {
get() {
return this[kCancelable]
},
set(nextCancelable) {
this[kCancelable] = nextCancelable
},
},
defaultPrevented: {
get() {
return this[kDefaultPrevented]
},
set(nextDefaultPrevented) {
return (this[kDefaultPrevented] = nextDefaultPrevented)
},
},
preventDefault: {
value() {
if (this.cancelable && !this[kDefaultPrevented]) {
this[kDefaultPrevented] = true
}
},
enumerable: true,
},
})

return event
}

/**
* A `MessageEvent` superset that supports event cancellation
* in Node.js. It's rather non-intrusive so it can be safely
Expand All @@ -54,10 +9,35 @@ function makeCancelableEvent(event: Event) {
* @see https://github.com/nodejs/node/issues/51767
*/
export class CancelableMessageEvent<T = any> extends MessageEvent<T> {
[kCancelable]: boolean;
[kDefaultPrevented]: boolean

constructor(type: string, init: MessageEventInit<T>) {
super(type, init)
this[kCancelable] = !!init.cancelable
this[kDefaultPrevented] = false
}

makeCancelableEvent(this)
get cancelable() {
return this[kCancelable]
}

set cancelable(nextCancelable) {
this[kCancelable] = nextCancelable
}

get defaultPrevented() {
return this[kDefaultPrevented]
}

set defaultPrevented(nextDefaultPrevented) {
this[kDefaultPrevented] = nextDefaultPrevented
}

public preventDefault(): void {
if (this.cancelable && !this[kDefaultPrevented]) {
this[kDefaultPrevented] = true
}
}
}

Expand All @@ -81,8 +61,34 @@ export class CloseEvent extends Event {
}

export class CancelableCloseEvent extends CloseEvent {
constructor(type: string, init: CloseEventInit) {
[kCancelable]: boolean;
[kDefaultPrevented]: boolean

constructor(type: string, init: EventInit) {
super(type, init)
makeCancelableEvent(this)
this[kCancelable] = !!init.cancelable
this[kDefaultPrevented] = false
}

get cancelable() {
return this[kCancelable]
}

set cancelable(nextCancelable) {
this[kCancelable] = nextCancelable
}

get defaultPrevented() {
return this[kDefaultPrevented]
}

set defaultPrevented(nextDefaultPrevented) {
this[kDefaultPrevented] = nextDefaultPrevented
}

public preventDefault(): void {
if (this.cancelable && !this[kDefaultPrevented]) {
this[kDefaultPrevented] = true
}
}
}

0 comments on commit 1183ea9

Please sign in to comment.