From f7bc5a73e1df49246f44ffb3ddc5b31b06a0187b Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Mon, 23 Jul 2018 17:42:33 -0400 Subject: [PATCH] LP#1775466 String service queue pending Signed-off-by: Bill Erickson --- .../src/eg2/src/app/share/string/string.service.ts | 56 ++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/share/string/string.service.ts b/Open-ILS/src/eg2/src/app/share/string/string.service.ts index 37a07d913d..9eb1196c74 100644 --- a/Open-ILS/src/eg2/src/app/share/string/string.service.ts +++ b/Open-ILS/src/eg2/src/app/share/string/string.service.ts @@ -5,24 +5,74 @@ interface StringAssignment { resolver: (ctx: any) => Promise; } +interface PendingInterpolation { + key: string; + ctx: any; + resolve: (string) => any; + reject: (string) => any; +} + @Injectable() export class StringService { strings: {[key: string]: StringAssignment} = {}; - constructor() {} + // This service can only interpolate one string at a time, since it + // maintains only one string component instance. Avoid clobbering + // in-process interpolation requests by maintaining a request queue. + private pending: PendingInterpolation[]; + + constructor() { + this.pending = []; + } register(assn: StringAssignment) { this.strings[assn.key] = assn; } interpolate(key: string, ctx?: any): Promise { + if (!this.strings[key]) { - return Promise.reject('No Such String'); + return Promise.reject(`String key not found: "${key}"`); } - return this.strings[key].resolver(ctx); + + return new Promise( (resolve, reject) => { + const pend: PendingInterpolation = { + key: key, + ctx: ctx, + resolve: resolve, + reject: reject + } + + this.pending.push(pend); + + // Avoid launching the pending string processer with >1 + // pending, because the processor will have already started. + if (this.pending.length === 1) { + this.processPending(); + } + }); } + processPending() { + const pstring = this.pending[0]; + this.strings[pstring.key].resolver(pstring.ctx).then( + txt => { + pstring.resolve(txt); + this.pending.shift(); + if (this.pending.length) { + this.processPending(); + } + }, + err => { + pstring.reject(err); + this.pending.shift(); + if (this.pending.length) { + this.processPending(); + } + } + ); + } } -- 2.11.0