[英]Singleton class in javascript with conditional parameter
我有兩個班級:
使用Service.ts
import { useMemo } from 'react';
/**
* Hook will take the singletone service instance later keeping it memoized
* @param service
*/
export function useService<T> ( service: { new (): T; getInstance (): T } ): T {
return useMemo<T>(() => service.getInstance(), []);
}
/**
* Hook will take instance of the given class memoized
* @param Class
* @param args
*/
export function useClass<S, A extends []> ( Class: { new ( ...args: A ): S }, ...args: A ): S {
return useMemo<S>(() => new Class(...args), []);
}
購物車服務.ts
var CART_ITEMS_KEY = 'SOME_KEY';
export class CartService {
private static __SELF__: CartService;
private __items: CartItem[] = [];
private auth: AuthService;
private api: APIService;
private endpoint: AxiosInstance;
constructor (cartItemsKey) {
CART_ITEMS_KEY = cartItemsKey;
this.auth = AuthService.getInstance();
this.api = APIService.getInstance();
this.endpoint = this.api.createEndpoint('cart');
this.init();
}
/**
* Get singletone service instance
*/
public static getInstance (): CartService {
if ( !CartService.__SELF__ ) {
CartService.__SELF__ = new CartService();
}
return CartService.__SELF__;
}
}
我想初始化一個 CartService 對象並像這樣在 userService 中傳遞它。
使用服務(購物車服務(“SOME_NEW_KEY”))
我嘗試了很多方法,但都出現錯誤。
userService(CartService("SOME_NEW_KEY"))
打字稿中的語法無效,您可能會收到錯誤,例如Value of type 'typeof CartService' is not callable.
在實例化CartService 時,我們需要將cartItemsKey傳遞給構造函數。
/**
* Get singleton service instance
*/
public static getInstance(cartItemsKey): CartService {
if (!CartService.__SELF__) {
CartService.__SELF__ = new CartService(cartItemsKey);
}
return CartService.__SELF__;
}
像下面這樣調用
userService(CartService.getInstance("SOME_NEW_KEY"))
嘗試這個:
使用Service.ts
import { useMemo } from 'react';
/**
* Hook will take the singleton service instance later keeping it memoized
* @param service
*/
export function useService<T>(
service: {
new (): T;
getInstance(...args: String[]): T;
},
args: String[]
): T {
return useMemo<T>(() => service.getInstance(...args), []);
}
/**
* Hook will take instance of the given class memoized
* @param Class
* @param args
*/
export function useClass<S, A extends []>(
Class: { new (...args: A): S },
...args: A
): S {
return useMemo<S>(() => new Class(...args), []);
}
購物車服務.ts
export class CartService {
private static __SELF__: CartService;
private cartItemsKey: String;
private cartId: String;
constructor(cartItemsKey: String = 'default', cartId: String = 'default') {
this.cartItemsKey = cartItemsKey;
this.cartId = cartId;
}
log() {
console.log('cartId: ' + this.cartId);
console.log('cartItemsKey: ' + this.cartItemsKey);
}
/**
* Get singletone service instance
*/
public static getInstance(...args: String[]): CartService {
if (!CartService.__SELF__) {
CartService.__SELF__ = new CartService(...args);
}
return CartService.__SELF__;
}
}
然后像這樣使用它:
let cartService = useService(CartService, [
'SOME_KEY_FROM_TEST',
'SOME_ID_FROM_TEST'
]);
cartService.log()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.