web-storage is added

This commit is contained in:
병준 박 2022-08-05 08:58:41 +00:00
parent d80ff7368a
commit a2faa116c7
10 changed files with 204 additions and 7 deletions

View File

@ -17,6 +17,7 @@ import { AppComponent } from 'app/app.component';
import { appRoutes } from 'app/app.routing';
import { NatsModule } from 'app/core/nats/nats.module';
import { WebStorageModule } from 'app/core/web-storage/web-storage.module';
import { environment } from 'environments/environment';
@ -50,6 +51,7 @@ const routerConfig: ExtraOptions = {
MatIconModule,
NatsModule.forRoot(environment.nats),
WebStorageModule.forRoot({}),
MemberModule.forRoot(),
],

View File

@ -0,0 +1 @@
export interface ModuleConfig {}

View File

@ -0,0 +1,5 @@
import { InjectionToken } from '@angular/core';
export const _MODULE_CONFIG = new InjectionToken(
'@bet/beteran web-storage config of module'
);

View File

@ -0,0 +1,9 @@
import { Type } from '@angular/core';
import { WebLocalStorageService } from './web-local-storage.service';
import { WebSessionStorageService } from './web-session-storage.service';
export const SERVICES: Type<any>[] = [
WebLocalStorageService,
WebSessionStorageService,
];

View File

@ -0,0 +1,25 @@
import { Inject, Injectable } from '@angular/core';
import { ModuleConfig } from '../config/module-config';
import { _MODULE_CONFIG } from '../config/token';
import { WebStorageService } from './web-storage.service';
@Injectable({
providedIn: 'root',
})
export class WebLocalStorageService extends WebStorageService {
/**
* Constructor
*/
constructor() {
super(localStorage);
}
// -----------------------------------------------------------------------------------------------------
// @ Accessors
// -----------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------
// @ Public methods
// -----------------------------------------------------------------------------------------------------
}

View File

@ -0,0 +1,25 @@
import { Inject, Injectable } from '@angular/core';
import { ModuleConfig } from '../config/module-config';
import { _MODULE_CONFIG } from '../config/token';
import { WebStorageService } from './web-storage.service';
@Injectable({
providedIn: 'root',
})
export class WebSessionStorageService extends WebStorageService {
/**
* Constructor
*/
constructor() {
super(sessionStorage);
}
// -----------------------------------------------------------------------------------------------------
// @ Accessors
// -----------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------
// @ Public methods
// -----------------------------------------------------------------------------------------------------
}

View File

@ -0,0 +1,87 @@
import { BehaviorSubject, Observable } from 'rxjs';
import { ModuleConfig } from '../config/module-config';
import { _MODULE_CONFIG } from '../config/token';
export abstract class WebStorageService {
private __storage: Storage;
private __subjects: Map<string, BehaviorSubject<any>>;
/**
* Constructor
*/
constructor(storage: Storage) {
this.__storage = storage;
this.__subjects = new Map<string, BehaviorSubject<any>>();
}
// -----------------------------------------------------------------------------------------------------
// @ Accessors
// -----------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------
// @ Public methods
// -----------------------------------------------------------------------------------------------------
/**
* watch data of given key
* @param key
* @param defaultValue
*/
watch(key: string, defaultValue: any = null): Observable<any> {
if (!this.__subjects.has(key)) {
this.__subjects.set(key, new BehaviorSubject<any>(defaultValue));
}
let item = this.__storage.getItem(key);
if (item !== null) {
item = JSON.parse(item);
}
(this.__subjects.get(key) as BehaviorSubject<any>).next(item);
return (this.__subjects.get(key) as BehaviorSubject<any>).asObservable();
}
/**
* get data of given key
* @param key
*/
get(key: string): any {
let item = this.__storage.getItem(key);
if (item !== null) {
item = JSON.parse(item);
}
return item;
}
/**
* set value on given key
* @param key
* @param value
*/
set(key: string, value: any) {
this.__storage.setItem(key, JSON.stringify(value));
if (!this.__subjects.has(key)) {
this.__subjects.set(key, new BehaviorSubject<any>(value));
} else {
(this.__subjects.get(key) as BehaviorSubject<any>).next(value);
}
}
/**
* remove given key
* @param key
*/
remove(key: string) {
if (this.__subjects.has(key)) {
(this.__subjects.get(key) as BehaviorSubject<any>).complete();
this.__subjects.delete(key);
}
this.__storage.removeItem(key);
}
/**
* clear all available keys
*/
clear() {
this.__subjects.clear();
this.__storage.clear();
}
}

View File

@ -0,0 +1,20 @@
import { ModuleWithProviders, NgModule } from '@angular/core';
import { ModuleConfig } from './config/module-config';
import { _MODULE_CONFIG } from './config/token';
import { SERVICES } from './services';
@NgModule({})
export class WebStorageRootModule {}
@NgModule({})
export class WebStorageModule {
public static forRoot(
config: ModuleConfig
): ModuleWithProviders<WebStorageRootModule> {
return {
ngModule: WebStorageRootModule,
providers: [{ provide: _MODULE_CONFIG, useValue: config }, ...SERVICES],
};
}
}

View File

@ -198,10 +198,7 @@ export class ListComponent implements OnInit, AfterViewInit, OnDestroy {
__onClickSearch(): void {
this.__isSearchOpened = !this.__isSearchOpened;
console.log(
'kkkk',
this._identityService.checkUsernameForDuplication('sdfsdfsdfsf')
);
console.log('kkkk', this._identityService.captcha());
}
/**

View File

@ -2,11 +2,18 @@ import { Injectable } from '@angular/core';
import { NatsService } from 'app/core/nats/services/nats.service';
import * as nats from 'nats.ws';
import { Error } from 'app/modules/protobuf/protobuf/rpc/error_pb';
import {
CheckUsernameForDuplicationRequest,
CheckUsernameForDuplicationResponse,
CaptchaRequest,
CaptchaResponse,
} from 'app/modules/protobuf/c2se/common/identity_pb';
import { SUBJECT_CHECK_USERNAME_FOR_DUPLICATION } from 'app/modules/protobuf/c2se/backend/identity_pb';
import {
SUBJECT_CHECK_USERNAME_FOR_DUPLICATION,
SUBJECT_CAPTCHA,
} from 'app/modules/protobuf/c2se/backend/identity_pb';
@Injectable({
providedIn: 'root',
@ -39,8 +46,27 @@ export class IdentityService {
.then((result) => {
console.log('success', result, result.getDuplicated());
})
.catch((e) => {
console.log('failed', e);
.catch((e: Error) => {
console.log('failed', e.getCode(), e.getMessage(), e.getData());
});
});
}
captcha(): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
let req = new CaptchaRequest();
this.__natsService
.request<CaptchaResponse.Result>(
SUBJECT_CAPTCHA,
req.serializeBinary(),
CaptchaResponse.deserializeBinary
)
.then((result) => {
console.log('success', result, result.getToken(), result.getImage());
})
.catch((e: Error) => {
console.log('failed', e.getCode(), e.getMessage(), e.getData());
});
});
}