Skip to content

Instantly share code, notes, and snippets.

@n-j-m
Last active October 14, 2017 06:21
Show Gist options
  • Select an option

  • Save n-j-m/03f1503e5fc1b6731fcd61b8d0343d61 to your computer and use it in GitHub Desktop.

Select an option

Save n-j-m/03f1503e5fc1b6731fcd61b8d0343d61 to your computer and use it in GitHub Desktop.

Revisions

  1. n-j-m renamed this gist Apr 8, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. n-j-m created this gist Apr 8, 2017.
    86 changes: 86 additions & 0 deletions Gapi client loader
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    const app_settings = require('json!./app_settings.json')

    let clientsLoaded = 0
    let auth2Loaded = false
    let signin2Loaded = false
    let auth2

    const apiLoader = {
    client() {
    return new Promise(resolve => {
    let ids = 0
    const check = () => {
    if (ids ++ > 1000 || app_settings.libraries.length === clientsLoaded) {
    resolve()
    }
    else {
    setTimeout(check, 50)
    }
    }

    check()
    })
    },

    auth() {
    return new Promise(resolve => {
    const check = () => {
    if (auth2Loaded && signin2Loaded) {
    resolve()
    }
    else {
    setTimeout(check, 50)
    }
    }
    check()
    })
    },

    gapiLoaded() {
    return new Promise(resolve => {
    const hasgapi = () => {
    if (typeof gapi !== 'undefined' && gapi.client) {
    resolve()
    }
    else {
    setTimeout(hasgapi, 50)
    }
    }
    hasgapi()
    })
    },

    getAuth2() {
    return auth2
    },

    signId() {
    const options = new gapi.auth2.SigninOptionsBuilder({
    scopes: app_settings.scopes.join(' ')
    })

    this.getAuth2().signIn(options)
    }
    }

    apiLoader.gapiLoaded()
    .then(() => {
    gapi.load('auth2', () => {
    auth2 = gapi.auth2.init({
    client_id: CLIENT_ID,
    scopes: app_settings.scopes.join(' ')
    })
    auth2Loaded = true
    })

    gapi.load('signin2', () => signin2Loaded = true)

    const incrementClients = () => clientsLoaded++

    for (let i = 0, l = app_settings.libraries.length; i < l; i++) {
    let client = app_settings.libraries[i]
    gapi.client.load(client.name, client.version, incrementClients)
    }
    })

    export default apiLoader