Skip to content

Commit 07773b5

Browse files
committed
build: v0.3.21 cami.slice subscribe
1 parent 09ce002 commit 07773b5

File tree

8 files changed

+60
-38
lines changed

8 files changed

+60
-38
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
⚠️ Expect API changes until v1.0.0 ⚠️
44

5-
Current version: 0.3.20.
5+
Current version: 0.3.21.
66

77
Bundle Size: 14kb minified & gzipped.
88

build/cami.cdn.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/cami.cdn.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/cami.module.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/cami.module.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/021_cartSlice.html

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,12 @@ <h2>Cart</h2>
7878
products = [];
7979

8080
onConnect() {
81-
appStore.subscribe(state => {
82-
this.cartItems = state.cart.cartItems;
83-
this.products = state.products;
81+
productSlice.subscribe(state => {
82+
this.products = state;
83+
});
84+
85+
cartSlice.subscribe(state => {
86+
this.cartItems = state.cartItems;
8487
});
8588

8689
productSlice.setStatus('pending');
@@ -133,8 +136,8 @@ <h2>Cart</h2>
133136
class CartElement extends ReactiveElement {
134137
cartItems = [];
135138
onConnect() {
136-
appStore.subscribe(state => {
137-
this.cartItems = state.cart.cartItems;
139+
cartSlice.subscribe(state => {
140+
this.cartItems = state.cartItems;
138141
});
139142
}
140143

examples/partials/_021_cartSlice.html

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ <h2>Cart</h2>
7070
products = [];
7171

7272
onConnect() {
73-
appStore.subscribe(state => {
74-
this.cartItems = state.cart.cartItems;
75-
this.products = state.products;
73+
productSlice.subscribe(state => {
74+
this.products = state;
75+
});
76+
77+
cartSlice.subscribe(state => {
78+
this.cartItems = state.cartItems;
7679
});
7780

7881
productSlice.setStatus('pending');
@@ -125,8 +128,8 @@ <h2>Cart</h2>
125128
class CartElement extends ReactiveElement {
126129
cartItems = [];
127130
onConnect() {
128-
appStore.subscribe(state => {
129-
this.cartItems = state.cart.cartItems;
131+
cartSlice.subscribe(state => {
132+
this.cartItems = state.cartItems;
130133
});
131134
}
132135

src/observables/observable-store.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -539,49 +539,50 @@ class ObservableStore extends Observable {
539539
}
540540

541541
/**
542+
* Creates a slice of the store with its own state and actions, namespaced to avoid conflicts.
543+
*
542544
* @function slice
543545
* @param {Object} store - The main store instance.
544546
* @param {Object} options - The options for creating the slice.
545547
* @param {string} options.name - The name of the slice.
546548
* @param {Object} options.state - The initial state of the slice.
547549
* @param {Object} options.actions - The actions for the slice.
548550
* @returns {Object} - An object containing the action methods for the slice.
549-
* @description Creates a slice of the store with its own state and actions, namespaced to avoid conflicts.
551+
*
550552
* @example
551-
* ```javascript
552-
* const userSlice = slice(appStore, {
553-
* name: 'user',
553+
* const cartSlice = slice(appStore, {
554+
* name: 'cart',
554555
* state: {
555-
* userInfo: null,
556-
* isLoggedIn: false,
556+
* cartItems: [],
557557
* },
558558
* actions: {
559-
* login(state, userInfo) {
560-
* state.userInfo = userInfo;
561-
* state.isLoggedIn = true;
559+
* add(state, product) {
560+
* const newItem = { ...product, id: Date.now() };
561+
* state.cartItems.push(newItem);
562562
* },
563-
* logout(state) {
564-
* state.userInfo = null;
565-
* state.isLoggedIn = false;
563+
* remove(state, product) {
564+
* state.cartItems = state.cartItems.filter(item => item.id !== product.id);
566565
* },
567566
* }
568567
* });
569-
* ```
568+
*
569+
* cartSlice.add({ name: 'Product 1', price: 100 });
570+
* cartSlice.remove({ id: 123456789 });
570571
*/
571572
const slice = (store, { name, state, actions }) => {
572573
if (store.slices && store.slices[name]) {
573574
throw new Error(`[Cami.js] Slice name ${name} is already in use.`);
574575
}
575576

576-
// Initialize slices if not already done
577577
if (!store.slices) {
578578
store.slices = {};
579579
}
580580

581-
store.slices[name] = true; // Mark the slice as registered
581+
store.slices[name] = true;
582582
store.state[name] = state;
583583

584584
const sliceActions = {};
585+
const sliceSubscribers = [];
585586

586587
Object.keys(actions).forEach(actionKey => {
587588
const namespacedAction = `${name}/${actionKey}`;
@@ -594,7 +595,22 @@ const slice = (store, { name, state, actions }) => {
594595
};
595596
});
596597

597-
return sliceActions;
598+
const subscribe = (callback) => {
599+
sliceSubscribers.push(callback);
600+
return () => {
601+
const index = sliceSubscribers.indexOf(callback);
602+
if (index > -1) {
603+
sliceSubscribers.splice(index, 1);
604+
}
605+
};
606+
};
607+
608+
store.subscribe((newState) => {
609+
const sliceState = newState[name];
610+
sliceSubscribers.forEach(callback => callback(sliceState));
611+
});
612+
613+
return { ...sliceActions, subscribe };
598614
};
599615

600616
/**

0 commit comments

Comments
 (0)