generated from legendecas/tc39-proposal
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the polyfill to the latest API (#71)
* Update the polyfill to the lastest API * Fix filename case * Move wrap onto Snapshot * Fix polyfill
- Loading branch information
1 parent
a8a2961
commit dc92ab9
Showing
10 changed files
with
191 additions
and
132 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,37 +1,8 @@ | ||
import { Storage } from "./storage"; | ||
|
||
type AnyFunc<T> = (this: T, ...args: any) => any; | ||
|
||
export class AsyncContext<T> { | ||
static wrap<F extends AnyFunc<any>>(fn: F): F { | ||
const snapshot = Storage.snapshot(); | ||
|
||
function wrap(this: ThisType<F>, ...args: Parameters<F>): ReturnType<F> { | ||
const head = Storage.switch(snapshot); | ||
try { | ||
return fn.apply(this, args); | ||
} finally { | ||
Storage.restore(head); | ||
} | ||
} | ||
|
||
return wrap as unknown as F; | ||
} | ||
|
||
run<F extends AnyFunc<null>>( | ||
value: T, | ||
fn: F, | ||
...args: Parameters<F> | ||
): ReturnType<F> { | ||
const revert = Storage.set(this, value); | ||
try { | ||
return fn.apply(null, args); | ||
} finally { | ||
Storage.restore(revert); | ||
} | ||
} | ||
|
||
get(): T | undefined { | ||
return Storage.get(this); | ||
} | ||
} | ||
import { Snapshot } from "./snapshot"; | ||
import { Variable } from "./variable"; | ||
|
||
export const AsyncContext = { | ||
Snapshot, | ||
Variable, | ||
}; | ||
export { Snapshot, Variable }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Storage } from "./storage"; | ||
|
||
import type { FrozenRevert } from "./fork"; | ||
import type { AnyFunc } from "./types"; | ||
|
||
export class Snapshot { | ||
#snapshot = Storage.snapshot(); | ||
|
||
static wrap<F extends AnyFunc<any>>(fn: F): F { | ||
const snapshot = Storage.snapshot(); | ||
|
||
function wrap(this: ThisType<F>, ...args: Parameters<F>): ReturnType<F> { | ||
return run(fn, this, args, snapshot); | ||
} | ||
|
||
return wrap as unknown as F; | ||
} | ||
|
||
run<F extends AnyFunc<null>>(fn: F, ...args: Parameters<F>) { | ||
return run(fn, null as any, args, this.#snapshot); | ||
} | ||
} | ||
|
||
function run<F extends AnyFunc<any>>( | ||
fn: F, | ||
context: ThisType<F>, | ||
args: any[], | ||
snapshot: FrozenRevert | ||
): ReturnType<F> { | ||
const revert = Storage.switch(snapshot); | ||
try { | ||
return fn.apply(context, args); | ||
} finally { | ||
Storage.restore(revert); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export type AnyFunc<T> = (this: T, ...args: any) => any; |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Storage } from "./storage"; | ||
|
||
import type { AnyFunc } from "./types"; | ||
|
||
export interface VariableOptions<T> { | ||
name?: string; | ||
defaultValue?: T; | ||
} | ||
|
||
export class Variable<T> { | ||
#name = ""; | ||
#defaultValue: T | undefined; | ||
|
||
constructor(options?: VariableOptions<T>) { | ||
if (options) { | ||
if ("name" in options) { | ||
this.#name = String(options.name); | ||
} | ||
this.#defaultValue = options.defaultValue; | ||
} | ||
} | ||
|
||
get name() { | ||
return this.#name; | ||
} | ||
|
||
run<F extends AnyFunc<null>>( | ||
value: T, | ||
fn: F, | ||
...args: Parameters<F> | ||
): ReturnType<F> { | ||
const revert = Storage.set(this, value); | ||
try { | ||
return fn.apply(null, args); | ||
} finally { | ||
Storage.restore(revert); | ||
} | ||
} | ||
|
||
get(): T | undefined { | ||
return Storage.has(this) ? Storage.get(this) : this.#defaultValue; | ||
} | ||
} |
Oops, something went wrong.