reactjs - Singleton-klass i JavaScript med villkorlig parameter
Jag har två klasser:
useService.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), []);
}
CartService.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__;
}
}
Jag vill initialisera ett CartService-objekt och skicka det till userService på det här sättet.
useService(CartService("SOME_NEW_KEY"))
Jag har försökt många tillvägagångssätt men får fel.
Svar #1
userService(CartService("SOME_NEW_KEY")) Det är ogiltig syntax i typescript, du kan få ett fel som Value of type 'typeof CartService' is not callable.
När vi skapar en instans av CartService behöver vi skicka cartItemsKey till konstruktorn.
/**
* Get singleton service instance
*/
public static getInstance(cartItemsKey): CartService {
if (!CartService.__SELF__) {
CartService.__SELF__ = new CartService(cartItemsKey);
}
return CartService.__SELF__;
}
Ring som nedan
userService(CartService.getInstance("SOME_NEW_KEY"))
Svar #2
Prova detta:
useService.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), []);
}
CartService.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__;
}
}
Och sedan använd det så här:
let cartService = useService(CartService, [
'SOME_KEY_FROM_TEST',
'SOME_ID_FROM_TEST'
]);
cartService.log()
