autocomplete_database.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { autocomplete_request } from "./autocomplete_request";
  2. import { product } from "./product";
  3. export class autocomplete_database {
  4. #dataset;
  5. #completed;
  6. #current;
  7. #on_autocomplete;
  8. #on_create;
  9. #on_single_fail;
  10. #on_skip;
  11. #on_single_success;
  12. #finally;
  13. constructor(dataset) {
  14. this.#dataset = dataset;
  15. this.#completed = new Array();
  16. this.#current = null;
  17. }
  18. on_autocomplete(target) {
  19. this.#on_autocomplete = target;
  20. return this;
  21. }
  22. on_create(target) {
  23. this.#on_create = target;
  24. return this;
  25. }
  26. on_single_fail(target) {
  27. this.#on_single_fail = target;
  28. return this;
  29. }
  30. on_skip(target) {
  31. this.#on_skip = target;
  32. return this;
  33. }
  34. on_single_success(target) {
  35. this.#on_single_success = target;
  36. return this;
  37. }
  38. finally(target) {
  39. this.#finally = target;
  40. return this;
  41. }
  42. get current() {
  43. return this.#current;
  44. }
  45. async process() {
  46. for (const count of this.#dataset) {
  47. this.#current = count;
  48. if (query !== null) {
  49. query(count);
  50. }
  51. const request = new autocomplete_request(count.barcode);
  52. const response = await request.connect();
  53. if (!response.result) {
  54. throw new Error("Product barcode is not correct.");
  55. }
  56. const found = response.found;
  57. count.image = found.image;
  58. count.description = found.description;
  59. this.#completed.push(count);
  60. }
  61. return this;
  62. }
  63. }