Skip to content

Commit

Permalink
[CM-667] Cleanup & Reenable lint step (#101)
Browse files Browse the repository at this point in the history
* wip

* [CM-667] Reenable lint step

* Update readme

* adjust storagehandler for tests

* refactor callhandler for tests

* bind all the things

* fix test

* fix integration tests

* more types
  • Loading branch information
mschuwalow authored Jan 23, 2023
1 parent 08e1365 commit 167a912
Show file tree
Hide file tree
Showing 32 changed files with 402 additions and 302 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ___
### Initialization
The initialisation part should be straight forward, considering the snippet:
```javascript
import { LiveConnect } from 'live-connect-js/src/live-connect'
import { LiveConnect } from 'live-connect-js'
const lc = LiveConnect(configOptions)
```

Expand All @@ -60,7 +60,7 @@ The object returned after initialisation (`lc` in the snippet above) is exposing
- `resolutionCallUrl` function returns the URL to be called in order to receive the resolution to a stable identifier.

### Overriding the StorageHandler and CallHandler
LiveConnect is initialized in a way so that it does not manipulate storage and ajax on the device on it's own.
LiveConnect is initialized in a way so that it does not manipulate storage and ajax on the device on its own.

The StorageHandler is an object with functions that adheres to the signature:
- `function localStorageIsEnabled ()`
Expand All @@ -78,9 +78,9 @@ and the `fallback` is a `function()`
- `function pixelGet (uri, onload)`
where the `onload` is a `function()`

If one of the functions is not available in the external handler, LiveConnect will fall back to its own implementation to ensure that the functionality isn't being affected.
If one of the functions is not available in the external handler, LiveConnect will fall back to stubs to ensure that the overall functionality isn't being affected. It is recommended to provide full implementations of the interfaces. An example for StorageHandler ([here](./test/shared/utils/storage.ts)) and for CallHandler ([here](./test/shared/utils/calls.ts)) are provided.

One way to achieve that is, for example, to initialize LC like this:
With custom implementations the initialisation looks like this:
```javascript
import { LiveConnect } from 'live-connect-js/src/live-connect'
const storageHandler = {
Expand Down
134 changes: 134 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"CONFIGURATION_OPTIONS.md"
],
"scripts": {
"test:unit": "nyc ts-mocha --config test-config/mocha.unit.json",
"test:unit": "npm run lint && nyc ts-mocha --config test-config/mocha.unit.json",
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
"test:it:browserstack": "npm run build-test-resources && wdio test-config/wdio.browserstack.conf.js",
"test:it:docker:chrome": "npm run build-test-resources && wdio test-config/wdio.docker.chrome.conf.js",
Expand Down Expand Up @@ -62,8 +62,14 @@
"@rollup/plugin-replace": "^2.3.3",
"@rollup/plugin-strip": "^1.3.3",
"@rollup/plugin-typescript": "^8.5.0",
"rollup-plugin-mjs-entry": "^0.1.1",
"@types/chai": "^4.3.4",
"@types/compression": "^1.7.2",
"@types/cors": "^2.8.13",
"@types/dirty-chai": "^2.0.2",
"@types/express": "^4.17.15",
"@types/js-cookie": "^3.0.2",
"@types/mocha": "^10.0.1",
"@types/sinon": "^10.0.13",
"@typescript-eslint/eslint-plugin": "^5.38.0",
"@typescript-eslint/parser": "^5.38.0",
"@wdio/browserstack-service": "^7.16.3",
Expand Down Expand Up @@ -107,6 +113,7 @@
"rollup-plugin-cleaner": "^1.0.0",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-mjs-entry": "^0.1.1",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-ts": "^3.0.2",
Expand Down
4 changes: 2 additions & 2 deletions src/events/error-pixel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PixelSender } from '../pixel/sender'
import { StateWrapper } from '../pixel/state'
import * as page from '../enrichers/page'
import * as C from '../utils/consts'
import { ErrorDetails, EventBus, ICallHandler, IPixelSender, State } from '../types'
import { EventBus, ICallHandler, IPixelSender, State } from '../types'

let _state = null
let _pixelSender: IPixelSender = null
Expand Down Expand Up @@ -35,7 +35,7 @@ function _truncate (value: string): string {
}
}

export function asErrorDetails (e: any): ErrorDetails {
export function asErrorDetails (e: any): State {
if (e) {
return {
errorDetails: {
Expand Down
4 changes: 2 additions & 2 deletions src/events/event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function LocalEventBus (size = 5) {
return initBus(size)
}

export function GlobalEventBus (name: string, size: number, errorCallback: (error: any) => void) {
export function GlobalEventBus (name: string, size: number, errorCallback: (error: any) => void): EventBus {
try {
if (!window) {
errorCallback(new Error('Bus can only be attached to the window, which is not present'))
Expand All @@ -45,7 +45,7 @@ export function GlobalEventBus (name: string, size: number, errorCallback: (erro
}
}

export function getAvailableBus (name: string): EventBus {
export function getAvailableBus (name: string): EventBus {
const eventBus = window[name].eventBus || window[C.EVENT_BUS_NAMESPACE]
extendBusIfNeeded(eventBus)
return eventBus
Expand Down
12 changes: 5 additions & 7 deletions src/events/replayemitter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as C from '../utils/consts'
import { ErrorDetails, EventBus } from '../types'
import { EventBus } from '../types'

type Callback<Ctx> = (ctx: Ctx, event: any) => void
type Callback<Ctx> = (ctx: Ctx, data: any[]) => void

interface EventHandler<Ctx> {
ctx?: Ctx,
Expand Down Expand Up @@ -43,17 +43,15 @@ export class ReplayEmitter implements EventBus {
}

once <Ctx> (name: string, callback: Callback<Ctx>, ctx: Ctx): EventBus {
const self = this

const eventQueue = this.q[name] || []
if (eventQueue.length > 0) {
callback.apply(ctx, eventQueue[0])

return this
} else {
const listener = function () {
self.off(name, listener)
callback.apply(ctx, arguments)
const listener = (...args: any[]) => {
this.off(name, listener)
callback.apply(ctx, args)
}

listener._ = callback
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/call-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function CallHandler (externalCallHandler: ExternalCallHandler, eventBus:
function _externalOrError (functionName: string) {
const hasExternal = externalCallHandler && externalCallHandler[functionName] && isFunction(externalCallHandler[functionName])
if (hasExternal) {
return externalCallHandler[functionName]
return externalCallHandler[functionName].bind(externalCallHandler)
} else {
errors.push(functionName)
return _noOp
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/read-storage-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function StorageHandler (storageStrategy: StorageStrategyMode, externalSt
if (strEqualsIgnoreCase(storageStrategy, StorageStrategy.disabled)) {
return _noOp
} else if (hasExternal) {
return externalStorageHandler[functionName]
return externalStorageHandler[functionName].bind(externalStorageHandler)
} else {
errors.push(functionName)
return _noOp
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/storage-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ const _noOp = () => undefined
export function StorageHandler (storageStrategy: StorageStrategyMode, externalStorageHandler: ExternalStorageHandler, eventBus: EventBus): IStorageHandler {
const errors = []

function _externalOrError (functionName) {
function _externalOrError (functionName: string): CallableFunction {
const hasExternal = externalStorageHandler && externalStorageHandler[functionName] && isFunction(externalStorageHandler[functionName])
if (strEqualsIgnoreCase(storageStrategy, StorageStrategy.disabled)) {
return _noOp
} else if (hasExternal) {
return externalStorageHandler[functionName]
return externalStorageHandler[functionName].bind(externalStorageHandler)
} else {
errors.push(functionName)
return _noOp
}
}

const _orElseNoOp = (fName) => strEqualsIgnoreCase(storageStrategy, StorageStrategy.none) ? _noOp : _externalOrError(fName)
const _orElseNoOp = (fName: string) => strEqualsIgnoreCase(storageStrategy, StorageStrategy.none) ? _noOp : _externalOrError(fName)

const functions = {
localStorageIsEnabled: _orElseNoOp('localStorageIsEnabled'),
Expand Down
2 changes: 1 addition & 1 deletion src/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { EventBus, ExternalCallHandler, ExternalStorageHandler, ILiveConnect, Li
import { EVENT_BUS_NAMESPACE } from './utils/consts'
import { GlobalEventBus } from './events/event-bus'

export function LiveConnect (liveConnectConfig: LiveConnectConfig, externalStorageHandler: ExternalStorageHandler, externalCallHandler: ExternalCallHandler, mode: string, externalEventBus: EventBus): ILiveConnect {
export function LiveConnect (liveConnectConfig: LiveConnectConfig, externalStorageHandler: ExternalStorageHandler, externalCallHandler: ExternalCallHandler, mode: string, externalEventBus?: EventBus): ILiveConnect {
const minimalMode = mode === 'minimal' || process.env.LiveConnectMode === 'minimal'
const bus = externalEventBus || GlobalEventBus(EVENT_BUS_NAMESPACE, 5, err => console.error(err))
const configuration = (isObject(liveConnectConfig) && liveConnectConfig) || {}
Expand Down
2 changes: 1 addition & 1 deletion src/standard-live-connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function _standardInitialization (liveConnectConfig: LiveConnectConfig, external
}
}

export function StandardLiveConnect (liveConnectConfig: LiveConnectConfig, externalStorageHandler: ExternalStorageHandler, externalCallHandler: ExternalCallHandler, externalEventBus: EventBus): ILiveConnect {
export function StandardLiveConnect (liveConnectConfig: LiveConnectConfig, externalStorageHandler: ExternalStorageHandler, externalCallHandler: ExternalCallHandler, externalEventBus?: EventBus): ILiveConnect {
const configuration = (isObject(liveConnectConfig) && liveConnectConfig) || {}
configuration.globalVarName = configuration.globalVarName || 'liQ'
const eventBus = externalEventBus || LocalEventBus()
Expand Down
4 changes: 1 addition & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export type IdentityResultionResult = object
export interface ExternalCallHandler {
ajaxGet?: (
url: string,
onSuccess: (responseText: string) => void,
onSuccess: (responseText: string, response: any) => void,
onError?: (error: any) => void,
timeout?: number
) => void,
Expand Down Expand Up @@ -141,8 +141,6 @@ export interface State extends LiveConnectConfig {
referrer?: string
}

type ParamOrEmpty = [string, string][] | [string, string] | []

export interface HemStore {
hashedEmail?: HashedEmail[]
}
Expand Down
Loading

0 comments on commit 167a912

Please sign in to comment.