1 import {Injectable} from '@angular/core';
3 interface StringAssignment {
4 key: string; // keyboard command
5 resolver: (ctx: any) => Promise<string>;
8 interface PendingInterpolation {
11 resolve: (string) => any;
12 reject: (string) => any;
16 export class StringService {
18 strings: {[key: string]: StringAssignment} = {};
20 // This service can only interpolate one string at a time, since it
21 // maintains only one string component instance. Avoid clobbering
22 // in-process interpolation requests by maintaining a request queue.
23 private pending: PendingInterpolation[];
29 register(assn: StringAssignment) {
30 this.strings[assn.key] = assn;
33 interpolate(key: string, ctx?: any): Promise<string> {
35 if (!this.strings[key]) {
36 return Promise.reject(`String key not found: "${key}"`);
39 return new Promise( (resolve, reject) => {
40 const pend: PendingInterpolation = {
47 this.pending.push(pend);
49 // Avoid launching the pending string processer with >1
50 // pending, because the processor will have already started.
51 if (this.pending.length === 1) {
52 this.processPending();
58 const pstring = this.pending[0];
59 this.strings[pstring.key].resolver(pstring.ctx).then(
63 if (this.pending.length) {
64 this.processPending();
70 if (this.pending.length) {
71 this.processPending();