@dereekb/browser 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/README.md +4 -9
- package/package.json +1 -1
- package/src/index.d.ts +1 -1
- package/src/index.js +1 -1
- package/src/index.js.map +1 -1
- package/src/lib/index.d.ts +3 -0
- package/src/lib/index.js +7 -0
- package/src/lib/index.js.map +1 -0
- package/src/lib/service.d.ts +23 -0
- package/src/lib/service.js +93 -0
- package/src/lib/service.js.map +1 -0
- package/src/lib/vh100.d.ts +6 -0
- package/src/lib/vh100.js +25 -0
- package/src/lib/vh100.js.map +1 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
|
|
4
|
+
|
|
5
|
+
# 1.0.0 (2022-02-23)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* added vh100 functionality ([10b86b4](https://github.com/dereekb/dbcomponents/commit/10b86b42fb6653c338212b32d7f3555109747787))
|
|
11
|
+
* segment analytics ([b81d5a6](https://github.com/dereekb/dbcomponents/commit/b81d5a6a70ecf3bc35852d441cfd79e91e5dcb51))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# 0.1.0 (2022-01-29)
|
package/README.md
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
@dereekb/browser
|
|
2
|
+
=======
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
The sources for this package are in the main [@dereekb/dbcomponents](https://github.com/dereekb/dbcomponents) repo. Please file issues and pull requests against that repo.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Run `nx build browser` to build the library.
|
|
8
|
-
|
|
9
|
-
## Running unit tests
|
|
10
|
-
|
|
11
|
-
Run `nx test browser` to execute the unit tests via [Jest](https://jestjs.io).
|
|
6
|
+
License: MIT
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './lib
|
|
1
|
+
export * from './lib';
|
package/src/index.js
CHANGED
package/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/browser/src/index.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/browser/src/index.ts"],"names":[],"mappings":";;;AAAA,qDAAsB"}
|
package/src/lib/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
(0, tslib_1.__exportStar)(require("./service"), exports);
|
|
5
|
+
(0, tslib_1.__exportStar)(require("./vh100"), exports);
|
|
6
|
+
(0, tslib_1.__exportStar)(require("./window"), exports);
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/browser/src/lib/index.ts"],"names":[],"mappings":";;;AAAA,yDAA0B;AAC1B,uDAAwB;AACxB,wDAAyB"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
/**
|
|
3
|
+
* Used for loading services in the browser that are imported from other scripts, such as Facebook, Segment, Stripe, etc.
|
|
4
|
+
*/
|
|
5
|
+
export declare abstract class AbstractAsyncWindowLoadedService<T> {
|
|
6
|
+
private _windowKey;
|
|
7
|
+
private _callbackKey?;
|
|
8
|
+
private _serviceName;
|
|
9
|
+
private _loading;
|
|
10
|
+
readonly loading$: Observable<Promise<T>>;
|
|
11
|
+
readonly service$: Observable<T>;
|
|
12
|
+
/**
|
|
13
|
+
* @param _windowKey Key that is attached to the window for the object that is the service when finished loading.
|
|
14
|
+
* @param _callbackKey Optional key attached to window that is a function that is executed when the setup is complete.
|
|
15
|
+
*/
|
|
16
|
+
constructor(_windowKey: string, _callbackKey?: string | undefined, _serviceName?: string, preload?: boolean);
|
|
17
|
+
getService(): Promise<T>;
|
|
18
|
+
protected loadService(): Promise<T>;
|
|
19
|
+
protected _onLoadServiceFailure(): Promise<T> | void;
|
|
20
|
+
private completeLoadingService;
|
|
21
|
+
protected _prepareCompleteLoadingService(): Promise<any>;
|
|
22
|
+
protected _initService(service: T): Promise<T | void>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AbstractAsyncWindowLoadedService = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const rxjs_1 = require("@dereekb/rxjs");
|
|
6
|
+
const rxjs_2 = require("rxjs");
|
|
7
|
+
/**
|
|
8
|
+
* Used for loading services in the browser that are imported from other scripts, such as Facebook, Segment, Stripe, etc.
|
|
9
|
+
*/
|
|
10
|
+
class AbstractAsyncWindowLoadedService {
|
|
11
|
+
/**
|
|
12
|
+
* @param _windowKey Key that is attached to the window for the object that is the service when finished loading.
|
|
13
|
+
* @param _callbackKey Optional key attached to window that is a function that is executed when the setup is complete.
|
|
14
|
+
*/
|
|
15
|
+
constructor(_windowKey, _callbackKey, _serviceName = _windowKey, preload = true) {
|
|
16
|
+
this._windowKey = _windowKey;
|
|
17
|
+
this._callbackKey = _callbackKey;
|
|
18
|
+
this._serviceName = _serviceName;
|
|
19
|
+
this._loading = new rxjs_2.BehaviorSubject(undefined);
|
|
20
|
+
this.loading$ = this._loading.pipe((0, rxjs_1.tapFirst)(() => this.loadService()), (0, rxjs_1.filterMaybe)(), (0, rxjs_2.shareReplay)(1));
|
|
21
|
+
this.service$ = this.loading$.pipe((0, rxjs_2.switchMap)(x => (0, rxjs_2.from)(x)), (0, rxjs_2.shareReplay)(1));
|
|
22
|
+
if (preload) {
|
|
23
|
+
// Begin loading the service immediately.
|
|
24
|
+
setTimeout(() => this.loadService().catch());
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
getService() {
|
|
28
|
+
return (0, rxjs_2.firstValueFrom)(this.service$);
|
|
29
|
+
}
|
|
30
|
+
// MARK: Loading
|
|
31
|
+
loadService() {
|
|
32
|
+
if (!this._loading.value) {
|
|
33
|
+
const loadingPromise = new Promise((resolve, reject) => {
|
|
34
|
+
let loadTry = 0;
|
|
35
|
+
const rejectWithError = () => {
|
|
36
|
+
reject(new Error(`Service "${this._serviceName}" failed loading with windowKey "${this._windowKey}"`));
|
|
37
|
+
};
|
|
38
|
+
const tryLoad = () => {
|
|
39
|
+
const windowRef = window;
|
|
40
|
+
// Loaded before the promise.
|
|
41
|
+
if (windowRef[this._windowKey]) {
|
|
42
|
+
// Not yet finished loading async. Intercept the function.
|
|
43
|
+
// console.log('Window key.');
|
|
44
|
+
return resolve(this.completeLoadingService());
|
|
45
|
+
}
|
|
46
|
+
else if (this._callbackKey && windowRef[this._callbackKey]) {
|
|
47
|
+
// console.log('Callback key.');
|
|
48
|
+
windowRef[this._callbackKey] = () => resolve(this.completeLoadingService());
|
|
49
|
+
}
|
|
50
|
+
else if (loadTry < 10) {
|
|
51
|
+
loadTry += 1;
|
|
52
|
+
// console.log('Try reload...');
|
|
53
|
+
setTimeout(() => tryLoad(), 1000);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
const retry = this._onLoadServiceFailure();
|
|
57
|
+
if (retry) {
|
|
58
|
+
retry.then(x => resolve(x)).catch(() => rejectWithError());
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
rejectWithError();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
tryLoad();
|
|
66
|
+
});
|
|
67
|
+
this._loading.next(loadingPromise);
|
|
68
|
+
}
|
|
69
|
+
return this._loading.value;
|
|
70
|
+
}
|
|
71
|
+
_onLoadServiceFailure() {
|
|
72
|
+
}
|
|
73
|
+
completeLoadingService() {
|
|
74
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
|
|
75
|
+
yield this._prepareCompleteLoadingService();
|
|
76
|
+
const service = window[this._windowKey];
|
|
77
|
+
if (!service) {
|
|
78
|
+
throw new Error(`Service "${this._serviceName}" could not complete loading.`);
|
|
79
|
+
}
|
|
80
|
+
// Init the API
|
|
81
|
+
const initializedService = yield this._initService(service);
|
|
82
|
+
return initializedService !== null && initializedService !== void 0 ? initializedService : service;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
_prepareCompleteLoadingService() {
|
|
86
|
+
return Promise.resolve();
|
|
87
|
+
}
|
|
88
|
+
_initService(service) {
|
|
89
|
+
return Promise.resolve(service);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.AbstractAsyncWindowLoadedService = AbstractAsyncWindowLoadedService;
|
|
93
|
+
//# sourceMappingURL=service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../../../../../packages/browser/src/lib/service.ts"],"names":[],"mappings":";;;;AACA,wCAAsD;AACtD,+BAAsG;AAEtG;;GAEG;AACH,MAAsB,gCAAgC;IAOpD;;;OAGG;IACH,YACU,UAAkB,EAClB,YAAqB,EACrB,eAAuB,UAAU,EACzC,UAAmB,IAAI;QAHf,eAAU,GAAV,UAAU,CAAQ;QAClB,iBAAY,GAAZ,YAAY,CAAS;QACrB,iBAAY,GAAZ,YAAY,CAAqB;QAZnC,aAAQ,GAAG,IAAI,sBAAe,CAAoB,SAAS,CAAC,CAAC;QAE5D,aAAQ,GAA2B,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAA,eAAQ,EAAC,GAAG,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,EAAE,IAAA,kBAAW,GAAE,EAAE,IAAA,kB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CAAC,OAAU;QAC/B,OAAO,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAClC,CAAC;CAEF;AAhGD,4EAgGC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const DEFAULT_VH100_VARIABLE_NAME = "vh100";
|
|
2
|
+
export declare function refreshVh100Function(cssVariableName?: string): () => void;
|
|
3
|
+
/**
|
|
4
|
+
* Adds window event listeners to populate the css variable `vh100`, or another input variable name, with the current window height.
|
|
5
|
+
*/
|
|
6
|
+
export declare function watchWindowAndUpdateVh100StyleProperty(cssVariableName?: string): void;
|
package/src/lib/vh100.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// https://dev.to/maciejtrzcinski/100vh-problem-with-ios-safari-3ge9
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.watchWindowAndUpdateVh100StyleProperty = exports.refreshVh100Function = exports.DEFAULT_VH100_VARIABLE_NAME = void 0;
|
|
5
|
+
exports.DEFAULT_VH100_VARIABLE_NAME = 'vh100';
|
|
6
|
+
function refreshVh100Function(cssVariableName = exports.DEFAULT_VH100_VARIABLE_NAME) {
|
|
7
|
+
const cssProperty = `--${cssVariableName}`;
|
|
8
|
+
return () => {
|
|
9
|
+
const doc = document.documentElement;
|
|
10
|
+
doc.style.setProperty(cssProperty, `${window.innerHeight}px`);
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
exports.refreshVh100Function = refreshVh100Function;
|
|
14
|
+
;
|
|
15
|
+
/**
|
|
16
|
+
* Adds window event listeners to populate the css variable `vh100`, or another input variable name, with the current window height.
|
|
17
|
+
*/
|
|
18
|
+
function watchWindowAndUpdateVh100StyleProperty(cssVariableName) {
|
|
19
|
+
const refreshPropertyValue = refreshVh100Function(cssVariableName);
|
|
20
|
+
window.addEventListener('resize', refreshPropertyValue);
|
|
21
|
+
window.addEventListener('orientationchange', refreshPropertyValue);
|
|
22
|
+
refreshPropertyValue();
|
|
23
|
+
}
|
|
24
|
+
exports.watchWindowAndUpdateVh100StyleProperty = watchWindowAndUpdateVh100StyleProperty;
|
|
25
|
+
//# sourceMappingURL=vh100.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vh100.js","sourceRoot":"","sources":["../../../../../packages/browser/src/lib/vh100.ts"],"names":[],"mappings":";AAAA,oEAAoE;;;AAEvD,QAAA,2BAA2B,GAAG,OAAO,CAAC;AAEnD,SAAgB,oBAAoB,CAAC,eAAe,GAAG,mCAA2B;IAChF,MAAM,WAAW,GAAG,KAAK,eAAe,EAAE,CAAC;IAC3C,OAAO,GAAG,EAAE;QACV,MAAM,GAAG,GAAG,QAAQ,CAAC,eAAe,CAAC;QACrC,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,WAAW,EAAE,GAAG,MAAM,CAAC,WAAW,IAAI,CAAC,CAAC;IAChE,CAAC,CAAA;AACH,CAAC;AAND,oDAMC;AAAA,CAAC;AAEF;;GAEG;AACH,SAAgB,sCAAsC,CAAC,eAAwB;IAC7E,MAAM,oBAAoB,GAAG,oBAAoB,CAAC,eAAe,CAAC,CAAC;IAEnE,MAAM,CAAC,gBAAgB,CAAC,QAAQ,EAAE,oBAAoB,CAAC,CAAC;IACxD,MAAM,CAAC,gBAAgB,CAAC,mBAAmB,EAAE,oBAAoB,CAAC,CAAC;IACnE,oBAAoB,EAAE,CAAC;AAEzB,CAAC;AAPD,wFAOC"}
|