| 1 | import createMachine from "./fsm.js";
|
|---|
| 2 |
|
|---|
| 3 | /**
|
|---|
| 4 | * @typedef {Object} ShowOverlayData
|
|---|
| 5 | * @property {'warning' | 'error'} level
|
|---|
| 6 | * @property {Array<string | { moduleIdentifier?: string, moduleName?: string, loc?: string, message?: string }>} messages
|
|---|
| 7 | * @property {'build' | 'runtime'} messageSource
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | /**
|
|---|
| 11 | * @typedef {Object} CreateOverlayMachineOptions
|
|---|
| 12 | * @property {(data: ShowOverlayData) => void} showOverlay
|
|---|
| 13 | * @property {() => void} hideOverlay
|
|---|
| 14 | */
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * @param {CreateOverlayMachineOptions} options
|
|---|
| 18 | */
|
|---|
| 19 | var createOverlayMachine = function createOverlayMachine(options) {
|
|---|
| 20 | var hideOverlay = options.hideOverlay,
|
|---|
| 21 | showOverlay = options.showOverlay;
|
|---|
| 22 | var overlayMachine = createMachine({
|
|---|
| 23 | initial: "hidden",
|
|---|
| 24 | context: {
|
|---|
| 25 | level: "error",
|
|---|
| 26 | messages: [],
|
|---|
| 27 | messageSource: "build"
|
|---|
| 28 | },
|
|---|
| 29 | states: {
|
|---|
| 30 | hidden: {
|
|---|
| 31 | on: {
|
|---|
| 32 | BUILD_ERROR: {
|
|---|
| 33 | target: "displayBuildError",
|
|---|
| 34 | actions: ["setMessages", "showOverlay"]
|
|---|
| 35 | },
|
|---|
| 36 | RUNTIME_ERROR: {
|
|---|
| 37 | target: "displayRuntimeError",
|
|---|
| 38 | actions: ["setMessages", "showOverlay"]
|
|---|
| 39 | }
|
|---|
| 40 | }
|
|---|
| 41 | },
|
|---|
| 42 | displayBuildError: {
|
|---|
| 43 | on: {
|
|---|
| 44 | DISMISS: {
|
|---|
| 45 | target: "hidden",
|
|---|
| 46 | actions: ["dismissMessages", "hideOverlay"]
|
|---|
| 47 | },
|
|---|
| 48 | BUILD_ERROR: {
|
|---|
| 49 | target: "displayBuildError",
|
|---|
| 50 | actions: ["appendMessages", "showOverlay"]
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 | },
|
|---|
| 54 | displayRuntimeError: {
|
|---|
| 55 | on: {
|
|---|
| 56 | DISMISS: {
|
|---|
| 57 | target: "hidden",
|
|---|
| 58 | actions: ["dismissMessages", "hideOverlay"]
|
|---|
| 59 | },
|
|---|
| 60 | RUNTIME_ERROR: {
|
|---|
| 61 | target: "displayRuntimeError",
|
|---|
| 62 | actions: ["appendMessages", "showOverlay"]
|
|---|
| 63 | },
|
|---|
| 64 | BUILD_ERROR: {
|
|---|
| 65 | target: "displayBuildError",
|
|---|
| 66 | actions: ["setMessages", "showOverlay"]
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 | }, {
|
|---|
| 72 | actions: {
|
|---|
| 73 | dismissMessages: function dismissMessages() {
|
|---|
| 74 | return {
|
|---|
| 75 | messages: [],
|
|---|
| 76 | level: "error",
|
|---|
| 77 | messageSource: "build"
|
|---|
| 78 | };
|
|---|
| 79 | },
|
|---|
| 80 | appendMessages: function appendMessages(context, event) {
|
|---|
| 81 | return {
|
|---|
| 82 | messages: context.messages.concat(event.messages),
|
|---|
| 83 | level: event.level || context.level,
|
|---|
| 84 | messageSource: event.type === "RUNTIME_ERROR" ? "runtime" : "build"
|
|---|
| 85 | };
|
|---|
| 86 | },
|
|---|
| 87 | setMessages: function setMessages(context, event) {
|
|---|
| 88 | return {
|
|---|
| 89 | messages: event.messages,
|
|---|
| 90 | level: event.level || context.level,
|
|---|
| 91 | messageSource: event.type === "RUNTIME_ERROR" ? "runtime" : "build"
|
|---|
| 92 | };
|
|---|
| 93 | },
|
|---|
| 94 | hideOverlay: hideOverlay,
|
|---|
| 95 | showOverlay: showOverlay
|
|---|
| 96 | }
|
|---|
| 97 | });
|
|---|
| 98 | return overlayMachine;
|
|---|
| 99 | };
|
|---|
| 100 | export default createOverlayMachine; |
|---|