Skip to content

Commit

Permalink
Merge pull request #57 from GuoXiCheng/dev-h
Browse files Browse the repository at this point in the history
add singleton-factory
  • Loading branch information
GuoXiCheng authored Dec 28, 2023
2 parents 1ac12c2 + 23c828f commit f487e27
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 32 deletions.
13 changes: 3 additions & 10 deletions src/__tests__/helper/book-storage.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { GithubStorage } from "../../storage-lib";
import { SingletonFactory } from "../../utils";
import { BookModel } from "./book-model";
import { GITHUB_NUMBER, githubRequest } from "./helper";

class BookStorage extends GithubStorage<BookModel> {
private static instance: BookStorage;
private constructor() {
constructor() {
super(githubRequest, GITHUB_NUMBER);
}

public static getInstance(): BookStorage {
if (!BookStorage.instance) {
BookStorage.instance = new BookStorage();
}
return BookStorage.instance;
}
}

/**
* test github api with a book storage instance.
*/
export const Book = BookStorage.getInstance();
export const Book = SingletonFactory.createInstance(BookStorage);
13 changes: 3 additions & 10 deletions src/__tests__/helper/chat-storage.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { GitlabStorage } from "../../storage-lib";
import { ChatModel } from "./chat-model";
import { GITLAB_NUMBER, gitlabRequest } from "./helper";
import { SingletonFactory } from "../../utils";

class ChatStorage extends GitlabStorage<ChatModel> {
private static instance: ChatStorage;
private constructor() {
constructor() {
super(gitlabRequest, GITLAB_NUMBER);
}

public static getInstance(): ChatStorage {
if (!ChatStorage.instance) {
ChatStorage.instance = new ChatStorage();
}
return ChatStorage.instance;
}
}

/**
* test gitlab api with a chat storage instance.
*/
export const Chat = ChatStorage.getInstance();
export const Chat = SingletonFactory.createInstance(ChatStorage);
14 changes: 3 additions & 11 deletions src/__tests__/helper/user-storage.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import { GiteeStorage } from '../..';
import { SingletonFactory } from '../../utils';
import { GITEE_NUMBER, giteeRequest } from './helper';
import { UserModel } from './user-model';

export class UserStorage extends GiteeStorage<UserModel> {
private static instance: UserStorage;
private constructor() {
constructor() {
super(giteeRequest, GITEE_NUMBER);
}

public static getInstance(): UserStorage {
if (!UserStorage.instance) {
UserStorage.instance = new UserStorage();
}
return UserStorage.instance;
}

}

/**
* test gitee api with a user storage instance.
*/
export const User = UserStorage.getInstance();
export const User = SingletonFactory.createInstance(UserStorage);
2 changes: 1 addition & 1 deletion src/__tests__/user-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('Test User Storage', () => {
});

test('Test find user with params since', async () => {
const time = dayjs().subtract(1, 'second').format();
const time = dayjs().subtract(3, 'second').format();
const result = await User.find({ since: time });
expect(result.length).toBeGreaterThan(0);
result.forEach(item => {
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { SingletonFactory } from './singleton-factory';
13 changes: 13 additions & 0 deletions src/utils/singleton-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class SingletonFactory {
private static instances = new Map<any, any>();

static createInstance<T>(classRef: { new (): T }): T {
// 检查实例是否已经存在
if (!SingletonFactory.instances.has(classRef)) {
// 如果不存在,创建新实例并存储
SingletonFactory.instances.set(classRef, new classRef());
}
// 返回已存在的或新创建的实例
return SingletonFactory.instances.get(classRef);
}
}

0 comments on commit f487e27

Please sign in to comment.