fix(core): initial

This commit is contained in:
Philipp Kunz 2019-02-21 21:48:39 +01:00
commit 1663e31a2e
16 changed files with 2032 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
.nogit/
node_modules/
coverage/
public/
pages/
.yarn/

150
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,150 @@
# gitzone standard
image: hosttoday/ht-docker-node:npmci
cache:
paths:
- .npmci_cache/
key: "$CI_BUILD_STAGE"
stages:
- security
- test
- release
- metadata
# ====================
# security stage
# ====================
mirror:
stage: security
script:
- npmci git mirror
tags:
- docker
- notpriv
snyk:
stage: security
script:
- npmci npm prepare
- npmci command npm install -g snyk
- npmci command npm install --ignore-scripts
- npmci command snyk test
tags:
- docker
- notpriv
sast:
stage: security
image: registry.gitlab.com/hosttoday/ht-docker-dbase:npmci
variables:
DOCKER_DRIVER: overlay2
allow_failure: true
services:
- docker:stable-dind
script:
- npmci npm prepare
- npmci npm install
- npmci command npm run build
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
- docker run
--env SAST_CONFIDENCE_LEVEL="${SAST_CONFIDENCE_LEVEL:-3}"
--volume "$PWD:/code"
--volume /var/run/docker.sock:/var/run/docker.sock
"registry.gitlab.com/gitlab-org/security-products/sast:$SP_VERSION" /app/bin/run /code
artifacts:
reports:
sast: gl-sast-report.json
tags:
- docker
- priv
# ====================
# test stage
# ====================
testLTS:
stage: test
script:
- npmci npm prepare
- npmci node install lts
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
testSTABLE:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
release:
stage: release
script:
- npmci node install stable
- npmci npm publish
only:
- tags
tags:
- docker
- notpriv
# ====================
# metadata stage
# ====================
codequality:
stage: metadata
image: docker:stable
allow_failure: true
services:
- docker:stable-dind
script:
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
- docker run
--env SOURCE_CODE="$PWD"
--volume "$PWD":/code
--volume /var/run/docker.sock:/var/run/docker.sock
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
artifacts:
paths: [codeclimate.json]
tags:
- docker
- priv
trigger:
stage: metadata
script:
- npmci trigger
only:
- tags
tags:
- docker
- notpriv
pages:
image: hosttoday/ht-docker-node:npmci
stage: metadata
script:
- npmci command npm install -g typedoc typescript
- npmci npm prepare
- npmci npm install
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
tags:
- docker
- notpriv
only:
- tags
artifacts:
expire_in: 1 week
paths:
- public
allow_failure: true

19
license Normal file
View File

@ -0,0 +1,19 @@
Copyright (c) 2019 Lossless GmbH (hello@lossless.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

16
npmextra.json Normal file
View File

@ -0,0 +1,16 @@
{
"npmci": {
"npmGlobalTools": [],
"npmAccessLevel": "public"
},
"gitzone": {
"module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",
"gitrepo": "smartstate",
"shortDescription": "a package that handles state in a good way",
"npmPackagename": "@pushrocks/smartstate",
"license": "MIT"
}
}
}

1575
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

27
package.json Normal file
View File

@ -0,0 +1,27 @@
{
"name": "@pushrocks/smartstate",
"version": "1.0.7",
"private": false,
"description": "a package that handles state in a good way",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"author": "Lossless GmbH",
"license": "MIT",
"scripts": {
"test": "(tstest test/)",
"build": "(tsbuild)",
"format": "(gitzone format)"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.8",
"@gitzone/tstest": "^1.0.15",
"@pushrocks/tapbundle": "^3.0.7",
"@types/node": "^11.9.4",
"tslint": "^5.12.1",
"tslint-config-prettier": "^1.18.0"
},
"dependencies": {
"@pushrocks/lik": "^3.0.4",
"rxjs": "^6.4.0"
}
}

26
readme.md Normal file
View File

@ -0,0 +1,26 @@
# @pushrocks/smartstate
a package that handles state in a good way
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartstate)
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartstate)
* [github.com (source mirror)](https://github.com/pushrocks/smartstate)
* [docs (typedoc)](https://pushrocks.gitlab.io/smartstate/)
## Status for master
[![build status](https://gitlab.com/pushrocks/smartstate/badges/master/build.svg)](https://gitlab.com/pushrocks/smartstate/commits/master)
[![coverage report](https://gitlab.com/pushrocks/smartstate/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartstate/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/smartstate.svg)](https://www.npmjs.com/package/@pushrocks/smartstate)
[![Known Vulnerabilities](https://snyk.io/test/npm/@pushrocks/smartstate/badge.svg)](https://snyk.io/test/npm/@pushrocks/smartstate)
[![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
## Usage
For further information read the linked docs at the top of this readme.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://maintainedby.lossless.com)

41
test/test.ts Normal file
View File

@ -0,0 +1,41 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as smartstate from '../ts/index';
type TMyStateParts = 'testStatePart';
interface TStatePartPayload {
currentFavorites: string[];
deep: {
hi: number;
};
}
let testState: smartstate.Smartstate<TMyStateParts>;
let testStatePart: smartstate.StatePart<TMyStateParts, TStatePartPayload>;
tap.test('should create a new SmartState', async () => {
testState = new smartstate.Smartstate<TMyStateParts>();
expect(testState).to.be.instanceOf(smartstate.Smartstate);
});
tap.test('should create a new StatePart', async () => {
testStatePart = testState.getStatePart<TStatePartPayload>('testStatePart', {
currentFavorites: [],
deep: {
hi: 2
}
});
expect(testStatePart).to.be.instanceOf(smartstate.StatePart);
console.log(testStatePart);
});
tap.test('should select something', async () => {
testStatePart
.select(state => state.deep)
.subscribe(substate => {
console.log(substate);
});
});
tap.test('should dispatch a state action', async () => {});
tap.start();

5
ts/index.ts Normal file
View File

@ -0,0 +1,5 @@
export * from './smartstate.classes.smartstate';
export * from './smartstate.classes.statepart';
export * from './smartstate.classes.statecollection';
export * from './smartstate.classes.stateaction';
export * from './smartstate.classes.stateobservable';

View File

@ -0,0 +1,47 @@
import * as plugins from './smartstate.plugins';
import { StatePart } from './smartstate.classes.statepart';
/**
* Smartstate takes care of providing state
*/
export class Smartstate<StatePartNameType> {
statePartMap: { [key: string]: StatePart<StatePartNameType, any> } = {};
constructor() {}
getStatePart<PayloadType>(
statePartNameArg: StatePartNameType,
initialArg?: PayloadType
): StatePart<StatePartNameType, PayloadType> {
if (this.statePartMap[statePartNameArg as any]) {
if (initialArg) {
throw new Error(
`${statePartNameArg} already exists, yet you try to set an initial state again`
);
}
return this.statePartMap[statePartNameArg as any];
} else {
if (!initialArg) {
throw new Error(
`${statePartNameArg} does not yet exist, yet you don't provide an initial state`
);
}
return this.createStatePart<PayloadType>(statePartNameArg, initialArg);
}
}
private createStatePart<PayloadType>(
statePartName: StatePartNameType,
initialPayloadArg: PayloadType
): StatePart<StatePartNameType, PayloadType> {
const newState = new StatePart<StatePartNameType, PayloadType>(statePartName);
newState.setState(initialPayloadArg);
this.statePartMap[statePartName as any] = newState;
return newState;
}
/**
* dispatches an action on the main level
*/
dispatch() {}
}

View File

@ -0,0 +1,8 @@
import * as plugins from './smartstate.plugins';
/**
* an actionmodifier for the state
*/
export class StateAction<StatePayload> {
constructor(public actionDef: (stateArg: StatePayload) => StatePayload) {}
}

View File

@ -0,0 +1,12 @@
import * as plugins from './smartstate.plugins';
import { StatePart } from './smartstate.classes.statepart';
/**
* A StatePartCollection is a collection of StateParts.
* It can be used for expressing interest in a certain set of StateParts.
*/
export class StatePartCollection<StatePartNameType, T> extends StatePart<StatePartNameType, T> {
constructor(nameArg: StatePartNameType) {
super(nameArg);
}
}

View File

@ -0,0 +1,13 @@
import * as plugins from './smartstate.plugins';
/**
* State observable observes a StatePart and notifies everyone interested
*/
export class StateObservable {
/**
* creates an observable from a StateCollection
*/
public static fromStatePartCollection(filterArg?: () => any) {}
constructor() {}
}

View File

@ -0,0 +1,63 @@
import * as plugins from './smartstate.plugins';
import { Observable, Subject } from 'rxjs';
import { startWith, takeUntil, map } from 'rxjs/operators';
import { StateAction } from './smartstate.classes.stateaction';
export class StatePart<StatePartNameType, PayloadType> {
name: StatePartNameType;
state = new Subject<PayloadType>();
stateStore: PayloadType;
constructor(nameArg: StatePartNameType) {
this.name = nameArg;
}
/**
* gets the state from the state store
*/
getState(): PayloadType {
return this.stateStore;
}
/**
* sets the stateStore to the new state
* @param newStateArg
*/
setState(newStateArg: PayloadType) {
this.stateStore = newStateArg;
this.notifyChange();
}
/**
* notifies of a change on the state
*/
notifyChange() {
this.state.next(this.stateStore);
}
/**
* selects a state or a substate
*/
select<T = PayloadType>(selectorFn?: (state: PayloadType) => T): Observable<T> {
if (!selectorFn) {
selectorFn = (state: PayloadType) => <T>(<any>state);
}
const mapped = this.state.pipe(
startWith(this.getState()),
map(selectorFn)
);
return mapped;
}
/**
* dispatches an action on the statepart level
*/
async dispatch(stateAction: StateAction<PayloadType>) {
const newState = stateAction.actionDef(this.getState());
this.setState(newState);
}
}

7
ts/smartstate.plugins.ts Normal file
View File

@ -0,0 +1,7 @@
import { Observable } from 'rxjs';
const rxjsPart = {
Observable
};
export { rxjsPart };

17
tslint.json Normal file
View File

@ -0,0 +1,17 @@
{
"extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
]
}
}
},
"defaultSeverity": "warning"
}