Skip to content

Commit 71785c1

Browse files
committed
breakup limit store
1 parent cf34e45 commit 71785c1

File tree

6 files changed

+62
-60
lines changed

6 files changed

+62
-60
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
},
8585
{
8686
"path": "./build/releases/OneSignalSDK.page.es6.js",
87-
"limit": "47.26 kB",
87+
"limit": "47.2 kB",
8888
"gzip": true
8989
},
9090
{

src/page/bell/Button.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { addDomElement, removeDomElement } from 'src/shared/helpers/dom';
22
import { registerForPushNotifications } from 'src/shared/helpers/init';
3-
import LimitStore from 'src/shared/services/LimitStore';
3+
import {
4+
limitGetLast,
5+
limitIsEmpty,
6+
limitStorePut,
7+
} from 'src/shared/services/limitStore2';
48
import OneSignalEvent from 'src/shared/services/OneSignalEvent';
59
import AnimatedElement from './AnimatedElement';
610
import type Bell from './Bell';
@@ -66,16 +70,16 @@ export default class Button extends AnimatedElement {
6670

6771
_onHovering() {
6872
if (
69-
LimitStore.isEmpty(this._events.mouse) ||
70-
LimitStore.getLast(this._events.mouse) === 'out'
73+
limitIsEmpty(this._events.mouse) ||
74+
limitGetLast(this._events.mouse) === 'out'
7175
) {
7276
OneSignalEvent._trigger(BellEvent._Hovering);
7377
}
74-
LimitStore.put(this._events.mouse, 'over');
78+
limitStorePut(this._events.mouse, 'over');
7579
}
7680

7781
_onHovered() {
78-
LimitStore.put(this._events.mouse, 'out');
82+
limitStorePut(this._events.mouse, 'out');
7983
OneSignalEvent._trigger(BellEvent._Hovered);
8084
}
8185

@@ -107,7 +111,7 @@ export default class Button extends AnimatedElement {
107111
return;
108112
}
109113

110-
const optedOut = LimitStore.getLast<boolean>('subscription.optedOut');
114+
const optedOut = limitGetLast<boolean>('subscription.optedOut');
111115
if (this._bell._unsubscribed && !optedOut) {
112116
// The user is actually subscribed, register him for notifications
113117
registerForPushNotifications();

src/shared/helpers/init.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { OptionKey } from '../database/types';
77
import Log from '../libraries/Log';
88
import { CustomLinkManager } from '../managers/CustomLinkManager';
99
import { SubscriptionStrategyKind } from '../models/SubscriptionStrategyKind';
10-
import LimitStore from '../services/LimitStore';
10+
import { limitStorePut } from '../services/limitStore2';
1111
import OneSignalEvent from '../services/OneSignalEvent';
1212
import { IS_SERVICE_WORKER } from '../utils/EnvVariables';
1313
import { once } from '../utils/utils';
@@ -138,7 +138,7 @@ async function storeInitialValues() {
138138
await OneSignal._context._permissionManager._getPermissionStatus();
139139
const isOptedOut =
140140
await OneSignal._context._subscriptionManager._isOptedOut();
141-
LimitStore.put('subscription.optedOut', isOptedOut);
141+
limitStorePut('subscription.optedOut', isOptedOut);
142142
await db.put('Options', {
143143
key: 'isPushEnabled',
144144
value: isPushEnabled,

src/shared/listeners.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
NotificationClickEventInternal,
1414
} from './notifications/types';
1515
import { isCategorySlidedownConfigured } from './prompts/helpers';
16-
import LimitStore from './services/LimitStore';
16+
import { limitStorePut } from './services/limitStore2';
1717
import OneSignalEvent from './services/OneSignalEvent';
1818
import { logMethodCall } from './utils/utils';
1919

@@ -189,7 +189,7 @@ function onSubscriptionChanged_updateCustomLink() {
189189
}
190190

191191
export async function onInternalSubscriptionSet(optedOut: boolean) {
192-
LimitStore.put('subscription.optedOut', optedOut);
192+
limitStorePut('subscription.optedOut', optedOut);
193193
}
194194

195195
async function onSubscriptionChanged_showWelcomeNotification(

src/shared/services/LimitStore.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/shared/services/limitStore2.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
LimitStore.put('colorado', 'rocky');
3+
["rocky"]
4+
LimitStore.put('colorado', 'mountain');
5+
["rocky", "mountain"]
6+
LimitStore.put('colorado', 'national');
7+
["mountain", "national"]
8+
LimitStore.put('colorado', 'park');
9+
["national", "park"]
10+
*/
11+
const LIMIT = 2;
12+
const store: Record<string, Array<unknown>> = {};
13+
14+
export function limitStorePut<T>(key: string, value: T) {
15+
if (store[key] === undefined) {
16+
store[key] = [null, null];
17+
}
18+
store[key].push(value);
19+
if (store[key].length == LIMIT + 1) {
20+
store[key].shift();
21+
}
22+
return store[key];
23+
}
24+
25+
export function limitStoreGet<T>(key: string): T[] {
26+
if (store[key] === undefined) {
27+
store[key] = [null, null];
28+
}
29+
return store[key] as T[];
30+
}
31+
32+
export function limitGetFirst<T>(key: string) {
33+
return limitStoreGet<T>(key)[0];
34+
}
35+
36+
export function limitGetLast<T>(key: string) {
37+
return limitStoreGet<T>(key)[1];
38+
}
39+
40+
export function limitRemove(key: string) {
41+
delete store[key];
42+
}
43+
44+
export function limitIsEmpty(key: string) {
45+
const values = limitStoreGet(key);
46+
return values[0] === null && values[1] === null;
47+
}

0 commit comments

Comments
 (0)