|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2020 Daniel Ennis (Aikar) - MIT License |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | + * a copy of this software and associated documentation files (the |
| 6 | + * "Software"), to deal in the Software without restriction, including |
| 7 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | + * permit persons to whom the Software is furnished to do so, subject to |
| 10 | + * the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be |
| 13 | + * included in all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 19 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 20 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 21 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +package co.aikar.commands; |
| 25 | + |
| 26 | +import kotlin.coroutines.CoroutineContext; |
| 27 | +import kotlin.jvm.functions.Function2; |
| 28 | +import kotlinx.coroutines.ThreadContextElement; |
| 29 | +import org.jetbrains.annotations.NotNull; |
| 30 | +import org.jetbrains.annotations.Nullable; |
| 31 | + |
| 32 | +public final class ThreadLocalRestorer implements ThreadContextElement<Void> { |
| 33 | + |
| 34 | + private final CommandOperationContext context; |
| 35 | + private final Key key = new Key(); |
| 36 | + |
| 37 | + public ThreadLocalRestorer(CommandOperationContext context) { |
| 38 | + this.context = context; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void restoreThreadContext(@NotNull CoroutineContext coroutineContext, Void unused) { |
| 43 | + CommandManager.commandOperationContext.get().pop(); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Void updateThreadContext(@NotNull CoroutineContext coroutineContext) { |
| 48 | + CommandManager.commandOperationContext.get().push(this.context); |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + @NotNull |
| 53 | + @Override |
| 54 | + public CoroutineContext plus(@NotNull CoroutineContext coroutineContext) { |
| 55 | + return DefaultImpls.plus(this, coroutineContext); |
| 56 | + } |
| 57 | + |
| 58 | + @NotNull |
| 59 | + @Override |
| 60 | + public Key getKey() { |
| 61 | + return this.key; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public <R> R fold(R r, @NotNull Function2<? super R, ? super Element, ? extends R> function2) { |
| 66 | + return DefaultImpls.fold(this, r, function2); |
| 67 | + } |
| 68 | + |
| 69 | + @Nullable |
| 70 | + @Override |
| 71 | + public <E extends Element> E get(@NotNull CoroutineContext.Key<E> key) { |
| 72 | + return DefaultImpls.get(this, key); |
| 73 | + } |
| 74 | + |
| 75 | + @NotNull |
| 76 | + @Override |
| 77 | + public CoroutineContext minusKey(@NotNull CoroutineContext.Key key) { |
| 78 | + return DefaultImpls.minusKey(this, key); |
| 79 | + } |
| 80 | + |
| 81 | + public static final class Key implements kotlin.coroutines.CoroutineContext.Key<ThreadLocalRestorer> { |
| 82 | + } |
| 83 | +} |
0 commit comments