Skip to content

Commit

Permalink
fix: optimize fetch data
Browse files Browse the repository at this point in the history
  • Loading branch information
mars committed Feb 6, 2025
1 parent 80f4ba6 commit 85e8531
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 157 deletions.
19 changes: 11 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# Change Log

## 1.0.5 (2025-02-06)

Optimize fetch data

## 1.0.4 (2025-02-06)

Optimize fetch data

## 1.0.3 (2025-01-27)

- Optimize display effect
Optimize display effect

## 1.0.2 (2025-01-26)

- Transfer to LeekHub organization
Transfer to LeekHub organization

## 1.0.0 (2025-01-25)

Initial release of leek-fund-lite:

- Support fund data display
- Support stock data display
- Auto refresh data
- Manual refresh data
Initial release
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "leek-fund-lite",
"displayName": "Leek Fund Lite",
"description": "A VSCode extension for viewing fund and stock data",
"version": "1.0.3",
"version": "1.0.5",
"license": "MIT",
"engines": {
"vscode": "^1.52.0"
Expand Down Expand Up @@ -144,7 +144,8 @@
"dependencies": {
"iconv-lite": "^0.6.3",
"lodash": "^4.17.21",
"node-fetch": "^2.6.7"
"node-fetch": "^2.6.7",
"p-limit": "^3.1.0"
},
"devDependencies": {
"@types/lodash": "^4.17.14",
Expand Down
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const APP_NAME = 'Leek Fund Lite';
export const CONCURRENCY = 10;
export const FUND_CONFIG_KEY = 'leek-fund.funds';
export const STOCK_CONFIG_KEY = 'leek-fund.stocks';
35 changes: 21 additions & 14 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as vscode from 'vscode';
import { APP_NAME } from './constants';
import { FundService } from './service/fundService';
import { StockService } from './service/stockService';
import { LeekTreeItem } from './utils/leekTreeItem';
import { logger } from './utils/logger';

export function activate(context: vscode.ExtensionContext) {
const fundService = new FundService();
Expand All @@ -13,15 +15,20 @@ export function activate(context: vscode.ExtensionContext) {
let refreshTimer: NodeJS.Timeout | null = null;
let isViewVisible = false;

const outputChannel = vscode.window.createOutputChannel(APP_NAME, {
log: true,
});
logger.initOutputChannel(outputChannel);

const refresh = () => {
if (refreshTimer) {
clearTimeout(refreshTimer);
refreshTimer = null;
}

refreshTimer = setTimeout(() => {
fundService.refresh();
stockService.refresh();
fundService.reload();
stockService.reload();
refreshTimer = null;
}, 100);
};
Expand All @@ -34,13 +41,13 @@ export function activate(context: vscode.ExtensionContext) {
if (isViewVisible) {
if (!loopTimer) {
loopTimer = setInterval(refresh, interval);
console.log('[LEEK_FUND_LITE] Started polling');
logger.info('Started polling');
}
} else {
if (loopTimer) {
clearInterval(loopTimer);
loopTimer = null;
console.log('[LEEK_FUND_LITE] Stopped polling');
logger.info('Stopped polling');
}
}
};
Expand Down Expand Up @@ -78,17 +85,17 @@ export function activate(context: vscode.ExtensionContext) {
// Register commands
context.subscriptions.push(
vscode.commands.registerCommand('leek-fund-lite.refreshFund', async () => {
console.log('[LEEK_FUND_LITE] Refreshing fund data...');
await fundService.refresh();
console.log('[LEEK_FUND_LITE] Fund data refreshed');
logger.info('Refreshing fund data...');
await fundService.reload();
logger.info('Fund data refreshed');
}),
vscode.commands.registerCommand('leek-fund-lite.refreshStock', async () => {
console.log('[LEEK_FUND_LITE] Refreshing stock data...');
await stockService.refresh();
console.log('[LEEK_FUND_LITE] Stock data refreshed');
logger.info('Refreshing stock data...');
await stockService.reload();
logger.info('Stock data refreshed');
}),
vscode.commands.registerCommand('leek-fund-lite.addFund', async () => {
console.log('[LEEK_FUND_LITE] Adding fund...');
logger.info('Adding fund...');
const code = await vscode.window.showInputBox({
prompt: 'Please input fund code',
placeHolder: 'e.g. 000001',
Expand All @@ -98,7 +105,7 @@ export function activate(context: vscode.ExtensionContext) {
}
}),
vscode.commands.registerCommand('leek-fund-lite.addStock', async () => {
console.log('[LEEK_FUND_LITE] Adding stock...');
logger.info('Adding stock...');
const code = await vscode.window.showInputBox({
prompt: 'Please input stock code',
placeHolder: 'e.g. sh000001',
Expand Down Expand Up @@ -136,11 +143,11 @@ export function activate(context: vscode.ExtensionContext) {
stockTreeView.dispose();
stockTreeView = null;
}
console.log('[LEEK_FUND_LITE] Extension is now deactivated');
logger.info('Extension is now deactivated');
},
});

console.log('[LEEK_FUND_LITE] Extension is now active!');
logger.info('Extension is now active!');
}

export function deactivate() {
Expand Down
76 changes: 41 additions & 35 deletions src/service/fundService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as vscode from 'vscode';
import type { FundData } from '../utils/fetch';
import { fetchFundData } from '../utils/fetch';
import { LeekTreeItem } from '../utils/leekTreeItem';

const FUND_CONFIG_KEY = 'leek-fund.funds';
import { logger } from '../utils/logger';
import { FUND_CONFIG_KEY } from '../constants';

export class FundService implements vscode.TreeDataProvider<LeekTreeItem> {
private _onDidChangeTreeData: vscode.EventEmitter<
Expand All @@ -14,6 +14,7 @@ export class FundService implements vscode.TreeDataProvider<LeekTreeItem> {
LeekTreeItem | undefined | null | void
> = this._onDidChangeTreeData.event;

private loading = false;
private fundData: FundData[] = [];

getCodes(): string[] {
Expand All @@ -29,12 +30,12 @@ export class FundService implements vscode.TreeDataProvider<LeekTreeItem> {
const codes = this.getCodes();
if (!codes.includes(code)) {
await config.update(FUND_CONFIG_KEY, [...codes, code], true);
this._onDidChangeTreeData.fire();
console.log(`[LEEK_FUND_LITE] Fund ${code} added`);
this.refresh();
logger.info(`Fund ${code} added`);
} else {
console.log(`[LEEK_FUND_LITE] Fund ${code} already exists`);
logger.info(`Fund ${code} already exists`);
}
this.refresh();
this.reload();
}

async deleteCode(code: string): Promise<void> {
Expand All @@ -46,53 +47,58 @@ export class FundService implements vscode.TreeDataProvider<LeekTreeItem> {
codes.filter((c) => c !== code),
true
);
this._onDidChangeTreeData.fire();
console.log(`[LEEK_FUND_LITE] Fund ${code} deleted`);
this.refresh();
logger.info(`Fund ${code} deleted`);
} else {
console.log(`[LEEK_FUND_LITE] Fund ${code} does not exist`);
logger.info(`Fund ${code} does not exist`);
}
this.refresh();
this.reload();
}

async refresh(): Promise<void> {
try {
this.fundData = await fetchFundData(this.getCodes());
this._onDidChangeTreeData.fire();
} catch (error) {
console.error(`[LEEK_FUND_LITE] Failed to refresh fund data:`, error);
vscode.window.showErrorMessage('Failed to refresh fund data');
this._onDidChangeTreeData.fire();
async reload(): Promise<void> {
if (this.loading) {
return;
}

this.loading = true;
this._onDidChangeTreeData.fire();
}

async refresh(): Promise<void> {
this._onDidChangeTreeData.fire();
}

getTreeItem(element: LeekTreeItem): vscode.TreeItem {
return element;
}

getChildren(): Thenable<LeekTreeItem[]> {
async getChildren(): Promise<LeekTreeItem[]> {
const codes = this.getCodes();

if (!codes.length) {
return Promise.resolve([new LeekTreeItem('0', '', '', '0', 'fund')]);
}

if (this.loading) {
this.fundData = await fetchFundData(this.getCodes());
this.loading = false;
}

const fundMap = new Map(this.fundData.map((fund) => [fund.code, fund]));

return Promise.resolve(
codes.map((code) => {
const fund = fundMap.get(code);
if (!fund) {
return new LeekTreeItem(code, code, '-', '0', 'fund');
}

return new LeekTreeItem(
fund.code,
fund.name,
fund.estimatedWorth,
fund.estimatedWorthPercent,
'fund'
);
})
);
return codes.map((code) => {
const fund = fundMap.get(code);
if (!fund) {
return new LeekTreeItem(code, code, '-', '0', 'fund');
}

return new LeekTreeItem(
fund.code,
fund.name,
fund.estimatedWorth,
fund.estimatedWorthPercent,
'fund'
);
});
}
}
Loading

0 comments on commit 85e8531

Please sign in to comment.