1

I’m using react-native-offline library in my app for offline queueing of requests to server and the action changeQueueSemaphore had caused me few difficulties. I’ll try explaining by an example.

Let’s say my app posts offline issues in Stack Overflow by three basic actions:

const createIssue = (issue) => action("CREATE_ISSUE",{issue},{retry: true});
const addComment = (comment) => action("ADD_COMMENT",{comment},{retry:true});
const closeIssue = (timeOfClosing) => action("CLOSE_ISSUE", {timeOfCLosing}, {retry: true});

When my app works in offline mode the actions are entered by order to the queue and then being dispatched one after another when network connection is regained. In order to dispatch the last two actions I first need to dispatch the create issue action and get a generated id from my server. Thus, when listening to createIssue action using redux saga, in the handler I'm dispatching changeQueueSemaphore('RED') -> sends post request of creating an issue -> retrieving the id from the response and saving it in state, then releases the queue again:

function* createIssueHandler(action: ActionType<typeof createIssue>) {
    const {issue} = action.payload;
    const {changeQueueSemaphore} = offlineActionCreators;

    // stops the queue before generating id
    yield put(changeQueueSemaphore('RED');

    try {
    const id = // Api Call to create Issue

    // saves the id in state
    yield put(createIssueSuccess(id));

    // releases queue again
    yield put(changeQueueSemaphore('GREEN');
    } catch(err) {
    // error handeling
}

function* addCommentHandler(action: ActionType<typeof addComment>) {
  const issueId = yield select(getIssueIdFromState);
enter code here
  // api call to `SERVER_URL/addComment/${issueId}`;
}

function* closeIssue(action: ActionType<typeof closeIssue>) {
  // same as addCommentHandler
}

After looking at the action logs I saw that addComment and closeIssue actions are being dispatched before changeQueueSemaphore causes the queue to stop.

Any help will be pleased <3.

0 Answers0