Skip to content

Commit

Permalink
feat: init accelerator lib and topic to share states (#11)
Browse files Browse the repository at this point in the history
* feat: init accelerator lib and topic to share states

* feat: remove console log

* feat: delete jest-marbles package

* feat: edit read me

---------

Co-authored-by: kim.tran <[email protected]>
  • Loading branch information
KimFFVII and kim.tran authored Nov 14, 2023
1 parent 5316430 commit b79d1a4
Show file tree
Hide file tree
Showing 23 changed files with 601 additions and 12 deletions.
40 changes: 40 additions & 0 deletions libs/accelerator/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts"],
"extends": ["plugin:@nx/angular", "plugin:@angular-eslint/template/process-inline-templates"],
"rules": {
"@angular-eslint/directive-selector": [
"error",
{
"type": "attribute",
"prefix": "onecx",
"style": "camelCase"
}
],
"@angular-eslint/component-selector": [
"error",
{
"type": "element",
"prefix": "onecx",
"style": "kebab-case"
}
]
}
},
{
"files": ["*.html"],
"extends": ["plugin:@nx/angular-template"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
}
]
}
7 changes: 7 additions & 0 deletions libs/accelerator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# accelerator

This library contains all framework independent accelerators.

## Running unit tests

Run `nx test accelerator` to execute the unit tests.
22 changes: 22 additions & 0 deletions libs/accelerator/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable */
export default {
displayName: 'accelerator',
preset: '../../jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
coverageDirectory: '../../coverage/libs/accelerator',
transform: {
'^.+\\.(ts|mjs|js|html)$': [
'jest-preset-angular',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
},
],
},
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
snapshotSerializers: [
'jest-preset-angular/build/serializers/no-ng-attributes',
'jest-preset-angular/build/serializers/ng-snapshot',
'jest-preset-angular/build/serializers/html-comment',
],
}
8 changes: 8 additions & 0 deletions libs/accelerator/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../dist/libs/accelerator",
"lib": {
"entryFile": "src/index.ts"
},
"assets": ["CHANGELOG.md", "./assets/**"]
}
13 changes: 13 additions & 0 deletions libs/accelerator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@onecx/accelerator",
"version": "3.1.1",
"peerDependencies": {
"@angular/common": "^15.2.7",
"@angular/core": "^15.2.7",
"rxjs": "^7.8.0"
},
"publishConfig": {
"access": "public"
}
}

63 changes: 63 additions & 0 deletions libs/accelerator/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "accelerator",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/accelerator/src",
"prefix": "onecx",
"tags": [],
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/angular:package",
"outputs": [
"{workspaceRoot}/dist/{projectRoot}"
],
"options": {
"project": "libs/accelerator/ng-package.json"
},
"configurations": {
"production": {
"tsConfig": "libs/accelerator/tsconfig.lib.prod.json"
},
"development": {
"tsConfig": "libs/accelerator/tsconfig.lib.json"
}
},
"defaultConfiguration": "production"
},
"test": {
"executor": "@nx/jest:jest",
"outputs": [
"{workspaceRoot}/coverage/{projectRoot}"
],
"options": {
"jestConfig": "libs/accelerator/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": [
"{options.outputFile}"
],
"options": {
"lintFilePatterns": [
"libs/accelerator/**/*.ts",
"libs/accelerator/**/*.html",
"libs/accelerator/package.json"
]
}
},
"release": {
"executor": "nx-release:build-update-publish",
"options": {
"libName": "accelerator"
}
}
}
}
2 changes: 2 additions & 0 deletions libs/accelerator/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './lib/accelerator.module'
export * from './lib/topic/topic'
7 changes: 7 additions & 0 deletions libs/accelerator/src/lib/accelerator.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NgModule } from '@angular/core'
import { CommonModule } from '@angular/common'

@NgModule({
imports: [CommonModule],
})
export class AcceleratorModule {}
7 changes: 7 additions & 0 deletions libs/accelerator/src/lib/topic/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class Message {
timestamp: number

constructor(public type: string) {
this.timestamp = window.performance.now()
}
}
8 changes: 8 additions & 0 deletions libs/accelerator/src/lib/topic/topic-data-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { TopicMessage } from './topic-message'
import { TopicMessageType } from './topic-message-type'

export class TopicDataMessage<T> extends TopicMessage {
constructor(type: TopicMessageType, name: string, version: number, public data: T) {
super(type, name, version)
}
}
5 changes: 5 additions & 0 deletions libs/accelerator/src/lib/topic/topic-message-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const enum TopicMessageType {
TopicNext = 'TopicNext',
TopicGet = 'TopicGet',
}

12 changes: 12 additions & 0 deletions libs/accelerator/src/lib/topic/topic-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Message } from "./message";
import { TopicMessageType } from "./topic-message-type";

export class TopicMessage extends Message {
constructor(
type: TopicMessageType,
public name: string,
public version: number
) {
super(type);
}
}
147 changes: 147 additions & 0 deletions libs/accelerator/src/lib/topic/topic.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { map } from 'rxjs'
import { Topic } from './topic'

describe('Topic', () => {
const origAddEventListener = window.addEventListener
const origPostMessage = window.postMessage

let listeners: any[] = []
window.addEventListener = (_type: any, listener: any) => {
listeners.push(listener)
}

window.removeEventListener = (_type: any, listener: any, ) => {
listeners = listeners.filter((l) => l !== listener)
}

window.postMessage = (m: any) => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
listeners.forEach((l) => l({ data: m, stopImmediatePropagation: () => {}, stopPropagation: () => {} }))
}

afterAll(() => {
window.addEventListener = origAddEventListener
window.postMessage = origPostMessage
})

let values1: any[]
let values2: any[]

let testTopic1: Topic<string>
let testTopic2: Topic<string>

beforeEach(() => {
listeners = []

values1 = []
values2 = []

testTopic1 = new Topic<string>('test', 1)
testTopic2 = new Topic<string>('test', 1)

testTopic1.subscribe((v) => values1.push(v))
testTopic2.subscribe((v) => values2.push(v))

})

it('should have correct value for 2 topics after first topic publishes', () => {
testTopic1.publish('value1')

expect(values1).toEqual(['value1'])
expect(values2).toEqual(['value1'])
})

it('should have correct value for 2 topics after second topic publishes', () => {
testTopic2.publish('value1')

expect(values1).toEqual(['value1'])
expect(values2).toEqual(['value1'])
})

it('should have same value for a new initialized topic like the already existing topics', () => {
testTopic1.publish('value1')

expect(values1).toEqual(['value1'])
expect(values2).toEqual(['value1'])

const values3: any[] = []
const testTopic3 = new Topic<string>('test', 1)
testTopic3.subscribe((v) => values3.push(v))

expect(values3).toEqual(['value1'])
})

it('should have same values for both topics after one topic publishes 2 values', () => {
testTopic1.publish('value1')
testTopic2.publish('value2')

expect(values1).toEqual(['value1', 'value2'])
expect(values2).toEqual(['value1', 'value2'])
})

it('should has no value if message name is different', () => {
testTopic1.publish('value1')

expect(values1).toEqual(['value1'])
expect(values2).toEqual(['value1'])

const values3: any[] = []
const testTopic3 = new Topic<string>('test123', 1)
testTopic3.subscribe((v) => values3.push(v))

expect(values3).toEqual([])
})

it('should has no value if message version is different', () => {
testTopic1.publish('value1')

expect(values1).toEqual(['value1'])
expect(values2).toEqual(['value1'])

const values3: any[] = []
const testTopic3 = new Topic<string>('test', 2)
testTopic3.subscribe((v) => values3.push(v))

expect(values3).toEqual([])
})

it('should has no value if message is undefined', () => {
testTopic1.publish('value1')

expect(values1).toEqual(['value1'])
expect(values2).toEqual(['value1'])

const values3: any[] = []
const testTopic3 = new Topic<undefined>('', 0)
testTopic3.subscribe((v) => values3.push(v))
testTopic3.publish(undefined)

expect(values3).toEqual([])
})

it('should get correct value', () => {
expect(testTopic1.getValue()).toEqual(undefined)

testTopic1.publish('value1')

expect(testTopic1.getValue()).toEqual('value1')
expect(testTopic2.getValue()).toEqual('value1')
})

it('should remove event listener', () => {
testTopic1.destroy()
testTopic2.publish('value1')

expect(values1).toEqual([])
expect(values2).toEqual(['value1'])
})

it('should pipe to get the length of the value', () => {
let v = 0
testTopic1.pipe(map((v) => v.length)).subscribe((s) => v = s)
testTopic1.publish('value1')

expect(v).toEqual(6)
expect(values1).toEqual(['value1'])
})
})
Loading

0 comments on commit b79d1a4

Please sign in to comment.