-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Update typings for MonkeyPatched (#143)
- Loading branch information
Showing
7 changed files
with
92 additions
and
82 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,39 @@ | ||
import * as shimmer from 'shimmer'; | ||
|
||
export type MonkeyPatch = { | ||
nodule: object; | ||
name: string; | ||
// tslint:disable-next-line: ban-types | ||
wrapper: Function; | ||
}; | ||
type Wrapper<W> = () => W; | ||
export interface MonkeyPatch< | ||
Nodule extends object, | ||
FieldName extends keyof Nodule | ||
> { | ||
nodule: Nodule; | ||
name: FieldName; | ||
wrapper: Wrapper<(original: Nodule[FieldName]) => Nodule[FieldName]>; | ||
} | ||
|
||
export abstract class MonkeyPatched { | ||
private enabled: boolean; | ||
export abstract class MonkeyPatched< | ||
Nodule extends object, | ||
FieldName extends keyof Nodule | ||
> { | ||
public enable = this.patch.bind(this, true); | ||
public disable = this.patch.bind(this, false); | ||
|
||
constructor() { | ||
this.enabled = false; | ||
} | ||
protected abstract patches: MonkeyPatch<Nodule, FieldName>[]; | ||
|
||
public enable() { | ||
if (!this.enabled) { | ||
this.enabled = true; | ||
for (const patch of this.patches()) { | ||
shimmer.wrap(patch.nodule, patch.name, patch.wrapper()); | ||
} | ||
} | ||
} | ||
private enabled: boolean = false; | ||
|
||
public disable() { | ||
if (this.enabled) { | ||
this.enabled = false; | ||
for (const patch of this.patches()) { | ||
shimmer.unwrap(patch.nodule, patch.name); | ||
private patch(shouldPatch: boolean = true) { | ||
if (this.enabled !== shouldPatch) { | ||
this.enabled = shouldPatch; | ||
const patchMethod = shouldPatch | ||
? shimmer.wrap.bind(shimmer) | ||
: shimmer.unwrap.bind(shimmer); | ||
for (const patch of this.patches) { | ||
patchMethod( | ||
patch.nodule, | ||
patch.name, | ||
shouldPatch ? patch.wrapper() : undefined | ||
); | ||
} | ||
} | ||
} | ||
|
||
protected abstract patches(): MonkeyPatch[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters