From 80c04f849a8e72fdfdbdf31a5fcb9ad19fb60b4b Mon Sep 17 00:00:00 2001 From: Andrew Imm Date: Fri, 11 Sep 2015 17:42:39 -0700 Subject: [PATCH] Get current user working in RN, bump to 0.5.0 --- package.json | 4 +-- src/LocalSubscriptions.js | 53 +++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 4d02c17..38505f7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "parse-react", - "version": "0.4.3", + "version": "0.5.0", "description": "Use Parse data in React applications", "homepage": "https://github.com/ParsePlatform/ParseReact", "keywords": [ @@ -17,7 +17,7 @@ "bugs": "https://github.com/ParsePlatform/ParseReact/issues", "files": [ "index.js", - "class.js", + "react-native.js", "lib/", "LICENSE", "README.md" diff --git a/src/LocalSubscriptions.js b/src/LocalSubscriptions.js index 49820ac..d16ccc8 100644 --- a/src/LocalSubscriptions.js +++ b/src/LocalSubscriptions.js @@ -45,7 +45,15 @@ var currentUser = { this.subscribers[observerId] = callbacks; var id; - if (Parse.User.current()) { + var current = null; + try { + // Attempt to get the user synchronously, if it's in cache + current = Parse.User.current(); + } catch(e) { + // Using an asynchronous storage with no cache + // Fail over to the currentAsync() fetch + } + if (current) { id = new Id('_User', Parse.User.current().id); if (!ObjectStore.getLatest(id)) { ObjectStore.storeObject(flatten(Parse.User.current())); @@ -74,31 +82,32 @@ var currentUser = { }, update: function(changes: { [key: string]: any }) { - var current = Parse.User.current(); - if (current !== null) { - for (var attr in changes) { - if (attr !== 'id' && - attr !== 'objectId' && - attr !== 'className' && - attr !== 'sessionToken' && - attr !== 'createdAt' && - attr !== 'updatedAt') { - current.set(attr, changes[attr]); + Parse.User.currentAsync().then((current) => { + if (current !== null) { + for (var attr in changes) { + if (attr !== 'id' && + attr !== 'objectId' && + attr !== 'className' && + attr !== 'sessionToken' && + attr !== 'createdAt' && + attr !== 'updatedAt') { + current.set(attr, changes[attr]); + } } + Parse.CoreManager.getUserController().setCurrentUser(current); } - Parse.CoreManager.getUserController().setCurrentUser(current); - } - for (var oid in this.subscribers) { - var latest = null; - if (current) { - latest = ObjectStore.getLatest(new Id('_User', current.id)); - if (latest === null) { - latest = flatten(current); - ObjectStore.storeObject(latest); + for (var oid in this.subscribers) { + var latest = null; + if (current) { + latest = ObjectStore.getLatest(new Id('_User', current.id)); + if (latest === null) { + latest = flatten(current); + ObjectStore.storeObject(latest); + } } + this.subscribers[oid].onNext(latest); } - this.subscribers[oid].onNext(latest); - } + }); } };