| 1 | // Should be no imports here!
|
|---|
| 2 |
|
|---|
| 3 | // Some things that should be evaluated before all else...
|
|---|
| 4 |
|
|---|
| 5 | // We only want to know if non-polyfilled symbols are available
|
|---|
| 6 | const hasSymbol =
|
|---|
| 7 | typeof Symbol !== "undefined" && typeof Symbol("x") === "symbol"
|
|---|
| 8 | export const hasMap = typeof Map !== "undefined"
|
|---|
| 9 | export const hasSet = typeof Set !== "undefined"
|
|---|
| 10 | export const hasProxies =
|
|---|
| 11 | typeof Proxy !== "undefined" &&
|
|---|
| 12 | typeof Proxy.revocable !== "undefined" &&
|
|---|
| 13 | typeof Reflect !== "undefined"
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * The sentinel value returned by producers to replace the draft with undefined.
|
|---|
| 17 | */
|
|---|
| 18 | export const NOTHING: Nothing = hasSymbol
|
|---|
| 19 | ? Symbol.for("immer-nothing")
|
|---|
| 20 | : ({["immer-nothing"]: true} as any)
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * To let Immer treat your class instances as plain immutable objects
|
|---|
| 24 | * (albeit with a custom prototype), you must define either an instance property
|
|---|
| 25 | * or a static property on each of your custom classes.
|
|---|
| 26 | *
|
|---|
| 27 | * Otherwise, your class instance will never be drafted, which means it won't be
|
|---|
| 28 | * safe to mutate in a produce callback.
|
|---|
| 29 | */
|
|---|
| 30 | export const DRAFTABLE: unique symbol = hasSymbol
|
|---|
| 31 | ? Symbol.for("immer-draftable")
|
|---|
| 32 | : ("__$immer_draftable" as any)
|
|---|
| 33 |
|
|---|
| 34 | export const DRAFT_STATE: unique symbol = hasSymbol
|
|---|
| 35 | ? Symbol.for("immer-state")
|
|---|
| 36 | : ("__$immer_state" as any)
|
|---|
| 37 |
|
|---|
| 38 | // Even a polyfilled Symbol might provide Symbol.iterator
|
|---|
| 39 | export const iteratorSymbol: typeof Symbol.iterator =
|
|---|
| 40 | (typeof Symbol != "undefined" && Symbol.iterator) || ("@@iterator" as any)
|
|---|
| 41 |
|
|---|
| 42 | /** Use a class type for `nothing` so its type is unique */
|
|---|
| 43 | export class Nothing {
|
|---|
| 44 | // This lets us do `Exclude<T, Nothing>`
|
|---|
| 45 | // @ts-ignore
|
|---|
| 46 | private _!: unique symbol
|
|---|
| 47 | }
|
|---|