|
| 1 | +import { assertEvent, assign, fromPromise, setup } from 'xstate'; |
| 2 | +import api from '../api/chat.endpoint'; |
| 3 | +import type { Message, MessageObj } from '../types/chat.type'; |
| 4 | + |
| 5 | +export const chatMachine = setup({ |
| 6 | + types: { |
| 7 | + context: {} as { |
| 8 | + question: Message; |
| 9 | + answer: Message; |
| 10 | + messageList: MessageObj[]; |
| 11 | + state: 'idle' | 'processing' | 'complete'; |
| 12 | + }, |
| 13 | + events: {} as |
| 14 | + | { |
| 15 | + type: 'SUBMIT_EVENT'; |
| 16 | + } |
| 17 | + | { |
| 18 | + type: 'RECEIVE_ANSWER'; |
| 19 | + answer: string; |
| 20 | + } |
| 21 | + | { |
| 22 | + type: 'TYPING_QUESTION'; |
| 23 | + question: string; |
| 24 | + }, |
| 25 | + }, |
| 26 | + actors: { |
| 27 | + submitQuestion: fromPromise( |
| 28 | + async ({ input }: { input: { question: string } }) => { |
| 29 | + const response = await api.ask(input.question); |
| 30 | + return await response.json(); |
| 31 | + }, |
| 32 | + ), |
| 33 | + }, |
| 34 | + actions: { |
| 35 | + switchStateToIdle: assign({ |
| 36 | + state: 'idle', |
| 37 | + }), |
| 38 | + switchStateToProcessing: assign({ |
| 39 | + state: 'processing', |
| 40 | + }), |
| 41 | + switchStateToComplete: assign({ |
| 42 | + state: 'complete', |
| 43 | + }), |
| 44 | + assignQuestion: assign({ |
| 45 | + question: ({ event }) => { |
| 46 | + assertEvent(event, 'TYPING_QUESTION'); |
| 47 | + return event.question; |
| 48 | + }, |
| 49 | + }), |
| 50 | + assignQuestionToMessageList: assign({ |
| 51 | + messageList: ({ event, context }) => { |
| 52 | + assertEvent(event, 'SUBMIT_EVENT'); |
| 53 | + return [ |
| 54 | + ...context.messageList, |
| 55 | + { |
| 56 | + message: context.question, |
| 57 | + isMine: true, |
| 58 | + }, |
| 59 | + ]; |
| 60 | + }, |
| 61 | + }), |
| 62 | + resetQuestion: assign({ |
| 63 | + question: '', |
| 64 | + }), |
| 65 | + assignErrorMessage: assign({ |
| 66 | + answer: ({ event }) => { |
| 67 | + return '답변을 받아오는데 실패했습니다.'; |
| 68 | + }, |
| 69 | + }), |
| 70 | + }, |
| 71 | +}).createMachine({ |
| 72 | + id: 'chatMachine', |
| 73 | + context: { |
| 74 | + question: '', |
| 75 | + answer: '', |
| 76 | + messageList: [], |
| 77 | + state: 'idle', |
| 78 | + }, |
| 79 | + initial: 'EMPTY QUESTION FIELD', |
| 80 | + states: { |
| 81 | + 'EMPTY QUESTION FIELD': { |
| 82 | + on: { |
| 83 | + TYPING_QUESTION: { |
| 84 | + target: 'FILLING QUESTION FIELD', |
| 85 | + actions: 'assignQuestion', |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + 'FILLING QUESTION FIELD': { |
| 90 | + on: { |
| 91 | + TYPING_QUESTION: { |
| 92 | + actions: 'assignQuestion', |
| 93 | + }, |
| 94 | + SUBMIT_EVENT: { |
| 95 | + target: 'SENDING QUESTION', |
| 96 | + actions: ['assignQuestionToMessageList', 'switchStateToProcessing'], |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + 'SENDING QUESTION': { |
| 101 | + invoke: { |
| 102 | + src: 'submitQuestion', |
| 103 | + input: ({ context }) => ({ question: context.question }), |
| 104 | + onDone: { |
| 105 | + target: 'RECEIVED ANSWER', |
| 106 | + actions: [ |
| 107 | + 'switchStateToComplete', |
| 108 | + 'resetQuestion', |
| 109 | + assign({ |
| 110 | + messageList: ({ event, context }) => { |
| 111 | + return [ |
| 112 | + ...context.messageList, |
| 113 | + { message: event.output, isMine: false }, |
| 114 | + ]; |
| 115 | + }, |
| 116 | + }), |
| 117 | + ], |
| 118 | + }, |
| 119 | + onError: { |
| 120 | + target: 'RECEIVED ANSWER', |
| 121 | + actions: ['assignErrorMessage'], |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + 'RECEIVED ANSWER': { |
| 126 | + always: { |
| 127 | + target: 'EMPTY QUESTION FIELD', |
| 128 | + actions: ['switchStateToIdle'], |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | +}); |
0 commit comments