[TIL] Redux-saga 와 expo FileSystem

햄스터아저씨·2021년 7월 18일
0

FileSystem 은 보통 디렉토리를 생성하는 과정과 읽기/쓰기 과정으로 분리된다.

redux와 같이 사용할 경우, saga에서 읽기 부분을 처리하는것이 일반적인데, 그 때 FileSystem 라이브러리는 아래와 같이 사용할 수 있다.

  • 쓰기만 할거면 call 함수를 쓰지 않아도 된다
  • 읽을땐 반드시 call을 써주자. call을 써야, 파일을 다 읽을 때 까지 기다린다.
# 오류가 있을 수 있는 코드입니다. 주의하세요
import { call, put, takeLatest, select } from "redux-saga/effects";
import { actions } from "@store/Room";
import * as FileSystem from "expo-file-system";
import { ensureChatDirExists, getChatFileUri } from "@components/File/ensure";

function* openRooms(action) {
  const { updateRoomChatData } = actions;
  try {
    yield call(ensureChatDirExists);
    const stringData = yield call(FileSystem.readAsStringAsync,
                                  getChatFileUri(action.payload)
    );
    const data = JSON.parse(stringData);
    yield put(updateRoomChatData(data));
  } catch (err) {
    //TODO sentry
    console.log("FAIL Read Chat File!!!", err);
  }
}

export function* watchOpeningRoom() {
  const { openRoom } = actions;
  yield takeLatest(openRoom, openRooms);
}

참고: https://gist.github.com/jdrouet/29db4a5f337e88d84f8d10bfd2b17673

profile
서버도 하고 웹도 하고 시스템이나 인프라나 네트워크나 그냥 다 함.

0개의 댓글