ayanami
Version:
A better way to react with state
48 lines (47 loc) • 1.47 kB
JavaScript
import { noop } from 'rxjs';
import { combineWithIkari, destroyIkariFrom } from './ikari';
import { moduleNameKey, globalKey } from '../ssr/ssr-module';
import { isSSREnabled } from '../ssr/flag';
const globalScope = typeof self !== 'undefined'
? self
: typeof window !== 'undefined'
? window
: typeof global !== 'undefined'
? global
: {};
export class Ayanami {
constructor() {
// @internal
this.ssrLoadKey = Symbol('SSR_LOADED');
if (!isSSREnabled()) {
const name = Object.getPrototypeOf(this)[moduleNameKey];
if (!name) {
return;
}
// @ts-ignore
const globalCache = globalScope[globalKey];
if (globalCache) {
const moduleCache = globalCache[name];
if (moduleCache) {
Reflect.defineMetadata(this.ssrLoadKey, true, this);
Object.defineProperty(this, 'defaultState', {
get: () => moduleCache[this.scopeName],
set: noop,
});
}
}
}
}
destroy() {
destroyIkariFrom(this);
}
getState$() {
return combineWithIkari(this).state.state$;
}
getState() {
return combineWithIkari(this).state.getState();
}
getActions() {
return combineWithIkari(this).effectActionFactories;
}
}