From 924bcc7bd28f22401913a2bb48c28eb120ba77ea Mon Sep 17 00:00:00 2001 From: Like Zhu Date: Mon, 7 Jun 2021 14:46:15 -0700 Subject: [PATCH] Clean up internal dependencies --- packages/overreact/package.json | 8 ---- .../instrumentation/instrumentation-utils.js | 2 +- packages/overreact/src/store/record-group.js | 2 +- .../src/store/schema-extension/data-ref.js | 2 +- .../src/utils/observer-pattern/index.js | 1 + .../src/utils/observer-pattern/subject.js | 48 +++++++++++++++++++ .../src/utils/primitive-utilities/date.js | 13 +++++ .../src/utils/primitive-utilities/index.js | 1 + 8 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 packages/overreact/src/utils/observer-pattern/index.js create mode 100644 packages/overreact/src/utils/observer-pattern/subject.js create mode 100644 packages/overreact/src/utils/primitive-utilities/date.js create mode 100644 packages/overreact/src/utils/primitive-utilities/index.js diff --git a/packages/overreact/package.json b/packages/overreact/package.json index a0fb1af..edca1a8 100644 --- a/packages/overreact/package.json +++ b/packages/overreact/package.json @@ -5,8 +5,6 @@ "main": "dist/index", "license": "MIT", "dependencies": { - "@bingads-webui-universal/observer-pattern": "*", - "@bingads-webui-universal/primitive-utilities": "^1.4.5", "bluebird": "3.5.0", "json-stable-stringify": "*", "prop-types": ">=15.6.0", @@ -17,12 +15,6 @@ "uuid": "^3.3.3" }, "devDependencies": { - "@bingads-webui/mca-odata-schemas": ">=1.0.0-alpha.2019091919", - "@bingads-webui/edm-core": ">=1.0.0", - "@bingads-webui/edm-odata": "^1.1.6", - "@bingads-webui/edm-resource-identifiers": ">=1.0.0", - "@bingads-webui/http-util": "^1.0.0", - "@bingads-webui/reflection": ">=1.0.0", "fetch-mock": "^9.10.1", "jquery": "2.2.4", "json-stable-stringify": "*", diff --git a/packages/overreact/src/middleware/instrumentation/instrumentation-utils.js b/packages/overreact/src/middleware/instrumentation/instrumentation-utils.js index bdc37c2..e35ae7b 100644 --- a/packages/overreact/src/middleware/instrumentation/instrumentation-utils.js +++ b/packages/overreact/src/middleware/instrumentation/instrumentation-utils.js @@ -1,7 +1,7 @@ /* eslint-disable no-param-reassign */ import _ from 'underscore'; -import { getTimestamp } from '@bingads-webui-universal/primitive-utilities'; +import { getTimestamp } from '../../utils/primitive-utilities'; export const defaultStubOptions = { serverErrorCodes: [-1], diff --git a/packages/overreact/src/store/record-group.js b/packages/overreact/src/store/record-group.js index 05559db..3e95d49 100644 --- a/packages/overreact/src/store/record-group.js +++ b/packages/overreact/src/store/record-group.js @@ -1,7 +1,7 @@ -import { Subject } from '@bingads-webui-universal/observer-pattern'; import _ from 'underscore'; import { Record } from './record'; import { OVERREACT_ID_FIELD_NAME } from './consts'; +import { Subject } from '../utils/observer-pattern' export class RecordGroup extends Subject { constructor(schemaType) { diff --git a/packages/overreact/src/store/schema-extension/data-ref.js b/packages/overreact/src/store/schema-extension/data-ref.js index a4dcb64..821fb5c 100644 --- a/packages/overreact/src/store/schema-extension/data-ref.js +++ b/packages/overreact/src/store/schema-extension/data-ref.js @@ -1,5 +1,5 @@ -import { Subject } from '@bingads-webui-universal/observer-pattern'; import _ from 'underscore'; +import { Subject } from '../../utils/observer-pattern'; export class DataRef extends Subject { constructor(key) { diff --git a/packages/overreact/src/utils/observer-pattern/index.js b/packages/overreact/src/utils/observer-pattern/index.js new file mode 100644 index 0000000..c1c20c0 --- /dev/null +++ b/packages/overreact/src/utils/observer-pattern/index.js @@ -0,0 +1 @@ +export { Subject } from './subject'; diff --git a/packages/overreact/src/utils/observer-pattern/subject.js b/packages/overreact/src/utils/observer-pattern/subject.js new file mode 100644 index 0000000..8570db0 --- /dev/null +++ b/packages/overreact/src/utils/observer-pattern/subject.js @@ -0,0 +1,48 @@ +/* global Set */ +// Use the basic APIs of ES6 Set. If your site supports browsers not +// having Set natively, you should use a polyfill + +/* Subject to observe */ +export class Subject { + constructor() { + this.observers = new Set(); + } + + /** + * Subscribe an observer to the subject + * @param {Object} observer - The observer subscribing to the subject + * @returns {void} + */ + subscribe(observer) { + if (!(observer instanceof Object)) { + throw new Error('Invalid observer'); + } + this.observers.add(observer); + } + + /** + * Unsubscribe an observer from the subject + * @param {Object} observer - The observer to unsubscribe + * @returns {void} + */ + unsubscribe(observer) { + this.observers.delete(observer); + } + + /** + * Notify the observers for a certain action + * @param {string} action - + * Name of the action, it will map to the handler method name on the observer + * @param {...*} args - Additional arguments + * @returns {void} + */ + notify(action, ...args) { + this.observers.forEach((observer) => { + if (typeof observer[action] === 'function') { + observer[action](this, ...args); + } + }); + } +} + + \ No newline at end of file diff --git a/packages/overreact/src/utils/primitive-utilities/date.js b/packages/overreact/src/utils/primitive-utilities/date.js new file mode 100644 index 0000000..4248ba1 --- /dev/null +++ b/packages/overreact/src/utils/primitive-utilities/date.js @@ -0,0 +1,13 @@ + +/** + * Get the current timestamp + * @returns {number} - the timestamp since epoch in milliseconds +*/ +export function getTimestamp() { + // TODO: this method should not be here, as it depends on Web API performance + if (window.performance && window.performance.now && window.performance.timing) { + return window.performance.timing.navigationStart + window.performance.now(); + } + + return Date.now(); +} \ No newline at end of file diff --git a/packages/overreact/src/utils/primitive-utilities/index.js b/packages/overreact/src/utils/primitive-utilities/index.js new file mode 100644 index 0000000..ca6993b --- /dev/null +++ b/packages/overreact/src/utils/primitive-utilities/index.js @@ -0,0 +1 @@ +export * from './date'; \ No newline at end of file