Skip to content

Commit

Permalink
new appolo
Browse files Browse the repository at this point in the history
  • Loading branch information
shmoop207 committed Nov 17, 2020
1 parent 8902c15 commit a779693
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 20 deletions.
9 changes: 6 additions & 3 deletions lib/store.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,16 @@ export class Store<T extends { [index: string]: any }> {
return this._client.stateAt(index);
}

public async set<K extends string & keyof T>(key: K, value: T[K], options?: SetStateOptions): Promise<T> {

public async setState(value: Partial<T> | T, options?: SetStateOptions) {
let dto: Partial<T> = {};

dto[key] = value;

return this.setState(dto, options);
}

public async setState(value: Partial<T> | T, options?: SetStateOptions): Promise<T> {

if (!options) {
options = SetStateDefaults
Expand All @@ -147,6 +155,8 @@ export class Store<T extends { [index: string]: any }> {

this._isLocked = false;

return state

}

public async increment(path: string, inc: number): Promise<void> {
Expand Down Expand Up @@ -209,9 +219,6 @@ export class Store<T extends { [index: string]: any }> {

}

public async publish(name: string, data: any) {
await this._client.publish(name, data)
}

public async quit() {
clearTimeout(this._lockedInterval);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"test": "mocha test --timeout 5000 --exit"
},
"main": "./index.js",
"version": "8.0.1",
"version": "8.0.2",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
4 changes: 4 additions & 0 deletions test/unit.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 18 additions & 11 deletions test/unit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chai = require("chai");
import { IOptions, Store} from "../index"
import {IOptions, Store} from "../index"
import {transaction} from "../lib/decorators";

let should = chai.should();
Expand Down Expand Up @@ -56,6 +56,15 @@ describe("State", () => {

});

it.only("Should set state with set", async () => {


await store.set("counter", 1);

(await store.getState()).counter.should.be.eq(1);

});


it("Should set state multi store", async () => {

Expand Down Expand Up @@ -332,15 +341,15 @@ describe("State", () => {

it("Should merge state", async () => {

class TempStore extends Store<{item: {item3?: number,item2?: number}}> {
class TempStore extends Store<{ item: { item3?: number, item2?: number } }> {

constructor(options: IOptions) {
super();
this.setInitialState({item: {item2: 1}})
this.setOptions({...options,...{name:"TempStore"}})
this.setOptions({...options, ...{name: "TempStore"}})
}

async setCounter(value:number) {
async setCounter(value: number) {
await this.setState({item: {item3: value}});
}
}
Expand All @@ -351,16 +360,15 @@ describe("State", () => {
await store.reset()


await store.setCounter(5);
await store.setCounter(5);

store.setCounter(5);
store.setCounter(5);

let {state} = await store.once("item")

state.item.item3.should.be.eq(5);



});

it("Should setState concurrent", async () => {
Expand All @@ -387,9 +395,9 @@ describe("State", () => {
it("Should lock concurrent", async () => {

let store2 = await new Store<{ counter: number }>().setInitialState({counter: 0}).setOptions(RedisParams).initialize();
let store3 = await new Store<{ counter: number }>().setInitialState({counter: 0}).setOptions( RedisParams).initialize();
let store3 = await new Store<{ counter: number }>().setInitialState({counter: 0}).setOptions(RedisParams).initialize();

function inc(store: Store<{ counter: number }>):Promise<void> {
function inc(store: Store<{ counter: number }>): Promise<void> {
return new Promise(async (resolve, reject) => {

let state = await store.lock();
Expand Down Expand Up @@ -418,7 +426,7 @@ describe("State", () => {

it("Should multi lock concurrent", async () => {

function inc(store: Store<{ counter: number }>):Promise<void> {
function inc(store: Store<{ counter: number }>): Promise<void> {
return new Promise(async (resolve, reject) => {

let state = await store.lock();
Expand Down Expand Up @@ -448,7 +456,6 @@ describe("State", () => {
let store3 = await new Store<{ counter: number }>().setInitialState({counter: 0}).setOptions(RedisParams).initialize();



await Promise.all([
store.increment("counter", 1), store3.increment("counter", 2), store2.increment("counter", 3)]);

Expand Down

0 comments on commit a779693

Please sign in to comment.