Skip to content

Commit

Permalink
Merge pull request #980 from manifoldco/dangodev/wait-for-auth
Browse files Browse the repository at this point in the history
use waitForAuth on resource components
  • Loading branch information
DangoDev authored Mar 30, 2020
2 parents 1b59da3 + 4483e5d commit fa4d872
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [0.9.8] - 2020-03-30

### Fixed

Expand Down Expand Up @@ -543,7 +543,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Changed the `manifold-resource-credentials` component to use the standalone `manifold-credentials`
component.

[unreleased]: https://github.com/manifoldco/ui/compare/v0.9.7...HEAD
[0.9.8]: https://github.com/manifoldco/ui/compare/v0.9.7...v0.9.8
[0.9.7]: https://github.com/manifoldco/ui/compare/v0.9.6...v0.9.7
[0.9.6]: https://github.com/manifoldco/ui/compare/v0.9.5...v0.9.6
[0.9.5]: https://github.com/manifoldco/ui/compare/v0.9.4...v0.9.5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { h, Component, Prop, State, Event, EventEmitter, Element } from '@stencil/core';

import { connection } from '../../global/app';
import { waitForAuthToken } from '../../utils/auth';
import logger, { loadMark } from '../../utils/logger';
import fetchAllPages from '../../utils/fetchAllPages';
import { GraphqlFetch } from '../../utils/graphqlFetch';
Expand Down Expand Up @@ -38,7 +39,17 @@ export class ManifoldDataResourceList {
@Event({ eventName: 'manifold-resourceList-click', bubbles: true }) clickEvent: EventEmitter;

@loadMark()
componentWillLoad() {
async componentWillLoad() {
// if auth token missing, wait
if (!connection.getAuthToken()) {
await waitForAuthToken(
() => connection.authToken,
connection.waitTime,
() => Promise.resolve()
);
}

// start polling
this.fetchResources();
if (!this.paused) {
this.interval = window.setInterval(() => this.fetchResources(), 3000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { h, Element, Component, Prop, State, Watch, Event, EventEmitter } from '

import ResourceTunnel from '../../data/resource';
import { connection } from '../../global/app';
import { waitForAuthToken } from '../../utils/auth';
import logger, { loadMark } from '../../utils/logger';
import {
ResourceStatusLabel,
Expand Down Expand Up @@ -42,7 +43,17 @@ export class ManifoldResourceContainer {
}

@loadMark()
componentWillLoad() {
async componentWillLoad() {
// if auth token missing, wait
if (!connection.getAuthToken()) {
await waitForAuthToken(
() => connection.authToken,
connection.waitTime,
() => Promise.resolve()
);
}

// fetch initial resource
this.fetchResource(this.resourceLabel);
}

Expand All @@ -55,11 +66,6 @@ export class ManifoldResourceContainer {
return;
}

// for this component, wait for auth before fetching
if (!connection.getAuthToken()) {
return;
}

const variables: ResourceWithOwnerQueryVariables = { resourceLabel, owner: this.ownerId };
const { data, errors } = await this.graphqlFetch<ResourceWithOwnerQuery>({
query: queryWithOwner,
Expand Down
18 changes: 12 additions & 6 deletions src/components/manifold-resource-list/manifold-resource-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { h, Component, Prop, State, Element, Watch } from '@stencil/core';

import { GraphqlFetch } from '../../utils/graphqlFetch';
import { connection } from '../../global/app';
import { waitForAuthToken } from '../../utils/auth';
import logger, { loadMark } from '../../utils/logger';
import queryWithOwner from './resources-with-owner.graphql';
import { Query, ResourceEdge, Resources_With_OwnerQueryVariables } from '../../types/graphql';
Expand Down Expand Up @@ -36,7 +37,17 @@ export class ManifoldResourceList {
}

@loadMark()
componentWillLoad() {
async componentWillLoad() {
// if auth token missing, wait
if (!connection.getAuthToken()) {
await waitForAuthToken(
() => connection.authToken,
connection.waitTime,
() => Promise.resolve()
);
}

// start polling
this.fetchResources();
if (!this.paused) {
this.interval = window.setInterval(() => this.fetchResources(), 3000);
Expand All @@ -54,11 +65,6 @@ export class ManifoldResourceList {
return;
}

// for this component, wait for auth before fetching
if (!connection.getAuthToken()) {
return;
}

const variables: Resources_With_OwnerQueryVariables = {
first: 50,
after: '',
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export async function ensureAuthToken() {
return connection.authToken;
}

await waitForAuthToken(() => connection.authToken, connection.waitTime, Promise.resolve);
await waitForAuthToken(
() => connection.authToken,
connection.waitTime,
() => Promise.resolve()
);

return connection.authToken;
}

1 comment on commit fa4d872

@vercel
Copy link

@vercel vercel bot commented on fa4d872 Mar 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.