diff --git a/package-lock.json b/package-lock.json index 180afa8..20ff2c7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@ulms/api-clients", - "version": "7.21.0", + "version": "7.22.0-dev.1-ULMS-3379", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@ulms/api-clients", - "version": "7.21.0", + "version": "7.22.0-dev.1-ULMS-3379", "license": "MIT", "dependencies": { "axios": "1.6.2", diff --git a/package.json b/package.json index 8a15d9c..374aeb5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ulms/api-clients", - "version": "7.21.0", + "version": "7.22.0-dev.1-ULMS-3379", "description": "JavaScript API clients for ULMS platform", "keywords": [], "homepage": "https://github.com/foxford/ulms-api-clients-js#readme", diff --git a/src/common.js b/src/common.js index 9b3b45c..7ee129a 100644 --- a/src/common.js +++ b/src/common.js @@ -191,3 +191,47 @@ export function mergeSignals(signals) { return controller.signal } + +// eslint-disable-next-line unicorn/prevent-abbreviations +export function listStateSet(client, roomId, set, params = {}) { + let result = [] + + function loop(room, parameters, callback) { + return rejectByTimeout(client.readState(room, [set], parameters)) + .then((response) => { + if (response[set]?.length > 0) { + result = [...result, ...response[set]] + + if (response.has_next) { + loop( + room, + { + ...parameters, + original_occurred_at: + // eslint-disable-next-line unicorn/prefer-at + response[set][response[set].length - 1].original_occurred_at, + }, + callback, + ) + } else { + callback() // eslint-disable-line promise/no-callback-in-promise + } + } else { + callback() // eslint-disable-line promise/no-callback-in-promise + } + }) + .catch((error) => callback(error)) // eslint-disable-line promise/no-callback-in-promise + } + + return new Promise((resolve, reject) => { + loop(roomId, params, (error) => { + if (error) { + reject(error) + + return + } + + resolve(result) + }) + }) +}