redux-saga/effects#getContext TypeScript Examples

The following examples show how to use redux-saga/effects#getContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.ts    From camus with GNU Affero General Public License v3.0 6 votes vote down vote up
function* doSetUsername({ payload }: PayloadAction<string>) {
    const manager: Manager = yield getContext('manager');
    const username = payload;

    try {
        yield apply(manager, manager.setUsername, [username]);
        yield put({ type: 'MANAGER_UPDATED' });
    } catch (err) {
        yield put({ type: 'MANAGER_ERROR', payload: err });
    }
}
Example #2
Source File: index.ts    From camus with GNU Affero General Public License v3.0 6 votes vote down vote up
function* doSendChatMessage({ payload }: PayloadAction<string>) {
    const manager: Manager = yield getContext('manager');
    const message = payload;
    const from = manager.username;

    try {
        yield apply(manager.signaler, manager.signaler.text, [message, from]);
        yield put({ type: 'MANAGER_UPDATED' });
    } catch (err) {
        yield put({ type: 'MANAGER_ERROR', payload: err });
    }
}
Example #3
Source File: index.ts    From camus with GNU Affero General Public License v3.0 6 votes vote down vote up
function* doSetLocalAudio({ payload }: PayloadAction<MediaStreamTrack | null>) {
    const manager: Manager = yield getContext('manager');
    const track = payload;

    try {
        if (track) {
            yield apply(manager, manager.setTrack, ['audio', track]);
        } else {
            yield apply(manager, manager.stopTrack, ['audio']);
        }

        yield put({ type: 'MANAGER_UPDATED' });
    } catch (err) {
        yield put({ type: 'MANAGER_ERROR', payload: err });
    }
}
Example #4
Source File: index.ts    From camus with GNU Affero General Public License v3.0 6 votes vote down vote up
function* doSetLocalVideo({ payload }: PayloadAction<MediaStreamTrack | null>) {
    const manager: Manager = yield getContext('manager');
    const track = payload;

    try {
        if (track) {
            yield apply(manager, manager.setTrack, ['video', track]);
        } else {
            yield apply(manager, manager.stopTrack, ['video']);
        }

        yield put({ type: 'MANAGER_UPDATED' });
    } catch (err) {
        yield put({ type: 'MANAGER_ERROR', payload: err });
    }
}
Example #5
Source File: index.ts    From camus with GNU Affero General Public License v3.0 6 votes vote down vote up
function* doDisableRemoteVideo({ payload }: PayloadAction<string>) {
    const manager: Manager = yield getContext('manager');
    const id = payload;

    try {
        const peer: MediaPeer = manager.mediaPeers.get(id) as MediaPeer;
        yield apply(peer, peer.disableRemoteVideo, []);
        yield put({ type: 'PEER_UPDATED' });
    } catch (err) {
        yield put({ type: 'PEER_ERROR', payload: err });
    }
}
Example #6
Source File: index.ts    From camus with GNU Affero General Public License v3.0 6 votes vote down vote up
function* doEnableRemoteVideo({ payload }: PayloadAction<string>) {
    const manager: Manager = yield getContext('manager');
    const id = payload;

    try {
        const peer: MediaPeer = manager.mediaPeers.get(id) as MediaPeer;
        yield apply(peer, peer.enableRemoteVideo, []);
        yield put({ type: 'PEER_UPDATED' });
    } catch (err) {
        yield put({ type: 'PEER_ERROR', payload: err });
    }
}
Example #7
Source File: index.ts    From camus with GNU Affero General Public License v3.0 6 votes vote down vote up
function* doSetIceServers() {
    const manager: Manager = yield getContext('manager');
    const iceServers: IceServer[] = yield select((state) => state.iceServers);
    const servers = iceServers
        .filter((server) => {
            return server.enabled;
        })
        .map((server) => {
            return {
                urls: server.urls,
                username: server.username,
                credential: server.credential,
            };
        }) as IceServer[];

    try {
        yield apply(manager, manager.setIceServers, [servers]);
        yield put({ type: 'MANAGER_UPDATED' });
    } catch (err) {
        console.log(err);
        yield put({ type: 'MANAGER_ERROR', payload: err });
    }
}
Example #8
Source File: sagas.ts    From firebase-tools-ui with Apache License 2.0 5 votes vote down vote up
export function* configureAuthSaga(): Generator {
  const api = yield getContext(AUTH_API_CONTEXT);
  return api;
}
Example #9
Source File: sagas.test.ts    From firebase-tools-ui with Apache License 2.0 4 votes vote down vote up
describe('Auth sagas', () => {
  describe('authFetchUsers', () => {
    it('dispatches authFetchUsersSuccess action with the resulting info', () => {
      const gen = fetchAuthUsers();
      const fakeAuthApi = { fetchUsers: jest.fn() };
      const fakeUsers: AuthUser[] = [];
      expect(gen.next()).toEqual({
        done: false,
        value: call(configureAuthSaga),
      });
      expect(gen.next(fakeAuthApi)).toEqual({
        done: false,
        value: call([fakeAuthApi, 'fetchUsers']),
      });
      expect(gen.next(fakeUsers)).toEqual({
        done: false,
        value: put(authFetchUsersSuccess(fakeUsers)),
      });
      expect(gen.next().done).toBe(true);
    });
  });

  describe('createUser', () => {
    it('dispatches createUserSuccess', () => {
      const user = { displayName: 'lol' } as AddAuthUserPayload;
      const fakeAuthApi = { createUser: jest.fn() };
      const gen = createUser(createUserRequest({ user }));

      expect(gen.next()).toEqual({
        done: false,
        value: call(configureAuthSaga),
      });

      expect(gen.next(fakeAuthApi as unknown as AuthApi & AuthUser)).toEqual({
        done: false,
        value: put(setAuthUserDialogLoading(true)),
      });

      expect(gen.next()).toEqual({
        done: false,
        value: call([fakeAuthApi, 'createUser'], user),
      });

      expect(gen.next(user as unknown as AuthApi & AuthUser)).toEqual({
        done: false,
        value: put(createUserSuccess({ user: user as unknown as AuthUser })),
      });

      expect(gen.next()).toEqual({
        done: false,
        value: put(clearAuthUserDialogData()),
      });

      expect(gen.next()).toEqual({
        done: false,
        value: put(setAuthUserDialogLoading(false)),
      });

      expect(gen.next().done).toBe(true);
    });

    it('makes a separate call to update customAttributes', () => {
      const user = {
        displayName: 'lol',
        customAttributes: '{"a": 1}',
      } as AddAuthUserPayload;
      const fakeAuthApi = {
        createUser: jest.fn(),
        updateUser: jest.fn(),
      };

      const gen = createUser(createUserRequest({ user }));

      expect(gen.next()).toEqual({
        done: false,
        value: call(configureAuthSaga),
      });

      expect(gen.next(fakeAuthApi as unknown as AuthApi & AuthUser)).toEqual({
        done: false,
        value: put(setAuthUserDialogLoading(true)),
      });

      expect(gen.next()).toEqual({
        done: false,
        value: call([fakeAuthApi, 'createUser'], user),
      });

      const newUser = {
        localId: 'lol',
        displayName: user.displayName,
      } as AuthUser;

      expect(gen.next(newUser as unknown as AuthApi & AuthUser)).toEqual({
        done: false,
        value: call([fakeAuthApi, 'updateUser'], {
          localId: newUser.localId,
          customAttributes: user.customAttributes,
        }),
      });

      newUser.customAttributes = user.customAttributes;

      expect(gen.next(newUser as unknown as AuthApi & AuthUser)).toEqual({
        done: false,
        value: put(createUserSuccess({ user: newUser })),
      });

      expect(gen.next()).toEqual({
        done: false,
        value: put(clearAuthUserDialogData()),
      });

      expect(gen.next()).toEqual({
        done: false,
        value: put(setAuthUserDialogLoading(false)),
      });

      expect(gen.next().done).toBe(true);
    });

    it('handles errors', () => {
      const user = { displayName: 'lol' } as AddAuthUserPayload;
      const errorMessage = 'ops';
      const fakeAuthApi = {
        createUser: jest.fn(),
      };

      const gen = createUser(createUserRequest({ user }));

      expect(gen.next()).toEqual({
        done: false,
        value: call(configureAuthSaga),
      });

      expect(gen.next(fakeAuthApi as unknown as AuthApi & AuthUser)).toEqual({
        done: false,
        value: put(setAuthUserDialogLoading(true)),
      });

      expect(gen.next()).toEqual({
        done: false,
        value: call([fakeAuthApi, 'createUser'], user),
      });

      gen.throw(new Error(errorMessage));

      expect(gen.next()).toEqual({
        done: false,
        value: put(setAuthUserDialogLoading(false)),
      });

      expect(gen.next().done).toBe(true);
    });

    it('does not close the dialog', () => {
      const user = { displayName: 'lol' } as AddAuthUserPayload;
      const errorMessage = 'ops';
      const fakeAuthApi = {
        createUser: jest.fn(),
      };

      const gen = createUser(createUserRequest({ user, keepDialogOpen: true }));

      expect(gen.next()).toEqual({
        done: false,
        value: call(configureAuthSaga),
      });

      expect(gen.next(fakeAuthApi as unknown as AuthApi & AuthUser)).toEqual({
        done: false,
        value: put(setAuthUserDialogLoading(true)),
      });

      expect(gen.next()).toEqual({
        done: false,
        value: call([fakeAuthApi, 'createUser'], user),
      });

      gen.throw(new Error(errorMessage));

      expect(gen.next()).toEqual({
        done: false,
        value: put(setAuthUserDialogLoading(false)),
      });

      expect(gen.next().done).toBe(true);
    });
  });

  describe('initAuth', () => {
    it('does nothing if auth is disabled', () => {
      const gen = initAuth(updateAuthConfig(null));

      expect(gen.next({})).toEqual({
        done: true,
      });
    });

    it('dispatches appropriate actions if auth is enabled', () => {
      const gen = initAuth(
        updateAuthConfig({
          auth: {
            hostAndPort: 'localhost:9099',
            host: 'localhost',
            port: 9099,
          },
          projectId: 'hello',
        })
      );
      expect(gen.next()).toEqual({
        done: false,
        value: setContext(expect.anything()),
      });

      expect(gen.next()).toEqual({
        done: false,
        value: all(
          expect.arrayContaining([
            put(authFetchUsersRequest()),
            put(getAllowDuplicateEmailsRequest()),
          ])
        ),
      });

      expect(gen.next()).toEqual({
        done: true,
      });
    });
  });

  describe('deleteUser', () => {
    it('dispatches deleteUserSuccess action with the resulting info', () => {
      const localId = 'pirojok';
      const fakeAuthApi = { deleteUser: jest.fn() };
      const payload = { localId };
      const gen = deleteUser(deleteUserRequest(payload));
      expect(gen.next()).toEqual({
        done: false,
        value: call(configureAuthSaga),
      });
      expect(gen.next(fakeAuthApi as unknown as AuthApi & AuthUser)).toEqual({
        done: false,
        value: call([fakeAuthApi, 'deleteUser'], payload),
      });
      expect(gen.next(payload as unknown as AuthApi & AuthUser)).toEqual({
        done: false,
        value: put(deleteUserSuccess(payload)),
      });

      expect(gen.next().done).toBe(true);
    });
  });

  describe('updateUser', () => {
    it('dispatches updateUserSuccess action with the resulting info', () => {
      const localId = 'pirojok';
      const user = { displayName: 'lol' } as AddAuthUserPayload;
      const fakeAuthApi = { updateUser: jest.fn() };
      const newUser = { ...user, localId };
      const gen = updateUser(updateUserRequest({ user, localId }));

      expect(gen.next()).toEqual({
        done: false,
        value: call(configureAuthSaga),
      });

      expect(gen.next(fakeAuthApi as unknown as AuthApi & AuthUser)).toEqual({
        done: false,
        value: put(setAuthUserDialogLoading(true)),
      });

      expect(gen.next()).toEqual({
        done: false,
        value: call([fakeAuthApi, 'updateUser'], newUser),
      });
      expect(gen.next(newUser as unknown as AuthApi & AuthUser)).toEqual({
        done: false,
        value: put(updateUserSuccess({ user: newUser as AuthUser })),
      });

      expect(gen.next()).toEqual({
        done: false,
        value: put(clearAuthUserDialogData()),
      });

      expect(gen.next(fakeAuthApi as unknown as AuthApi & AuthUser)).toEqual({
        done: false,
        value: put(setAuthUserDialogLoading(false)),
      });

      expect(gen.next().done).toBe(true);
    });
  });

  describe('setUserDisabled', () => {
    it('dispatches updateUserSuccess action with the resulting info', () => {
      const localId = 'pirojok';
      const fakeAuthApi = { updateUser: jest.fn() };
      const isDisabled = true;
      const payload = { disabled: isDisabled, localId };
      const gen = setUserDisabled(setUserDisabledRequest(payload));

      expect(gen.next()).toEqual({
        done: false,
        value: call(configureAuthSaga),
      });
      expect(gen.next(fakeAuthApi as unknown as AuthApi & AuthUser)).toEqual({
        done: false,
        value: call([fakeAuthApi, 'updateUser'], {
          disableUser: isDisabled,
          localId,
        }),
      });
      expect(gen.next()).toEqual({
        done: false,
        value: put(setUserDisabledSuccess(payload)),
      });

      expect(gen.next().done).toBe(true);
    });
  });

  describe('nukeUsers', () => {
    it('dispatches nukeUsersSuccess action', () => {
      const gen = nukeUsers();
      const fakeAuthApi = { nukeUsers: jest.fn() };

      expect(gen.next()).toEqual({
        done: false,
        value: call(configureAuthSaga),
      });
      expect(gen.next(fakeAuthApi as unknown as AuthApi & AuthUser)).toEqual({
        done: false,
        value: call([fakeAuthApi, 'nukeUsers']),
      });
      expect(gen.next()).toEqual({
        done: false,
        value: put(nukeUsersSuccess()),
      });

      expect(gen.next().done).toBe(true);
    });
  });

  describe('setAllowDuplicateEmails', () => {
    it('triggers appropriate API endpoint', () => {
      const fakeAuthApi = { updateConfig: jest.fn() };
      const gen = setAllowDuplicateEmails(setAllowDuplicateEmailsRequest(true));
      expect(gen.next()).toEqual({
        done: false,
        value: call(configureAuthSaga),
      });
      expect(gen.next(fakeAuthApi as unknown as AuthApi & AuthUser)).toEqual({
        done: false,
        value: call([fakeAuthApi, 'updateConfig'], true),
      });
      expect(gen.next()).toEqual({
        done: false,
        value: put(setAllowDuplicateEmailsSuccess(true)),
      });
    });
  });

  describe('getAllowDuplicateEmails', () => {
    it('triggers appropriate API endpoint', () => {
      const fakeAuthApi = { getConfig: jest.fn() };
      const gen = getAllowDuplicateEmails();
      expect(gen.next()).toEqual({
        done: false,
        value: call(configureAuthSaga),
      });
      expect(gen.next(fakeAuthApi as unknown as AuthApi & boolean)).toEqual({
        done: false,
        value: call([fakeAuthApi, 'getConfig']),
      });
      expect(gen.next(false as unknown as AuthApi & boolean)).toEqual({
        done: false,
        value: put(setAllowDuplicateEmailsSuccess(false)),
      });
    });
  });

  describe('configureAuthSaga', () => {
    it('returns api', () => {
      const gen = configureAuthSaga();

      expect(gen.next()).toEqual({
        done: false,
        value: getContext(AUTH_API_CONTEXT),
      });

      const fakeAuthApi = { getConfig: jest.fn() };
      expect(gen.next(fakeAuthApi)).toEqual({
        done: true,
        value: fakeAuthApi,
      });
    });
  });
});