vona-module-a-redlock 5.0.26 → 5.0.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,3 @@
1
- import type * as Redlock from '@sesamecare-oss/redlock';
2
1
  import type { VonaApplication } from 'vona';
3
- import type { IRedisClientRecord } from 'vona-module-a-redis';
4
- export declare function config(app: VonaApplication): {
5
- redlock: {
6
- clients: (keyof IRedisClientRecord)[];
7
- lockTTL: number;
8
- options: Redlock.Settings;
9
- };
10
- };
2
+ import type { ConfigRedlock } from '../types/redlock.ts';
3
+ export declare function config(app: VonaApplication): ConfigRedlock;
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { BeanInfo, BeanBase, instanceDesp, BeanScopeBase, SymbolModuleBelong } from 'vona';
1
+ import { BeanInfo, BeanBase, instanceDesp, deepExtend, BeanScopeBase, SymbolModuleBelong } from 'vona';
2
2
  import { Redlock } from '@sesamecare-oss/redlock';
3
3
  import { Service, Scope } from 'vona-module-a-bean';
4
4
  import { getRedisClientKeyPrefix } from 'vona-module-a-redis';
@@ -14,7 +14,7 @@ let ServiceRedlock = (_dec$1 = Service(), _dec2$1 = BeanInfo({
14
14
  async lock(resource, fn, options) {
15
15
  const instanceName = options?.instanceName === undefined ? this.ctx?.instanceName : options?.instanceName;
16
16
  const redlock = options?.redlock ?? this.redlockDefault;
17
- const lockTTL = options?.lockTTL ?? this.scope.config.redlock.lockTTL;
17
+ const lockTTL = options?.lockTTL ?? this.scope.config.lockTTL;
18
18
  // resource
19
19
  const _lockResource = `${getRedisClientKeyPrefix('redlock', this.app)}${instanceDesp(instanceName)}:${resource}`;
20
20
  // lock
@@ -54,28 +54,32 @@ let ServiceRedlock = (_dec$1 = Service(), _dec2$1 = BeanInfo({
54
54
  }
55
55
  get redlockDefault() {
56
56
  if (!this._redlockDefault) {
57
- this._redlockDefault = this.create(this.scope.config.redlock.options);
57
+ this._redlockDefault = this._create(this.scope.config.base);
58
58
  }
59
59
  return this._redlockDefault;
60
60
  }
61
61
  create(options) {
62
+ const options2 = options ? deepExtend({}, this.scope.config.base, options) : this.scope.config.base;
63
+ return this._create(options2);
64
+ }
65
+ _create(options) {
62
66
  // clients
63
67
  const clients = [];
64
- for (const clientName of this.scope.config.redlock.clients) {
68
+ for (const clientName of options.clients) {
65
69
  const client = this.app.bean.redis.get(clientName);
66
70
  clients.push(client);
67
71
  }
68
72
  // create
69
- return new Redlock(clients, options);
73
+ return new Redlock(clients, options.options);
70
74
  }
71
75
  }) || _class$1) || _class$1);
72
76
 
73
77
  function config(app) {
74
78
  const lockTTL = app.meta.isDev ? 8 : app.meta.isTest ? 60 : 30;
75
- return {
76
- redlock: {
79
+ const redlock = {
80
+ lockTTL: lockTTL * 1000,
81
+ base: {
77
82
  clients: ['redlock'],
78
- lockTTL: lockTTL * 1000,
79
83
  // https://github.com/mike-marcacci/node-redlock#configuration
80
84
  options: {
81
85
  driftFactor: 0.01,
@@ -85,6 +89,7 @@ function config(app) {
85
89
  }
86
90
  }
87
91
  };
92
+ return redlock;
88
93
  }
89
94
 
90
95
  var _dec, _dec2, _class;
@@ -1,6 +1,5 @@
1
- import type { Settings } from '@sesamecare-oss/redlock';
2
- import type { FunctionAsync } from 'vona';
3
- import type { IRedlockLockIsolateOptions, IRedlockLockOptions } from '../types/redlock.ts';
1
+ import type { FunctionAsync, PowerPartial } from 'vona';
2
+ import type { IRedlockClientOptions, IRedlockLockIsolateOptions, IRedlockLockOptions } from '../types/redlock.ts';
4
3
  import { Redlock } from '@sesamecare-oss/redlock';
5
4
  import { BeanBase } from 'vona';
6
5
  export declare class ServiceRedlock extends BeanBase {
@@ -8,5 +7,6 @@ export declare class ServiceRedlock extends BeanBase {
8
7
  lock<RESULT>(resource: string, fn: FunctionAsync<RESULT>, options?: IRedlockLockOptions): Promise<RESULT>;
9
8
  lockIsolate<RESULT>(resource: string, fn: FunctionAsync<RESULT>, options?: IRedlockLockIsolateOptions): Promise<RESULT>;
10
9
  private get redlockDefault();
11
- create(options: Settings): Redlock;
10
+ create(options?: PowerPartial<IRedlockClientOptions>): Redlock;
11
+ private _create;
12
12
  }
@@ -1,9 +1,18 @@
1
- import type { Redlock } from '@sesamecare-oss/redlock';
1
+ import type * as Redlock from '@sesamecare-oss/redlock';
2
2
  import type { IInstanceRecord } from 'vona';
3
3
  import type { IDbInfo } from 'vona-module-a-orm';
4
+ import type { IRedisClientRecord } from 'vona-module-a-redis';
5
+ export interface IRedlockClientOptions {
6
+ clients: (keyof IRedisClientRecord)[];
7
+ options: Redlock.Settings;
8
+ }
9
+ export interface ConfigRedlock {
10
+ lockTTL: number;
11
+ base: IRedlockClientOptions;
12
+ }
4
13
  export interface IRedlockLockOptions {
5
14
  instanceName?: keyof IInstanceRecord | undefined | null;
6
- redlock?: Redlock;
15
+ redlock?: Redlock.Redlock;
7
16
  lockTTL?: number;
8
17
  }
9
18
  export interface IRedlockLockIsolateOptions extends IRedlockLockOptions, Partial<IDbInfo> {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vona-module-a-redlock",
3
3
  "type": "module",
4
- "version": "5.0.26",
4
+ "version": "5.0.27",
5
5
  "title": "a-redlock",
6
6
  "vonaModule": {
7
7
  "dependencies": {},