Full PromisifiedStorage + Simplifications

This commit is contained in:
Chi Vinh Le
2018-09-06 23:53:29 +02:00
parent 3a1b043eb7
commit e132087682
41 changed files with 642 additions and 238 deletions
@@ -33,24 +33,3 @@ it("Removes auth token from localStorage", () => {
commit(environment, { authToken: null }, context as any);
expect(context.localStorage.getItem("authToken")).toBeNull();
});
it("Sets auth token to pymLocalStorage", async () => {
const context = {
pymLocalStorage: createInMemoryStorage(),
localStorage: createInMemoryStorage(),
};
const authToken = "auth token";
commit(environment, { authToken }, context as any);
expect(source.get(LOCAL_ID)!.authToken).toEqual(authToken);
expect(await context.pymLocalStorage.getItem("authToken")).toEqual(authToken);
});
it("Removes auth token from pymLocalStorage", async () => {
const context = {
pymLocalStorage: createInMemoryStorage(),
localStorage: createInMemoryStorage(),
};
localStorage.setItem("authToken", "tmp");
commit(environment, { authToken: null }, context as any);
expect(await context.pymLocalStorage.getItem("authToken")).toBeNull();
});
@@ -13,16 +13,15 @@ export type SetAuthTokenMutation = (input: SetAuthTokenInput) => Promise<void>;
export async function commit(
environment: Environment,
input: SetAuthTokenInput,
{ localStorage, pymLocalStorage }: TalkContext
{ localStorage }: TalkContext
) {
return commitLocalUpdate(environment, store => {
const record = store.get(LOCAL_ID)!;
record.setValue(input.authToken, "authToken");
const storage = pymLocalStorage || localStorage;
if (input.authToken) {
storage.setItem("authToken", input.authToken);
localStorage.setItem("authToken", input.authToken);
} else {
storage.removeItem("authToken");
localStorage.removeItem("authToken");
}
// Increment auth revision to indicate a change in auth state.
record.setValue(record.getValue("authRevision") + 1, "authRevision");