-
Notifications
You must be signed in to change notification settings - Fork 928
Add a type parameter to Persistence #1047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
2c93300
96d732c
59121ba
4b75267
29505fc
d03f608
e7efc7b
fd1359a
9a76422
a7b46dd
3abdbbe
5b66917
a2848ff
70e8e51
ca4bc5d
7909aef
1bd431e
9fc2b89
d4f1dcc
a94c026
fa05379
068a4af
8e46ab9
091038c
f0ed2dd
ca53e31
4193323
1a39850
80a2459
7d14f14
9e570c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,10 @@ const UNSUPPORTED_PLATFORM_ERROR_MSG = | |
' IndexedDB or is known to have an incomplete implementation. Offline' + | ||
' persistence has been disabled.'; | ||
|
||
export class IndexedDbTransaction { | ||
constructor(readonly simpleDbTransaction: SimpleDbTransaction) {} | ||
} | ||
|
||
/** | ||
* An IndexedDB-backed instance of Persistence. Data is stored persistently | ||
* across sessions. | ||
|
@@ -88,7 +92,7 @@ const UNSUPPORTED_PLATFORM_ERROR_MSG = | |
* which acts as an indicator that another tab should go ahead and take the | ||
* owner lease immediately regardless of the current lease timestamp. | ||
*/ | ||
export class IndexedDbPersistence implements Persistence { | ||
export class IndexedDbPersistence implements Persistence<IndexedDbTransaction> { | ||
/** | ||
* The name of the main (and currently only) IndexedDB database. this name is | ||
* appended to the prefix provided to the IndexedDbPersistence constructor. | ||
|
@@ -162,21 +166,21 @@ export class IndexedDbPersistence implements Persistence { | |
return this._started; | ||
} | ||
|
||
getMutationQueue(user: User): MutationQueue { | ||
getMutationQueue(user: User): MutationQueue<IndexedDbTransaction> { | ||
return IndexedDbMutationQueue.forUser(user, this.serializer); | ||
} | ||
|
||
getQueryCache(): QueryCache { | ||
getQueryCache(): QueryCache<IndexedDbTransaction> { | ||
return new IndexedDbQueryCache(this.serializer); | ||
} | ||
|
||
getRemoteDocumentCache(): RemoteDocumentCache { | ||
getRemoteDocumentCache(): RemoteDocumentCache<IndexedDbTransaction> { | ||
return new IndexedDbRemoteDocumentCache(this.serializer); | ||
} | ||
|
||
runTransaction<T>( | ||
action: string, | ||
operation: (transaction: SimpleDbTransaction) => PersistencePromise<T> | ||
operation: (transaction: IndexedDbTransaction) => PersistencePromise<T> | ||
): Promise<T> { | ||
if (this.persistenceError) { | ||
return Promise.reject(this.persistenceError); | ||
|
@@ -186,10 +190,17 @@ export class IndexedDbPersistence implements Persistence { | |
|
||
// Do all transactions as readwrite against all object stores, since we | ||
// are the only reader/writer. | ||
return this.simpleDb.runTransaction('readwrite', ALL_STORES, txn => { | ||
// Verify that we still have the owner lease as part of every transaction. | ||
return this.ensureOwnerLease(txn).next(() => operation(txn)); | ||
}); | ||
return this.simpleDb.runTransaction( | ||
'readwrite', | ||
ALL_STORES, | ||
simpleDbTxn => { | ||
// Verify that we still have the owner lease as part of every transaction. | ||
const txn = new IndexedDbTransaction(simpleDbTxn); | ||
return this.ensureOwnerLease(txn.simpleDbTransaction).next(() => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's all the same, I'd rather keep the longer name. I think the abbreviation is a little harder to read. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was suggesting you use the existing variable that's in scope. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah. That makes more sense. Done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a general rule if I ever ask somebody to abbreviate a name in code review feedback you can assume my account has been hacked. 😁 |
||
operation(txn) | ||
); | ||
} | ||
); | ||
} | ||
|
||
static isAvailable(): boolean { | ||
|
@@ -326,12 +337,16 @@ export class IndexedDbPersistence implements Persistence { | |
// would increase the chances of us not refreshing on time if the queue is | ||
// backed up for some reason. | ||
this.ownerLeaseRefreshHandle = setInterval(() => { | ||
const txResult = this.runTransaction('Refresh owner timestamp', txn => { | ||
// NOTE: We don't need to validate the current owner contents, since | ||
// runTransaction does that automatically. | ||
const store = txn.store<DbOwnerKey, DbOwner>(DbOwner.store); | ||
return store.put('owner', new DbOwner(this.ownerId, Date.now())); | ||
}); | ||
const txResult = this.simpleDb.runTransaction( | ||
'readwrite', | ||
ALL_STORES, | ||
txn => { | ||
// NOTE: We don't need to validate the current owner contents, since | ||
// runTransaction does that automatically. | ||
const store = txn.store<DbOwnerKey, DbOwner>(DbOwner.store); | ||
return store.put('owner', new DbOwner(this.ownerId, Date.now())); | ||
} | ||
); | ||
|
||
txResult.catch(reason => { | ||
// Probably means we lost the lease. Report the error and stop trying to | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
obsolete