@yuants/app-virtual-exchange 0.9.3 → 0.10.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.
Files changed (45) hide show
  1. package/dist/position.js +17 -0
  2. package/dist/position.js.map +1 -1
  3. package/dist/quote/__tests__/implementations.test.js +218 -0
  4. package/dist/quote/__tests__/implementations.test.js.map +1 -0
  5. package/dist/quote/implementations/v0.js +7 -9
  6. package/dist/quote/implementations/v0.js.map +1 -1
  7. package/dist/quote/implementations/v1.js +10 -4
  8. package/dist/quote/implementations/v1.js.map +1 -1
  9. package/dist/quote/prefix-matcher.js +7 -0
  10. package/dist/quote/prefix-matcher.js.map +1 -0
  11. package/dist/quote/request-key.js +20 -0
  12. package/dist/quote/request-key.js.map +1 -0
  13. package/dist/quote/service.js +38 -15
  14. package/dist/quote/service.js.map +1 -1
  15. package/dist/quote/upstream-routing.js +300 -0
  16. package/dist/quote/upstream-routing.js.map +1 -0
  17. package/lib/position.d.ts.map +1 -1
  18. package/lib/position.js +17 -0
  19. package/lib/position.js.map +1 -1
  20. package/lib/quote/__tests__/implementations.test.d.ts +2 -0
  21. package/lib/quote/__tests__/implementations.test.d.ts.map +1 -0
  22. package/lib/quote/__tests__/implementations.test.js +220 -0
  23. package/lib/quote/__tests__/implementations.test.js.map +1 -0
  24. package/lib/quote/implementations/v0.d.ts.map +1 -1
  25. package/lib/quote/implementations/v0.js +7 -9
  26. package/lib/quote/implementations/v0.js.map +1 -1
  27. package/lib/quote/implementations/v1.d.ts.map +1 -1
  28. package/lib/quote/implementations/v1.js +10 -4
  29. package/lib/quote/implementations/v1.js.map +1 -1
  30. package/lib/quote/prefix-matcher.d.ts +8 -0
  31. package/lib/quote/prefix-matcher.d.ts.map +1 -0
  32. package/lib/quote/prefix-matcher.js +11 -0
  33. package/lib/quote/prefix-matcher.js.map +1 -0
  34. package/lib/quote/request-key.d.ts +2 -0
  35. package/lib/quote/request-key.d.ts.map +1 -0
  36. package/lib/quote/request-key.js +24 -0
  37. package/lib/quote/request-key.js.map +1 -0
  38. package/lib/quote/service.js +38 -15
  39. package/lib/quote/service.js.map +1 -1
  40. package/lib/quote/upstream-routing.d.ts +15 -0
  41. package/lib/quote/upstream-routing.d.ts.map +1 -0
  42. package/lib/quote/upstream-routing.js +304 -0
  43. package/lib/quote/upstream-routing.js.map +1 -0
  44. package/package.json +11 -11
  45. package/temp/package-deps.json +21 -17
@@ -0,0 +1,220 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const implementations_1 = require("../implementations");
4
+ // 测试所有实现的通用测试套件
5
+ describe('IQuoteState implementations', () => {
6
+ // 测试每个实现
7
+ Object.entries(implementations_1.implementations).forEach(([name, createQuoteState]) => {
8
+ describe(`${name} implementation`, () => {
9
+ let quoteState;
10
+ beforeEach(() => {
11
+ quoteState = createQuoteState();
12
+ });
13
+ // 测试 1: 初始状态应该是空的
14
+ it('should have empty initial state', () => {
15
+ const dumped = quoteState.dumpAsObject();
16
+ expect(dumped).toEqual({});
17
+ });
18
+ // 测试 2: 更新单个产品的单个字段
19
+ it('should update single field for single product', () => {
20
+ const updateAction = {
21
+ product_001: {
22
+ last_price: ['100.5', 1000],
23
+ },
24
+ };
25
+ quoteState.update(updateAction);
26
+ // 验证通过 getValueTuple 获取
27
+ const tuple = quoteState.getValueTuple('product_001', 'last_price');
28
+ expect(tuple).toEqual(['100.5', 1000]);
29
+ // 验证 dumpAsObject
30
+ const dumped = quoteState.dumpAsObject();
31
+ expect(dumped).toEqual(updateAction);
32
+ });
33
+ // 测试 3: 更新多个产品和多个字段
34
+ it('should update multiple fields for multiple products', () => {
35
+ const updateAction = {
36
+ product_001: {
37
+ last_price: ['100.5', 1000],
38
+ ask_price: ['101.0', 1001],
39
+ },
40
+ product_002: {
41
+ bid_price: ['99.0', 1002],
42
+ ask_volume: ['500', 1003],
43
+ },
44
+ };
45
+ quoteState.update(updateAction);
46
+ // 验证所有字段
47
+ expect(quoteState.getValueTuple('product_001', 'last_price')).toEqual(['100.5', 1000]);
48
+ expect(quoteState.getValueTuple('product_001', 'ask_price')).toEqual(['101.0', 1001]);
49
+ expect(quoteState.getValueTuple('product_002', 'bid_price')).toEqual(['99.0', 1002]);
50
+ expect(quoteState.getValueTuple('product_002', 'ask_volume')).toEqual(['500', 1003]);
51
+ // 验证 dumpAsObject
52
+ const dumped = quoteState.dumpAsObject();
53
+ expect(dumped).toEqual(updateAction);
54
+ });
55
+ // 测试 4: 更新应该覆盖旧的时间戳,但忽略更旧的时间戳
56
+ it('should overwrite with newer timestamp, ignore older timestamp', () => {
57
+ // 初始更新
58
+ quoteState.update({
59
+ product_001: {
60
+ last_price: ['100.0', 1000],
61
+ },
62
+ });
63
+ // 用更新的时间戳覆盖
64
+ quoteState.update({
65
+ product_001: {
66
+ last_price: ['200.0', 2000],
67
+ },
68
+ });
69
+ expect(quoteState.getValueTuple('product_001', 'last_price')).toEqual(['200.0', 2000]);
70
+ // 用更旧的时间戳尝试覆盖(应该被忽略)
71
+ quoteState.update({
72
+ product_001: {
73
+ last_price: ['300.0', 1500], // 时间戳 1500 比 2000 旧
74
+ },
75
+ });
76
+ // 应该仍然是旧的值
77
+ expect(quoteState.getValueTuple('product_001', 'last_price')).toEqual(['200.0', 2000]);
78
+ });
79
+ // 测试 5: 获取不存在的产品或字段应该返回 undefined
80
+ it('should return undefined for non-existent product or field', () => {
81
+ expect(quoteState.getValueTuple('non_existent', 'last_price')).toBeUndefined();
82
+ // 添加一个产品但不包含该字段
83
+ quoteState.update({
84
+ product_001: {
85
+ ask_price: ['101.0', 1000],
86
+ },
87
+ });
88
+ expect(quoteState.getValueTuple('product_001', 'last_price')).toBeUndefined();
89
+ expect(quoteState.getValueTuple('product_002', 'last_price')).toBeUndefined();
90
+ });
91
+ // 测试 6: filter 方法应该返回符合条件的数据
92
+ it('should filter data by product_ids, fields, and updated_at', () => {
93
+ // 设置测试数据
94
+ quoteState.update({
95
+ product_001: {
96
+ last_price: ['100.0', 1000],
97
+ ask_price: ['101.0', 2000],
98
+ bid_price: ['99.0', 500], // 较旧的时间戳
99
+ },
100
+ product_002: {
101
+ last_price: ['200.0', 1500],
102
+ ask_price: ['201.0', 2500],
103
+ },
104
+ product_003: {
105
+ last_price: ['300.0', 1200],
106
+ },
107
+ });
108
+ // 测试 1: 过滤特定产品和字段,时间阈值 1000
109
+ const result1 = quoteState.filter(['product_001', 'product_002'], ['last_price', 'ask_price'], 1000);
110
+ expect(result1).toEqual({
111
+ product_001: {
112
+ last_price: ['100.0', 1000],
113
+ ask_price: ['101.0', 2000],
114
+ },
115
+ product_002: {
116
+ last_price: ['200.0', 1500],
117
+ ask_price: ['201.0', 2500],
118
+ },
119
+ });
120
+ // 测试 2: 过滤时间阈值 1500(排除较旧的数据)
121
+ const result2 = quoteState.filter(['product_001', 'product_002', 'product_003'], ['last_price', 'bid_price'], 1500);
122
+ expect(result2).toEqual({
123
+ product_001: {
124
+ // last_price 时间戳 1000 < 1500,应该被排除
125
+ // bid_price 时间戳 500 < 1500,应该被排除
126
+ },
127
+ product_002: {
128
+ last_price: ['200.0', 1500],
129
+ },
130
+ product_003: {
131
+ // last_price 时间戳 1200 < 1500,应该被排除
132
+ },
133
+ });
134
+ // 测试 3: 过滤不存在的产品应该返回空对象
135
+ const result3 = quoteState.filter(['non_existent'], ['last_price'], 0);
136
+ expect(result3).toEqual({
137
+ non_existent: {},
138
+ });
139
+ });
140
+ // 测试 7: 空更新不应该改变状态
141
+ it('should handle empty update action', () => {
142
+ // 先添加一些数据
143
+ quoteState.update({
144
+ product_001: {
145
+ last_price: ['100.0', 1000],
146
+ },
147
+ });
148
+ const beforeDump = quoteState.dumpAsObject();
149
+ // 空更新
150
+ quoteState.update({});
151
+ const afterDump = quoteState.dumpAsObject();
152
+ expect(afterDump).toEqual(beforeDump);
153
+ });
154
+ // 测试 8: 更新时只提供部分字段,不应影响其他字段
155
+ it('should not affect other fields when updating partial fields', () => {
156
+ // 初始设置两个字段
157
+ quoteState.update({
158
+ product_001: {
159
+ last_price: ['100.0', 1000],
160
+ ask_price: ['101.0', 1001],
161
+ },
162
+ });
163
+ // 只更新一个字段
164
+ quoteState.update({
165
+ product_001: {
166
+ last_price: ['200.0', 2000],
167
+ },
168
+ });
169
+ // last_price 应该更新,ask_price 应该保持不变
170
+ expect(quoteState.getValueTuple('product_001', 'last_price')).toEqual(['200.0', 2000]);
171
+ expect(quoteState.getValueTuple('product_001', 'ask_price')).toEqual(['101.0', 1001]);
172
+ });
173
+ // 测试 9: 重复的更新应该保持幂等性
174
+ it('should be idempotent for duplicate updates', () => {
175
+ const updateAction = {
176
+ product_001: {
177
+ last_price: ['100.0', 1000],
178
+ },
179
+ };
180
+ // 多次执行相同的更新
181
+ quoteState.update(updateAction);
182
+ quoteState.update(updateAction);
183
+ quoteState.update(updateAction);
184
+ // 状态应该与单次更新相同
185
+ expect(quoteState.dumpAsObject()).toEqual(updateAction);
186
+ });
187
+ // 测试 10: 字段名称应该是 IQuoteKey 类型
188
+ it('should handle all IQuoteKey field types', () => {
189
+ const allFields = [
190
+ 'last_price',
191
+ 'ask_price',
192
+ 'ask_volume',
193
+ 'bid_volume',
194
+ 'bid_price',
195
+ 'interest_rate_short',
196
+ 'open_interest',
197
+ 'interest_rate_prev_settled_at',
198
+ 'interest_rate_next_settled_at',
199
+ 'interest_rate_long',
200
+ ];
201
+ const updateAction = {
202
+ product_001: {},
203
+ };
204
+ // 为每个字段设置值
205
+ allFields.forEach((field, index) => {
206
+ updateAction['product_001'][field] = [`value_${index}`, 1000 + index];
207
+ });
208
+ quoteState.update(updateAction);
209
+ // 验证每个字段
210
+ allFields.forEach((field, index) => {
211
+ expect(quoteState.getValueTuple('product_001', field)).toEqual([`value_${index}`, 1000 + index]);
212
+ });
213
+ // 验证 dumpAsObject
214
+ const dumped = quoteState.dumpAsObject();
215
+ expect(dumped).toEqual(updateAction);
216
+ });
217
+ });
218
+ });
219
+ });
220
+ //# sourceMappingURL=implementations.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"implementations.test.js","sourceRoot":"","sources":["../../../src/quote/__tests__/implementations.test.ts"],"names":[],"mappings":";;AAAA,wDAAqD;AAGrD,gBAAgB;AAChB,QAAQ,CAAC,6BAA6B,EAAE,GAAG,EAAE;IAC3C,SAAS;IACT,MAAM,CAAC,OAAO,CAAC,iCAAe,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,EAAE,gBAAgB,CAAC,EAAE,EAAE;QACnE,QAAQ,CAAC,GAAG,IAAI,iBAAiB,EAAE,GAAG,EAAE;YACtC,IAAI,UAA+C,CAAC;YAEpD,UAAU,CAAC,GAAG,EAAE;gBACd,UAAU,GAAG,gBAAgB,EAAE,CAAC;YAClC,CAAC,CAAC,CAAC;YAEH,kBAAkB;YAClB,EAAE,CAAC,iCAAiC,EAAE,GAAG,EAAE;gBACzC,MAAM,MAAM,GAAG,UAAU,CAAC,YAAY,EAAE,CAAC;gBACzC,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;YAC7B,CAAC,CAAC,CAAC;YAEH,oBAAoB;YACpB,EAAE,CAAC,+CAA+C,EAAE,GAAG,EAAE;gBACvD,MAAM,YAAY,GAAuB;oBACvC,WAAW,EAAE;wBACX,UAAU,EAAE,CAAC,OAAO,EAAE,IAAI,CAAC;qBAC5B;iBACF,CAAC;gBAEF,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;gBAEhC,wBAAwB;gBACxB,MAAM,KAAK,GAAG,UAAU,CAAC,aAAa,CAAC,aAAa,EAAE,YAAY,CAAC,CAAC;gBACpE,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;gBAEvC,kBAAkB;gBAClB,MAAM,MAAM,GAAG,UAAU,CAAC,YAAY,EAAE,CAAC;gBACzC,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;YACvC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E,EAAE,IAAI,GAAG,KAAK,CAAC,CAAC;gBACxE,CAAC,CAAC,CAAC;gBAEH,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;gBAEhC,SAAS;gBACT,SAAS,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;oBACjC,MAAM,CAAC,UAAU,CAAC,aAAa,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,SAAS,KAAK,EAAE,EAAE,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC;gBACnG,CAAC,CAAC,CAAC;gBAEH,kBAAkB;gBAClB,MAAM,MAAM,GAAG,UAAU,CAAC,YAAY,EAAE,CAAC;gBACzC,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;YACvC,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { implementations } from '../implementations';\nimport { IQuoteKey, IQuoteUpdateAction } from '../types';\n\n// 测试所有实现的通用测试套件\ndescribe('IQuoteState implementations', () => {\n // 测试每个实现\n Object.entries(implementations).forEach(([name, createQuoteState]) => {\n describe(`${name} implementation`, () => {\n let quoteState: ReturnType<typeof createQuoteState>;\n\n beforeEach(() => {\n quoteState = createQuoteState();\n });\n\n // 测试 1: 初始状态应该是空的\n it('should have empty initial state', () => {\n const dumped = quoteState.dumpAsObject();\n expect(dumped).toEqual({});\n });\n\n // 测试 2: 更新单个产品的单个字段\n it('should update single field for single product', () => {\n const updateAction: IQuoteUpdateAction = {\n product_001: {\n last_price: ['100.5', 1000],\n },\n };\n\n quoteState.update(updateAction);\n\n // 验证通过 getValueTuple 获取\n const tuple = quoteState.getValueTuple('product_001', 'last_price');\n expect(tuple).toEqual(['100.5', 1000]);\n\n // 验证 dumpAsObject\n const dumped = quoteState.dumpAsObject();\n expect(dumped).toEqual(updateAction);\n });\n\n // 测试 3: 更新多个产品和多个字段\n it('should update multiple fields for multiple products', () => {\n const updateAction: IQuoteUpdateAction = {\n product_001: {\n last_price: ['100.5', 1000],\n ask_price: ['101.0', 1001],\n },\n product_002: {\n bid_price: ['99.0', 1002],\n ask_volume: ['500', 1003],\n },\n };\n\n quoteState.update(updateAction);\n\n // 验证所有字段\n expect(quoteState.getValueTuple('product_001', 'last_price')).toEqual(['100.5', 1000]);\n expect(quoteState.getValueTuple('product_001', 'ask_price')).toEqual(['101.0', 1001]);\n expect(quoteState.getValueTuple('product_002', 'bid_price')).toEqual(['99.0', 1002]);\n expect(quoteState.getValueTuple('product_002', 'ask_volume')).toEqual(['500', 1003]);\n\n // 验证 dumpAsObject\n const dumped = quoteState.dumpAsObject();\n expect(dumped).toEqual(updateAction);\n });\n\n // 测试 4: 更新应该覆盖旧的时间戳,但忽略更旧的时间戳\n it('should overwrite with newer timestamp, ignore older timestamp', () => {\n // 初始更新\n quoteState.update({\n product_001: {\n last_price: ['100.0', 1000],\n },\n });\n\n // 用更新的时间戳覆盖\n quoteState.update({\n product_001: {\n last_price: ['200.0', 2000],\n },\n });\n\n expect(quoteState.getValueTuple('product_001', 'last_price')).toEqual(['200.0', 2000]);\n\n // 用更旧的时间戳尝试覆盖(应该被忽略)\n quoteState.update({\n product_001: {\n last_price: ['300.0', 1500], // 时间戳 1500 比 2000 旧\n },\n });\n\n // 应该仍然是旧的值\n expect(quoteState.getValueTuple('product_001', 'last_price')).toEqual(['200.0', 2000]);\n });\n\n // 测试 5: 获取不存在的产品或字段应该返回 undefined\n it('should return undefined for non-existent product or field', () => {\n expect(quoteState.getValueTuple('non_existent', 'last_price')).toBeUndefined();\n\n // 添加一个产品但不包含该字段\n quoteState.update({\n product_001: {\n ask_price: ['101.0', 1000],\n },\n });\n\n expect(quoteState.getValueTuple('product_001', 'last_price')).toBeUndefined();\n expect(quoteState.getValueTuple('product_002', 'last_price')).toBeUndefined();\n });\n\n // 测试 6: filter 方法应该返回符合条件的数据\n it('should filter data by product_ids, fields, and updated_at', () => {\n // 设置测试数据\n quoteState.update({\n product_001: {\n last_price: ['100.0', 1000],\n ask_price: ['101.0', 2000], // 较新的时间戳\n bid_price: ['99.0', 500], // 较旧的时间戳\n },\n product_002: {\n last_price: ['200.0', 1500],\n ask_price: ['201.0', 2500],\n },\n product_003: {\n last_price: ['300.0', 1200],\n },\n });\n\n // 测试 1: 过滤特定产品和字段,时间阈值 1000\n const result1 = quoteState.filter(['product_001', 'product_002'], ['last_price', 'ask_price'], 1000);\n\n expect(result1).toEqual({\n product_001: {\n last_price: ['100.0', 1000],\n ask_price: ['101.0', 2000],\n },\n product_002: {\n last_price: ['200.0', 1500],\n ask_price: ['201.0', 2500],\n },\n });\n\n // 测试 2: 过滤时间阈值 1500(排除较旧的数据)\n const result2 = quoteState.filter(\n ['product_001', 'product_002', 'product_003'],\n ['last_price', 'bid_price'],\n 1500,\n );\n\n expect(result2).toEqual({\n product_001: {\n // last_price 时间戳 1000 < 1500,应该被排除\n // bid_price 时间戳 500 < 1500,应该被排除\n },\n product_002: {\n last_price: ['200.0', 1500],\n },\n product_003: {\n // last_price 时间戳 1200 < 1500,应该被排除\n },\n });\n\n // 测试 3: 过滤不存在的产品应该返回空对象\n const result3 = quoteState.filter(['non_existent'], ['last_price'], 0);\n\n expect(result3).toEqual({\n non_existent: {},\n });\n });\n\n // 测试 7: 空更新不应该改变状态\n it('should handle empty update action', () => {\n // 先添加一些数据\n quoteState.update({\n product_001: {\n last_price: ['100.0', 1000],\n },\n });\n\n const beforeDump = quoteState.dumpAsObject();\n\n // 空更新\n quoteState.update({});\n\n const afterDump = quoteState.dumpAsObject();\n expect(afterDump).toEqual(beforeDump);\n });\n\n // 测试 8: 更新时只提供部分字段,不应影响其他字段\n it('should not affect other fields when updating partial fields', () => {\n // 初始设置两个字段\n quoteState.update({\n product_001: {\n last_price: ['100.0', 1000],\n ask_price: ['101.0', 1001],\n },\n });\n\n // 只更新一个字段\n quoteState.update({\n product_001: {\n last_price: ['200.0', 2000],\n },\n });\n\n // last_price 应该更新,ask_price 应该保持不变\n expect(quoteState.getValueTuple('product_001', 'last_price')).toEqual(['200.0', 2000]);\n expect(quoteState.getValueTuple('product_001', 'ask_price')).toEqual(['101.0', 1001]);\n });\n\n // 测试 9: 重复的更新应该保持幂等性\n it('should be idempotent for duplicate updates', () => {\n const updateAction: IQuoteUpdateAction = {\n product_001: {\n last_price: ['100.0', 1000],\n },\n };\n\n // 多次执行相同的更新\n quoteState.update(updateAction);\n quoteState.update(updateAction);\n quoteState.update(updateAction);\n\n // 状态应该与单次更新相同\n expect(quoteState.dumpAsObject()).toEqual(updateAction);\n });\n\n // 测试 10: 字段名称应该是 IQuoteKey 类型\n it('should handle all IQuoteKey field types', () => {\n const allFields: IQuoteKey[] = [\n 'last_price',\n 'ask_price',\n 'ask_volume',\n 'bid_volume',\n 'bid_price',\n 'interest_rate_short',\n 'open_interest',\n 'interest_rate_prev_settled_at',\n 'interest_rate_next_settled_at',\n 'interest_rate_long',\n ];\n\n const updateAction: IQuoteUpdateAction = {\n product_001: {},\n };\n\n // 为每个字段设置值\n allFields.forEach((field, index) => {\n updateAction['product_001'][field] = [`value_${index}`, 1000 + index];\n });\n\n quoteState.update(updateAction);\n\n // 验证每个字段\n allFields.forEach((field, index) => {\n expect(quoteState.getValueTuple('product_001', field)).toEqual([`value_${index}`, 1000 + index]);\n });\n\n // 验证 dumpAsObject\n const dumped = quoteState.dumpAsObject();\n expect(dumped).toEqual(updateAction);\n });\n });\n });\n});\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"v0.d.ts","sourceRoot":"","sources":["../../../src/quote/implementations/v0.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,WAAW,EAAsB,MAAM,UAAU,CAAC;AAEtE;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,WAAW,CAgEhD"}
1
+ {"version":3,"file":"v0.d.ts","sourceRoot":"","sources":["../../../src/quote/implementations/v0.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,WAAW,EAAsB,MAAM,UAAU,CAAC;AAEtE;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,WAAW,CA+DhD"}
@@ -47,18 +47,16 @@ function createQuoteStateV0() {
47
47
  const result = {};
48
48
  for (const product_id of product_ids) {
49
49
  const productMap = data.get(product_id);
50
- if (!productMap)
51
- continue;
52
50
  const productData = {};
53
- for (const field of fields) {
54
- const tuple = productMap.get(field);
55
- if (tuple && tuple[1] >= updated_at) {
56
- productData[field] = tuple;
51
+ if (productMap) {
52
+ for (const field of fields) {
53
+ const tuple = productMap.get(field);
54
+ if (tuple && tuple[1] >= updated_at) {
55
+ productData[field] = tuple;
56
+ }
57
57
  }
58
58
  }
59
- if (Object.keys(productData).length > 0) {
60
- result[product_id] = productData;
61
- }
59
+ result[product_id] = productData;
62
60
  }
63
61
  return result;
64
62
  };
@@ -1 +1 @@
1
- {"version":3,"file":"v0.js","sourceRoot":"","sources":["../../../src/quote/implementations/v0.ts"],"names":[],"mappings":";;;AAEA;;;GAGG;AACH,SAAgB,kBAAkB;IAChC,sDAAsD;IACtD,MAAM,IAAI,GAAG,IAAI,GAAG,EAA4C,CAAC;IAEjE,MAAM,MAAM,GAAG,CAAC,MAA0B,EAAE,EAAE;QAC5C,KAAK,MAAM,UAAU,IAAI,MAAM,EAAE;YAC/B,IAAI,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;YACtC,IAAI,CAAC,UAAU,EAAE;gBACf,UAAU,GAAG,IAAI,GAAG,EAAE,CAAC;gBACvB,IAAI,CAAC,GAAG,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;aAClC;YAED,MAAM,MAAM,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;YAClC,KAAK,MAAM,UAAU,IAAI,MAAM,EAAE;gBAC/B,MAAM,KAAK,GAAG,UAAuB,CAAC;gBACtC,MAAM,CAAC,KAAK,EAAE,UAAU,CAAC,GAAG,MAAM,CAAC,KAAK,CAAE,CAAC;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AAiD,EAAE,CAAC;YACrE,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE;gBAC1B,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;gBACpC,IAAI,KAAK,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,UAAU,EAAE;oBACnC,WAAW,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;iBAC5B;aACF;YAED,IAAI,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE;gBACvC,MAAM,CAAC,UAAU,CAAC,GAAG,WAAW,CAAC;aAClC;SACF;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IAEF,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,EAAE,CAAC;AACzD,CAAC;AAhED,gDAgEC","sourcesContent":["import { IQuoteKey, IQuoteState, IQuoteUpdateAction } from '../types';\n\n/**\n * 创建一个简单的基于 Map 的实现,用于对比测试\n * 这个实现使用嵌套 Map 结构,作为性能对比的基准\n */\nexport function createQuoteStateV0(): IQuoteState {\n // 使用三层嵌套结构:product_id -> field -> [value, updated_at]\n const data = new Map<string, Map<IQuoteKey, [string, number]>>();\n\n const update = (action: IQuoteUpdateAction) => {\n for (const product_id in action) {\n let productMap = data.get(product_id);\n if (!productMap) {\n productMap = new Map();\n data.set(product_id, productMap);\n }\n\n const fields = action[product_id];\n for (const field_name in fields) {\n const field = field_name as IQuoteKey;\n const [value, updated_at] = fields[field]!;\n const existing = productMap.get(field);\n if (!existing || updated_at >= existing[1]) {\n productMap.set(field, [value, updated_at]);\n }\n }\n }\n };\n\n const dumpAsObject = (): IQuoteUpdateAction => {\n const result: IQuoteUpdateAction = {};\n data.forEach((productMap, product_id) => {\n const productData: Partial<Record<IQuoteKey, [string, number]>> = {};\n productMap.forEach((tuple, field) => {\n productData[field] = tuple;\n });\n result[product_id] = productData;\n });\n return result;\n };\n\n const getValueTuple = (product_id: string, field: IQuoteKey): [string, number] | undefined => {\n const productMap = data.get(product_id);\n if (!productMap) return undefined;\n return productMap.get(field);\n };\n\n const filter = (product_ids: string[], fields: IQuoteKey[], updated_at: number): IQuoteUpdateAction => {\n const result: IQuoteUpdateAction = {};\n for (const product_id of product_ids) {\n const productMap = data.get(product_id);\n if (!productMap) continue;\n\n const productData: Partial<Record<IQuoteKey, [string, number]>> = {};\n for (const field of fields) {\n const tuple = productMap.get(field);\n if (tuple && tuple[1] >= updated_at) {\n productData[field] = tuple;\n }\n }\n\n if (Object.keys(productData).length > 0) {\n result[product_id] = productData;\n }\n }\n return result;\n };\n\n return { update, dumpAsObject, getValueTuple, filter };\n}\n"]}
1
+ {"version":3,"file":"v0.js","sourceRoot":"","sources":["../../../src/quote/implementations/v0.ts"],"names":[],"mappings":";;;AAEA;;;GAGG;AACH,SAAgB,kBAAkB;IAChC,sDAAsD;IACtD,MAAM,IAAI,GAAG,IAAI,GAAG,EAA4C,CAAC;IAEjE,MAAM,MAAM,GAAG,CAAC,MAA0B,EAAE,EAAE;QAC5C,KAAK,MAAM,UAAU,IAAI,MAAM,EAAE;YAC/B,IAAI,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;YACtC,IAAI,CAAC,UAAU,EAAE;gBACf,UAAU,GAAG,IAAI,GAAG,EAAE,CAAC;gBACvB,IAAI,CAAC,GAAG,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;aAClC;YAED,MAAM,MAAM,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;YAClC,KAAK,MAAM,UAAU,IAAI,MAAM,EAAE;gBAC/B,MAAM,KAAK,GAAG,UAAuB,CAAC;gBACtC,MAAM,CAAC,KAAK,EAAE,UAAU,CAAC,GAAG,MAAM,CAAC,KAAK,CAAE,CAAC;gBAC3C,MAAM,QAAQ,GAAG,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;gBACvC,IAAI,CAAC,QAAQ,IAAI,UAAU,IAAI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;oBACpC,IAAI,KAAK,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,UAAU,EAAE;wBACnC,WAAW,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;qBAC5B;iBACF;aACF;YAED,MAAM,CAAC,UAAU,CAAC,GAAG,WAAW,CAAC;SAClC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IAEF,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,EAAE,CAAC;AACzD,CAAC;AA/DD,gDA+DC","sourcesContent":["import { IQuoteKey, IQuoteState, IQuoteUpdateAction } from '../types';\n\n/**\n * 创建一个简单的基于 Map 的实现,用于对比测试\n * 这个实现使用嵌套 Map 结构,作为性能对比的基准\n */\nexport function createQuoteStateV0(): IQuoteState {\n // 使用三层嵌套结构:product_id -> field -> [value, updated_at]\n const data = new Map<string, Map<IQuoteKey, [string, number]>>();\n\n const update = (action: IQuoteUpdateAction) => {\n for (const product_id in action) {\n let productMap = data.get(product_id);\n if (!productMap) {\n productMap = new Map();\n data.set(product_id, productMap);\n }\n\n const fields = action[product_id];\n for (const field_name in fields) {\n const field = field_name as IQuoteKey;\n const [value, updated_at] = fields[field]!;\n const existing = productMap.get(field);\n if (!existing || updated_at >= existing[1]) {\n productMap.set(field, [value, updated_at]);\n }\n }\n }\n };\n\n const dumpAsObject = (): IQuoteUpdateAction => {\n const result: IQuoteUpdateAction = {};\n data.forEach((productMap, product_id) => {\n const productData: Partial<Record<IQuoteKey, [string, number]>> = {};\n productMap.forEach((tuple, field) => {\n productData[field] = tuple;\n });\n result[product_id] = productData;\n });\n return result;\n };\n\n const getValueTuple = (product_id: string, field: IQuoteKey): [string, number] | undefined => {\n const productMap = data.get(product_id);\n if (!productMap) return undefined;\n return productMap.get(field);\n };\n\n const filter = (product_ids: string[], fields: IQuoteKey[], updated_at: number): IQuoteUpdateAction => {\n const result: IQuoteUpdateAction = {};\n for (const product_id of product_ids) {\n const productMap = data.get(product_id);\n const productData: Partial<Record<IQuoteKey, [string, number]>> = {};\n\n if (productMap) {\n for (const field of fields) {\n const tuple = productMap.get(field);\n if (tuple && tuple[1] >= updated_at) {\n productData[field] = tuple;\n }\n }\n }\n\n result[product_id] = productData;\n }\n return result;\n };\n\n return { update, dumpAsObject, getValueTuple, filter };\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"v1.d.ts","sourceRoot":"","sources":["../../../src/quote/implementations/v1.ts"],"names":[],"mappings":"AACA,OAAO,EAAa,WAAW,EAAsB,MAAM,UAAU,CAAC;AAoBtE;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,QAAO,WAyFrC,CAAC"}
1
+ {"version":3,"file":"v1.d.ts","sourceRoot":"","sources":["../../../src/quote/implementations/v1.ts"],"names":[],"mappings":"AACA,OAAO,EAAa,WAAW,EAAsB,MAAM,UAAU,CAAC;AAoBtE;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,QAAO,WA8FrC,CAAC"}
@@ -32,9 +32,7 @@ const createQuoteStateV1 = () => {
32
32
  const getFieldOffset = (product_id, field) => {
33
33
  let baseIndex = mapProductIdToIndex.get(product_id);
34
34
  if (baseIndex === undefined) {
35
- baseIndex = mapProductIdToIndex.size * FIELD_COUNT * 2;
36
- products.push(product_id);
37
- mapProductIdToIndex.set(product_id, baseIndex);
35
+ return undefined;
38
36
  }
39
37
  const fieldOffset = mapFieldNameToOffset[field];
40
38
  if (fieldOffset === undefined)
@@ -43,6 +41,8 @@ const createQuoteStateV1 = () => {
43
41
  };
44
42
  const getValueTuple = (product_id, field) => {
45
43
  const offset = getFieldOffset(product_id, field);
44
+ if (offset === undefined)
45
+ return undefined;
46
46
  const value = data[offset];
47
47
  if (value === undefined)
48
48
  return undefined;
@@ -50,7 +50,13 @@ const createQuoteStateV1 = () => {
50
50
  return [value, updated_at];
51
51
  };
52
52
  const setValueTuple = (product_id, field, value, updated_at) => {
53
- const offset = getFieldOffset(product_id, field);
53
+ let offset = getFieldOffset(product_id, field);
54
+ if (offset === undefined) {
55
+ const baseIndex = mapProductIdToIndex.size * FIELD_COUNT * 2;
56
+ products.push(product_id);
57
+ mapProductIdToIndex.set(product_id, baseIndex);
58
+ offset = baseIndex + mapFieldNameToOffset[field];
59
+ }
54
60
  data[offset] = value;
55
61
  data[offset + 1] = updated_at;
56
62
  };
@@ -1 +1 @@
1
- {"version":3,"file":"v1.js","sourceRoot":"","sources":["../../../src/quote/implementations/v1.ts"],"names":[],"mappings":";;;AAAA,yCAAyC;AAGzC,wBAAwB;AACxB,MAAM,MAAM,GAAG,CAAC,CAAC,CAAiC,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,EAAiB,CAAC,CAAC;IAC3F,mDAAmD;IACnD,UAAU,EAAE,CAAC;IACb,SAAS,EAAE,CAAC;IACZ,UAAU,EAAE,CAAC;IACb,UAAU,EAAE,CAAC;IACb,SAAS,EAAE,CAAC;IACZ,mBAAmB,EAAE,CAAC;IACtB,aAAa,EAAE,CAAC;IAChB,6BAA6B,EAAE,CAAC;IAChC,6BAA6B,EAAE,CAAC;IAChC,kBAAkB,EAAE,CAAC;CACtB,CAAC,CAAC;AAEH,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC;AAClC,MAAM,oBAAoB,GAAG,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;AAElG;;;;GAIG;AACI,MAAM,kBAAkB,GAAG,GAAgB,EAAE;IAClD,+BAA+B;IAC/B,MAAM,IAAI,GAAwB,EAAE,CAAC;IACrC,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,MAAM,mBAAmB,GAAG,IAAI,GAAG,EAAkB,CAAC;IACtD,2CAA2C;IAC3C,MAAM,cAAc,GAAG,CAAC,UAAkB,EAAE,KAAa,EAAU,EAAE;QACnE,IA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,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;YAEtC,MAAM,CAAC,UAAU,CAAC,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;SACtD;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IAEF;;;;;;OAMG;IACH,MAAM,MAAM,GAAG,CAAC,WAAqB,EAAE,MAAmB,EAAE,UAAkB,EAAsB,EAAE;QACpG,MAAM,MAAM,GAAuB,EAAE,CAAC;QACtC,KAAK,MAAM,UAAU,IAAI,WAAW,EAAE;YACpC,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC;YACxB,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE;gBAC1B,MAAM,KAAK,GAAG,aAAa,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;gBAC/C,IAAI,KAAK,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,UAAU,EAAE;oBACnC,MAAM,CAAC,UAAU,CAAE,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;iBACpC;aACF;SACF;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IAEF,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,EAAE,CAAC;AACzD,CAAC,CAAC;AAzFW,QAAA,kBAAkB,sBAyF7B","sourcesContent":["import { newError } from '@yuants/utils';\nimport { IQuoteKey, IQuoteState, IQuoteUpdateAction } from '../types';\n\n// TRICK: 固定字段顺序,方便计算偏移量\nconst FIELDS = ((x: { [key in IQuoteKey]: number }) => Object.keys(x).sort() as IQuoteKey[])({\n // TS TRICK: 强制运行时数组具有 IQuoteKey 的所有字段。不重不漏,味道真是好极了\n last_price: 0,\n ask_price: 0,\n ask_volume: 0,\n bid_volume: 0,\n bid_price: 0,\n interest_rate_short: 0,\n open_interest: 0,\n interest_rate_prev_settled_at: 0,\n interest_rate_next_settled_at: 0,\n interest_rate_long: 0,\n});\n\nconst FIELD_COUNT = FIELDS.length;\nconst mapFieldNameToOffset = Object.fromEntries(FIELDS.map((field, index) => [field, index * 2]));\n\n/**\n * 高效的行情状态管理器\n * 内部使用扁平化数组存储数据,避免内存碎片化和过多的 Map/对象开销\n * 提供高效的读写接口,支持按需更新和查询\n */\nexport const createQuoteStateV1 = (): IQuoteState => {\n // 内部数据结构的设计需要考虑高效的读写性能,防止内存碎片化\n const data: (string | number)[] = [];\n const products: string[] = [];\n const mapProductIdToIndex = new Map<string, number>();\n // 0~20 (10 fields * 2 (value, updated_at))\n const getFieldOffset = (product_id: string, field: string): number => {\n let baseIndex = mapProductIdToIndex.get(product_id);\n if (baseIndex === undefined) {\n baseIndex = mapProductIdToIndex.size * FIELD_COUNT * 2;\n products.push(product_id);\n mapProductIdToIndex.set(product_id, baseIndex);\n }\n const fieldOffset = mapFieldNameToOffset[field];\n if (fieldOffset === undefined) throw newError('INVALID_FIELD_NAME', { field, available_fields: FIELDS });\n return baseIndex + fieldOffset;\n };\n\n const getValueTuple = (product_id: string, field: IQuoteKey): [string, number] | undefined => {\n const offset = getFieldOffset(product_id, field);\n const value = data[offset] as string;\n if (value === undefined) return undefined;\n const updated_at = data[offset + 1] as number;\n return [value, updated_at];\n };\n\n const setValueTuple = (product_id: string, field: IQuoteKey, value: string, updated_at: number) => {\n const offset = getFieldOffset(product_id, field);\n data[offset] = value;\n data[offset + 1] = updated_at;\n };\n\n const update = (action: IQuoteUpdateAction) => {\n for (const product_id in action) {\n const fields = action[product_id];\n for (const field_name in fields) {\n const field = field_name as IQuoteKey;\n const [value, updated_at] = fields[field]!;\n const oldTuple = getValueTuple(product_id, field);\n if (oldTuple === undefined || updated_at >= oldTuple[1]) {\n setValueTuple(product_id, field, value, updated_at);\n }\n }\n }\n };\n\n const dumpAsObject = (): IQuoteUpdateAction => {\n const result: IQuoteUpdateAction = {};\n for (const product_id of products) {\n result[product_id] = {};\n }\n for (let i = 0; i < data.length; i += 2) {\n const value = data[i] as string;\n if (value === undefined) continue;\n const updated_at = data[i + 1] as number;\n\n const productIndex = Math.floor(i / (FIELD_COUNT * 2));\n const product_id = products[productIndex];\n\n const fieldIndex = (i % (FIELD_COUNT * 2)) / 2;\n const field_name = FIELDS[fieldIndex];\n\n result[product_id][field_name] = [value, updated_at];\n }\n return result;\n };\n\n /**\n * 过滤状态,返回指定 product_id 列表和字段列表中,且更新时间不早于指定时间的字段数据\n * @param product_ids - 需要过滤的 product_id 列表\n * @param fields - 需要过滤的字段列表\n * @param updated_at - 需要过滤的更新时间阈值 (仅返回更新时间不早于该值的字段)\n * @returns 过滤后的数据\n */\n const filter = (product_ids: string[], fields: IQuoteKey[], updated_at: number): IQuoteUpdateAction => {\n const result: IQuoteUpdateAction = {};\n for (const product_id of product_ids) {\n result[product_id] = {};\n for (const field of fields) {\n const tuple = getValueTuple(product_id, field);\n if (tuple && tuple[1] >= updated_at) {\n result[product_id]![field] = tuple;\n }\n }\n }\n return result;\n };\n\n return { update, dumpAsObject, getValueTuple, filter };\n};\n"]}
1
+ {"version":3,"file":"v1.js","sourceRoot":"","sources":["../../../src/quote/implementations/v1.ts"],"names":[],"mappings":";;;AAAA,yCAAyC;AAGzC,wBAAwB;AACxB,MAAM,MAAM,GAAG,CAAC,CAAC,CAAiC,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,EAAiB,CAAC,CAAC;IAC3F,mDAAmD;IACnD,UAAU,EAAE,CAAC;IACb,SAAS,EAAE,CAAC;IACZ,UAAU,EAAE,CAAC;IACb,UAAU,EAAE,CAAC;IACb,SAAS,EAAE,CAAC;IACZ,mBAAmB,EAAE,CAAC;IACtB,aAAa,EAAE,CAAC;IAChB,6BAA6B,EAAE,CAAC;IAChC,6BAA6B,EAAE,CAAC;IAChC,kBAAkB,EAAE,CAAC;CACtB,CAAC,CAAC;AAEH,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC;AAClC,MAAM,oBAAoB,GAAG,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC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AAC;iBACpC;aACF;SACF;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IAEF,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,EAAE,CAAC;AACzD,CAAC,CAAC;AA9FW,QAAA,kBAAkB,sBA8F7B","sourcesContent":["import { newError } from '@yuants/utils';\nimport { IQuoteKey, IQuoteState, IQuoteUpdateAction } from '../types';\n\n// TRICK: 固定字段顺序,方便计算偏移量\nconst FIELDS = ((x: { [key in IQuoteKey]: number }) => Object.keys(x).sort() as IQuoteKey[])({\n // TS TRICK: 强制运行时数组具有 IQuoteKey 的所有字段。不重不漏,味道真是好极了\n last_price: 0,\n ask_price: 0,\n ask_volume: 0,\n bid_volume: 0,\n bid_price: 0,\n interest_rate_short: 0,\n open_interest: 0,\n interest_rate_prev_settled_at: 0,\n interest_rate_next_settled_at: 0,\n interest_rate_long: 0,\n});\n\nconst FIELD_COUNT = FIELDS.length;\nconst mapFieldNameToOffset = Object.fromEntries(FIELDS.map((field, index) => [field, index * 2]));\n\n/**\n * 高效的行情状态管理器\n * 内部使用扁平化数组存储数据,避免内存碎片化和过多的 Map/对象开销\n * 提供高效的读写接口,支持按需更新和查询\n */\nexport const createQuoteStateV1 = (): IQuoteState => {\n // 内部数据结构的设计需要考虑高效的读写性能,防止内存碎片化\n const data: (string | number)[] = [];\n const products: string[] = [];\n const mapProductIdToIndex = new Map<string, number>();\n // 0~20 (10 fields * 2 (value, updated_at))\n const getFieldOffset = (product_id: string, field: string): number | undefined => {\n let baseIndex = mapProductIdToIndex.get(product_id);\n if (baseIndex === undefined) {\n return undefined;\n }\n const fieldOffset = mapFieldNameToOffset[field];\n if (fieldOffset === undefined) throw newError('INVALID_FIELD_NAME', { field, available_fields: FIELDS });\n return baseIndex + fieldOffset;\n };\n\n const getValueTuple = (product_id: string, field: IQuoteKey): [string, number] | undefined => {\n const offset = getFieldOffset(product_id, field);\n if (offset === undefined) return undefined;\n const value = data[offset] as string;\n if (value === undefined) return undefined;\n const updated_at = data[offset + 1] as number;\n return [value, updated_at];\n };\n\n const setValueTuple = (product_id: string, field: IQuoteKey, value: string, updated_at: number) => {\n let offset = getFieldOffset(product_id, field);\n if (offset === undefined) {\n const baseIndex = mapProductIdToIndex.size * FIELD_COUNT * 2;\n products.push(product_id);\n mapProductIdToIndex.set(product_id, baseIndex);\n offset = baseIndex + mapFieldNameToOffset[field];\n }\n data[offset] = value;\n data[offset + 1] = updated_at;\n };\n\n const update = (action: IQuoteUpdateAction) => {\n for (const product_id in action) {\n const fields = action[product_id];\n for (const field_name in fields) {\n const field = field_name as IQuoteKey;\n const [value, updated_at] = fields[field]!;\n const oldTuple = getValueTuple(product_id, field);\n if (oldTuple === undefined || updated_at >= oldTuple[1]) {\n setValueTuple(product_id, field, value, updated_at);\n }\n }\n }\n };\n\n const dumpAsObject = (): IQuoteUpdateAction => {\n const result: IQuoteUpdateAction = {};\n for (const product_id of products) {\n result[product_id] = {};\n }\n for (let i = 0; i < data.length; i += 2) {\n const value = data[i] as string;\n if (value === undefined) continue;\n const updated_at = data[i + 1] as number;\n\n const productIndex = Math.floor(i / (FIELD_COUNT * 2));\n const product_id = products[productIndex];\n\n const fieldIndex = (i % (FIELD_COUNT * 2)) / 2;\n const field_name = FIELDS[fieldIndex];\n\n result[product_id][field_name] = [value, updated_at];\n }\n return result;\n };\n\n /**\n * 过滤状态,返回指定 product_id 列表和字段列表中,且更新时间不早于指定时间的字段数据\n * @param product_ids - 需要过滤的 product_id 列表\n * @param fields - 需要过滤的字段列表\n * @param updated_at - 需要过滤的更新时间阈值 (仅返回更新时间不早于该值的字段)\n * @returns 过滤后的数据\n */\n const filter = (product_ids: string[], fields: IQuoteKey[], updated_at: number): IQuoteUpdateAction => {\n const result: IQuoteUpdateAction = {};\n for (const product_id of product_ids) {\n result[product_id] = {};\n for (const field of fields) {\n const tuple = getValueTuple(product_id, field);\n if (tuple && tuple[1] >= updated_at) {\n result[product_id]![field] = tuple;\n }\n }\n }\n return result;\n };\n\n return { update, dumpAsObject, getValueTuple, filter };\n};\n"]}
@@ -0,0 +1,8 @@
1
+ export interface IPrefixMatcher<T> {
2
+ match: (value: string) => T[];
3
+ }
4
+ export declare const createSortedPrefixMatcher: <T>(entries: {
5
+ prefix: string;
6
+ value: T;
7
+ }[]) => IPrefixMatcher<T>;
8
+ //# sourceMappingURL=prefix-matcher.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prefix-matcher.d.ts","sourceRoot":"","sources":["../../src/quote/prefix-matcher.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc,CAAC,CAAC;IAC/B,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,CAAC,EAAE,CAAC;CAC/B;AAED,eAAO,MAAM,yBAAyB;YACX,MAAM;;yBAMhC,CAAC"}
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSortedPrefixMatcher = void 0;
4
+ const createSortedPrefixMatcher = (entries) => {
5
+ const sorted = [...entries].sort((a, b) => b.prefix.length - a.prefix.length);
6
+ return {
7
+ match: (value) => sorted.filter((x) => value.startsWith(x.prefix)).map((x) => x.value),
8
+ };
9
+ };
10
+ exports.createSortedPrefixMatcher = createSortedPrefixMatcher;
11
+ //# sourceMappingURL=prefix-matcher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prefix-matcher.js","sourceRoot":"","sources":["../../src/quote/prefix-matcher.ts"],"names":[],"mappings":";;;AAIO,MAAM,yBAAyB,GAAG,CACvC,OAA4C,EACzB,EAAE;IACrB,MAAM,MAAM,GAAG,CAAC,GAAG,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC9E,OAAO;QACL,KAAK,EAAE,CAAC,KAAa,EAAO,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;KACpG,CAAC;AACJ,CAAC,CAAC;AAPW,QAAA,yBAAyB,6BAOpC","sourcesContent":["export interface IPrefixMatcher<T> {\n match: (value: string) => T[];\n}\n\nexport const createSortedPrefixMatcher = <T>(\n entries: Array<{ prefix: string; value: T }>,\n): IPrefixMatcher<T> => {\n const sorted = [...entries].sort((a, b) => b.prefix.length - a.prefix.length);\n return {\n match: (value: string): T[] => sorted.filter((x) => value.startsWith(x.prefix)).map((x) => x.value),\n };\n};\n"]}
@@ -0,0 +1,2 @@
1
+ export declare const fnv1a64HexFromStrings: (parts: string[]) => string;
2
+ //# sourceMappingURL=request-key.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-key.d.ts","sourceRoot":"","sources":["../../src/quote/request-key.ts"],"names":[],"mappings":"AAsBA,eAAO,MAAM,qBAAqB,UAAW,MAAM,EAAE,KAAG,MAA0C,CAAC"}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fnv1a64HexFromStrings = void 0;
4
+ const util_1 = require("util");
5
+ const utils_1 = require("@yuants/utils");
6
+ const SEP_BYTE = new Uint8Array([0xff]);
7
+ const encodeStrings = (parts) => {
8
+ const buffers = [];
9
+ for (const part of parts) {
10
+ buffers.push(new util_1.TextEncoder().encode(part));
11
+ buffers.push(SEP_BYTE);
12
+ }
13
+ const totalLength = buffers.reduce((sum, b) => sum + b.length, 0);
14
+ const result = new Uint8Array(totalLength);
15
+ let offset = 0;
16
+ for (const b of buffers) {
17
+ result.set(b, offset);
18
+ offset += b.length;
19
+ }
20
+ return result;
21
+ };
22
+ const fnv1a64HexFromStrings = (parts) => (0, utils_1.fnv1a64Hex)(encodeStrings(parts));
23
+ exports.fnv1a64HexFromStrings = fnv1a64HexFromStrings;
24
+ //# sourceMappingURL=request-key.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-key.js","sourceRoot":"","sources":["../../src/quote/request-key.ts"],"names":[],"mappings":";;;AAAA,+BAAmC;AAEnC,yCAA2C;AAE3C,MAAM,QAAQ,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;AAExC,MAAM,aAAa,GAAG,CAAC,KAAe,EAAc,EAAE;IACpD,MAAM,OAAO,GAAiB,EAAE,CAAC;IACjC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;QACxB,OAAO,CAAC,IAAI,CAAC,IAAI,kBAAW,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;QAC7C,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;KACxB;IACD,MAAM,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAClE,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,WAAW,CAAC,CAAC;IAC3C,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,KAAK,MAAM,CAAC,IAAI,OAAO,EAAE;QACvB,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;QACtB,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC;KACpB;IACD,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAEK,MAAM,qBAAqB,GAAG,CAAC,KAAe,EAAU,EAAE,CAAC,IAAA,kBAAU,EAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC;AAAtF,QAAA,qBAAqB,yBAAiE","sourcesContent":["import { TextEncoder } from 'util';\n\nimport { fnv1a64Hex } from '@yuants/utils';\n\nconst SEP_BYTE = new Uint8Array([0xff]);\n\nconst encodeStrings = (parts: string[]): Uint8Array => {\n const buffers: Uint8Array[] = [];\n for (const part of parts) {\n buffers.push(new TextEncoder().encode(part));\n buffers.push(SEP_BYTE);\n }\n const totalLength = buffers.reduce((sum, b) => sum + b.length, 0);\n const result = new Uint8Array(totalLength);\n let offset = 0;\n for (const b of buffers) {\n result.set(b, offset);\n offset += b.length;\n }\n return result;\n};\n\nexport const fnv1a64HexFromStrings = (parts: string[]): string => fnv1a64Hex(encodeStrings(parts));\n"]}
@@ -1,16 +1,50 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const protocol_1 = require("@yuants/protocol");
4
+ const utils_1 = require("@yuants/utils");
4
5
  const state_1 = require("./state");
6
+ const upstream_routing_1 = require("./upstream-routing");
5
7
  const terminal = protocol_1.Terminal.fromNodeEnv();
6
8
  const quoteState = (0, state_1.createQuoteState)();
9
+ const assertFreshnessSatisfied = (data, params) => {
10
+ var _a;
11
+ console.info('[VEX][Quote] Asserting freshness satisfied for requested quotes.', JSON.stringify(params), JSON.stringify(data));
12
+ const { product_ids, fields, updated_at } = params;
13
+ const stillMissed = [];
14
+ for (const product_id of product_ids) {
15
+ for (const field of fields) {
16
+ if (!((_a = data[product_id]) === null || _a === void 0 ? void 0 : _a[field])) {
17
+ stillMissed.push({ product_id, field });
18
+ }
19
+ }
20
+ }
21
+ if (stillMissed.length > 0) {
22
+ throw (0, utils_1.newError)('VEX_QUOTE_FRESHNESS_NOT_SATISFIED', {
23
+ updated_at,
24
+ missed: stillMissed.slice(0, 200),
25
+ missed_total: stillMissed.length,
26
+ });
27
+ }
28
+ };
7
29
  terminal.server.provideService('VEX/UpdateQuotes', {}, async (msg) => {
8
30
  quoteState.update(msg.req);
9
31
  return { res: { code: 0, message: 'OK' } };
10
32
  });
11
- terminal.server.provideService('VEX/DumpQuoteState', {}, async (msg) => {
33
+ terminal.server.provideService('VEX/DumpQuoteState', {}, async () => {
12
34
  return { res: { code: 0, message: 'OK', data: quoteState.dumpAsObject() } };
13
35
  });
36
+ const computeCacheMissed = (quoteState, product_ids, fields, updated_at) => {
37
+ const cacheMissed = [];
38
+ for (const product_id of product_ids) {
39
+ for (const field of fields) {
40
+ const tuple = quoteState.getValueTuple(product_id, field);
41
+ if (tuple === undefined || tuple[1] < updated_at) {
42
+ cacheMissed.push({ product_id, field });
43
+ }
44
+ }
45
+ }
46
+ return cacheMissed;
47
+ };
14
48
  terminal.server.provideService('VEX/QueryQuotes', {
15
49
  type: 'object',
16
50
  required: ['product_ids', 'fields', 'updated_at'],
@@ -27,21 +61,10 @@ terminal.server.provideService('VEX/QueryQuotes', {
27
61
  },
28
62
  }, async (msg) => {
29
63
  const { product_ids, fields, updated_at } = msg.req;
30
- // 分析缓存缺失的字段
31
- const cacheMissed = [];
32
- for (const product_id of product_ids) {
33
- for (const field of fields) {
34
- const tuple = quoteState.getValueTuple(product_id, field);
35
- if (tuple === undefined || tuple[1] < updated_at) {
36
- cacheMissed.push({ product_id, field });
37
- }
38
- }
39
- }
40
- // TODO: 集中规划需要发送的查询请求,并更新到状态中
41
- // 注意需要限制在途请求数量和复用在途请求的结果,以免过载和浪费资源
42
- // await Promise.all;
43
- // 从状态中获取数据返回
64
+ const cacheMissed = computeCacheMissed(quoteState, product_ids, fields, updated_at);
65
+ await (0, upstream_routing_1.fillQuoteStateFromUpstream)({ terminal, quoteState, cacheMissed, updated_at });
44
66
  const data = quoteState.filter(product_ids, fields, updated_at);
67
+ assertFreshnessSatisfied(data, { product_ids, fields, updated_at });
45
68
  return { res: { code: 0, message: 'OK', data } };
46
69
  });
47
70
  //# sourceMappingURL=service.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"service.js","sourceRoot":"","sources":["../../src/quote/service.ts"],"names":[],"mappings":";;AAAA,+CAA4C;AAC5C,mCAA2C;AAG3C,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,UAAU,GAAG,IAAA,wBAAgB,GAAE,CAAC;AAEtC,QAAQ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U,CAAC,aAAa,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;YAC1D,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,CAAC,CAAC,GAAG,UAAU,EAAE;gBAChD,WAAW,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;aACzC;SACF;KACF;IACD,8BAA8B;IAC9B,mCAAmC;IACnC,qBAAqB;IAErB,aAAa;IACb,MAAM,IAAI,GAAG,UAAU,CAAC,MAAM,CAAC,WAAW,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;IAEhE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC;AACnD,CAAC,CACF,CAAC","sourcesContent":["import { Terminal } from '@yuants/protocol';\nimport { createQuoteState } from './state';\nimport { IQuoteKey, IQuoteUpdateAction } from './types';\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst quoteState = createQuoteState();\n\nterminal.server.provideService<IQuoteUpdateAction>('VEX/UpdateQuotes', {}, async (msg) => {\n quoteState.update(msg.req);\n return { res: { code: 0, message: 'OK' } };\n});\n\nterminal.server.provideService<{}, IQuoteUpdateAction>('VEX/DumpQuoteState', {}, async (msg) => {\n return { res: { code: 0, message: 'OK', data: quoteState.dumpAsObject() } };\n});\n\nterminal.server.provideService<\n { product_ids: string[]; fields: IQuoteKey[]; updated_at: number },\n IQuoteUpdateAction\n>(\n 'VEX/QueryQuotes',\n {\n type: 'object',\n required: ['product_ids', 'fields', 'updated_at'],\n properties: {\n product_ids: {\n type: 'array',\n items: { type: 'string' },\n },\n fields: {\n type: 'array',\n items: { type: 'string' },\n },\n updated_at: { type: 'number' },\n },\n },\n async (msg) => {\n const { product_ids, fields, updated_at } = msg.req;\n // 分析缓存缺失的字段\n const cacheMissed: Array<{ product_id: string; field: IQuoteKey }> = [];\n for (const product_id of product_ids) {\n for (const field of fields) {\n const tuple = quoteState.getValueTuple(product_id, field);\n if (tuple === undefined || tuple[1] < updated_at) {\n cacheMissed.push({ product_id, field });\n }\n }\n }\n // TODO: 集中规划需要发送的查询请求,并更新到状态中\n // 注意需要限制在途请求数量和复用在途请求的结果,以免过载和浪费资源\n // await Promise.all;\n\n // 从状态中获取数据返回\n const data = quoteState.filter(product_ids, fields, updated_at);\n\n return { res: { code: 0, message: 'OK', data } };\n },\n);\n"]}
1
+ {"version":3,"file":"service.js","sourceRoot":"","sources":["../../src/quote/service.ts"],"names":[],"mappings":";;AAAA,+CAA4C;AAC5C,yCAAyC;AACzC,mCAA2C;AAE3C,yDAA4E;AAE5E,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,UAAU,GAAG,IAAA,wBAAgB,GAAE,CAAC;AAEtC,MAAM,wBAAwB,GAAG,CAC/B,IAAwB,EACxB,MAA0E,EAC1E,EAAE;;IACF,OAAO,CAAC,IAAI,CACV,kEAAkE,EAClE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,EACtB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CACrB,CAAC;IACF,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,MAAM,CAAC;IACnD,MAAM,WAAW,GAAoD,EAAE,CAAC;IACxE,KAAK,MAAM,UAAU,IAAI,WAAW,EAAE;QACpC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE;YAC1B,IAAI,CAAC,CAAA,MAAA,IAAI,CAAC,UAAU,CAAC,0CAAG,KAAK,CAAC,CAAA,EAAE;gBAC9B,WAAW,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;aACzC;SACF;KACF;IACD,IAAI,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE;QAC1B,MAAM,IAAA,gBAAQ,EAAC,mCAAmC,EAAE;YAClD,UAAU;YACV,MAAM,EAAE,WAAW,CAAC,KAAK,CAAC,CAAC,EAA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CAAC,aAAa,EAAE,QAAQ,EAAE,YAAY,CAAC;IACjD,UAAU,EAAE;QACV,WAAW,EAAE;YACX,IAAI,EAAE,OAAO;YACb,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;SAC1B;QACD,MAAM,EAAE;YACN,IAAI,EAAE,OAAO;YACb,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;SAC1B;QACD,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;KAC/B;CACF,EACD,KAAK,EAAE,GAAG,EAAE,EAAE;IACZ,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,GAAG,CAAC,GAAG,CAAC;IAEpD,MAAM,WAAW,GAAG,kBAAkB,CAAC,UAAU,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;IACpF,MAAM,IAAA,6CAA0B,EAAC,EAAE,QAAQ,EAAE,UAAU,EAAE,WAAW,EAAE,UAAU,EAAE,CAAC,CAAC;IAEpF,MAAM,IAAI,GAAG,UAAU,CAAC,MAAM,CAAC,WAAW,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;IAChE,wBAAwB,CAAC,IAAI,EAAE,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,CAAC;IACpE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC;AACnD,CAAC,CACF,CAAC","sourcesContent":["import { Terminal } from '@yuants/protocol';\nimport { newError } from '@yuants/utils';\nimport { createQuoteState } from './state';\nimport { IQuoteKey, IQuoteState, IQuoteUpdateAction } from './types';\nimport { fillQuoteStateFromUpstream, IQuoteMiss } from './upstream-routing';\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst quoteState = createQuoteState();\n\nconst assertFreshnessSatisfied = (\n data: IQuoteUpdateAction,\n params: { product_ids: string[]; fields: IQuoteKey[]; updated_at: number },\n) => {\n console.info(\n '[VEX][Quote] Asserting freshness satisfied for requested quotes.',\n JSON.stringify(params),\n JSON.stringify(data),\n );\n const { product_ids, fields, updated_at } = params;\n const stillMissed: Array<{ product_id: string; field: IQuoteKey }> = [];\n for (const product_id of product_ids) {\n for (const field of fields) {\n if (!data[product_id]?.[field]) {\n stillMissed.push({ product_id, field });\n }\n }\n }\n if (stillMissed.length > 0) {\n throw newError('VEX_QUOTE_FRESHNESS_NOT_SATISFIED', {\n updated_at,\n missed: stillMissed.slice(0, 200),\n missed_total: stillMissed.length,\n });\n }\n};\n\nterminal.server.provideService<IQuoteUpdateAction>('VEX/UpdateQuotes', {}, async (msg) => {\n quoteState.update(msg.req);\n return { res: { code: 0, message: 'OK' } };\n});\n\nterminal.server.provideService<{}, IQuoteUpdateAction>('VEX/DumpQuoteState', {}, async () => {\n return { res: { code: 0, message: 'OK', data: quoteState.dumpAsObject() } };\n});\n\nconst computeCacheMissed = (\n quoteState: IQuoteState,\n product_ids: string[],\n fields: IQuoteKey[],\n updated_at: number,\n): IQuoteMiss[] => {\n const cacheMissed: IQuoteMiss[] = [];\n for (const product_id of product_ids) {\n for (const field of fields) {\n const tuple = quoteState.getValueTuple(product_id, field);\n if (tuple === undefined || tuple[1] < updated_at) {\n cacheMissed.push({ product_id, field });\n }\n }\n }\n return cacheMissed;\n};\n\nterminal.server.provideService<\n { product_ids: string[]; fields: IQuoteKey[]; updated_at: number },\n IQuoteUpdateAction\n>(\n 'VEX/QueryQuotes',\n {\n type: 'object',\n required: ['product_ids', 'fields', 'updated_at'],\n properties: {\n product_ids: {\n type: 'array',\n items: { type: 'string' },\n },\n fields: {\n type: 'array',\n items: { type: 'string' },\n },\n updated_at: { type: 'number' },\n },\n },\n async (msg) => {\n const { product_ids, fields, updated_at } = msg.req;\n\n const cacheMissed = computeCacheMissed(quoteState, product_ids, fields, updated_at);\n await fillQuoteStateFromUpstream({ terminal, quoteState, cacheMissed, updated_at });\n\n const data = quoteState.filter(product_ids, fields, updated_at);\n assertFreshnessSatisfied(data, { product_ids, fields, updated_at });\n return { res: { code: 0, message: 'OK', data } };\n },\n);\n"]}
@@ -0,0 +1,15 @@
1
+ import { Terminal } from '@yuants/protocol';
2
+ import { IQuoteKey, IQuoteState } from './types';
3
+ export interface IQuoteMiss {
4
+ product_id: string;
5
+ field: IQuoteKey;
6
+ }
7
+ declare const terminal: Terminal;
8
+ export declare const fillQuoteStateFromUpstream: (params: {
9
+ terminal: Terminal;
10
+ quoteState: IQuoteState;
11
+ cacheMissed: IQuoteMiss[];
12
+ updated_at: number;
13
+ }) => Promise<void>;
14
+ export {};
15
+ //# sourceMappingURL=upstream-routing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upstream-routing.d.ts","sourceRoot":"","sources":["../../src/quote/upstream-routing.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAK5C,OAAO,EAAE,SAAS,EAAE,WAAW,EAAsB,MAAM,SAAS,CAAC;AAErE,MAAM,WAAW,UAAU;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,SAAS,CAAC;CAClB;AA2BD,QAAA,MAAM,QAAQ,UAAyB,CAAC;AAsUxC,eAAO,MAAM,0BAA0B,WAAkB;IACvD,UAAU,QAAQ,CAAC;IACnB,UAAU,EAAE,WAAW,CAAC;IACxB,WAAW,EAAE,UAAU,EAAE,CAAC;IAC1B,UAAU,EAAE,MAAM,CAAC;CACpB,KAAG,QAAQ,IAAI,CA6Ef,CAAC"}