Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/LeanplumInbox.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ArgsBuilder from './ArgsBuilder'
import Constants from './Constants'
import StorageManager from './StorageManager'
import { CreateRequestFunction, MessageObject } from './types/internal'
import { Action, Inbox, InboxMessage } from './types/public'

Expand Down Expand Up @@ -85,14 +86,18 @@ export default class LeanplumInbox implements Inbox {
}

private save(): void {
sessionStorage.setItem(
StorageManager.save(
Constants.DEFAULT_KEYS.INBOX_MESSAGES,
JSON.stringify(this.messageMap)
JSON.stringify(this.messageMap),
'session'
)
}

private load(): void {
const state = sessionStorage.getItem(Constants.DEFAULT_KEYS.INBOX_MESSAGES)
const state = StorageManager.get(
Constants.DEFAULT_KEYS.INBOX_MESSAGES,
'session'
)
this.messageMap = JSON.parse(state) || {}
}

Expand Down
10 changes: 5 additions & 5 deletions src/LeanplumInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import InternalState from './InternalState'
import LeanplumInbox from './LeanplumInbox'
import LeanplumRequest from './LeanplumRequest'
import LeanplumSocket from './LeanplumSocket'
import LocalStorageManager from './LocalStorageManager'
import StorageManager from './StorageManager'
import PushManager from './PushManager'
import Messages from './Messages'
import EventEmitter from './EventEmitter'
Expand Down Expand Up @@ -406,7 +406,7 @@ Use "npm update leanplum-sdk" or go to https://docs.leanplum.com/reference#javas
sendNow: true,
queued: true,
response: () => {
LocalStorageManager.removeFromLocalStorage(SESSION_KEY)
StorageManager.remove(SESSION_KEY)
},
})
}
Expand Down Expand Up @@ -473,7 +473,7 @@ Use "npm update leanplum-sdk" or go to https://docs.leanplum.com/reference#javas

if (userId) {
this._lpRequest.userId = userId
LocalStorageManager.saveToLocalStorage(Constants.DEFAULT_KEYS.USER_ID, this._lpRequest.userId)
StorageManager.save(Constants.DEFAULT_KEYS.USER_ID, this._lpRequest.userId)
}
}

Expand Down Expand Up @@ -635,7 +635,7 @@ Use "npm update leanplum-sdk" or go to https://docs.leanplum.com/reference#javas
}

const currentTime = Date.now()
const lastActive = parseInt(LocalStorageManager.getFromLocalStorage(SESSION_KEY))
const lastActive = parseInt(StorageManager.get(SESSION_KEY))

if (isNaN(lastActive)) {
return false
Expand All @@ -649,6 +649,6 @@ Use "npm update leanplum-sdk" or go to https://docs.leanplum.com/reference#javas
}

private updateSession(): void {
LocalStorageManager.saveToLocalStorage(SESSION_KEY, String(Date.now()))
StorageManager.save(SESSION_KEY, String(Date.now()))
}
}
8 changes: 4 additions & 4 deletions src/LeanplumRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import ArgsBuilder from './ArgsBuilder'
import Constants from './Constants'
import LocalStorageManager from './LocalStorageManager'
import StorageManager from './StorageManager'
import Network from './Network'

export default class LeanplumRequest {
Expand Down Expand Up @@ -214,15 +214,15 @@ export default class LeanplumRequest {
}

private loadLocal<T>(key: string): T {
return LocalStorageManager.getFromLocalStorage(key)
return StorageManager.get(key)
}

private saveLocal<T>(key: string, value: T): void {
LocalStorageManager.saveToLocalStorage(key, value)
StorageManager.save(key, value)
}

private removeLocal(key: string): void {
LocalStorageManager.removeFromLocalStorage(key)
StorageManager.remove(key)
}
}

Expand Down
59 changes: 0 additions & 59 deletions src/LocalStorageManager.ts

This file was deleted.

10 changes: 5 additions & 5 deletions src/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CreateRequestFunction, Message, MessageVariables } from './types/intern
import EventEmitter from './EventEmitter'
import Network from './Network'
import isEqual from 'lodash.isequal'
import LocalStorageManager from './LocalStorageManager'
import StorageManager from './StorageManager'
import ValueTransforms from './ValueTransforms'

/* eslint-disable @typescript-eslint/ban-types */
Expand Down Expand Up @@ -76,7 +76,7 @@ class OccurrenceTracker {
}

load(): void {
const cache = LocalStorageManager.getFromLocalStorage(Constants.DEFAULT_KEYS.MESSAGE_OCCURRENCES)
const cache = StorageManager.get(Constants.DEFAULT_KEYS.MESSAGE_OCCURRENCES)
if (cache) {
const json = JSON.parse(cache)
this.session = json.session
Expand All @@ -87,7 +87,7 @@ class OccurrenceTracker {

save(): void {
const key = Constants.DEFAULT_KEYS.MESSAGE_OCCURRENCES
LocalStorageManager.saveToLocalStorage(key, JSON.stringify({
StorageManager.save(key, JSON.stringify({
session: this.session,
triggers: this.triggers,
occurrences: this.occurrences,
Expand Down Expand Up @@ -131,7 +131,7 @@ export default class Messages {
})
events.on('resume', () => {
const key = Constants.DEFAULT_KEYS.MESSAGE_CACHE
const cache = LocalStorageManager.getFromLocalStorage(key)
const cache = StorageManager.get(key)
this._messageCache = cache ? JSON.parse(cache) : this._messageCache

this.occurrenceTracker.load()
Expand Down Expand Up @@ -201,7 +201,7 @@ export default class Messages {
onMessagesReceived(receivedMessages): void {
const messages = receivedMessages || {}
this._messageCache = messages
LocalStorageManager.saveToLocalStorage(Constants.DEFAULT_KEYS.MESSAGE_CACHE, JSON.stringify(messages))
StorageManager.save(Constants.DEFAULT_KEYS.MESSAGE_CACHE, JSON.stringify(messages))
}

shouldShowMessage(id: string, message, context: TriggerContext): boolean {
Expand Down
6 changes: 3 additions & 3 deletions src/PushManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import ArgsBuilder from './ArgsBuilder'
import Constants from './Constants'
import LocalStorageManager from './LocalStorageManager'
import StorageManager from './StorageManager'
import { CreateRequestFunction } from './types/internal'

const APPLICATION_SERVER_PUBLIC_KEY =
Expand Down Expand Up @@ -193,12 +193,12 @@ export default class PushManager {
if (subscription) {
const preparedSubscription = this.prepareSubscription(subscription)
const preparedSubscriptionString = JSON.stringify(preparedSubscription)
const existingSubscriptionString = LocalStorageManager.getFromLocalStorage(
const existingSubscriptionString = StorageManager.get(
Constants.DEFAULT_KEYS.PUSH_SUBSCRIPTION
) as string

if (existingSubscriptionString !== preparedSubscriptionString) {
LocalStorageManager.saveToLocalStorage(
StorageManager.save(
Constants.DEFAULT_KEYS.PUSH_SUBSCRIPTION,
preparedSubscriptionString
)
Expand Down
80 changes: 80 additions & 0 deletions src/StorageManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
*
* Copyright 2020 Leanplum Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*
*/

const storageEnabled = {
local: true,
session: true,
}
const alternateStorage = {
local: {},
session: {},
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Value = any
type StorageType = 'local' | 'session'

export default class StorageManager {
static get(key: string, type: StorageType = 'local'): Value {
if (!storageEnabled[type]) {
return alternateStorage[type][key]
}

if (type === 'local') {
return localStorage[key]
} else {
return sessionStorage.getItem(key)
}
}

static save(key: string, value: Value, type: StorageType = 'local'): void {
if (!storageEnabled[type]) {
alternateStorage[type][key] = value
return
}

try {
if (type === 'local') {
localStorage[key] = value
} else {
sessionStorage.setItem(key, value)
}
} catch (e) {
storageEnabled[type] = false
alternateStorage[type][key] = value
}
}

static remove(key: string, type: StorageType = 'local'): void {
if (!storageEnabled[type]) {
delete alternateStorage[type][key]
return
}

try {
if (type === 'local') {
localStorage.removeItem(key)
} else {
sessionStorage.removeItem(key)
}
} catch (e) {
storageEnabled[type] = false
delete alternateStorage[type][key]
}
}
}
6 changes: 3 additions & 3 deletions src/VarCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import ArgsBuilder from './ArgsBuilder'
import Constants from './Constants'
import LocalStorageManager from './LocalStorageManager'
import StorageManager from './StorageManager'
import { CreateRequestFunction } from './types/internal'
import { ActionParameter, MessageTemplateOptions } from './types/public'
import ValueTransforms from './ValueTransforms'
Expand Down Expand Up @@ -188,11 +188,11 @@ export default class VarCache {
}

private loadLocal<T>(key: string): T {
return LocalStorageManager.getFromLocalStorage(key)
return StorageManager.get(key)
}

private saveLocal<T>(key: string, value: T): void {
LocalStorageManager.saveToLocalStorage(key, value)
StorageManager.save(key, value)
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/specs/LeanplumInbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe(LeanplumInbox, () => {
inbox.onChanged(handler)

createRequestSpy.mockImplementationOnce(
(method, args, options) => {
(_, __, options) => {
options.response({
response: [{
success:true,
Expand All @@ -64,7 +64,7 @@ describe(LeanplumInbox, () => {
inbox.onChanged(handler)

createRequestSpy.mockImplementationOnce(
(method, args, options) => {
(_, __, options) => {
options.response(null)
}
)
Expand Down Expand Up @@ -441,7 +441,7 @@ describe(LeanplumInbox, () => {

function mockMessages(newsfeedMessages: any): void {
createRequestSpy.mockImplementationOnce(
(method, args, options) => {
(_, __, options) => {
options.response({
response: [ { newsfeedMessages } ],
})
Expand Down
1 change: 0 additions & 1 deletion test/specs/LeanplumIntegration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import Constants from '../../src/Constants'
import LeanplumInternal from '../../src/LeanplumInternal'
import { startResponse } from '../data/responses'
import { windowMock } from '../mocks/external'
Expand Down
4 changes: 2 additions & 2 deletions test/specs/LeanplumRequest.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import LeanplumRequest from '../../src/LeanplumRequest'
import LocalStorageManager from '../../src/LocalStorageManager'
import StorageManager from '../../src/StorageManager'
import Constants from '../../src/Constants'
import Network from '../../src/Network'
import ArgsBuilder from '../../src/ArgsBuilder'
Expand Down Expand Up @@ -32,7 +32,7 @@ describe(LeanplumRequest, () => {

describe('userId resolution', () => {
beforeEach(() => {
lsGetSpy = jest.spyOn(LocalStorageManager, 'getFromLocalStorage').mockReturnValue(undefined)
lsGetSpy = jest.spyOn(StorageManager, 'get').mockReturnValue(undefined)
})

afterEach(() => {
Expand Down
Loading