|
| 1 | +package scommons.react.redux.task |
| 2 | + |
| 3 | +import scommons.react._ |
| 4 | +import scommons.react.hooks._ |
| 5 | + |
| 6 | +import scala.scalajs.js |
| 7 | +import scala.scalajs.js.{Error, JavaScriptException} |
| 8 | +import scala.util.{Failure, Success, Try} |
| 9 | + |
| 10 | +case class TaskManagerProps(startTask: Option[AbstractTask]) |
| 11 | + |
| 12 | +/** |
| 13 | + * Handles status of running tasks. |
| 14 | + */ |
| 15 | +object TaskManager extends FunctionComponent[TaskManagerProps] { |
| 16 | + |
| 17 | + var uiComponent: UiComponent[TaskManagerUiProps] = _ |
| 18 | + |
| 19 | + var errorHandler: PartialFunction[Try[_], (Option[String], Option[String])] = PartialFunction.empty |
| 20 | + |
| 21 | + private case class TaskManagerState(taskCount: Int = 0, |
| 22 | + status: Option[String] = None, |
| 23 | + error: Option[String] = None, |
| 24 | + errorDetails: Option[String] = None) |
| 25 | + |
| 26 | + protected def render(compProps: Props): ReactElement = { |
| 27 | + val props = compProps.wrapped |
| 28 | + val (state, setState) = useStateUpdater(() => TaskManagerState()) |
| 29 | + |
| 30 | + if (uiComponent == null) { |
| 31 | + throw JavaScriptException(Error("TaskManager.uiComponent is not specified")) |
| 32 | + } |
| 33 | + |
| 34 | + useEffect({ () => |
| 35 | + props.startTask.foreach { task => |
| 36 | + onTaskStart(setState, task) |
| 37 | + } |
| 38 | + }, List(props.startTask match { |
| 39 | + case None => js.undefined |
| 40 | + case Some(task) => task.asInstanceOf[js.Any] |
| 41 | + })) |
| 42 | + |
| 43 | + <(uiComponent())(^.wrapped := TaskManagerUiProps( |
| 44 | + showLoading = state.taskCount > 0, |
| 45 | + status = state.status, |
| 46 | + onHideStatus = { () => |
| 47 | + setState(_.copy(status = None)) |
| 48 | + }, |
| 49 | + error = state.error, |
| 50 | + errorDetails = state.errorDetails, |
| 51 | + onCloseErrorPopup = { () => |
| 52 | + setState(_.copy(error = None, errorDetails = None)) |
| 53 | + } |
| 54 | + ))() |
| 55 | + } |
| 56 | + |
| 57 | + private def onTaskStart(setState: js.Function1[js.Function1[TaskManagerState, TaskManagerState], Unit], |
| 58 | + task: AbstractTask): Unit = { |
| 59 | + |
| 60 | + task.onComplete { value: Try[_] => |
| 61 | + onTaskFinish(setState, task, value) |
| 62 | + } |
| 63 | + |
| 64 | + setState(s => s.copy( |
| 65 | + taskCount = s.taskCount + 1, |
| 66 | + status = Some(s"${task.message}...") |
| 67 | + )) |
| 68 | + } |
| 69 | + |
| 70 | + private def onTaskFinish(setState: js.Function1[js.Function1[TaskManagerState, TaskManagerState], Unit], |
| 71 | + task: AbstractTask, |
| 72 | + value: Try[_]): Unit = { |
| 73 | + |
| 74 | + val durationMillis = System.currentTimeMillis() - task.startTime |
| 75 | + val statusMessage = s"${task.message}...Done ${formatDuration(durationMillis)} sec." |
| 76 | + |
| 77 | + def defaultErrorHandler(value: Try[_]): (Option[String], Option[String]) = value match { |
| 78 | + case Success(_) => (None, None) |
| 79 | + case Failure(e) => (Some(e.toString), Some(printStackTrace(e))) |
| 80 | + } |
| 81 | + |
| 82 | + val (error, errorDetails) = errorHandler.applyOrElse(value, defaultErrorHandler) |
| 83 | + |
| 84 | + setState(s => s.copy( |
| 85 | + taskCount = s.taskCount - 1, |
| 86 | + status = Some(statusMessage), |
| 87 | + error = error, |
| 88 | + errorDetails = errorDetails |
| 89 | + )) |
| 90 | + } |
| 91 | + |
| 92 | + private[task] def formatDuration(durationMillis: Long): String = { |
| 93 | + "%.3f".format(durationMillis / 1000.0) |
| 94 | + } |
| 95 | + |
| 96 | + private[task] def printStackTrace(x: Throwable): String = { |
| 97 | + val sb = new StringBuilder(x.toString) |
| 98 | + val trace = x.getStackTrace |
| 99 | + for (t <- trace) { |
| 100 | + sb.append("\n\tat ").append(t) |
| 101 | + } |
| 102 | + |
| 103 | + val cause = x.getCause |
| 104 | + if (cause != null) { |
| 105 | + printStackTraceAsCause(sb, cause, trace) |
| 106 | + } |
| 107 | + |
| 108 | + sb.toString |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Print stack trace as a cause for the specified stack trace. |
| 113 | + */ |
| 114 | + private def printStackTraceAsCause(sb: StringBuilder, |
| 115 | + cause: Throwable, |
| 116 | + causedTrace: Array[StackTraceElement]): Unit = { |
| 117 | + |
| 118 | + // Compute number of frames in common between this and caused |
| 119 | + val trace = cause.getStackTrace |
| 120 | + var m = trace.length - 1 |
| 121 | + var n = causedTrace.length - 1 |
| 122 | + while (m >= 0 && n >= 0 && trace(m) == causedTrace(n)) { |
| 123 | + m -= 1 |
| 124 | + n -= 1 |
| 125 | + } |
| 126 | + |
| 127 | + val framesInCommon = trace.length - 1 - m |
| 128 | + sb.append("\nCaused by: " + cause) |
| 129 | + |
| 130 | + for (i <- 0 to m) { |
| 131 | + sb.append("\n\tat ").append(trace(i)) |
| 132 | + } |
| 133 | + |
| 134 | + if (framesInCommon != 0) { |
| 135 | + sb.append("\n\t... ").append(framesInCommon).append(" more") |
| 136 | + } |
| 137 | + |
| 138 | + // Recurse if we have a cause |
| 139 | + val ourCause = cause.getCause |
| 140 | + if (ourCause != null) { |
| 141 | + printStackTraceAsCause(sb, ourCause, trace) |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments