forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-apps-script.lock.d.ts
60 lines (55 loc) · 2.23 KB
/
google-apps-script.lock.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Type definitions for Google Apps Script 2015-11-12
// Project: https://developers.google.com/apps-script/
// Definitions by: motemen <https://github.com/motemen/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="google-apps-script.types.d.ts" />
declare namespace GoogleAppsScript {
export module Lock {
/**
* A representation of a mutual-exclusion lock.
*
* This class allows scripts to make sure that only one instance of the script is executing a given
* section of code at a time. This is particularly useful for callbacks and triggers, where a user
* action may cause changes to a shared resource and you want to ensure that aren't collisions.
*
* The following examples shows how to use a lock in a form submit handler.
*
* // Generates a unique ticket number for every form submission.
* function onFormSubmit(e) {
* var targetCell = e.range.offset(0, e.range.getNumColumns(), 1, 1);
*
* // Get a script lock, because we're about to modify a shared resource.
* var lock = LockService.getScriptLock();
* // Wait for up to 30 seconds for other processes to finish.
* lock.waitLock(30000);
*
* var ticketNumber = Number(ScriptProperties.getProperty('lastTicketNumber')) + 1;
* ScriptProperties.setProperty('lastTicketNumber', ticketNumber);
*
* // Release the lock so that other processes can continue.
* lock.releaseLock();
*
* targetCell.setValue(ticketNumber);
* }
*
* lastTicketNumber
* ScriptProperties
*/
export interface Lock {
hasLock(): boolean;
releaseLock(): void;
tryLock(timeoutInMillis: Integer): boolean;
waitLock(timeoutInMillis: Integer): void;
}
/**
* Prevents concurrent access to sections of code. This can be useful when you have multiple users
* or processes modifying a shared resource and want to prevent collisions.
*/
export interface LockService {
getDocumentLock(): Lock;
getScriptLock(): Lock;
getUserLock(): Lock;
}
}
}
declare var LockService: GoogleAppsScript.Lock.LockService;