@swear-js/core
Advanced tools
Comparing version 2.1.0 to 3.0.0
@@ -1,2 +0,11 @@ | ||
import { SwearCreateStoreArgs, SwearStoreReturnType } from './types'; | ||
export declare const createStore: (storeArgs?: SwearCreateStoreArgs) => SwearStoreReturnType; | ||
import { SwearCreateStoreArgs, SwearStoreType, SwearSubscriberOnUpdate, SwearType } from './types'; | ||
export declare const createStore: (storeArgs?: SwearCreateStoreArgs) => { | ||
getStore: () => SwearStoreType<any>; | ||
subscribe: <T, Y>(swear: SwearType<T, Y>, onUpdate: SwearSubscriberOnUpdate<T>) => void; | ||
unsubscribe: <T_1, Y_1>(swear: SwearType<T_1, Y_1>) => void; | ||
getSwearState: <T_2, Y_2>(swear: SwearType<T_2, Y_2>) => T_2; | ||
getSwearActions: <T_3, Y_3>(swear: SwearType<T_3, Y_3>) => { | ||
set: (payload: T_3 | ((prev: T_3) => T_3)) => void; | ||
reset: () => void; | ||
} & Y_3; | ||
}; |
@@ -20,5 +20,5 @@ "use strict"; | ||
return { | ||
getState: function () { return store; }, | ||
subscribe: function (_a) { | ||
var swearId = _a.swearId, defaultState = _a.defaultState, onUpdate = _a.onUpdate; | ||
getStore: function () { return store; }, | ||
subscribe: function (swear, onUpdate) { | ||
var swearId = swear[0], defaultState = swear[1]; | ||
if (!(swearId in store)) { | ||
@@ -29,24 +29,32 @@ store[swearId] = defaultState; | ||
}, | ||
unsubscribe: function (swearId) { | ||
unsubscribe: function (swear) { | ||
var swearId = swear[0]; | ||
delete subscribers[swearId]; | ||
}, | ||
getSwearValue: function (swearId) { return store[swearId]; }, | ||
setSwearValue: function (swearId, tag, payload) { | ||
if (store[swearId] !== payload) { | ||
var prev = __assign({}, store); | ||
store[swearId] = payload; | ||
if (subscribers[swearId]) { | ||
subscribers[swearId](payload); | ||
} | ||
if (storeArgs === null || storeArgs === void 0 ? void 0 : storeArgs.onPatch) { | ||
storeArgs.onPatch({ | ||
swearId: swearId, | ||
tag: tag, | ||
prev: prev, | ||
payload: payload, | ||
next: store, | ||
}); | ||
} | ||
} | ||
getSwearState: function (swear) { | ||
var swearId = swear[0]; | ||
return store[swearId]; | ||
}, | ||
getSwearActions: function (swear) { | ||
var swearId = swear[0], defaultState = swear[1], actions = swear[2]; | ||
var mutator = function (payload, tag) { | ||
var _a, _b; | ||
var finalPayload = payload instanceof Function ? payload(store[swearId]) : payload; | ||
var prev = store[swearId]; | ||
store[swearId] = finalPayload; | ||
(_a = storeArgs.onPatch) === null || _a === void 0 ? void 0 : _a.call(storeArgs, { | ||
swearId: swearId, | ||
tag: tag !== null && tag !== void 0 ? tag : null, | ||
prev: prev, | ||
payload: finalPayload, | ||
next: store[swearId], | ||
}); | ||
(_b = subscribers[swearId]) === null || _b === void 0 ? void 0 : _b.call(subscribers, store[swearId]); | ||
}; | ||
var defaultActions = function (mutate) { return ({ | ||
set: function (payload) { return (mutate(payload instanceof Function ? payload(store[swearId]) : payload)); }, | ||
reset: function () { return mutate(defaultState); }, | ||
}); }; | ||
return __assign(__assign({}, defaultActions(mutator)), actions(mutator)); | ||
}, | ||
}; | ||
@@ -53,0 +61,0 @@ }; |
export * from './createStore'; | ||
export * from './createSwear'; | ||
export * from './types'; |
@@ -14,3 +14,4 @@ "use strict"; | ||
__exportStar(require("./createStore"), exports); | ||
__exportStar(require("./createSwear"), exports); | ||
__exportStar(require("./types"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -15,13 +15,7 @@ export declare type SwearStoreType<T> = Record<string, T>; | ||
export declare type SwearStoreSubscribers = Record<SwearId, SwearSubscriberOnUpdate<any>>; | ||
export declare type SwearRegisterSubscribeArgs<T> = { | ||
swearId: SwearId; | ||
defaultState: T; | ||
onUpdate: SwearSubscriberOnUpdate<T>; | ||
}; | ||
export declare type SwearStoreReturnType = { | ||
getState: () => SwearStoreType<any>; | ||
getSwearValue: <T>(swearId: string) => T; | ||
setSwearValue: <T>(swearId: string, tag: SwearPatch['tag'], payload: T) => void; | ||
subscribe: <T>(swear: SwearRegisterSubscribeArgs<T>) => void; | ||
unsubscribe: (swearId: SwearId) => void; | ||
}; | ||
export declare type SwearMutateType<T> = ((payload: T | ((prev: T) => T), tag?: string) => void) | (() => void); | ||
export declare type SwearType<T, Y> = [ | ||
name: string, | ||
defaultValue: T, | ||
actions: ((mutate: SwearMutateType<T>) => Y) | ||
]; |
{ | ||
"name": "@swear-js/core", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "Simple observer state manager", | ||
@@ -47,3 +47,3 @@ "repository": { | ||
}, | ||
"gitHead": "ac4163c86f8f39d83f6cb172f6e7bb40cc7791b5" | ||
"gitHead": "51c80a56e5a450c35285aa94a74bc5731dba2fa4" | ||
} |
@@ -21,2 +21,10 @@ # 🍭 Swear JS | ||
## Features | ||
___ | ||
- 🍥 **Simple abstraction**. No need to get into FLUX, life cycle, the flow. You just have a state, and have functions(actions) that somehow mutates that state. Just like React's `useState`. No way simpler. | ||
- 🍩 **Deep type inferences**. No need to guess what type you're working with(hey, Redux)! | ||
- 🍡 **Code splitting**. Your swears can lay wherever you want. Even in the scope of another microapplication. | ||
- 🧊 **Framework-agnostic**. If your framework has no support, or you just simply don't use one, `core` package is for you. | ||
- 🪡 **Small size**. [584B](https://bundlephobia.com/package/@swear-js/core@2.1.0) | ||
- 🐞 **Logging**. Store has default support for logging. And SwearJS has it's our own beautiful [logger](https://npmjs.org/@swear-js/logger) | ||
@@ -38,30 +46,30 @@ ## Installation | ||
```typescript | ||
import { createStore } from "@swear-js/core"; | ||
import { createStore, createSwear } from "@swear-js/core"; | ||
const store = createStore(); | ||
``` | ||
Then you have to create | ||
```javascript | ||
import { createStore } from "@swear-js/core"; | ||
// Create swear | ||
const countSwear = createSwear('count', 0, (mutate) => ({ | ||
increase: () => mutate(prev => prev + 1), | ||
decrease: () => mutate(prev => prev - 1), | ||
})); | ||
const store = createStore(); | ||
// This will create swear, and triggers onUpdate everytime it changes | ||
store.subscribe({ | ||
swearId: 'count', | ||
defaultState: 0, | ||
onUpdate: (newValue) => { | ||
// You can here trigger things when it updates. You can trigger here your render function, or something | ||
} | ||
// Callback is triggered on every swear update | ||
store.subscribe(countSwear, (newValue) => { | ||
// Handle updates of countSwear | ||
console.log(newValue); | ||
}); | ||
const decreaseHandler = () => { | ||
store.setSwearValue('count', 'decrease', store.getSwearValue('count') - 1); | ||
}; | ||
const increaseHandler = () => { | ||
store.setSwearValue('count', 'increase', store.getSwearValue('count') + 1); | ||
}; | ||
// This will return object of actions with default actions: set() and reset(), and your own defined actions. | ||
const actions = store.getSwearActions(countSwear); | ||
const { set, reset, increase, decrease } = actions; // set() and reset() are default actions | ||
increase(); // When you dispatch an action, swear value is mutated, and subscribtion callback is triggered. | ||
``` | ||
## Default actions | ||
`store.getSwearActions()` returns an object of combined default actions and your actions. | ||
> `set()` action implements the same usage interface as React's useState setter. It means you can pass both a payload, and callback with previous value. | ||
> Example: `set((prev) => prev + 10);` | ||
## Logging | ||
@@ -103,1 +111,3 @@ ___ | ||
``` | ||
Inspired by [@artalar's](https://github.com/artalar) [Reatom](https://github.com/artalar/reatom). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13133
14
118
111