score:1

Accepted answer
import { eventChannel, END } from "redux-saga";

function createRegistrationFieldChannel(type: string) {
  return eventChannel(emitter => {
    Meteor.call("registerFormFields.insert", { type }, (error, _id) => {
      if (error) {
        emitter({ error });
        emitter(END);
      }

      emitter({ _id });
      emitter(END);
    });

    return () => {};
  });
}

function* createRegistrationField(action: CreateRegistrationFieldRequest) {
  yield call(function*() {
    yield put(CreateRegistrationFieldRequestRunningAction());
  });

  const { type } = action.payload;

  const channel = yield call(createRegistrationFieldChannel, type);

  try {
    while (true) {
      const { error = null, _id } = yield take(channel);

      if (error) {
      } else {
        yield put(RegistrationFieldCreatedAction(type, _id));
      }
    }
  } catch (err) {
    console.log(err);
  }
}

export function* watchCreateRegistrationField() {
  yield takeLatest(CREATE_REGISTRATION_FIELD_REQUEST, createRegistrationField);
}

Related Query

More Query from same tag