8000 refactor: Consolidate datafile-manager package into optimizely-sdk by ozayr-zaviar · Pull Request #781 · optimizely/javascript-sdk · GitHub
[go: up one dir, main page]

Skip to content

refactor: Consolidate datafile-manager package into optimizely-sdk #781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Sep 23, 2022
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2022, Optimizely
*
* 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
*
* http://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.
*/
let items: {[key: string]: string} = {}

export default class AsyncStorage {

static getItem(key: string, callback?: (error?: Error, result?: string) => void): Promise<string | null> {
return new Promise(resolve => {
setTimeout(() => resolve(items[key] || null), 1)
})
}

static setItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void> {
return new Promise((resolve) => {
setTimeout(() => {
items[key] = value
resolve()
}, 1)
})
}

static removeItem(key: string, callback?: (error?: Error, result?: string) => void): Promise<string | null> {
return new Promise(resolve => {
setTimeout(() => {
items[key] && delete items[key]
// @ts-ignore
resolve()
}, 1)
})
}

static dumpItems(): {[key: string]: string} {
return items
}

static clearStore(): void {
items = {}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2022, Optimizely
* Copyright 2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,17 +13,28 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let items: {[key: string]: string} = {}

let items: {[key: string]: string} = {}
export default class AsyncStorage {

static getItem(key: string, callback?: (error?: Error, result?: string) => void): Promise<string | null> {
return new Promise(resolve => {
setTimeout(() => resolve(items[key] || null), 1)
return new Promise((resolve, reject) => {
switch (key) {
case 'keyThatExists':
resolve('{ "name": "Awesome Object" }')
break
case 'keyThatDoesNotExist':
resolve(null)
break
case 'keyWithInvalidJsonObject':
resolve('bad json }')
break
default:
setTimeout(() => resolve(items[key] || null), 1)
}
})
}

static setItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void> {
static setItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void> {
return new Promise((resolve) => {
setTimeout(() => {
items[key] = value
Expand Down
14 changes: 13 additions & 1 deletion packages/optimizely-sdk/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@ module.exports = {
"^.+\\.(ts|tsx|js|jsx)$": "ts-jest",
},
"testRegex": "(/tests/.*|(\\.|/)(test|spec))\\.tsx?$",
moduleNameMapper: {
// Force module uuid to resolve with the CJS entry point, because Jest does not support package.json.exports. See https://github.com/uuidjs/uuid/issues/451
"uuid": require.resolve('uuid'),
},
"testPathIgnorePatterns" : [
"tests/testUtils.ts"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
],
"resetMocks": false,
"setupFiles": [
"jest-localstorage-mock",
],
testEnvironment: "jsdom"
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { cloneDeep } from 'lodash';

import { sprintf } from '../../utils/fns';
import * as logging from '../../modules/logging';
import * as datafileManager from '@optimizely/js-sdk-datafile-manager';
import datafileManager from '../../modules/datafile-manager/index.node';

import * as projectConfig from './index';
import { ERROR_MESSAGES, LOG_MESSAGES } from '../../utils/enums';
Expand Down
2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/index.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import eventProcessorConfigValidator from './utils/event_processor_config_valida
import { createNotificationCenter } from './core/notification_center';
import { default as eventProcessor } from './plugins/event_processor';
import { OptimizelyDecideOption, Client, Config } from './shared_types';
import { createHttpPollingDatafileManager } from './plugins/datafile_manager/http_polling_datafile_manager';
import { createHttpPollingDatafileManager } from './plugins/datafile_manager/browser_http_polling_datafile_manager';
import { EXECUTION_CONTEXT_TYPE } from './utils/enums';
import { ExecutionContext } from './utils/execution_context';

Expand Down
2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/index.react_native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import eventProcessorConfigValidator from './utils/event_processor_config_valida
import { createNotificationCenter } from './core/notification_center';
import { createEventProcessor } from './plugins/event_processor/index.react_native';
import { OptimizelyDecideOption, Client, Config } from './shared_types';
import { createHttpPollingDatafileManager } from './plugins/datafile_manager/http_polling_datafile_manager';
import { createHttpPollingDatafileManager } from './plugins/datafile_manager/react_native_http_polling_datafile_manager';
import { EXECUTION_CONTEXT_TYPE } from './utils/enums';
import { ExecutionContext } from './utils/execution_context';

Expand Down