@daiso-tech/core 0.26.1 → 0.28.0
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.
- package/README.md +13 -3
- package/dist/async/utilities/delay/delay.d.ts +1 -12
- package/dist/async/utilities/delay/delay.js +1 -12
- package/dist/async/utilities/delay/delay.js.map +1 -1
- package/dist/async/utilities/lazy-promise/lazy-promise.d.ts +143 -71
- package/dist/async/utilities/lazy-promise/lazy-promise.js +223 -142
- package/dist/async/utilities/lazy-promise/lazy-promise.js.map +1 -1
- package/dist/async/utilities/retry/retry-or-fail.d.ts +2 -0
- package/dist/async/utilities/retry/retry-or-fail.js.map +1 -1
- package/dist/cache/contracts/_module-exports.d.ts +1 -0
- package/dist/cache/contracts/_module-exports.js +1 -0
- package/dist/cache/contracts/_module-exports.js.map +1 -1
- package/dist/cache/contracts/cache-adapter.contract.d.ts +19 -21
- package/dist/cache/contracts/cache-adapter.contract.js +1 -4
- package/dist/cache/contracts/cache-adapter.contract.js.map +1 -1
- package/dist/cache/contracts/cache-factory.contract.d.ts +1 -1
- package/dist/cache/contracts/cache.contract.d.ts +144 -96
- package/dist/cache/contracts/cache.contract.js +1 -2
- package/dist/cache/contracts/cache.contract.js.map +1 -1
- package/dist/cache/contracts/cache.errors.js +2 -5
- package/dist/cache/contracts/cache.errors.js.map +1 -1
- package/dist/cache/contracts/cache.events.d.ts +10 -10
- package/dist/cache/contracts/cache.events.js +2 -5
- package/dist/cache/contracts/cache.events.js.map +1 -1
- package/dist/cache/contracts/database-cache-adapter.contract.d.ts +89 -0
- package/dist/cache/contracts/database-cache-adapter.contract.js +5 -0
- package/dist/cache/contracts/database-cache-adapter.contract.js.map +1 -0
- package/dist/cache/implementations/adapters/{kysely-sqlite-cache-adapter → kysely-cache-adapter}/_module.d.ts +1 -1
- package/dist/cache/implementations/adapters/kysely-cache-adapter/_module.js +2 -0
- package/dist/cache/implementations/adapters/kysely-cache-adapter/_module.js.map +1 -0
- package/dist/cache/implementations/adapters/kysely-cache-adapter/kysely-cache-adapter.d.ts +73 -0
- package/dist/cache/implementations/adapters/kysely-cache-adapter/kysely-cache-adapter.js +224 -0
- package/dist/cache/implementations/adapters/kysely-cache-adapter/kysely-cache-adapter.js.map +1 -0
- package/dist/cache/implementations/adapters/libsql-cache-adapter/libsql-cache-adapter.d.ts +29 -39
- package/dist/cache/implementations/adapters/libsql-cache-adapter/libsql-cache-adapter.js +45 -54
- package/dist/cache/implementations/adapters/libsql-cache-adapter/libsql-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/memory-cache-adapter/memory-cache-adapter.d.ts +17 -33
- package/dist/cache/implementations/adapters/memory-cache-adapter/memory-cache-adapter.js +43 -52
- package/dist/cache/implementations/adapters/memory-cache-adapter/memory-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter-serde.d.ts +13 -0
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter-serde.js +38 -0
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter-serde.js.map +1 -0
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.d.ts +27 -33
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.js +145 -197
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/no-op-cache-adapter/no-op-cache-adapter.d.ts +14 -13
- package/dist/cache/implementations/adapters/no-op-cache-adapter/no-op-cache-adapter.js +19 -17
- package/dist/cache/implementations/adapters/no-op-cache-adapter/no-op-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter-serde.d.ts +13 -0
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter-serde.js +39 -0
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter-serde.js.map +1 -0
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter.d.ts +16 -22
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter.js +51 -132
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/redis-cache-adapter/utilities.d.ts +13 -0
- package/dist/cache/implementations/adapters/redis-cache-adapter/utilities.js +69 -0
- package/dist/cache/implementations/adapters/redis-cache-adapter/utilities.js.map +1 -0
- package/dist/cache/implementations/adapters/sqlite-cache-adapter/sqlite-cache-adapter.d.ts +29 -41
- package/dist/cache/implementations/adapters/sqlite-cache-adapter/sqlite-cache-adapter.js +46 -55
- package/dist/cache/implementations/adapters/sqlite-cache-adapter/sqlite-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/derivables/_module-exports.d.ts +1 -1
- package/dist/cache/implementations/derivables/_module-exports.js +1 -1
- package/dist/cache/implementations/derivables/_module-exports.js.map +1 -1
- package/dist/cache/implementations/derivables/cache/_module.d.ts +1 -0
- package/dist/cache/implementations/derivables/cache/_module.js +1 -0
- package/dist/cache/implementations/derivables/cache/_module.js.map +1 -1
- package/dist/cache/implementations/derivables/cache/cache.d.ts +269 -507
- package/dist/cache/implementations/derivables/cache/cache.js +538 -822
- package/dist/cache/implementations/derivables/cache/cache.js.map +1 -1
- package/dist/cache/implementations/derivables/cache/database-cache-adapter.d.ts +21 -0
- package/dist/cache/implementations/derivables/cache/database-cache-adapter.js +110 -0
- package/dist/cache/implementations/derivables/cache/database-cache-adapter.js.map +1 -0
- package/dist/cache/implementations/derivables/cache/is-database-cache-adapter.d.ts +9 -0
- package/dist/cache/implementations/derivables/cache/is-database-cache-adapter.js +20 -0
- package/dist/cache/implementations/derivables/cache/is-database-cache-adapter.js.map +1 -0
- package/dist/cache/implementations/derivables/cache-factory/cache-factory.d.ts +69 -105
- package/dist/cache/implementations/derivables/cache-factory/cache-factory.js +117 -87
- package/dist/cache/implementations/derivables/cache-factory/cache-factory.js.map +1 -1
- package/dist/cache/implementations/test-utilities/_module-exports.d.ts +1 -0
- package/dist/cache/implementations/test-utilities/_module-exports.js +1 -0
- package/dist/cache/implementations/test-utilities/_module-exports.js.map +1 -1
- package/dist/cache/implementations/test-utilities/cache-adapter.test-suite.d.ts +9 -11
- package/dist/cache/implementations/test-utilities/cache-adapter.test-suite.js +246 -247
- package/dist/cache/implementations/test-utilities/cache-adapter.test-suite.js.map +1 -1
- package/dist/cache/implementations/test-utilities/cache.test-suite.d.ts +16 -30
- package/dist/cache/implementations/test-utilities/cache.test-suite.js +271 -996
- package/dist/cache/implementations/test-utilities/cache.test-suite.js.map +1 -1
- package/dist/cache/implementations/test-utilities/database-cache-dapter.test-suite.d.ts +60 -0
- package/dist/cache/implementations/test-utilities/database-cache-dapter.test-suite.js +698 -0
- package/dist/cache/implementations/test-utilities/database-cache-dapter.test-suite.js.map +1 -0
- package/dist/collection/contracts/async-collection.contract.d.ts +2 -3
- package/dist/collection/contracts/async-collection.contract.js +1 -12
- package/dist/collection/contracts/async-collection.contract.js.map +1 -1
- package/dist/collection/contracts/collection.contract.d.ts +1 -1
- package/dist/collection/contracts/collection.errors.d.ts +1 -1
- package/dist/collection/contracts/collection.errors.js +3 -6
- package/dist/collection/contracts/collection.errors.js.map +1 -1
- package/dist/collection/implementations/_shared.d.ts +4 -4
- package/dist/collection/implementations/_shared.js +6 -9
- package/dist/collection/implementations/_shared.js.map +1 -1
- package/dist/collection/implementations/async-iterable-collection/_shared/async-delay-iterable.js +2 -2
- package/dist/collection/implementations/async-iterable-collection/_shared/async-delay-iterable.js.map +1 -1
- package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.d.ts +5 -21
- package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.js +12 -8
- package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.js.map +1 -1
- package/dist/collection/implementations/iterable-collection/iterable-collection.d.ts +1 -1
- package/dist/collection/implementations/iterable-collection/iterable-collection.js +1 -1
- package/dist/collection/implementations/list-collection/list-collection.d.ts +1 -1
- package/dist/collection/implementations/list-collection/list-collection.js +1 -1
- package/dist/event-bus/contracts/event-bus-adapter.contract.d.ts +0 -9
- package/dist/event-bus/contracts/event-bus.contract.d.ts +5 -5
- package/dist/event-bus/contracts/event-bus.contract.js.map +1 -1
- package/dist/event-bus/contracts/event-bus.errors.js +2 -5
- package/dist/event-bus/contracts/event-bus.errors.js.map +1 -1
- package/dist/event-bus/implementations/adapters/memory-event-bus-adapter/memory-event-bus-adapter.d.ts +9 -29
- package/dist/event-bus/implementations/adapters/memory-event-bus-adapter/memory-event-bus-adapter.js +14 -35
- package/dist/event-bus/implementations/adapters/memory-event-bus-adapter/memory-event-bus-adapter.js.map +1 -1
- package/dist/event-bus/implementations/adapters/no-op-event-bus-adapter/no-op-event-bus-adapter.d.ts +2 -4
- package/dist/event-bus/implementations/adapters/no-op-event-bus-adapter/no-op-event-bus-adapter.js +2 -8
- package/dist/event-bus/implementations/adapters/no-op-event-bus-adapter/no-op-event-bus-adapter.js.map +1 -1
- package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.d.ts +8 -15
- package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.js +15 -34
- package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.js.map +1 -1
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.d.ts +85 -289
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.js +128 -325
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.js.map +1 -1
- package/dist/event-bus/implementations/derivables/event-bus/listener-store.d.ts +13 -0
- package/dist/event-bus/implementations/derivables/event-bus/listener-store.js +42 -0
- package/dist/event-bus/implementations/derivables/event-bus/listener-store.js.map +1 -0
- package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.d.ts +103 -86
- package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.js +135 -77
- package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.js.map +1 -1
- package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.d.ts +2 -48
- package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.js +31 -122
- package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.js.map +1 -1
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.d.ts +2 -50
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.js +41 -89
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.js.map +1 -1
- package/dist/lock/contracts/database-lock-adapter.contract.d.ts +0 -9
- package/dist/lock/contracts/lock-adapter.contract.d.ts +0 -9
- package/dist/lock/contracts/lock-provider.contract.d.ts +20 -6
- package/dist/lock/contracts/lock.contract.d.ts +37 -8
- package/dist/lock/contracts/lock.errors.js +2 -5
- package/dist/lock/contracts/lock.errors.js.map +1 -1
- package/dist/lock/contracts/lock.events.js +2 -5
- package/dist/lock/contracts/lock.events.js.map +1 -1
- package/dist/lock/implementations/adapters/_module-exports.d.ts +1 -0
- package/dist/lock/implementations/adapters/_module-exports.js +1 -0
- package/dist/lock/implementations/adapters/_module-exports.js.map +1 -1
- package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.d.ts +0 -5
- package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js +2 -21
- package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/libsql-lock-adapter/libsql-lock-adapter.d.ts +11 -16
- package/dist/lock/implementations/adapters/libsql-lock-adapter/libsql-lock-adapter.js +11 -20
- package/dist/lock/implementations/adapters/libsql-lock-adapter/libsql-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.d.ts +7 -26
- package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.js +7 -35
- package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.d.ts +13 -20
- package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js +13 -37
- package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/no-op-lock-adapter/_module.d.ts +1 -0
- package/dist/lock/implementations/adapters/no-op-lock-adapter/_module.js +2 -0
- package/dist/lock/implementations/adapters/no-op-lock-adapter/_module.js.map +1 -0
- package/dist/lock/implementations/adapters/no-op-lock-adapter/no-op-lock-adapter.d.ts +17 -0
- package/dist/lock/implementations/adapters/no-op-lock-adapter/no-op-lock-adapter.js +24 -0
- package/dist/lock/implementations/adapters/no-op-lock-adapter/no-op-lock-adapter.js.map +1 -0
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.d.ts +4 -22
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js +4 -31
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/sqlite-lock-adapter/sqlite-lock-adapter.d.ts +14 -20
- package/dist/lock/implementations/adapters/sqlite-lock-adapter/sqlite-lock-adapter.js +12 -21
- package/dist/lock/implementations/adapters/sqlite-lock-adapter/sqlite-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.d.ts +0 -2
- package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.js +2 -8
- package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/is-database-lock-adapter.js +1 -5
- package/dist/lock/implementations/derivables/lock-provider/is-database-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.d.ts +181 -369
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.js +259 -407
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.d.ts +15 -17
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.js +42 -40
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-state.d.ts +3 -3
- package/dist/lock/implementations/derivables/lock-provider/lock-state.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock.d.ts +194 -680
- package/dist/lock/implementations/derivables/lock-provider/lock.js +284 -744
- package/dist/lock/implementations/derivables/lock-provider/lock.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.d.ts +106 -143
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js +173 -101
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js.map +1 -1
- package/dist/lock/implementations/test-utilities/database-lock-adapter.test-suite.d.ts +4 -5
- package/dist/lock/implementations/test-utilities/database-lock-adapter.test-suite.js +172 -247
- package/dist/lock/implementations/test-utilities/database-lock-adapter.test-suite.js.map +1 -1
- package/dist/lock/implementations/test-utilities/lock-adapter.test-suite.d.ts +5 -8
- package/dist/lock/implementations/test-utilities/lock-adapter.test-suite.js +93 -98
- package/dist/lock/implementations/test-utilities/lock-adapter.test-suite.js.map +1 -1
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.d.ts +15 -20
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js +295 -145
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js.map +1 -1
- package/dist/serde/implementations/adapters/mongodb-serde/mongodb-serde.js +1 -2
- package/dist/serde/implementations/adapters/mongodb-serde/mongodb-serde.js.map +1 -1
- package/dist/serde/implementations/adapters/no-op-serde-adapter/no-op-serde-adapter.d.ts +3 -2
- package/dist/serde/implementations/adapters/no-op-serde-adapter/no-op-serde-adapter.js +2 -1
- package/dist/serde/implementations/adapters/no-op-serde-adapter/no-op-serde-adapter.js.map +1 -1
- package/dist/serde/implementations/adapters/redis-serde/redis-serde.js +1 -2
- package/dist/serde/implementations/adapters/redis-serde/redis-serde.js.map +1 -1
- package/dist/serde/implementations/adapters/sql-serde/sql-serde.js +1 -2
- package/dist/serde/implementations/adapters/sql-serde/sql-serde.js.map +1 -1
- package/dist/serde/implementations/adapters/super-json-serde-adapter/super-json-serde-adapter.d.ts +2 -2
- package/dist/serde/implementations/adapters/super-json-serde-adapter/super-json-serde-adapter.js +2 -2
- package/dist/serde/implementations/adapters/super-json-serde-adapter/super-json-serde-adapter.js.map +1 -1
- package/dist/serde/implementations/derivables/serde.d.ts +14 -14
- package/dist/serde/implementations/derivables/serde.js +14 -15
- package/dist/serde/implementations/derivables/serde.js.map +1 -1
- package/dist/serde/implementations/test-utilities/flexible-serde-adapter.test-suite.d.ts +4 -4
- package/dist/serde/implementations/test-utilities/flexible-serde-adapter.test-suite.js +3 -3
- package/dist/serde/implementations/test-utilities/flexible-serde.test-suite.d.ts +5 -5
- package/dist/serde/implementations/test-utilities/flexible-serde.test-suite.js +4 -4
- package/dist/utilities/_module-exports.d.ts +2 -3
- package/dist/utilities/_module-exports.js +2 -3
- package/dist/utilities/_module-exports.js.map +1 -1
- package/dist/utilities/classes/_module.d.ts +4 -0
- package/dist/utilities/classes/_module.js +5 -0
- package/dist/utilities/classes/_module.js.map +1 -0
- package/dist/utilities/classes/key-prefixer/_module.d.ts +2 -0
- package/dist/utilities/classes/key-prefixer/_module.js +3 -0
- package/dist/utilities/classes/key-prefixer/_module.js.map +1 -0
- package/dist/utilities/classes/key-prefixer/key-prefixer.contract.d.ts +28 -0
- package/dist/utilities/classes/key-prefixer/key-prefixer.contract.js +5 -0
- package/dist/utilities/classes/key-prefixer/key-prefixer.contract.js.map +1 -0
- package/dist/utilities/classes/key-prefixer/key-prefixer.d.ts +73 -0
- package/dist/utilities/classes/key-prefixer/key-prefixer.js +135 -0
- package/dist/utilities/classes/key-prefixer/key-prefixer.js.map +1 -0
- package/dist/utilities/classes/kysely-table-name-transformer-plugin/_module.d.ts +1 -0
- package/dist/utilities/classes/kysely-table-name-transformer-plugin/_module.js +2 -0
- package/dist/utilities/classes/kysely-table-name-transformer-plugin/_module.js.map +1 -0
- package/dist/utilities/classes/kysely-table-name-transformer-plugin/kysely-table-name-transformer-plugin.js.map +1 -0
- package/dist/utilities/classes/pipeline/_module.d.ts +1 -0
- package/dist/utilities/classes/pipeline/_module.js +2 -0
- package/dist/utilities/classes/pipeline/_module.js.map +1 -0
- package/dist/utilities/{pipeline → classes/pipeline}/pipeline.d.ts +3 -2
- package/dist/utilities/{pipeline → classes/pipeline}/pipeline.js +3 -2
- package/dist/utilities/classes/pipeline/pipeline.js.map +1 -0
- package/dist/utilities/classes/time-span/_module.d.ts +1 -0
- package/dist/utilities/classes/time-span/_module.js +2 -0
- package/dist/utilities/classes/time-span/_module.js.map +1 -0
- package/dist/utilities/{time-span → classes/time-span}/time-span.d.ts +2 -1
- package/dist/utilities/{time-span → classes/time-span}/time-span.js +1 -0
- package/dist/utilities/classes/time-span/time-span.js.map +1 -0
- package/dist/utilities/{_constants.d.ts → constants.d.ts} +0 -3
- package/dist/utilities/constants.js +5 -0
- package/dist/utilities/constants.js.map +1 -0
- package/dist/utilities/contracts/_module.d.ts +3 -1
- package/dist/utilities/contracts/_module.js +3 -1
- package/dist/utilities/contracts/_module.js.map +1 -1
- package/dist/utilities/contracts/prunable.contract.d.ts +11 -0
- package/dist/utilities/contracts/prunable.contract.js +5 -0
- package/dist/utilities/contracts/prunable.contract.js.map +1 -0
- package/dist/utilities/contracts/sqlite-database.contract.d.ts +26 -0
- package/dist/utilities/contracts/sqlite-database.contract.js +5 -0
- package/dist/utilities/contracts/sqlite-database.contract.js.map +1 -0
- package/dist/utilities/functions.d.ts +34 -2
- package/dist/utilities/functions.js +72 -4
- package/dist/utilities/functions.js.map +1 -1
- package/dist/utilities/types.d.ts +49 -16
- package/package.json +14 -13
- package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/_module.js +0 -2
- package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/_module.js.map +0 -1
- package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/kysely-sqlite-cache-adapter.d.ts +0 -61
- package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/kysely-sqlite-cache-adapter.js +0 -318
- package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/kysely-sqlite-cache-adapter.js.map +0 -1
- package/dist/utilities/_constants.js +0 -8
- package/dist/utilities/_constants.js.map +0 -1
- package/dist/utilities/kysely/_module.d.ts +0 -1
- package/dist/utilities/kysely/_module.js +0 -2
- package/dist/utilities/kysely/_module.js.map +0 -1
- package/dist/utilities/kysely/kysely-table-name-transformer-plugin.js.map +0 -1
- package/dist/utilities/pipeline/_module.d.ts +0 -1
- package/dist/utilities/pipeline/_module.js +0 -2
- package/dist/utilities/pipeline/_module.js.map +0 -1
- package/dist/utilities/pipeline/pipeline.js.map +0 -1
- package/dist/utilities/time-span/_module.d.ts +0 -1
- package/dist/utilities/time-span/_module.js +0 -2
- package/dist/utilities/time-span/_module.js.map +0 -1
- package/dist/utilities/time-span/time-span.js.map +0 -1
- /package/dist/utilities/{kysely → classes/kysely-table-name-transformer-plugin}/kysely-table-name-transformer-plugin.d.ts +0 -0
- /package/dist/utilities/{kysely → classes/kysely-table-name-transformer-plugin}/kysely-table-name-transformer-plugin.js +0 -0
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Lock
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import {} from "../../../../utilities/_module-exports.js";
|
|
5
|
+
import { resolveOneOrMoreStr } from "../../../../utilities/_module-exports.js";
|
|
6
|
+
import { KeyAcquiredLockEvent, KeyAlreadyAcquiredLockError, KeyAlreadyAcquiredLockEvent, KeyForceReleasedLockEvent, KeyRefreshedLockEvent, KeyReleasedLockEvent, UnableToAquireLockError, UnexpectedErrorLockEvent, UnownedRefreshLockError, UnownedRefreshLockEvent, UnownedReleaseLockError, UnownedReleaseLockEvent, } from "../../../../lock/contracts/_module-exports.js";
|
|
7
|
+
import {} from "../../../../lock/contracts/_module-exports.js";
|
|
8
|
+
import { LazyPromise } from "../../../../async/_module-exports.js";
|
|
7
9
|
/**
|
|
8
10
|
* IMPORTANT: This class is not intended to be instantiated directly, instead it should be created by the <i>LockProvider</i> class instance.
|
|
9
11
|
* @group Derivables
|
|
@@ -14,149 +16,153 @@ export class Lock {
|
|
|
14
16
|
*/
|
|
15
17
|
static serialize(deserializedValue) {
|
|
16
18
|
return {
|
|
17
|
-
group: deserializedValue.
|
|
18
|
-
key: deserializedValue.key,
|
|
19
|
+
group: deserializedValue.group,
|
|
20
|
+
key: deserializedValue.key.resolved,
|
|
19
21
|
owner: deserializedValue.owner,
|
|
20
22
|
ttlInMs: deserializedValue.ttl?.toMilliseconds() ?? null,
|
|
21
|
-
expirationInMs: deserializedValue.
|
|
23
|
+
expirationInMs: deserializedValue.lockState.get()?.getTime() ?? null,
|
|
22
24
|
};
|
|
23
25
|
}
|
|
24
|
-
|
|
26
|
+
createLazyPromise;
|
|
27
|
+
adapterPromise;
|
|
28
|
+
lockState;
|
|
25
29
|
lockEventBus;
|
|
26
|
-
|
|
30
|
+
lockProviderEventDispatcher;
|
|
27
31
|
key;
|
|
28
32
|
owner;
|
|
29
33
|
ttl;
|
|
30
34
|
defaultBlockingInterval;
|
|
31
35
|
defaultBlockingTime;
|
|
32
36
|
defaultRefreshTime;
|
|
33
|
-
|
|
34
|
-
state;
|
|
37
|
+
group;
|
|
35
38
|
/**
|
|
36
39
|
* @internal
|
|
37
40
|
*/
|
|
38
41
|
constructor(settings) {
|
|
39
|
-
const {
|
|
42
|
+
const { group, createLazyPromise, adapterPromise, lockState, lockEventBus, lockProviderEventDispatcher, key, owner, ttl, expirationInMs, defaultBlockingInterval, defaultBlockingTime, defaultRefreshTime, } = settings;
|
|
43
|
+
this.group = group;
|
|
44
|
+
this.createLazyPromise = createLazyPromise;
|
|
45
|
+
this.adapterPromise = adapterPromise;
|
|
46
|
+
this.lockState = lockState;
|
|
47
|
+
this.lockState.set(expirationInMs);
|
|
48
|
+
this.lockEventBus = lockEventBus;
|
|
40
49
|
this.lockProviderEventDispatcher = lockProviderEventDispatcher;
|
|
41
|
-
this.
|
|
50
|
+
this.key = key;
|
|
51
|
+
this.owner = resolveOneOrMoreStr(owner);
|
|
52
|
+
this.ttl = ttl;
|
|
42
53
|
this.defaultBlockingInterval = defaultBlockingInterval;
|
|
43
54
|
this.defaultBlockingTime = defaultBlockingTime;
|
|
44
55
|
this.defaultRefreshTime = defaultRefreshTime;
|
|
45
|
-
this.adapter = adapter;
|
|
46
|
-
this.key = key;
|
|
47
|
-
this.owner = owner;
|
|
48
|
-
this.ttl = ttl;
|
|
49
|
-
this.lazyPromiseSettings = lazyPromiseSettings;
|
|
50
|
-
this.state = new LockState(stateRecord, key);
|
|
51
|
-
this.state.set(expirationInMs);
|
|
52
56
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
/**
|
|
58
|
+
* You can listen to the following <i>{@link LockEvents}</i> of the <i>{@link Lock}</i> instance.
|
|
59
|
+
* To understand how this method works, refer to <i>{@link IEventListenable}</i>.
|
|
60
|
+
*/
|
|
61
|
+
addListener(event, listener) {
|
|
62
|
+
return this.lockEventBus.addListener(event, listener);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* You can listen to the following <i>{@link LockEvents}</i> of the <i>{@link Lock}</i> instance.
|
|
66
|
+
* To understand how this method works, refer to <i>{@link IEventListenable}</i>.
|
|
67
|
+
*/
|
|
68
|
+
addListenerMany(events, listener) {
|
|
69
|
+
return this.lockEventBus.addListenerMany(events, listener);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* You can listen to the following <i>{@link LockEvents}</i> of the <i>{@link Lock}</i> instance.
|
|
73
|
+
* To understand how this method works, refer to <i>{@link IEventListenable}</i>.
|
|
74
|
+
*/
|
|
75
|
+
removeListener(event, listener) {
|
|
76
|
+
return this.lockEventBus.removeListener(event, listener);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* You can listen to the following <i>{@link LockEvents}</i> of the <i>{@link Lock}</i> instance.
|
|
80
|
+
* To understand how this method works, refer to <i>{@link IEventListenable}</i>.
|
|
81
|
+
*/
|
|
82
|
+
removeListenerMany(events, listener) {
|
|
83
|
+
return this.lockEventBus.removeListenerMany(events, listener);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* You can listen to the following <i>{@link LockEvents}</i> of lock instance.
|
|
87
|
+
* To understand how this method works, refer to <i>{@link IEventListenable}</i>.
|
|
88
|
+
*/
|
|
89
|
+
listenOnce(event, listener) {
|
|
90
|
+
return this.lockEventBus.listenOnce(event, listener);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* You can listen to the following <i>{@link LockEvents}</i> of lock instance.
|
|
94
|
+
* To understand how this method works, refer to <i>{@link IEventListenable}</i>.
|
|
95
|
+
*/
|
|
96
|
+
asPromise(event) {
|
|
97
|
+
return this.lockEventBus.asPromise(event);
|
|
59
98
|
}
|
|
60
99
|
/**
|
|
100
|
+
* You can listen to the following <i>{@link LockEvents}</i> of lock instance.
|
|
101
|
+
* To understand how this method works, refer to <i>{@link IEventListenable}</i>.
|
|
102
|
+
*/
|
|
103
|
+
subscribe(event, listener) {
|
|
104
|
+
return this.lockEventBus.subscribe(event, listener);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* You can listen to the following <i>{@link LockEvents}</i> of lock instance.
|
|
108
|
+
* To understand how this method works, refer to <i>{@link IEventListenable}</i>.
|
|
109
|
+
*/
|
|
110
|
+
subscribeMany(events, listener) {
|
|
111
|
+
return this.lockEventBus.subscribeMany(events, listener);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* You can pass in a sync function or async function.
|
|
61
115
|
* @example
|
|
62
116
|
* ```ts
|
|
63
|
-
* import {
|
|
64
|
-
* import {
|
|
65
|
-
* import {
|
|
66
|
-
* import
|
|
67
|
-
* import {
|
|
68
|
-
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
69
|
-
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
70
|
-
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
71
|
-
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
72
|
-
* import { delay } from "@daiso-tech/core/async";
|
|
117
|
+
* import { LockProvider } from "@daiso-tech/core/lock";
|
|
118
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/adapters";
|
|
119
|
+
* import { KeyPrefixer, TimeSpan, LazyPromise } from "@daiso-tech/core/utilities";
|
|
120
|
+
* import { Serde } from "@daiso-tech/core/adapter";
|
|
121
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/adapter/adapters";
|
|
73
122
|
*
|
|
74
|
-
* const
|
|
75
|
-
* adapter: new
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
79
|
-
* serde,
|
|
80
|
-
* adapter: new MemoryLockAdapter({
|
|
81
|
-
* rootGroup: "@global"
|
|
82
|
-
* }),
|
|
83
|
-
* eventBus,
|
|
123
|
+
* const lockProvider = new LockProvider({
|
|
124
|
+
* adapter: new MemoryLockAdapter(),
|
|
125
|
+
* keyPrefixer: new KeyPrefixer("lock"),
|
|
126
|
+
* serde: new Serde(new SuperJsonSerdeAdapter())
|
|
84
127
|
* });
|
|
85
128
|
*
|
|
86
129
|
* const lock = lockProvider.create("a");
|
|
87
130
|
*
|
|
88
|
-
* async
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* console.log("END: ", 2);
|
|
94
|
-
* });
|
|
95
|
-
* }
|
|
96
|
-
*
|
|
97
|
-
* await Promise.allSettled([
|
|
98
|
-
* fn(),
|
|
99
|
-
* fn(),
|
|
100
|
-
* fn(),
|
|
101
|
-
* fn(),
|
|
102
|
-
* ]);
|
|
131
|
+
* await lock.run(async () => {
|
|
132
|
+
* console.log("START");
|
|
133
|
+
* await LazyPromise.delay(TimeSpan.fromSeconds(10));
|
|
134
|
+
* console.log("END");
|
|
135
|
+
* });
|
|
103
136
|
* ```
|
|
104
137
|
*
|
|
105
|
-
* You can also pass
|
|
138
|
+
* You can also pass in a <i>{@link LazyPromise}</i>. This is useful because all other components in this library returns <i>{@link LazyPromise}</i>.
|
|
106
139
|
* @example
|
|
107
140
|
* ```ts
|
|
108
|
-
* import {
|
|
109
|
-
* import {
|
|
110
|
-
* import {
|
|
111
|
-
* import
|
|
112
|
-
* import {
|
|
113
|
-
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
114
|
-
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
115
|
-
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
116
|
-
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
117
|
-
* import { delay } from "@daiso-tech/core/async";
|
|
118
|
-
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
119
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
120
|
-
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
121
|
-
*
|
|
122
|
-
* const eventBus = new EventBus({
|
|
123
|
-
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
124
|
-
* });
|
|
125
|
-
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
126
|
-
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
127
|
-
* serde,
|
|
128
|
-
* adapter: new MemoryLockAdapter({
|
|
129
|
-
* rootGroup: "@global"
|
|
130
|
-
* }),
|
|
131
|
-
* eventBus,
|
|
132
|
-
* });
|
|
141
|
+
* import { LockProvider } from "@daiso-tech/core/lock";
|
|
142
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/adapters";
|
|
143
|
+
* import { KeyPrefixer, TimeSpan, LazyPromise } from "@daiso-tech/core/utilities";
|
|
144
|
+
* import { Serde } from "@daiso-tech/core/adapter";
|
|
145
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/adapter/adapters";
|
|
133
146
|
*
|
|
134
|
-
* const
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
* }),
|
|
139
|
-
* eventBus,
|
|
147
|
+
* const lockProvider = new LockProvider({
|
|
148
|
+
* adapter: new MemoryLockAdapter(),
|
|
149
|
+
* keyPrefixer: new KeyPrefixer("lock"),
|
|
150
|
+
* serde: new Serde(new SuperJsonSerdeAdapter())
|
|
140
151
|
* });
|
|
141
|
-
* await cache.put("a", 0);
|
|
142
152
|
*
|
|
143
153
|
* const lock = lockProvider.create("a");
|
|
144
154
|
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
* fn(),
|
|
153
|
-
* fn(),
|
|
154
|
-
* fn(),
|
|
155
|
-
* ]);
|
|
155
|
+
* await lock.run(
|
|
156
|
+
* new LazyPromise(async () => {
|
|
157
|
+
* console.log("START");
|
|
158
|
+
* await LazyPromise.delay(TimeSpan.fromSeconds(10));
|
|
159
|
+
* console.log("END");
|
|
160
|
+
* })
|
|
161
|
+
* );
|
|
156
162
|
* ```
|
|
157
163
|
*/
|
|
158
164
|
run(asyncFn) {
|
|
159
|
-
return this.
|
|
165
|
+
return this.createLazyPromise(async () => {
|
|
160
166
|
if (typeof asyncFn === "function") {
|
|
161
167
|
asyncFn = new LazyPromise(asyncFn);
|
|
162
168
|
}
|
|
@@ -165,7 +171,7 @@ export class Lock {
|
|
|
165
171
|
if (!hasAquired) {
|
|
166
172
|
return [
|
|
167
173
|
null,
|
|
168
|
-
new KeyAlreadyAcquiredLockError(`Key "${this.key}" already acquired`),
|
|
174
|
+
new KeyAlreadyAcquiredLockError(`Key "${this.key.resolved}" already acquired`),
|
|
169
175
|
];
|
|
170
176
|
}
|
|
171
177
|
return [await asyncFn, null];
|
|
@@ -176,110 +182,58 @@ export class Lock {
|
|
|
176
182
|
}).setRetryPolicy((error) => error instanceof UnableToAquireLockError);
|
|
177
183
|
}
|
|
178
184
|
/**
|
|
185
|
+
* You can pass in a sync function or async function.
|
|
179
186
|
* @example
|
|
180
187
|
* ```ts
|
|
181
|
-
* import {
|
|
182
|
-
* import {
|
|
183
|
-
* import {
|
|
184
|
-
* import
|
|
185
|
-
* import {
|
|
186
|
-
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
187
|
-
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
188
|
-
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
189
|
-
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
190
|
-
* import { delay } from "@daiso-tech/core/async";
|
|
188
|
+
* import { LockProvider } from "@daiso-tech/core/lock";
|
|
189
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/adapters";
|
|
190
|
+
* import { KeyPrefixer, TimeSpan, LazyPromise } from "@daiso-tech/core/utilities";
|
|
191
|
+
* import { Serde } from "@daiso-tech/core/adapter";
|
|
192
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/adapter/adapters";
|
|
191
193
|
*
|
|
192
|
-
* const
|
|
193
|
-
* adapter: new
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
197
|
-
* serde,
|
|
198
|
-
* adapter: new MemoryLockAdapter({
|
|
199
|
-
* rootGroup: "@global"
|
|
200
|
-
* }),
|
|
201
|
-
* eventBus,
|
|
194
|
+
* const lockProvider = new LockProvider({
|
|
195
|
+
* adapter: new MemoryLockAdapter(),
|
|
196
|
+
* keyPrefixer: new KeyPrefixer("lock"),
|
|
197
|
+
* serde: new Serde(new SuperJsonSerdeAdapter())
|
|
202
198
|
* });
|
|
203
199
|
*
|
|
204
200
|
* const lock = lockProvider.create("a");
|
|
205
201
|
*
|
|
206
|
-
* async
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
* console.log("END: ", 2);
|
|
212
|
-
* })
|
|
213
|
-
* // Retries 4 times to acquire the lock, then throws an error.
|
|
214
|
-
* .setRetryAttempts(4);
|
|
215
|
-
* }
|
|
216
|
-
*
|
|
217
|
-
* await Promise.allSettled([
|
|
218
|
-
* fn(),
|
|
219
|
-
* fn(),
|
|
220
|
-
* fn(),
|
|
221
|
-
* fn(),
|
|
222
|
-
* ]);
|
|
202
|
+
* await lock.runOrFail(async () => {
|
|
203
|
+
* console.log("START");
|
|
204
|
+
* await LazyPromise.delay(TimeSpan.fromSeconds(10));
|
|
205
|
+
* console.log("END");
|
|
206
|
+
* });
|
|
223
207
|
* ```
|
|
224
208
|
*
|
|
225
|
-
* You can also pass
|
|
209
|
+
* You can also pass in a <i>{@link LazyPromise}</i>. This is useful because all other components in this library returns <i>{@link LazyPromise}</i>.
|
|
226
210
|
* @example
|
|
227
211
|
* ```ts
|
|
228
|
-
* import {
|
|
229
|
-
* import {
|
|
230
|
-
* import {
|
|
231
|
-
* import
|
|
232
|
-
* import {
|
|
233
|
-
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
234
|
-
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
235
|
-
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
236
|
-
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
237
|
-
* import { delay } from "@daiso-tech/core/async";
|
|
238
|
-
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
239
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
240
|
-
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
212
|
+
* import { LockProvider } from "@daiso-tech/core/lock";
|
|
213
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/adapters";
|
|
214
|
+
* import { KeyPrefixer, TimeSpan, LazyPromise } from "@daiso-tech/core/utilities";
|
|
215
|
+
* import { Serde } from "@daiso-tech/core/adapter";
|
|
216
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/adapter/adapters";
|
|
241
217
|
*
|
|
242
|
-
* const
|
|
243
|
-
* adapter: new
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
247
|
-
* serde,
|
|
248
|
-
* adapter: new MemoryLockAdapter({
|
|
249
|
-
* rootGroup: "@global"
|
|
250
|
-
* }),
|
|
251
|
-
* eventBus,
|
|
218
|
+
* const lockProvider = new LockProvider({
|
|
219
|
+
* adapter: new MemoryLockAdapter(),
|
|
220
|
+
* keyPrefixer: new KeyPrefixer("lock"),
|
|
221
|
+
* serde: new Serde(new SuperJsonSerdeAdapter())
|
|
252
222
|
* });
|
|
253
223
|
*
|
|
254
|
-
* const cache: IGroupableCache = new Cache({
|
|
255
|
-
* // Let's pretend when the cache adapter increments a key it will occur in 2 async, get the value, increment the value in memory and update the key.
|
|
256
|
-
* adapter: new MemoryCacheAdapter({
|
|
257
|
-
* rootGroup: "@global"
|
|
258
|
-
* }),
|
|
259
|
-
* eventBus,
|
|
260
|
-
* });
|
|
261
|
-
* await cache.put("a", 0);
|
|
262
|
-
*
|
|
263
224
|
* const lock = lockProvider.create("a");
|
|
264
225
|
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
* .
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
* await Promise.allSettled([
|
|
274
|
-
* fn(),
|
|
275
|
-
* fn(),
|
|
276
|
-
* fn(),
|
|
277
|
-
* fn(),
|
|
278
|
-
* ]);
|
|
226
|
+
* await lock.runOrFail(
|
|
227
|
+
* new LazyPromise(async () => {
|
|
228
|
+
* console.log("START");
|
|
229
|
+
* await LazyPromise.delay(TimeSpan.fromSeconds(10));
|
|
230
|
+
* console.log("END");
|
|
231
|
+
* })
|
|
232
|
+
* );
|
|
279
233
|
* ```
|
|
280
234
|
*/
|
|
281
235
|
runOrFail(asyncFn) {
|
|
282
|
-
return this.
|
|
236
|
+
return this.createLazyPromise(async () => {
|
|
283
237
|
try {
|
|
284
238
|
if (typeof asyncFn === "function") {
|
|
285
239
|
asyncFn = new LazyPromise(asyncFn);
|
|
@@ -294,105 +248,58 @@ export class Lock {
|
|
|
294
248
|
error instanceof KeyAlreadyAcquiredLockError);
|
|
295
249
|
}
|
|
296
250
|
/**
|
|
251
|
+
* You can pass in a sync function or async function.
|
|
297
252
|
* @example
|
|
298
253
|
* ```ts
|
|
299
|
-
* import {
|
|
300
|
-
* import {
|
|
301
|
-
* import {
|
|
302
|
-
* import
|
|
303
|
-
* import {
|
|
304
|
-
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
305
|
-
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
306
|
-
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
307
|
-
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
308
|
-
* import { delay } from "@daiso-tech/core/async";
|
|
254
|
+
* import { LockProvider } from "@daiso-tech/core/lock";
|
|
255
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/adapters";
|
|
256
|
+
* import { KeyPrefixer, TimeSpan, LazyPromise } from "@daiso-tech/core/utilities";
|
|
257
|
+
* import { Serde } from "@daiso-tech/core/adapter";
|
|
258
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/adapter/adapters";
|
|
309
259
|
*
|
|
310
|
-
* const
|
|
311
|
-
* adapter: new
|
|
312
|
-
*
|
|
313
|
-
*
|
|
314
|
-
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
315
|
-
* serde,
|
|
316
|
-
* adapter: new MemoryLockAdapter({
|
|
317
|
-
* rootGroup: "@global"
|
|
318
|
-
* }),
|
|
319
|
-
* eventBus,
|
|
260
|
+
* const lockProvider = new LockProvider({
|
|
261
|
+
* adapter: new MemoryLockAdapter(),
|
|
262
|
+
* keyPrefixer: new KeyPrefixer("lock"),
|
|
263
|
+
* serde: new Serde(new SuperJsonSerdeAdapter())
|
|
320
264
|
* });
|
|
321
265
|
*
|
|
322
266
|
* const lock = lockProvider.create("a");
|
|
323
267
|
*
|
|
324
|
-
* async
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
* console.log("END: ", 2);
|
|
330
|
-
* });
|
|
331
|
-
* }
|
|
332
|
-
*
|
|
333
|
-
* await Promise.allSettled([
|
|
334
|
-
* fn(),
|
|
335
|
-
* fn(),
|
|
336
|
-
* fn(),
|
|
337
|
-
* fn(),
|
|
338
|
-
* ]);
|
|
268
|
+
* await lock.runBlocking(async () => {
|
|
269
|
+
* console.log("START");
|
|
270
|
+
* await LazyPromise.delay(TimeSpan.fromSeconds(10));
|
|
271
|
+
* console.log("END");
|
|
272
|
+
* });
|
|
339
273
|
* ```
|
|
340
274
|
*
|
|
341
|
-
* You can also pass
|
|
275
|
+
* You can also pass in a <i>{@link LazyPromise}</i>. This is useful because all other components in this library returns <i>{@link LazyPromise}</i>.
|
|
342
276
|
* @example
|
|
343
277
|
* ```ts
|
|
344
|
-
* import {
|
|
345
|
-
* import {
|
|
346
|
-
* import {
|
|
347
|
-
* import
|
|
348
|
-
* import {
|
|
349
|
-
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
350
|
-
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
351
|
-
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
352
|
-
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
353
|
-
* import { delay } from "@daiso-tech/core/async";
|
|
354
|
-
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
355
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
356
|
-
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
278
|
+
* import { LockProvider } from "@daiso-tech/core/lock";
|
|
279
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/adapters";
|
|
280
|
+
* import { KeyPrefixer, TimeSpan, LazyPromise } from "@daiso-tech/core/utilities";
|
|
281
|
+
* import { Serde } from "@daiso-tech/core/adapter";
|
|
282
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/adapter/adapters";
|
|
357
283
|
*
|
|
358
|
-
* const
|
|
359
|
-
* adapter: new
|
|
360
|
-
*
|
|
361
|
-
*
|
|
362
|
-
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
363
|
-
* serde,
|
|
364
|
-
* adapter: new MemoryLockAdapter({
|
|
365
|
-
* rootGroup: "@global"
|
|
366
|
-
* }),
|
|
367
|
-
* eventBus,
|
|
284
|
+
* const lockProvider = new LockProvider({
|
|
285
|
+
* adapter: new MemoryLockAdapter(),
|
|
286
|
+
* keyPrefixer: new KeyPrefixer("lock"),
|
|
287
|
+
* serde: new Serde(new SuperJsonSerdeAdapter())
|
|
368
288
|
* });
|
|
369
289
|
*
|
|
370
|
-
* const cache: IGroupableCache = new Cache({
|
|
371
|
-
* // Let's pretend when the cache adapter increments a key it will occur in 2 async, get the value, increment the value in memory and update the key.
|
|
372
|
-
* adapter: new MemoryCacheAdapter({
|
|
373
|
-
* rootGroup: "@global"
|
|
374
|
-
* }),
|
|
375
|
-
* eventBus,
|
|
376
|
-
* });
|
|
377
|
-
* await cache.put("a", 0);
|
|
378
|
-
*
|
|
379
290
|
* const lock = lockProvider.create("a");
|
|
380
291
|
*
|
|
381
|
-
*
|
|
382
|
-
*
|
|
383
|
-
*
|
|
384
|
-
*
|
|
385
|
-
*
|
|
386
|
-
*
|
|
387
|
-
*
|
|
388
|
-
* fn(),
|
|
389
|
-
* fn(),
|
|
390
|
-
* fn(),
|
|
391
|
-
* ]);
|
|
292
|
+
* await lock.runBlocking(
|
|
293
|
+
* new LazyPromise(async () => {
|
|
294
|
+
* console.log("START");
|
|
295
|
+
* await LazyPromise.delay(TimeSpan.fromSeconds(10));
|
|
296
|
+
* console.log("END");
|
|
297
|
+
* })
|
|
298
|
+
* );
|
|
392
299
|
* ```
|
|
393
300
|
*/
|
|
394
301
|
runBlocking(asyncFn, settings) {
|
|
395
|
-
return this.
|
|
302
|
+
return this.createLazyPromise(async () => {
|
|
396
303
|
if (typeof asyncFn === "function") {
|
|
397
304
|
asyncFn = new LazyPromise(asyncFn);
|
|
398
305
|
}
|
|
@@ -401,7 +308,7 @@ export class Lock {
|
|
|
401
308
|
if (!hasAquired) {
|
|
402
309
|
return [
|
|
403
310
|
null,
|
|
404
|
-
new KeyAlreadyAcquiredLockError(`Key "${this.key}" already acquired`),
|
|
311
|
+
new KeyAlreadyAcquiredLockError(`Key "${this.key.resolved}" already acquired`),
|
|
405
312
|
];
|
|
406
313
|
}
|
|
407
314
|
return [await asyncFn, null];
|
|
@@ -412,67 +319,80 @@ export class Lock {
|
|
|
412
319
|
}).setRetryPolicy((error) => error instanceof UnableToAquireLockError);
|
|
413
320
|
}
|
|
414
321
|
/**
|
|
322
|
+
* You can pass in a sync function or async function.
|
|
415
323
|
* @example
|
|
416
324
|
* ```ts
|
|
417
|
-
* import {
|
|
418
|
-
* import {
|
|
419
|
-
* import {
|
|
420
|
-
* import
|
|
421
|
-
* import {
|
|
422
|
-
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
423
|
-
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
424
|
-
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
425
|
-
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
426
|
-
* import { delay } from "@daiso-tech/core/async";
|
|
325
|
+
* import { LockProvider } from "@daiso-tech/core/lock";
|
|
326
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/adapters";
|
|
327
|
+
* import { KeyPrefixer, TimeSpan, LazyPromise } from "@daiso-tech/core/utilities";
|
|
328
|
+
* import { Serde } from "@daiso-tech/core/adapter";
|
|
329
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/adapter/adapters";
|
|
427
330
|
*
|
|
428
|
-
* const
|
|
429
|
-
* adapter: new
|
|
430
|
-
*
|
|
431
|
-
*
|
|
432
|
-
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
433
|
-
* serde,
|
|
434
|
-
* adapter: new MemoryLockAdapter({
|
|
435
|
-
* rootGroup: "@global"
|
|
436
|
-
* }),
|
|
437
|
-
* eventBus,
|
|
331
|
+
* const lockProvider = new LockProvider({
|
|
332
|
+
* adapter: new MemoryLockAdapter(),
|
|
333
|
+
* keyPrefixer: new KeyPrefixer("lock"),
|
|
334
|
+
* serde: new Serde(new SuperJsonSerdeAdapter())
|
|
438
335
|
* });
|
|
439
336
|
*
|
|
440
337
|
* const lock = lockProvider.create("a");
|
|
441
338
|
*
|
|
442
|
-
* async
|
|
443
|
-
*
|
|
444
|
-
*
|
|
445
|
-
*
|
|
446
|
-
*
|
|
447
|
-
*
|
|
448
|
-
* }
|
|
449
|
-
* console.log("START: ", 1);
|
|
450
|
-
* // Let’s pretend we’re doing async database work.
|
|
451
|
-
* await delay(TimeSpan.fromSeconds(2));
|
|
452
|
-
* console.log("END: ", 2);
|
|
453
|
-
* }
|
|
454
|
-
* finally {
|
|
455
|
-
* await lock.release();
|
|
456
|
-
* }
|
|
457
|
-
* }
|
|
339
|
+
* await lock.runBlockingOrFail(async () => {
|
|
340
|
+
* console.log("START");
|
|
341
|
+
* await LazyPromise.delay(TimeSpan.fromSeconds(10));
|
|
342
|
+
* console.log("END");
|
|
343
|
+
* });
|
|
344
|
+
* ```
|
|
458
345
|
*
|
|
459
|
-
*
|
|
460
|
-
*
|
|
461
|
-
*
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
*
|
|
346
|
+
* You can also pass in a <i>{@link LazyPromise}</i>. This is useful because all other components in this library returns <i>{@link LazyPromise}</i>.
|
|
347
|
+
* @example
|
|
348
|
+
* ```ts
|
|
349
|
+
* import { LockProvider } from "@daiso-tech/core/lock";
|
|
350
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/adapters";
|
|
351
|
+
* import { KeyPrefixer, TimeSpan, LazyPromise } from "@daiso-tech/core/utilities";
|
|
352
|
+
* import { Serde } from "@daiso-tech/core/adapter";
|
|
353
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/adapter/adapters";
|
|
354
|
+
*
|
|
355
|
+
* const lockProvider = new LockProvider({
|
|
356
|
+
* adapter: new MemoryLockAdapter(),
|
|
357
|
+
* keyPrefixer: new KeyPrefixer("lock"),
|
|
358
|
+
* serde: new Serde(new SuperJsonSerdeAdapter())
|
|
359
|
+
* });
|
|
360
|
+
*
|
|
361
|
+
* const lock = lockProvider.create("a");
|
|
362
|
+
*
|
|
363
|
+
* await lock.runBlockingOrFail(
|
|
364
|
+
* new LazyPromise(async () => {
|
|
365
|
+
* console.log("START");
|
|
366
|
+
* await LazyPromise.delay(TimeSpan.fromSeconds(10));
|
|
367
|
+
* console.log("END");
|
|
368
|
+
* })
|
|
369
|
+
* );
|
|
465
370
|
* ```
|
|
466
371
|
*/
|
|
372
|
+
runBlockingOrFail(asyncFn, settings) {
|
|
373
|
+
return new LazyPromise(async () => {
|
|
374
|
+
if (typeof asyncFn === "function") {
|
|
375
|
+
asyncFn = new LazyPromise(asyncFn);
|
|
376
|
+
}
|
|
377
|
+
try {
|
|
378
|
+
await this.acquireBlockingOrFail(settings);
|
|
379
|
+
return await asyncFn;
|
|
380
|
+
}
|
|
381
|
+
finally {
|
|
382
|
+
await this.release();
|
|
383
|
+
}
|
|
384
|
+
}).setRetryPolicy((error) => error instanceof UnableToAquireLockError);
|
|
385
|
+
}
|
|
467
386
|
acquire() {
|
|
468
|
-
return this.
|
|
387
|
+
return this.createLazyPromise(async () => {
|
|
469
388
|
try {
|
|
470
|
-
this.
|
|
471
|
-
|
|
389
|
+
const adapter = await this.adapterPromise;
|
|
390
|
+
this.lockState.remove();
|
|
391
|
+
const hasAquired = await adapter.acquire(this.key.prefixed, this.owner, this.ttl);
|
|
472
392
|
if (hasAquired) {
|
|
473
|
-
this.
|
|
393
|
+
this.lockState.set(this.ttl);
|
|
474
394
|
const event = new KeyAcquiredLockEvent({
|
|
475
|
-
key: this.key,
|
|
395
|
+
key: this.key.resolved,
|
|
476
396
|
owner: this.owner,
|
|
477
397
|
ttl: this.ttl,
|
|
478
398
|
});
|
|
@@ -481,7 +401,7 @@ export class Lock {
|
|
|
481
401
|
}
|
|
482
402
|
else {
|
|
483
403
|
const event = new KeyAlreadyAcquiredLockEvent({
|
|
484
|
-
key: this.key,
|
|
404
|
+
key: this.key.resolved,
|
|
485
405
|
owner: this.owner,
|
|
486
406
|
});
|
|
487
407
|
this.lockEventBus.dispatch(event).defer();
|
|
@@ -491,7 +411,7 @@ export class Lock {
|
|
|
491
411
|
}
|
|
492
412
|
catch (error) {
|
|
493
413
|
const event = new UnexpectedErrorLockEvent({
|
|
494
|
-
key: this.key,
|
|
414
|
+
key: this.key.resolved,
|
|
495
415
|
owner: this.owner,
|
|
496
416
|
ttl: this.ttl,
|
|
497
417
|
error,
|
|
@@ -502,140 +422,45 @@ export class Lock {
|
|
|
502
422
|
}
|
|
503
423
|
});
|
|
504
424
|
}
|
|
505
|
-
/**
|
|
506
|
-
* @example
|
|
507
|
-
* ```ts
|
|
508
|
-
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
509
|
-
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
510
|
-
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
511
|
-
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
512
|
-
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
513
|
-
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
514
|
-
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
515
|
-
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
516
|
-
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
517
|
-
* import { delay } from "@daiso-tech/core/async";
|
|
518
|
-
*
|
|
519
|
-
* const eventBus = new EventBus({
|
|
520
|
-
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
521
|
-
* });
|
|
522
|
-
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
523
|
-
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
524
|
-
* serde,
|
|
525
|
-
* adapter: new MemoryLockAdapter({
|
|
526
|
-
* rootGroup: "@global"
|
|
527
|
-
* }),
|
|
528
|
-
* eventBus,
|
|
529
|
-
* });
|
|
530
|
-
*
|
|
531
|
-
* const lock = lockProvider.create("a");
|
|
532
|
-
*
|
|
533
|
-
* async function fn(): Promise<void> {
|
|
534
|
-
* // Use try-finally when acquiring a lock to ensure it’s released if an error happens.
|
|
535
|
-
* try {
|
|
536
|
-
* // Retries 4 times to acquire the lock, then throws an error.
|
|
537
|
-
* await lock.acquireOrFail().setRetryAttempts(4);
|
|
538
|
-
* console.log("START: ", 1);
|
|
539
|
-
* // Let’s pretend we’re doing async database work.
|
|
540
|
-
* await delay(TimeSpan.fromSeconds(2));
|
|
541
|
-
* console.log("END: ", 2);
|
|
542
|
-
* }
|
|
543
|
-
* finally {
|
|
544
|
-
* await lock.release();
|
|
545
|
-
* }
|
|
546
|
-
* }
|
|
547
|
-
*
|
|
548
|
-
* await Promise.allSettled([
|
|
549
|
-
* fn(),
|
|
550
|
-
* fn(),
|
|
551
|
-
* fn(),
|
|
552
|
-
* fn(),
|
|
553
|
-
* ]);
|
|
554
|
-
* ```
|
|
555
|
-
*/
|
|
556
425
|
acquireOrFail() {
|
|
557
|
-
return this.
|
|
426
|
+
return this.createLazyPromise(async () => {
|
|
558
427
|
const hasAquired = await this.acquire();
|
|
559
428
|
if (!hasAquired) {
|
|
560
|
-
throw new KeyAlreadyAcquiredLockError(`Key "${this.key}" already acquired`);
|
|
429
|
+
throw new KeyAlreadyAcquiredLockError(`Key "${this.key.resolved}" already acquired`);
|
|
561
430
|
}
|
|
562
431
|
});
|
|
563
432
|
}
|
|
564
|
-
/**
|
|
565
|
-
* @example
|
|
566
|
-
* ```ts
|
|
567
|
-
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
568
|
-
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
569
|
-
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
570
|
-
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
571
|
-
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
572
|
-
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
573
|
-
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
574
|
-
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
575
|
-
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
576
|
-
* import { delay } from "@daiso-tech/core/async";
|
|
577
|
-
*
|
|
578
|
-
* const eventBus = new EventBus({
|
|
579
|
-
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
580
|
-
* });
|
|
581
|
-
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
582
|
-
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
583
|
-
* serde,
|
|
584
|
-
* adapter: new MemoryLockAdapter({
|
|
585
|
-
* rootGroup: "@global"
|
|
586
|
-
* }),
|
|
587
|
-
* eventBus,
|
|
588
|
-
* });
|
|
589
|
-
*
|
|
590
|
-
* const lock = lockProvider.create("a");
|
|
591
|
-
*
|
|
592
|
-
* async function fn(): Promise<void> {
|
|
593
|
-
* // Use try-finally when acquiring a lock to ensure it’s released if an error happens.
|
|
594
|
-
* try {
|
|
595
|
-
* const hasAquired = await lock.acquireBlocking();
|
|
596
|
-
* if (!hasAquired) {
|
|
597
|
-
* return;
|
|
598
|
-
* }
|
|
599
|
-
* console.log("START: ", 1);
|
|
600
|
-
* // Let’s pretend we’re doing async database work.
|
|
601
|
-
* await delay(TimeSpan.fromSeconds(2));
|
|
602
|
-
* console.log("END: ", 2);
|
|
603
|
-
* }
|
|
604
|
-
* finally {
|
|
605
|
-
* await lock.release();
|
|
606
|
-
* }
|
|
607
|
-
* }
|
|
608
|
-
*
|
|
609
|
-
* await Promise.allSettled([
|
|
610
|
-
* fn(),
|
|
611
|
-
* fn(),
|
|
612
|
-
* fn(),
|
|
613
|
-
* fn(),
|
|
614
|
-
* ]);
|
|
615
|
-
* ```
|
|
616
|
-
*/
|
|
617
433
|
acquireBlocking(settings = {}) {
|
|
618
434
|
return new LazyPromise(async () => {
|
|
619
|
-
const { time = this.
|
|
435
|
+
const { time = this.defaultBlockingTime, interval = this.defaultBlockingInterval, } = settings;
|
|
620
436
|
const endDate = time.toEndDate();
|
|
621
437
|
while (endDate.getTime() > new Date().getTime()) {
|
|
622
438
|
const hasAquired = await this.acquire();
|
|
623
439
|
if (hasAquired) {
|
|
624
440
|
return true;
|
|
625
441
|
}
|
|
626
|
-
await delay(interval);
|
|
442
|
+
await LazyPromise.delay(interval);
|
|
627
443
|
}
|
|
628
444
|
return false;
|
|
629
445
|
});
|
|
630
446
|
}
|
|
447
|
+
acquireBlockingOrFail(settings) {
|
|
448
|
+
return new LazyPromise(async () => {
|
|
449
|
+
const hasAquired = await this.acquireBlocking(settings);
|
|
450
|
+
if (!hasAquired) {
|
|
451
|
+
throw new KeyAlreadyAcquiredLockError(`Key "${this.key.resolved}" already acquired`);
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
}
|
|
631
455
|
release() {
|
|
632
|
-
return this.
|
|
456
|
+
return this.createLazyPromise(async () => {
|
|
633
457
|
try {
|
|
634
|
-
const
|
|
458
|
+
const adapter = await this.adapterPromise;
|
|
459
|
+
const hasReleased = await adapter.release(this.key.prefixed, this.owner);
|
|
635
460
|
if (hasReleased) {
|
|
636
|
-
this.
|
|
461
|
+
this.lockState.remove();
|
|
637
462
|
const event = new KeyReleasedLockEvent({
|
|
638
|
-
key: this.key,
|
|
463
|
+
key: this.key.resolved,
|
|
639
464
|
owner: this.owner,
|
|
640
465
|
});
|
|
641
466
|
this.lockEventBus.dispatch(event).defer();
|
|
@@ -643,7 +468,7 @@ export class Lock {
|
|
|
643
468
|
}
|
|
644
469
|
else {
|
|
645
470
|
const event = new UnownedReleaseLockEvent({
|
|
646
|
-
key: this.key,
|
|
471
|
+
key: this.key.resolved,
|
|
647
472
|
owner: this.owner,
|
|
648
473
|
});
|
|
649
474
|
this.lockEventBus.dispatch(event).defer();
|
|
@@ -653,7 +478,7 @@ export class Lock {
|
|
|
653
478
|
}
|
|
654
479
|
catch (error) {
|
|
655
480
|
const event = new UnexpectedErrorLockEvent({
|
|
656
|
-
key: this.key,
|
|
481
|
+
key: this.key.resolved,
|
|
657
482
|
owner: this.owner,
|
|
658
483
|
ttl: this.ttl,
|
|
659
484
|
error,
|
|
@@ -665,27 +490,28 @@ export class Lock {
|
|
|
665
490
|
});
|
|
666
491
|
}
|
|
667
492
|
releaseOrFail() {
|
|
668
|
-
return this.
|
|
493
|
+
return this.createLazyPromise(async () => {
|
|
669
494
|
const hasRelased = await this.release();
|
|
670
495
|
if (!hasRelased) {
|
|
671
|
-
throw new UnownedReleaseLockError(`Unonwed release on key "${this.key}" by owner "${this.owner}"`);
|
|
496
|
+
throw new UnownedReleaseLockError(`Unonwed release on key "${this.key.resolved}" by owner "${this.owner}"`);
|
|
672
497
|
}
|
|
673
498
|
});
|
|
674
499
|
}
|
|
675
500
|
forceRelease() {
|
|
676
|
-
return this.
|
|
501
|
+
return this.createLazyPromise(async () => {
|
|
677
502
|
try {
|
|
678
|
-
await this.
|
|
679
|
-
this.
|
|
503
|
+
const adapter = await this.adapterPromise;
|
|
504
|
+
await adapter.forceRelease(this.key.prefixed);
|
|
505
|
+
this.lockState.remove();
|
|
680
506
|
const event = new KeyForceReleasedLockEvent({
|
|
681
|
-
key: this.key,
|
|
507
|
+
key: this.key.resolved,
|
|
682
508
|
});
|
|
683
509
|
this.lockEventBus.dispatch(event).defer();
|
|
684
510
|
this.lockProviderEventDispatcher.dispatch(event).defer();
|
|
685
511
|
}
|
|
686
512
|
catch (error) {
|
|
687
513
|
const event = new UnexpectedErrorLockEvent({
|
|
688
|
-
key: this.key,
|
|
514
|
+
key: this.key.resolved,
|
|
689
515
|
owner: this.owner,
|
|
690
516
|
ttl: this.ttl,
|
|
691
517
|
error,
|
|
@@ -698,13 +524,13 @@ export class Lock {
|
|
|
698
524
|
}
|
|
699
525
|
isExpired() {
|
|
700
526
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
701
|
-
return this.
|
|
527
|
+
return this.createLazyPromise(async () => {
|
|
702
528
|
try {
|
|
703
|
-
return this.
|
|
529
|
+
return this.lockState.isExpired();
|
|
704
530
|
}
|
|
705
531
|
catch (error) {
|
|
706
532
|
const event = new UnexpectedErrorLockEvent({
|
|
707
|
-
key: this.key,
|
|
533
|
+
key: this.key.resolved,
|
|
708
534
|
owner: this.owner,
|
|
709
535
|
ttl: this.ttl,
|
|
710
536
|
error,
|
|
@@ -716,28 +542,29 @@ export class Lock {
|
|
|
716
542
|
});
|
|
717
543
|
}
|
|
718
544
|
isLocked() {
|
|
719
|
-
return this.
|
|
545
|
+
return this.createLazyPromise(async () => {
|
|
720
546
|
const isExpired = await this.isExpired();
|
|
721
547
|
return !isExpired;
|
|
722
548
|
});
|
|
723
549
|
}
|
|
724
550
|
refresh(ttl = this.defaultRefreshTime) {
|
|
725
|
-
return this.
|
|
551
|
+
return this.createLazyPromise(async () => {
|
|
726
552
|
try {
|
|
727
|
-
const
|
|
553
|
+
const adapter = await this.adapterPromise;
|
|
554
|
+
const hasRefreshed = await adapter.refresh(this.key.prefixed, this.owner, ttl);
|
|
728
555
|
if (hasRefreshed) {
|
|
729
556
|
const event = new KeyRefreshedLockEvent({
|
|
730
|
-
key: this.key,
|
|
557
|
+
key: this.key.resolved,
|
|
731
558
|
owner: this.owner,
|
|
732
559
|
ttl,
|
|
733
560
|
});
|
|
734
|
-
this.
|
|
561
|
+
this.lockState.set(ttl);
|
|
735
562
|
this.lockEventBus.dispatch(event).defer();
|
|
736
563
|
this.lockProviderEventDispatcher.dispatch(event).defer();
|
|
737
564
|
}
|
|
738
565
|
else {
|
|
739
566
|
const event = new UnownedRefreshLockEvent({
|
|
740
|
-
key: this.key,
|
|
567
|
+
key: this.key.resolved,
|
|
741
568
|
owner: this.owner,
|
|
742
569
|
});
|
|
743
570
|
this.lockEventBus.dispatch(event).defer();
|
|
@@ -747,7 +574,7 @@ export class Lock {
|
|
|
747
574
|
}
|
|
748
575
|
catch (error) {
|
|
749
576
|
const event = new UnexpectedErrorLockEvent({
|
|
750
|
-
key: this.key,
|
|
577
|
+
key: this.key.resolved,
|
|
751
578
|
owner: this.owner,
|
|
752
579
|
ttl: this.ttl,
|
|
753
580
|
error,
|
|
@@ -759,311 +586,24 @@ export class Lock {
|
|
|
759
586
|
});
|
|
760
587
|
}
|
|
761
588
|
refreshOrFail(ttl) {
|
|
762
|
-
return this.
|
|
589
|
+
return this.createLazyPromise(async () => {
|
|
763
590
|
const hasRefreshed = await this.refresh(ttl);
|
|
764
591
|
if (!hasRefreshed) {
|
|
765
|
-
throw new UnownedRefreshLockError(`Unonwed refresh on key "${this.key}" by owner "${this.owner}"`);
|
|
592
|
+
throw new UnownedRefreshLockError(`Unonwed refresh on key "${this.key.resolved}" by owner "${this.owner}"`);
|
|
766
593
|
}
|
|
767
594
|
});
|
|
768
595
|
}
|
|
769
596
|
getRemainingTime() {
|
|
770
597
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
771
|
-
return this.
|
|
772
|
-
return this.
|
|
598
|
+
return this.createLazyPromise(async () => {
|
|
599
|
+
return this.lockState.getRemainingTime();
|
|
773
600
|
});
|
|
774
601
|
}
|
|
775
602
|
getOwner() {
|
|
776
603
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
777
|
-
return this.
|
|
604
|
+
return this.createLazyPromise(async () => {
|
|
778
605
|
return this.owner;
|
|
779
606
|
});
|
|
780
607
|
}
|
|
781
|
-
/**
|
|
782
|
-
* You can listen to different events of the current <i>Lock</i> class instance.
|
|
783
|
-
*
|
|
784
|
-
* Refer to <i>{@link LockEvents}</i>, to se all events dispatched by <i>LockProvider</i> class instance.
|
|
785
|
-
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
786
|
-
* @example
|
|
787
|
-
* ```ts
|
|
788
|
-
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
789
|
-
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
790
|
-
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
791
|
-
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
792
|
-
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
793
|
-
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
794
|
-
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
795
|
-
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
796
|
-
*
|
|
797
|
-
* const eventBus = new EventBus({
|
|
798
|
-
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
799
|
-
* });
|
|
800
|
-
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
801
|
-
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
802
|
-
* serde,
|
|
803
|
-
* adapter: new MemoryLockAdapter({
|
|
804
|
-
* rootGroup: "@global"
|
|
805
|
-
* }),
|
|
806
|
-
* eventBus,
|
|
807
|
-
* });
|
|
808
|
-
*
|
|
809
|
-
* const lock = lockProvider.create("a");
|
|
810
|
-
*
|
|
811
|
-
* const listener: Invokable <LockEvents> = event => {
|
|
812
|
-
* console.log(event);
|
|
813
|
-
* }
|
|
814
|
-
* await lock.addListener(KeyAcquiredLockEvent, listener);
|
|
815
|
-
* await lock.acquire();
|
|
816
|
-
* ```
|
|
817
|
-
*/
|
|
818
|
-
addListener(event, listener) {
|
|
819
|
-
return this.lockEventBus.addListener(event, listener);
|
|
820
|
-
}
|
|
821
|
-
/**
|
|
822
|
-
* You can listen to different events of the current <i>Lock</i> class instance.
|
|
823
|
-
*
|
|
824
|
-
* Refer to <i>{@link LockEvents}</i>, to se all events dispatched by <i>LockProvider</i> class instance.
|
|
825
|
-
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
826
|
-
* @example
|
|
827
|
-
* ```ts
|
|
828
|
-
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
829
|
-
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
830
|
-
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
831
|
-
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
832
|
-
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
833
|
-
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
834
|
-
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
835
|
-
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
836
|
-
*
|
|
837
|
-
* const eventBus = new EventBus({
|
|
838
|
-
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
839
|
-
* });
|
|
840
|
-
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
841
|
-
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
842
|
-
* serde,
|
|
843
|
-
* adapter: new MemoryLockAdapter({
|
|
844
|
-
* rootGroup: "@global"
|
|
845
|
-
* }),
|
|
846
|
-
* eventBus,
|
|
847
|
-
* });
|
|
848
|
-
*
|
|
849
|
-
* const lock = lockProvider.create("a");
|
|
850
|
-
*
|
|
851
|
-
* const listener: Invokable <LockEvents> = event => {
|
|
852
|
-
* console.log(event);
|
|
853
|
-
* }
|
|
854
|
-
* await lock.addListenerMany([KeyAcquiredLockEvent], listener);
|
|
855
|
-
* await lock.acquire();
|
|
856
|
-
* ```
|
|
857
|
-
*/
|
|
858
|
-
addListenerMany(events, listener) {
|
|
859
|
-
return this.lockEventBus.addListenerMany(events, listener);
|
|
860
|
-
}
|
|
861
|
-
/**
|
|
862
|
-
* You can listen to different events of the current <i>Lock</i> class instance.
|
|
863
|
-
*
|
|
864
|
-
* Refer to <i>{@link LockEvents}</i>, to se all events dispatched by <i>LockProvider</i> class instance.
|
|
865
|
-
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
866
|
-
* @example
|
|
867
|
-
* ```ts
|
|
868
|
-
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
869
|
-
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
870
|
-
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
871
|
-
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
872
|
-
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
873
|
-
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
874
|
-
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
875
|
-
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
876
|
-
*
|
|
877
|
-
* const eventBus = new EventBus({
|
|
878
|
-
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
879
|
-
* });
|
|
880
|
-
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
881
|
-
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
882
|
-
* serde,
|
|
883
|
-
* adapter: new MemoryLockAdapter({
|
|
884
|
-
* rootGroup: "@global"
|
|
885
|
-
* }),
|
|
886
|
-
* eventBus,
|
|
887
|
-
* });
|
|
888
|
-
*
|
|
889
|
-
* const lock = lockProvider.create("a");
|
|
890
|
-
*
|
|
891
|
-
* const listener: Invokable <LockEvents> = event => {
|
|
892
|
-
* console.log(event);
|
|
893
|
-
* }
|
|
894
|
-
* await lock.addListener(KeyAcquiredLockEvent, listener);
|
|
895
|
-
* await lock.removeListener(KeyAcquiredLockEvent, listener);
|
|
896
|
-
* await lock.acquire();
|
|
897
|
-
* ```
|
|
898
|
-
*/
|
|
899
|
-
removeListener(event, listener) {
|
|
900
|
-
return this.lockEventBus.removeListener(event, listener);
|
|
901
|
-
}
|
|
902
|
-
/**
|
|
903
|
-
* You can listen to different events of the current <i>Lock</i> class instance.
|
|
904
|
-
*
|
|
905
|
-
* Refer to <i>{@link LockEvents}</i>, to se all events dispatched by <i>LockProvider</i> class instance.
|
|
906
|
-
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
907
|
-
* @example
|
|
908
|
-
* ```ts
|
|
909
|
-
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
910
|
-
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
911
|
-
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
912
|
-
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
913
|
-
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
914
|
-
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
915
|
-
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
916
|
-
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
917
|
-
*
|
|
918
|
-
* const eventBus = new EventBus({
|
|
919
|
-
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
920
|
-
* });
|
|
921
|
-
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
922
|
-
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
923
|
-
* serde,
|
|
924
|
-
* adapter: new MemoryLockAdapter({
|
|
925
|
-
* rootGroup: "@global"
|
|
926
|
-
* }),
|
|
927
|
-
* eventBus,
|
|
928
|
-
* });
|
|
929
|
-
*
|
|
930
|
-
* const lock = lockProvider.create("a");
|
|
931
|
-
*
|
|
932
|
-
* const listener: Invokable <LockEvents> = event => {
|
|
933
|
-
* console.log(event);
|
|
934
|
-
* }
|
|
935
|
-
* await lock.addListenerMany([KeyAcquiredLockEvent], listener);
|
|
936
|
-
* await lock.removeListenerMany([KeyAcquiredLockEvent], listener);
|
|
937
|
-
* await lock.acquire();
|
|
938
|
-
* ```
|
|
939
|
-
*/
|
|
940
|
-
removeListenerMany(events, listener) {
|
|
941
|
-
return this.lockEventBus.removeListenerMany(events, listener);
|
|
942
|
-
}
|
|
943
|
-
/**
|
|
944
|
-
* You can listen to different events of the current <i>Lock</i> class instance.
|
|
945
|
-
*
|
|
946
|
-
* Refer to <i>{@link LockEvents}</i>, to se all events dispatched by <i>LockProvider</i> class instance.
|
|
947
|
-
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
948
|
-
* @example
|
|
949
|
-
* ```ts
|
|
950
|
-
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
951
|
-
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
952
|
-
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
953
|
-
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
954
|
-
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
955
|
-
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
956
|
-
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
957
|
-
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
958
|
-
*
|
|
959
|
-
* const eventBus = new EventBus({
|
|
960
|
-
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
961
|
-
* });
|
|
962
|
-
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
963
|
-
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
964
|
-
* serde,
|
|
965
|
-
* adapter: new MemoryLockAdapter({
|
|
966
|
-
* rootGroup: "@global"
|
|
967
|
-
* }),
|
|
968
|
-
* eventBus,
|
|
969
|
-
* });
|
|
970
|
-
*
|
|
971
|
-
* const lock = lockProvider.create("a");
|
|
972
|
-
*
|
|
973
|
-
* const listener: Invokable <LockEvents> = event => {
|
|
974
|
-
* console.log(event);
|
|
975
|
-
* }
|
|
976
|
-
* await lock.listenOnce(KeyAcquiredLockEvent, listener);
|
|
977
|
-
* await lock.acquire();
|
|
978
|
-
* ```
|
|
979
|
-
*/
|
|
980
|
-
listenOnce(event, listener) {
|
|
981
|
-
return this.lockEventBus.listenOnce(event, listener);
|
|
982
|
-
}
|
|
983
|
-
asPromise(event) {
|
|
984
|
-
return this.lockEventBus.asPromise(event);
|
|
985
|
-
}
|
|
986
|
-
/**
|
|
987
|
-
* You can listen to different events of the current <i>Lock</i> class instance.
|
|
988
|
-
*
|
|
989
|
-
* Refer to <i>{@link LockEvents}</i>, to se all events dispatched by <i>LockProvider</i> class instance.
|
|
990
|
-
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
991
|
-
* @example
|
|
992
|
-
* ```ts
|
|
993
|
-
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
994
|
-
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
995
|
-
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
996
|
-
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
997
|
-
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
998
|
-
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
999
|
-
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
1000
|
-
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
1001
|
-
*
|
|
1002
|
-
* const eventBus = new EventBus({
|
|
1003
|
-
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
1004
|
-
* });
|
|
1005
|
-
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
1006
|
-
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
1007
|
-
* serde,
|
|
1008
|
-
* adapter: new MemoryLockAdapter({
|
|
1009
|
-
* rootGroup: "@global"
|
|
1010
|
-
* }),
|
|
1011
|
-
* eventBus,
|
|
1012
|
-
* });
|
|
1013
|
-
*
|
|
1014
|
-
* const lock = lockProvider.create("a");
|
|
1015
|
-
*
|
|
1016
|
-
* const listener: Invokable <LockEvents> = event => {
|
|
1017
|
-
* console.log(event);
|
|
1018
|
-
* }
|
|
1019
|
-
* const unsubscribe = await lock.subscribe(KeyAcquiredLockEvent, listener);
|
|
1020
|
-
* await lock.acquire();
|
|
1021
|
-
* await unsubscribe();
|
|
1022
|
-
* ```
|
|
1023
|
-
*/
|
|
1024
|
-
subscribe(event, listener) {
|
|
1025
|
-
return this.lockEventBus.subscribe(event, listener);
|
|
1026
|
-
}
|
|
1027
|
-
/**
|
|
1028
|
-
* You can listen to different events of the current <i>Lock</i> class instance.
|
|
1029
|
-
*
|
|
1030
|
-
* Refer to <i>{@link LockEvents}</i>, to se all events dispatched by <i>LockProvider</i> class instance.
|
|
1031
|
-
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
1032
|
-
* @example
|
|
1033
|
-
* ```ts
|
|
1034
|
-
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
1035
|
-
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
1036
|
-
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
1037
|
-
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
1038
|
-
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
1039
|
-
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
1040
|
-
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
1041
|
-
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
1042
|
-
*
|
|
1043
|
-
* const eventBus = new EventBus({
|
|
1044
|
-
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
1045
|
-
* });
|
|
1046
|
-
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
1047
|
-
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
1048
|
-
* serde,
|
|
1049
|
-
* adapter: new MemoryLockAdapter({
|
|
1050
|
-
* rootGroup: "@global"
|
|
1051
|
-
* }),
|
|
1052
|
-
* eventBus,
|
|
1053
|
-
* });
|
|
1054
|
-
*
|
|
1055
|
-
* const lock = lockProvider.create("a");
|
|
1056
|
-
*
|
|
1057
|
-
* const listener: Invokable <LockEvents> = event => {
|
|
1058
|
-
* console.log(event);
|
|
1059
|
-
* }
|
|
1060
|
-
* const unsubscribe = await lock.subscribeMany([KeyAcquiredLockEvent], listener);
|
|
1061
|
-
* await lock.acquire();
|
|
1062
|
-
* await unsubscribe();
|
|
1063
|
-
* ```
|
|
1064
|
-
*/
|
|
1065
|
-
subscribeMany(events, listener) {
|
|
1066
|
-
return this.lockEventBus.subscribeMany(events, listener);
|
|
1067
|
-
}
|
|
1068
608
|
}
|
|
1069
609
|
//# sourceMappingURL=lock.js.map
|