@storex/core
Advanced tools
Comparing version 1.0.21 to 1.0.22
import { Dispatcher } from "./dispatcher"; | ||
export declare function dispatch(eventNames?: string[]): (store: Dispatcher<any>, actionName: any, descriptor: any) => any; | ||
export declare function dispatch(eventNames?: string[]): (store: Dispatcher<any>, name: any, descriptor?: any) => any; | ||
export declare function dispatchIf(conditionFunc: () => boolean, eventNames?: string[]): (store: Dispatcher<any>, name: any, descriptor?: any) => any; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function dispatch(eventNames) { | ||
return (store, actionName, descriptor) => { | ||
let key = "value"; | ||
if (!descriptor.value) { | ||
if (descriptor.set) { | ||
key = "set"; | ||
return baseDispatch(eventNames); | ||
} | ||
exports.dispatch = dispatch; | ||
function dispatchIf(conditionFunc, eventNames) { | ||
return baseDispatch(eventNames, conditionFunc); | ||
} | ||
exports.dispatchIf = dispatchIf; | ||
function baseDispatch(eventNames, conditionFunc) { | ||
return (store, name, descriptor) => { | ||
if (descriptor) { | ||
return functionDispatchHandler(store, name, descriptor, eventNames, conditionFunc); | ||
} | ||
else { | ||
let events = [name]; | ||
if (eventNames instanceof Array) { | ||
events.concat(eventNames); | ||
} | ||
else if (descriptor.get) { | ||
key = "get"; | ||
return propDispatchHandler(store, name, events, conditionFunc); | ||
} | ||
}; | ||
} | ||
function propDispatchHandler(store, propName, eventNames, conditionFunc) { | ||
let _value; | ||
return { | ||
set(value) { | ||
if (_value === value) { | ||
return; // not update when there is no change | ||
} | ||
_value = value; | ||
if (typeof conditionFunc == "function" && !conditionFunc()) { | ||
// todo: maybe remove | ||
} | ||
else { | ||
store.dispatch.apply(this, [eventNames]); | ||
} | ||
}, | ||
get() { | ||
return _value; | ||
} | ||
let original = descriptor[key]; | ||
if (original) { | ||
descriptor[key] = function () { | ||
// console.log(`Arguments: ${arguments}`); | ||
try { | ||
this._dispatch_count++; | ||
const result = original.apply(this, arguments); | ||
// console.log(`Result: ${result}`); | ||
return result; | ||
}; | ||
} | ||
function functionDispatchHandler(store, actionName, descriptor, eventNames, conditionFunc) { | ||
let key = "value"; | ||
if (!descriptor.value) { | ||
if (descriptor.set) { | ||
key = "set"; | ||
} | ||
else if (descriptor.get) { | ||
key = "get"; | ||
} | ||
} | ||
let original = descriptor[key]; | ||
if (original) { | ||
descriptor[key] = function () { | ||
// console.log(`Arguments: ${arguments}`); | ||
try { | ||
this._dispatch_count++; | ||
const result = original.apply(this, arguments); | ||
// console.log(`Result: ${result}`); | ||
return result; | ||
} | ||
catch (e) { | ||
console.error(`Error: ${e}`); | ||
throw e; | ||
} | ||
finally { | ||
this._dispatch_count--; | ||
if (typeof conditionFunc == "function" && !conditionFunc()) { | ||
// todo: maybe remove | ||
} | ||
catch (e) { | ||
console.error(`Error: ${e}`); | ||
throw e; | ||
} | ||
finally { | ||
this._dispatch_count--; | ||
else { | ||
store.dispatch.apply(this, [eventNames]); | ||
} | ||
}; | ||
} | ||
return descriptor; | ||
}; | ||
} | ||
}; | ||
} | ||
return descriptor; | ||
} | ||
exports.dispatch = dispatch; |
@@ -28,2 +28,3 @@ export interface dependenceDispatcher { | ||
static register(func: any, dispatcher: (Dispatcher | DispatcherRegisterOptions)[]): void; | ||
dispatchOne: (func: any) => Promise<void>; | ||
static unregister(func: any, dispatcher: (Dispatcher | DispatcherRegisterOptions)[]): void; | ||
@@ -30,0 +31,0 @@ _sentOnChange: () => this; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -12,2 +20,19 @@ class Dispatcher { | ||
}; | ||
this.dispatchOne = (func) => __awaiter(this, void 0, void 0, function* () { | ||
const finish = () => { | ||
this._dispatch_count--; | ||
this.dispatch(); | ||
}; | ||
this._dispatch_count++; | ||
try { | ||
const r = func(); | ||
if (r instanceof Promise) { | ||
yield r; | ||
} | ||
finish(); | ||
} | ||
catch (err) { | ||
throw err; | ||
} | ||
}); | ||
this._sentOnChange = () => this; | ||
@@ -14,0 +39,0 @@ // todo dependencies |
@@ -33,2 +33,5 @@ "use strict"; | ||
__decorate([ | ||
_1.dispatch() | ||
], StoreIns.prototype, "args2", void 0); | ||
__decorate([ | ||
_1.dispatch([SortInsEvents.AddItem]) | ||
@@ -35,0 +38,0 @@ ], StoreIns.prototype, "action1", null); |
@@ -7,6 +7,1 @@ "use strict"; | ||
const dispatcher = new examples_1.StoreIns(); | ||
dispatcher.register(s => { | ||
count++; | ||
}); | ||
dispatcher.action2(items); // call to action1 multiple times | ||
expect(count).toBe(1); |
{ | ||
"name": "@storex/core", | ||
"version": "1.0.21", | ||
"version": "1.0.22", | ||
"description": "Binding store to your app", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
14724
394