You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-07-13 00:10:37 +02:00
This commit is contained in:
@ -26,6 +26,16 @@ export interface Lock {
|
||||
updatedTime?: number;
|
||||
}
|
||||
|
||||
const nullLock = (): Lock => {
|
||||
return {
|
||||
clientId: '',
|
||||
clientType: LockClientType.Desktop,
|
||||
type: LockType.None,
|
||||
id: 'NULL_LOCK',
|
||||
updatedTime: Date.now(),
|
||||
};
|
||||
};
|
||||
|
||||
function lockIsActive(lock: Lock, currentDate: Date, lockTtl: number): boolean {
|
||||
return currentDate.getTime() - lock.updatedTime < lockTtl;
|
||||
}
|
||||
@ -140,6 +150,7 @@ export default class LockHandler {
|
||||
private refreshTimers_: RefreshTimers = {};
|
||||
private autoRefreshInterval_: number = 1000 * 60;
|
||||
private lockTtl_: number = defaultLockTtl;
|
||||
private enabled_ = true;
|
||||
|
||||
public constructor(api: FileApi, options: LockHandlerOptions = null) {
|
||||
if (!options) options = {};
|
||||
@ -149,6 +160,14 @@ export default class LockHandler {
|
||||
if ('autoRefreshInterval' in options) this.autoRefreshInterval_ = options.autoRefreshInterval;
|
||||
}
|
||||
|
||||
public get enabled(): boolean {
|
||||
return this.enabled_;
|
||||
}
|
||||
|
||||
public set enabled(v: boolean) {
|
||||
this.enabled_ = v;
|
||||
}
|
||||
|
||||
public get lockTtl(): number {
|
||||
return this.lockTtl_;
|
||||
}
|
||||
@ -185,6 +204,8 @@ export default class LockHandler {
|
||||
}
|
||||
|
||||
public async locks(lockType: LockType = null): Promise<Lock[]> {
|
||||
if (!this.enabled) return [];
|
||||
|
||||
if (this.useBuiltInLocks) {
|
||||
const locks = (await this.api_.listLocks()).items;
|
||||
return locks;
|
||||
@ -352,6 +373,8 @@ export default class LockHandler {
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
||||
public startAutoLockRefresh(lock: Lock, errorHandler: Function): string {
|
||||
if (!this.enabled) return '';
|
||||
|
||||
const handle = this.autoLockRefreshHandle(lock);
|
||||
if (this.refreshTimers_[handle]) {
|
||||
throw new Error(`There is already a timer refreshing this lock: ${handle}`);
|
||||
@ -409,6 +432,8 @@ export default class LockHandler {
|
||||
}
|
||||
|
||||
public stopAutoLockRefresh(lock: Lock) {
|
||||
if (!this.enabled) return;
|
||||
|
||||
const handle = this.autoLockRefreshHandle(lock);
|
||||
if (!this.refreshTimers_[handle]) {
|
||||
// Should not throw an error because lock may have been cleared in startAutoLockRefresh
|
||||
@ -422,6 +447,8 @@ export default class LockHandler {
|
||||
}
|
||||
|
||||
public async acquireLock(lockType: LockType, clientType: LockClientType, clientId: string, options: AcquireLockOptions = null): Promise<Lock> {
|
||||
if (!this.enabled) return nullLock();
|
||||
|
||||
options = {
|
||||
...defaultAcquireLockOptions(),
|
||||
...options,
|
||||
@ -437,6 +464,8 @@ export default class LockHandler {
|
||||
}
|
||||
|
||||
public async releaseLock(lockType: LockType, clientType: LockClientType, clientId: string) {
|
||||
if (!this.enabled) return;
|
||||
|
||||
if (this.useBuiltInLocks) {
|
||||
await this.api_.releaseLock(lockType, clientType, clientId);
|
||||
return;
|
||||
|
Reference in New Issue
Block a user